2018-09-20 13:03:51 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2018-09-13 09:23:57 +02:00
|
|
|
import { ModelConstructor, BaseModel } from '../../shared/models/base/base-model';
|
2018-08-24 13:05:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registeres the mapping of collection strings <--> actual types. Every Model should register itself here.
|
|
|
|
*/
|
2018-09-20 13:03:51 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2018-08-24 13:05:03 +02:00
|
|
|
export class CollectionStringModelMapperService {
|
|
|
|
/**
|
|
|
|
* Mapps collection strings to model constructors. Accessed by {@method registerCollectionElement} and
|
|
|
|
* {@method getCollectionStringType}.
|
|
|
|
*/
|
2018-11-02 08:34:33 +01:00
|
|
|
private collectionStringsTypeMapping: { [collectionString: string]: ModelConstructor<BaseModel> } = {};
|
2018-09-20 13:03:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor to create the NotifyService. Registers itself to the WebsocketService.
|
|
|
|
* @param websocketService
|
|
|
|
*/
|
|
|
|
public constructor() {}
|
|
|
|
|
2018-08-24 13:05:03 +02:00
|
|
|
/**
|
|
|
|
* Registers the type to the collection string
|
|
|
|
* @param collectionString
|
|
|
|
* @param type
|
|
|
|
*/
|
2018-09-20 13:03:51 +02:00
|
|
|
public registerCollectionElement(collectionString: string, type: ModelConstructor<BaseModel>): void {
|
2018-11-02 08:34:33 +01:00
|
|
|
this.collectionStringsTypeMapping[collectionString] = type;
|
2018-08-24 13:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the constructor of the requested collection or undefined, if it is not registered.
|
|
|
|
* @param collectionString the requested collection
|
|
|
|
*/
|
2018-09-20 13:03:51 +02:00
|
|
|
public getModelConstructor(collectionString: string): ModelConstructor<BaseModel> {
|
2018-11-02 08:34:33 +01:00
|
|
|
return this.collectionStringsTypeMapping[collectionString];
|
2018-08-24 13:05:03 +02:00
|
|
|
}
|
2018-08-29 13:21:25 +02:00
|
|
|
|
2018-08-31 14:50:38 +02:00
|
|
|
/**
|
|
|
|
* Returns the collection string of a given ModelConstructor or undefined, if it is not registered.
|
|
|
|
* @param ctor
|
|
|
|
*/
|
2018-09-20 13:03:51 +02:00
|
|
|
public getCollectionString(ctor: ModelConstructor<BaseModel>): string {
|
2018-11-02 08:34:33 +01:00
|
|
|
return Object.keys(this.collectionStringsTypeMapping).find((collectionString: string) => {
|
|
|
|
return ctor === this.collectionStringsTypeMapping[collectionString];
|
|
|
|
});
|
2018-08-31 14:50:38 +02:00
|
|
|
}
|
2018-08-24 13:05:03 +02:00
|
|
|
}
|