aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/app/scripts/services/components/utils/composition-left-palette-service.ts
blob: 25ac1cdf17ba3297034c22ed57b8ea13fee4207d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
/**
 * Created by obarda on 3/13/2016.
 */
/// <reference path="../../../references"/>
module Sdc.Services.Components {

    'use strict';

    export class LeftPanelLatestVersion {
        uid: string;
        version: string;
    }

    export class LeftPaletteDataObject {
        currentUpdatingIdsList: Array<string>;
        latestVersionAndIdsList: Array<LeftPanelLatestVersion>;
        fullDataLeftPaletteComponents: Array<Models.Components.Component>;
        displayLeftPanelComponents: Array<Models.DisplayComponent>;
        onFinishLoadingEvent: string;

        constructor(onFinishEventListener: string) {

            this.fullDataLeftPaletteComponents = new Array<Models.Components.Component>();
            this.displayLeftPanelComponents = new Array<Models.DisplayComponent>();
            this.currentUpdatingIdsList = new Array<string>();
            this.latestVersionAndIdsList = new Array<LeftPanelLatestVersion>();
            this.onFinishLoadingEvent = onFinishEventListener;
        }
    }

    export class LeftPaletteLoaderService {

        static '$inject' = [
            'Restangular',
            'sdcConfig',
            '$q',
            '$base64',
            'ComponentFactory',
            'EventListenerService'

        ];

        constructor(protected restangular: restangular.IElement,
                    protected sdcConfig: Models.IAppConfigurtaion,
                    protected $q: ng.IQService,
                    protected $base64: any,
                    protected ComponentFactory: Utils.ComponentFactory,
                    protected EventListenerService: Services.EventListenerService) {

            this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
        }

        private serviceLeftPaletteData: LeftPaletteDataObject;
        private resourceLeftPaletteData: LeftPaletteDataObject;
        private productLeftPaletteData: LeftPaletteDataObject;
        private vlData: LeftPaletteDataObject;

        public loadLeftPanel = (): void => {

            this.serviceLeftPaletteData = new LeftPaletteDataObject(Utils.Constants.EVENTS.SERVICE_LEFT_PALETTE_UPDATE_EVENT);
            this.resourceLeftPaletteData = new LeftPaletteDataObject(Utils.Constants.EVENTS.RESOURCE_LEFT_PALETTE_UPDATE_EVENT);
            this.productLeftPaletteData = new LeftPaletteDataObject(Utils.Constants.EVENTS.PRODUCT_LEFT_PALETTE_UPDATE_EVENT);
            this.vlData = new LeftPaletteDataObject(Utils.Constants.EVENTS.VL_LEFT_PALETTE_UPDATE_EVENT);

            //initiating service palette
            this.updateComponentLeftPalette(Utils.Constants.ComponentType.SERVICE);

            //initiating resource palette
            this.updateComponentLeftPalette(Utils.Constants.ComponentType.RESOURCE);

            //initiating product palette
            this.updateComponentLeftPalette(Utils.Constants.ComponentType.PRODUCT);

            //initiating vl
            this.updateComponentLeftPalette(Utils.Constants.ResourceType.VL);
        };

        private updateData = (latestVersionComponents: Array<Models.Components.Component>, leftPaletteDataObj: LeftPaletteDataObject) => {

            let fullDataComponentsArray: Array<Models.Components.Component> = new Array<Models.Components.Component>();
            let displayComponentsArray: Array<Models.DisplayComponent> = new Array<Models.DisplayComponent>();

            _.forEach(latestVersionComponents, (componentObj: any) => {
                let component: Models.Components.Component = this.ComponentFactory.createComponent(componentObj);
                fullDataComponentsArray.push(component);
                displayComponentsArray.push(new Models.DisplayComponent(component));
            });

            leftPaletteDataObj.fullDataLeftPaletteComponents = leftPaletteDataObj.fullDataLeftPaletteComponents.concat(fullDataComponentsArray);
            leftPaletteDataObj.displayLeftPanelComponents = leftPaletteDataObj.displayLeftPanelComponents.concat(displayComponentsArray);
        };

