Allow multiple entry for map/list when tosca function is selected
[sdc.git] / catalog-ui / src / app / models / properties-inputs / derived-fe-property.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 import * as _ from "lodash";
22 import { SchemaPropertyGroupModel, SchemaProperty } from '../schema-property';
23 import { DerivedPropertyType, PropertyBEModel, PropertyFEModel } from '../../models';
24 import {SubPropertyToscaFunction} from "../sub-property-tosca-function";
25 import {ToscaFunction} from "../tosca-function";
26 import { PROPERTY_TYPES } from 'app/utils';
27 import { UUID } from "angular2-uuid";
28
29
30 export class DerivedFEProperty extends PropertyBEModel {
31     valueObj: any;
32     valueObjIsValid: boolean;
33     valueObjOrig: any;
34     valueObjIsChanged: boolean;
35     value: any
36     parentName: string;
37     propertiesName: string; //"network_assignments#ipv4_subnet#use_ipv4 =  parentPath + name
38     derivedDataType: DerivedPropertyType;
39     toscaFunction: ToscaFunction;
40     isDeclared: boolean;
41     isSelected: boolean;
42     isDisabled: boolean;
43     hidden: boolean;
44     isChildOfListOrMap: boolean;
45     canBeDeclared: boolean;
46     mapKey: string;
47     mapKeyError: string;
48     mapInlist: boolean
49     inputName: string;
50     parentMapKey: string;
51     toscaPath: string[];
52
53     constructor(property: PropertyBEModel, parentName?: string, createChildOfListOrMap?: boolean, key?:string, value?:any) {
54         if (!createChildOfListOrMap) { //creating a standard derived prop
55             super(property);
56             this.toscaPath = [];
57             this.parentName = parentName ? parentName : null;
58             this.propertiesName = (parentName) ? parentName + '#' + property.name : property.name;
59             this.canBeDeclared = true; //defaults to true
60             if (property instanceof DerivedFEProperty) {
61                 this.toscaPath = property.toscaPath != null ? property.toscaPath : [];
62             } else {
63                 this.toscaPath = property.parentToscaPath != null ? property.parentToscaPath : property.parentToscaPath;
64             }
65             if (this.toscaPath.length == 0 && parentName != null && parentName.indexOf('#') != -1) {
66                 let lastparent = parentName.split('#');
67                 this.toscaPath.push(lastparent[lastparent.length - 1]);
68             }
69             this.toscaPath.push(property.name);
70         } else { //creating a direct child of list or map (ie. Item that can be deleted, with UUID instead of name)
71             super(null);
72             let toscaPathCopy = null;
73             if (property instanceof DerivedFEProperty) {
74                 toscaPathCopy = property.toscaPath != null ? property.toscaPath .toString() : null;
75             } else {
76                 toscaPathCopy = property.parentToscaPath != null ? property.parentToscaPath.toString() : null;
77             }
78             this.toscaPath = toscaPathCopy != null ? toscaPathCopy.split(",") : [];
79             this.isChildOfListOrMap = true;
80             this.canBeDeclared = false;
81             this.name = UUID.UUID();
82             this.parentName = parentName;
83             this.propertiesName = parentName + '#' + this.name;
84             
85             if (property.type == PROPERTY_TYPES.LIST) {
86                 let parentKey : string = null;
87                 if (property instanceof DerivedFEProperty) {
88                     if (property.valueObj != '') {
89                         if (key != '') {
90                             let listIndex = Number(key);
91                             if (!isNaN(listIndex)) {
92                                 this.toscaPath.push(key);
93                             } else {
94                                let newIndex = Object.keys(property.valueObj).findIndex(valueKey => (valueKey == key));
95                                this.toscaPath.push(newIndex.toString());
96                             }
97                         } else {
98                             let toscaIndex = Object.keys(property.valueObj).sort().reverse()[0];
99                             this.toscaPath.push((Number(toscaIndex) + 1).toString());
100                         }
101                     } else {
102                         this.toscaPath.push((property.valueObj.length).toString());
103                     }
104                 } else {
105                     if (property instanceof PropertyFEModel && property.valueObj != '') {
106                         if (key != '') {
107                             parentKey = key;
108                         }else{
109                             let toscaIndex = Object.keys(property.valueObj).sort().reverse()[0];
110                             parentKey = (Number(toscaIndex) + 1).toString();
111                         }
112                     } else {
113                         parentKey = "0";
114                         if (property instanceof PropertyFEModel) {
115                             parentKey = (property.flattenedChildren.length).toString();
116                         }
117                     }
118                     this.toscaPath.push(parentKey);
119                 }
120                 if (property.schemaType != PROPERTY_TYPES.MAP) {
121                     this.mapKey = parentKey;
122                 }
123                 this.mapKeyError = null;
124                 this.type = property.schema.property.type;
125                 if (this.type == PROPERTY_TYPES.MAP){
126                     this.mapInlist = true;
127                     this.parentMapKey = parentKey;
128                 }
129                 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
130             } else { //map
131                 if (key) {
132                     this.mapKey = key;
133                     this.mapKeyError = null;
134                 } else {
135                     this.mapKey = '';
136                     this.mapKeyError = 'Key cannot be empty.';
137                 }
138                 this.type = property.type;
139                 if (property.schema.property.type == PROPERTY_TYPES.MAP){
140                     const schProp = new SchemaProperty();
141                     schProp.isSimpleType = true;
142                     schProp.isDataType = false;
143                     schProp.simpleType = PROPERTY_TYPES.STRING;
144                     this.schema = new SchemaPropertyGroupModel(schProp);
145                     this.schemaType = PROPERTY_TYPES.STRING;
146                     if (property instanceof DerivedFEProperty) {
147                         this.parentMapKey = property.parentMapKey;
148                         if (value != null && typeof value == 'object') {
149                             this.toscaFunction = property.toscaFunction;
150                         }
151                     }
152                 } else {
153                     this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
154                 }
155                 if (this.toscaPath != null) {
156                     let lastIndex = this.toscaPath[this.toscaPath.length - 1];
157                     if(this.mapKey != lastIndex){
158                         this.toscaPath.push(this.mapKey);
159                     }
160                 } else {
161                     this.toscaPath.push(this.mapKey);
162                 }
163             }
164             this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
165             if (value != null) {
166                 this.value = typeof value == 'object' ? JSON.stringify(value) : value;
167             }
168             this.updateValueObjOrig();
169         }
170         this.parentToscaPath = this.toscaPath;
171         if(property.subPropertyToscaFunctions != null){
172             property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction) => {
173                 if(item.subPropertyPath.toString() === this.toscaPath.toString() && this.uniqueId == null){
174                     this.toscaFunction = item.toscaFunction;
175                 }
176             });
177         }
178         this.valueObjIsValid = true;
179         this.derivedDataType = this.getDerivedPropertyType();
180         this.inputName = property.inputName;
181     }
182
183     public getActualMapKey() {
184         return (this.mapKeyError) ? this.name : this.mapKey;
185     }
186
187     public updateValueObj(valueObj:any, isValid:boolean) {
188         this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
189         this.valueObjIsValid = isValid;
190         this.valueObjIsChanged = this.hasValueObjChanged();
191     }
192
193     public updateValueObjOrig() {
194         this.valueObjOrig = _.cloneDeep(this.valueObj);
195         this.valueObjIsChanged = false;
196     }
197
198     public hasValueObjChanged() {
199         return !_.isEqual(this.valueObj, this.valueObjOrig);
200     }
201
202 }
203 export class DerivedFEPropertyMap {
204     [parentPath: string]: Array<DerivedFEProperty>;
205 }
206