Sync Integ to Master
[sdc.git] / catalog-ui / src / app / models / instance-inputs-properties-map.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by obarda on 9/12/2016.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {InputPropertyBase} from "./input-property-base";
27 import {PropertyModel} from "./properties";
28 import {InputModel} from "./inputs";
29
30 export class InstancesInputsOrPropertiesMapData {
31     [instanceId:string]:Array<InputPropertyBase>;
32 }
33
34 export class InstancesInputsPropertiesMap {
35     componentInstanceProperties:InstancesInputsOrPropertiesMapData;
36     componentInstanceInputsMap:InstancesInputsOrPropertiesMapData;
37
38     constructor(componentInstanceInputsMapData:InstancesInputsOrPropertiesMapData, componentInstanceInputsPropertiesMapData:InstancesInputsOrPropertiesMapData) {
39         this.componentInstanceInputsMap = componentInstanceInputsMapData;
40         this.componentInstanceProperties = componentInstanceInputsPropertiesMapData;
41     }
42
43     private removeUnnecessaryData = (properties:Array<InputPropertyBase>, instanceId:string, mapData:any) => {
44         mapData[instanceId] = [];
45         if (properties && properties.length > 0) {
46             _.forEach(properties, (propertyOrInput:InputPropertyBase) => {
47                 if (propertyOrInput instanceof PropertyModel) { // Handle Properties
48                     if (propertyOrInput && !propertyOrInput.isAlreadySelected) {
49                         mapData[instanceId].push(propertyOrInput);
50                     }
51                 }
52                 if (propertyOrInput instanceof InputModel) { // Handle Inputs
53                     if (propertyOrInput && !propertyOrInput.isAlreadySelected) {
54                         mapData[instanceId].push(propertyOrInput);
55                     }
56                 }
57             });
58             if (mapData[instanceId].length === 0) {
59                 delete mapData[instanceId];
60             }
61         } else {
62             delete mapData[instanceId];
63         }
64     }
65
66     /*
67      In the toJson we remove all inputs and property already selected (The check box selected but they are disable)
68      also we remove empty array in order to prevent Backend error
69      */
70
71     public cleanUnnecessaryDataBeforeSending = ():InstancesInputsPropertiesMap => {
72
73         let map:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(new InstancesInputsOrPropertiesMapData(), new InstancesInputsOrPropertiesMapData());
74         angular.copy(this, map);
75
76         //Removing unnecessary data from inputs map
77         _.forEach(map.componentInstanceInputsMap, (inputs:Array<InputModel>, instanceId:string) => {
78             this.removeUnnecessaryData(inputs, instanceId, map.componentInstanceInputsMap);
79         });
80
81         //Removing unnecessary data from properties map
82         _.forEach(map.componentInstanceProperties, (properties:Array<PropertyModel>, instanceId:string) => {
83             this.removeUnnecessaryData(properties, instanceId, map.componentInstanceProperties);
84         });
85
86         return map;
87     };
88 }