        private getTypeUrl = (componentType: string): string => {
            return Utils.Constants.ComponentType.PRODUCT === componentType ? "services" : "resources";
        };

        private onFinishLoading = (componentType: string, leftPaletteData: LeftPaletteDataObject): void => {
            leftPaletteData.currentUpdatingIdsList = [];
            this.EventListenerService.notifyObservers(leftPaletteData.onFinishLoadingEvent);
        };

        private getPartialLastVersionFullComponents = (componentType: string, componentInternalType: string, leftPaletteData: LeftPaletteDataObject): void => {
            this.restangular.one(this.getTypeUrl(componentType)).one('/latestversion/notabstract').customPOST(leftPaletteData.currentUpdatingIdsList, '', {'internalComponentType': componentInternalType}).then((componentsArray: any) => {
                this.updateData(componentsArray, leftPaletteData);
                this.onFinishLoading(componentType, leftPaletteData); //when finish loading update view
            });
        };

        private removeNotUpdatedComponents = (leftPaletteObject: LeftPaletteDataObject) => {

            leftPaletteObject.fullDataLeftPaletteComponents = _.filter(leftPaletteObject.fullDataLeftPaletteComponents, (component)=> {
                return leftPaletteObject.currentUpdatingIdsList.indexOf(component.uniqueId) != -1;
            });
            leftPaletteObject.displayLeftPanelComponents = _.filter(leftPaletteObject.displayLeftPanelComponents, (component)=> {
                return leftPaletteObject.currentUpdatingIdsList.indexOf(component.uniqueId) != -1;
            });
        };

        private findIdsToUpdate = (leftPaletteObj: LeftPaletteDataObject): Array<string> => {
            let idsToUpdate = <string[]>_.difference(leftPaletteObj.currentUpdatingIdsList, _.map(leftPaletteObj.fullDataLeftPaletteComponents, 'uniqueId'));
            let neededUpdate = _.filter(leftPaletteObj.fullDataLeftPaletteComponents, (component) => {
                let updated = _.find(leftPaletteObj.latestVersionAndIdsList, (versionAndId: LeftPanelLatestVersion) => {
                    return versionAndId.uid === component.uniqueId && versionAndId.version != component.version
                });
                return updated != undefined;
            });
            if (neededUpdate && neededUpdate.length > 0) {
                let neededUpdateIds = <string[]>_.map(neededUpdate, 'uid');
                idsToUpdate.concat(neededUpdateIds);
            }
            return idsToUpdate;
        };

        private updateCurrentIdsList = (componentType: string, leftPaletteObj: LeftPaletteDataObject): void=> {
            this.removeNotUpdatedComponents(leftPaletteObj);
            leftPaletteObj.currentUpdatingIdsList = this.findIdsToUpdate(leftPaletteObj);
            //remove all components that needed update from current lists
            if (leftPaletteObj.currentUpdatingIdsList.length > 0) {
                leftPaletteObj.displayLeftPanelComponents = _.filter(leftPaletteObj.displayLeftPanelComponents, (component)=> {
                    return leftPaletteObj.currentUpdatingIdsList.indexOf(component.uniqueId) === -1;
                });
                leftPaletteObj.fullDataLeftPaletteComponents = _.filter(leftPaletteObj.fullDataLeftPaletteComponents, (component)=> {
                    return leftPaletteObj.currentUpdatingIdsList.indexOf(component.uniqueId) === -1;
                });
            }
        };

