blob: 61eb1149f9efe2b10c772644fa3880e9370a6d13 [file] [log] [blame]
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08001/// <reference path="../../../../typings/index.d.ts"/>
Matteo Scandolo86bc26a2017-01-18 11:06:47 -08002import * as _ from 'lodash';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08003import {BehaviorSubject, Observable} from 'rxjs/Rx';
4import {IWSEvent, IWSEventService} from '../websocket/global';
5import {IXosResourceService} from '../rest/model.rest';
6import {IStoreHelpersService} from '../helpers/store.helpers';
7
Matteo Scandolo47860fe2017-02-02 12:05:55 -08008export interface IXosModelStoreService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -08009 query(model: string, apiUrl?: string): Observable<any>;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080010 search(modelName: string): any[];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080011}
12
Matteo Scandolo1aee1982017-02-17 08:33:23 -080013export class XosModelStore implements IXosModelStoreService {
Matteo Scandolo17bf8242017-01-23 17:30:39 -080014 static $inject = ['$log', 'WebSocket', 'StoreHelpers', 'ModelRest'];
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080015 private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080016 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080017 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080018 private webSocket: IWSEventService,
19 private storeHelpers: IStoreHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080020 private ModelRest: IXosResourceService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080021 ) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080022 this._collections = {};
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080023 }
24
Matteo Scandolo1aee1982017-02-17 08:33:23 -080025 public query(modelName: string, apiUrl: string): Observable<any> {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080026 // if there isn't already an observable for that item
Matteo Scandolo042ea632017-03-01 19:02:34 -080027 // create a new one and .next() is called by this.loadInitialData once data are received
Matteo Scandolo1aee1982017-02-17 08:33:23 -080028 if (!this._collections[modelName]) {
29 this._collections[modelName] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource
30 this.loadInitialData(modelName, apiUrl);
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080031 }
Matteo Scandolo042ea632017-03-01 19:02:34 -080032 // else manually trigger the next with the last know value to trigger the subscribe method of who's requestiong this data
33 else {
34 this._collections[modelName].next(this._collections[modelName].value);
35 }
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080036
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080037 this.webSocket.list()
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038 .filter((e: IWSEvent) => e.model === modelName)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080039 .subscribe(
40 (event: IWSEvent) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080041 this.storeHelpers.updateCollection(event, this._collections[modelName]);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080042 },
43 err => console.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080044 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080045
Matteo Scandolo1aee1982017-02-17 08:33:23 -080046 return this._collections[modelName].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080047 }
48
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080049 public search(modelName: string): any[] {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080050 try {
51 const res = _.reduce(Object.keys(this._collections), (results, k) => {
52 let partialRes;
53 // NOTE wrapped in a try catch as some subject may be errored, due to not available REST endpoint
54 try {
55 partialRes = _.filter(this._collections[k].value, i => {
56 if (i && i.humanReadableName) {
57 return i.humanReadableName.toLowerCase().indexOf(modelName) > -1;
58 }
59 else if (i && i.name) {
60 return i.name.toLowerCase().indexOf(modelName) > -1;
61 }
62 return false;
63 });
64 } catch (e) {
65 partialRes = [];
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080066 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080067 partialRes.map(m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080068 m.modelName = k;
69 return m;
70 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080071 return results.concat(partialRes);
72 }, []);
73 return res;
74 } catch (e) {
75 return [];
76 }
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080077 }
78
Matteo Scandolo04964232017-01-07 12:53:46 -080079 public get(model: string, id: number) {
80 // TODO implement a get method
81 }
82
Matteo Scandolo1aee1982017-02-17 08:33:23 -080083 private loadInitialData(model: string, apiUrl?: string) {
Matteo Scandolo042ea632017-03-01 19:02:34 -080084 // TODO provide always the apiUrl togheter with the query() params
Matteo Scandolo1aee1982017-02-17 08:33:23 -080085 if (!angular.isDefined(apiUrl)) {
86 // NOTE check what is the correct pattern to pluralize this
87 apiUrl = this.storeHelpers.urlFromCoreModel(model);
88 }
89 this.ModelRest.getResource(apiUrl).query().$promise
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080090 .then(
91 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080092 this._collections[model].next(res);
Matteo Scandolo17bf8242017-01-23 17:30:39 -080093 })
94 .catch(
Matteo Scandolo1aee1982017-02-17 08:33:23 -080095 err => {
96 this._collections[model].error(err);
Matteo Scandolo1aee1982017-02-17 08:33:23 -080097 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080098 );
99 }
100}