blob: fdc6f926e30e2b6f708d9c12596cf410c1fba3a9 [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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 Landoed64b5e2017-06-09 03:19:04 +030021/**
22 * Created by obarda on 9/12/2016.
23 */
24'use strict';
Michael Landoa5445102018-03-04 14:53:33 +020025import * as _ from "lodash";
Michael Landoed64b5e2017-06-09 03:19:04 +030026import {InputPropertyBase} from "./input-property-base";
27import {PropertyModel} from "./properties";
28import {InputModel} from "./inputs";
29
30export class InstancesInputsOrPropertiesMapData {
31 [instanceId:string]:Array<InputPropertyBase>;
32}
33
34export 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}