        private updateLeftPalette = (componentType, componentInternalType: string, leftPaletteData: LeftPaletteDataObject): void => {
            if (leftPaletteData.currentUpdatingIdsList.length > 0) return; //this means the service is still performing update
            this.restangular.one(this.getTypeUrl(componentType)).one('/latestversion/notabstract/uidonly').get({'internalComponentType': componentInternalType}).then((latestVersionUniqueIds: Array<LeftPanelLatestVersion>) => {
                leftPaletteData.latestVersionAndIdsList = latestVersionUniqueIds;
                leftPaletteData.currentUpdatingIdsList = <string[]>_.map(latestVersionUniqueIds, 'uid')

                if (leftPaletteData.fullDataLeftPaletteComponents.length === 0) { //this is when first loading product or resource left palette
                    this.getPartialLastVersionFullComponents(componentType, componentInternalType, leftPaletteData);
                } else {
                    this.updateCurrentIdsList(componentType, leftPaletteData);
                    if (leftPaletteData.currentUpdatingIdsList.length === 0) {
                        this.onFinishLoading(componentType, leftPaletteData); //when finish loading update view
                        return;
                    }
                    this.getPartialLastVersionFullComponents(componentType, componentInternalType, leftPaletteData);
                }
            });
        };

        public getLeftPanelComponentsForDisplay = (componentType: string): Array<Models.DisplayComponent> => {
            switch (componentType) {
                case Utils.Constants.ComponentType.SERVICE:
                    return this.serviceLeftPaletteData.displayLeftPanelComponents;
                case Utils.Constants.ComponentType.PRODUCT:
                    return this.productLeftPaletteData.displayLeftPanelComponents;
                default:
                    return this.resourceLeftPaletteData.displayLeftPanelComponents;
            }
        };

        public getFullDataComponentList = (componentType: string): Array<Models.Components.Component> => {
            switch (componentType) {
                case Utils.Constants.ResourceType.VL:
                    return this.vlData.fullDataLeftPaletteComponents;
                case Utils.Constants.ComponentType.SERVICE:
                    return this.serviceLeftPaletteData.fullDataLeftPaletteComponents;
                case Utils.Constants.ComponentType.PRODUCT:
                    return this.productLeftPaletteData.fullDataLeftPaletteComponents;
                default :
                    return this.resourceLeftPaletteData.fullDataLeftPaletteComponents;
            }
        };

        public getFullDataComponentListWithVls = (componentType: string): Array<Models.Components.Component> => {
            let listPart1: Array<Models.Components.Component>;
            let listPart2: Array<Models.Components.Component>;
            if (componentType === Utils.Constants.ResourceType.VL) {
                listPart1 = [];
                listPart2 = this.getFullDataComponentList(Utils.Constants.ResourceType.VL);
            } else {
                listPart1 = this.getFullDataComponentList(componentType);
                listPart2 = this.getFullDataComponentList(Utils.Constants.ResourceType.VL);
            }
            return listPart1.concat(listPart2);
        };

        public updateSpecificComponentLeftPalette = (component: Models.Components.Component, componentType: string): void => {
            let listComponents: Array<Models.Components.Component> = this.getFullDataComponentList(componentType);
            for (let i in listComponents) {
                if (listComponents[i].uniqueId === component.uniqueId) {
                    listComponents[i] = component;
                }
            }
        };

        public updateComponentLeftPalette = (componentType): void => {
            switch (componentType) {
                case Utils.Constants.ResourceType.VL:
                    this.updateLeftPalette(Utils.Constants.ComponentType.RESOURCE, Utils.Constants.ResourceType.VL, this.vlData);
                    break;
                case Utils.Constants.ComponentType.SERVICE:
                    this.updateLeftPalette(Utils.Constants.ComponentType.SERVICE, Utils.Constants.ComponentType.SERVICE, this.serviceLeftPaletteData);
                    break;
                case Utils.Constants.ComponentType.PRODUCT:
                    this.updateLeftPalette(Utils.Constants.ComponentType.PRODUCT, Utils.Constants.ComponentType.SERVICE, this.productLeftPaletteData);
                    break;
                default:
                    this.updateLeftPalette(Utils.Constants.ComponentType.RESOURCE, Utils.Constants.ResourceType.VF, this.resourceLeftPaletteData);
            }
        };
    }
}