blob: 514b870de324a6c8fbd4114f1da6186386aa6f6a [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo035c5932016-12-14 09:55:15 -080019import {BehaviorSubject} from 'rxjs';
20import * as _ from 'lodash';
21import {IWSEvent} from '../websocket/global';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080022import {IXosResourceService} from '../rest/model.rest';
Matteo Scandolo0e171f32017-09-26 17:21:41 -070023import {IXosModeldefsCache} from './modeldefs.service';
Matteo Scandolo035c5932016-12-14 09:55:15 -080024
25export interface IStoreHelpersService {
26 updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>;
Matteo Scandolofe0d4102017-11-21 10:41:28 -080027 removeItemFromCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>;
Matteo Scandolo035c5932016-12-14 09:55:15 -080028}
29
Matteo Scandolo1aee1982017-02-17 08:33:23 -080030export class StoreHelpers implements IStoreHelpersService {
Matteo Scandolo0e171f32017-09-26 17:21:41 -070031 static $inject = [
Matteo Scandolofe0d4102017-11-21 10:41:28 -080032 '$log',
Matteo Scandolo0e171f32017-09-26 17:21:41 -070033 'ModelRest',
34 'XosModeldefsCache'
35 ];
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080036
37 constructor (
Matteo Scandolofe0d4102017-11-21 10:41:28 -080038 private $log: ng.ILogService,
Matteo Scandolo0e171f32017-09-26 17:21:41 -070039 private modelRest: IXosResourceService,
40 private XosModeldefsCache: IXosModeldefsCache
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080041 ) {
42 }
43
Matteo Scandolo035c5932016-12-14 09:55:15 -080044 public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
Matteo Scandolofe0d4102017-11-21 10:41:28 -080045 if (event.deleted) {
46 this.$log.error('[XosStoreHelpers] updateCollection method has been called for a delete event, in this cale please use "removeItemFromCollection"', event);
47 return;
48 }
Matteo Scandolo035c5932016-12-14 09:55:15 -080049 const collection: any[] = subject.value;
50 const index: number = _.findIndex(collection, (i) => {
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080051 // NOTE evaluate to use event.msg.pk
Matteo Scandolo035c5932016-12-14 09:55:15 -080052 return i.id === event.msg.object.id;
53 });
54 const exist: boolean = index > -1;
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080055
56 // generate a resource for the model
Matteo Scandolo0e171f32017-09-26 17:21:41 -070057 const modelDef = this.XosModeldefsCache.get(event.model); // get the model definition
58 const endpoint = this.XosModeldefsCache.getApiUrlFromModel(modelDef);
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080059 const resource = this.modelRest.getResource(endpoint);
60 const model = new resource(event.msg.object);
61
Matteo Scandolo035c5932016-12-14 09:55:15 -080062 // Replace item at index using native splice
Matteo Scandolofe0d4102017-11-21 10:41:28 -080063 if (exist) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080064 collection.splice(index, 1, model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080065 }
Matteo Scandolofe0d4102017-11-21 10:41:28 -080066 // if the element does not exist add it
67 else {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080068 collection.push(model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080069 }
70
71 subject.next(collection);
72
73 return subject;
74 }
Matteo Scandolofe0d4102017-11-21 10:41:28 -080075
76 public removeItemFromCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
77 if (!event.deleted) {
78 this.$log.error('[XosStoreHelpers] removeItemFromCollection method has been called for an update event, in this cale please use "updateCollection"', event);
79 return;
80 }
81 const collection: any[] = subject.value;
82 _.remove(collection, {id: event.msg.object.id});
83 subject.next(collection);
84 return subject;
85 }
Matteo Scandolo035c5932016-12-14 09:55:15 -080086}