Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame^] | 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 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | /** |
| 22 | * Created by obarda on 9/12/2016. |
| 23 | */ |
| 24 | 'use strict'; |
| 25 | import {InputPropertyBase} from "./input-property-base"; |
| 26 | import {PropertyModel} from "./properties"; |
| 27 | import {InputModel} from "./inputs"; |
| 28 | |
| 29 | export class InstancesInputsOrPropertiesMapData { |
| 30 | [instanceId:string]:Array<InputPropertyBase>; |
| 31 | } |
| 32 | |
| 33 | export class InstancesInputsPropertiesMap { |
| 34 | componentInstanceProperties:InstancesInputsOrPropertiesMapData; |
| 35 | componentInstanceInputsMap:InstancesInputsOrPropertiesMapData; |
| 36 | |
| 37 | constructor(componentInstanceInputsMapData:InstancesInputsOrPropertiesMapData, componentInstanceInputsPropertiesMapData:InstancesInputsOrPropertiesMapData) { |
| 38 | this.componentInstanceInputsMap = componentInstanceInputsMapData; |
| 39 | this.componentInstanceProperties = componentInstanceInputsPropertiesMapData; |
| 40 | } |
| 41 | |
| 42 | private removeUnnecessaryData = (properties:Array<InputPropertyBase>, instanceId:string, mapData:any) => { |
| 43 | mapData[instanceId] = []; |
| 44 | if (properties && properties.length > 0) { |
| 45 | _.forEach(properties, (propertyOrInput:InputPropertyBase) => { |
| 46 | if (propertyOrInput instanceof PropertyModel) { // Handle Properties |
| 47 | if (propertyOrInput && !propertyOrInput.isAlreadySelected) { |
| 48 | mapData[instanceId].push(propertyOrInput); |
| 49 | } |
| 50 | } |
| 51 | if (propertyOrInput instanceof InputModel) { // Handle Inputs |
| 52 | if (propertyOrInput && !propertyOrInput.isAlreadySelected) { |
| 53 | mapData[instanceId].push(propertyOrInput); |
| 54 | } |
| 55 | } |
| 56 | }); |
| 57 | if (mapData[instanceId].length === 0) { |
| 58 | delete mapData[instanceId]; |
| 59 | } |
| 60 | } else { |
| 61 | delete mapData[instanceId]; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | In the toJson we remove all inputs and property already selected (The check box selected but they are disable) |
| 67 | also we remove empty array in order to prevent Backend error |
| 68 | */ |
| 69 | |
| 70 | public cleanUnnecessaryDataBeforeSending = ():InstancesInputsPropertiesMap => { |
| 71 | |
| 72 | let map:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(new InstancesInputsOrPropertiesMapData(), new InstancesInputsOrPropertiesMapData()); |
| 73 | angular.copy(this, map); |
| 74 | |
| 75 | //Removing unnecessary data from inputs map |
| 76 | _.forEach(map.componentInstanceInputsMap, (inputs:Array<InputModel>, instanceId:string) => { |
| 77 | this.removeUnnecessaryData(inputs, instanceId, map.componentInstanceInputsMap); |
| 78 | }); |
| 79 | |
| 80 | //Removing unnecessary data from properties map |
| 81 | _.forEach(map.componentInstanceProperties, (properties:Array<PropertyModel>, instanceId:string) => { |
| 82 | this.removeUnnecessaryData(properties, instanceId, map.componentInstanceProperties); |
| 83 | }); |
| 84 | |
| 85 | return map; |
| 86 | }; |
| 87 | } |