blob: 4958015070ffa4d27174c81f28a37a311431aa80 [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 Scandolo1aee1982017-02-17 08:33:23 -080027 if (!this._collections[modelName]) {
28 this._collections[modelName] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource
29 this.loadInitialData(modelName, apiUrl);
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080030 }
31
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080032 this.webSocket.list()
Matteo Scandolo1aee1982017-02-17 08:33:23 -080033 .filter((e: IWSEvent) => e.model === modelName)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080034 .subscribe(
35 (event: IWSEvent) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080036 this.storeHelpers.updateCollection(event, this._collections[modelName]);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080037 },
38 err => console.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080039 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080040
Matteo Scandolo1aee1982017-02-17 08:33:23 -080041 return this._collections[modelName].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080042 }
43
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080044 public search(modelName: string): any[] {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080045 try {
46 const res = _.reduce(Object.keys(this._collections), (results, k) => {
47 let partialRes;
48 // NOTE wrapped in a try catch as some subject may be errored, due to not available REST endpoint
49 try {
50 partialRes = _.filter(this._collections[k].value, i => {
51 if (i && i.humanReadableName) {
52 return i.humanReadableName.toLowerCase().indexOf(modelName) > -1;
53 }
54 else if (i && i.name) {
55 return i.name.toLowerCase().indexOf(modelName) > -1;
56 }
57 return false;
58 });
59 } catch (e) {
60 partialRes = [];
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080061 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080062 partialRes.map(m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080063 m.modelName = k;
64 return m;
65 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080066 return results.concat(partialRes);
67 }, []);
68 return res;
69 } catch (e) {
70 return [];
71 }
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080072 }
73
Matteo Scandolo04964232017-01-07 12:53:46 -080074 public get(model: string, id: number) {
75 // TODO implement a get method
76 }
77
Matteo Scandolo1aee1982017-02-17 08:33:23 -080078 private loadInitialData(model: string, apiUrl?: string) {
79 // TODO provide alway the apiUrl togheter with the query() params
80 if (!angular.isDefined(apiUrl)) {
81 // NOTE check what is the correct pattern to pluralize this
82 apiUrl = this.storeHelpers.urlFromCoreModel(model);
83 }
84 this.ModelRest.getResource(apiUrl).query().$promise
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080085 .then(
86 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080087 this._collections[model].next(res);
Matteo Scandolo17bf8242017-01-23 17:30:39 -080088 })
89 .catch(
Matteo Scandolo1aee1982017-02-17 08:33:23 -080090 // TODO understand how to send an error to an observable
91 err => {
92 this._collections[model].error(err);
93 // this.$log.log(`Error retrieving ${model}`, err);
94 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080095 );
96 }
97}