Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 19 | import {BehaviorSubject} from 'rxjs'; |
| 20 | import * as _ from 'lodash'; |
| 21 | import {IWSEvent} from '../websocket/global'; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 22 | import {IXosResourceService} from '../rest/model.rest'; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 23 | import {IXosModeldefsCache} from './modeldefs.service'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 24 | |
| 25 | export interface IStoreHelpersService { |
| 26 | updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>; |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 27 | removeItemFromCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 30 | export class StoreHelpers implements IStoreHelpersService { |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 31 | static $inject = [ |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 32 | '$log', |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 33 | 'ModelRest', |
| 34 | 'XosModeldefsCache' |
| 35 | ]; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 36 | |
| 37 | constructor ( |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 38 | private $log: ng.ILogService, |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 39 | private modelRest: IXosResourceService, |
| 40 | private XosModeldefsCache: IXosModeldefsCache |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 41 | ) { |
| 42 | } |
| 43 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 44 | public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> { |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 45 | 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 49 | const collection: any[] = subject.value; |
| 50 | const index: number = _.findIndex(collection, (i) => { |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 51 | // NOTE evaluate to use event.msg.pk |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 52 | return i.id === event.msg.object.id; |
| 53 | }); |
| 54 | const exist: boolean = index > -1; |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 55 | |
| 56 | // generate a resource for the model |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 57 | const modelDef = this.XosModeldefsCache.get(event.model); // get the model definition |
| 58 | const endpoint = this.XosModeldefsCache.getApiUrlFromModel(modelDef); |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 59 | const resource = this.modelRest.getResource(endpoint); |
| 60 | const model = new resource(event.msg.object); |
| 61 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 62 | // Replace item at index using native splice |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 63 | if (exist) { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 64 | collection.splice(index, 1, model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 65 | } |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 66 | // if the element does not exist add it |
| 67 | else { |
Matteo Scandolo | aa024ff | 2017-01-04 15:04:46 -0800 | [diff] [blame] | 68 | collection.push(model); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | subject.next(collection); |
| 72 | |
| 73 | return subject; |
| 74 | } |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 75 | |
| 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 Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 86 | } |