Provide input name when declaring service property as input
[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
52     constructor(property: PropertyBEModel, parentName?: string, createChildOfListOrMap?: boolean, key?:string, value?:any) {
53         if (!createChildOfListOrMap) { //creating a standard derived prop
54             super(property);
55             this.parentName = parentName ? parentName : null;
56             this.propertiesName = (parentName) ? parentName + '#' + property.name : property.name;
57             this.canBeDeclared = true; //defaults to true
58         } else { //creating a direct child of list or map (ie. Item that can be deleted, with UUID instead of name)
59             super(null);
60             if(property.subPropertyToscaFunctions != null){
61                 property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction) => {
62                     if(item.subPropertyPath[0] === key){
63                         this.toscaFunction = item.toscaFunction;
64                     }
65                 });
66             }
67             this.isChildOfListOrMap = true;
68             this.canBeDeclared = false;
69             this.name = UUID.UUID();
70             this.parentName = parentName;
71             this.propertiesName = parentName + '#' + this.name;
72             
73             if (property.type == PROPERTY_TYPES.LIST) {
74                 let parentKey : string = null;
75                 if(property.value != null) {
76                     const valueJson = JSON.parse(property.value);
77                     if (key != '') {
78                         parentKey = key;
79                     }else{
80                         let indexNumber = Number(Object.keys(valueJson).sort().reverse()[0]) + 1;
81                         parentKey = indexNumber.toString();
82                     }
83                 }else {
84                     parentKey = "0";
85                 }
86                 if (property.schemaType != PROPERTY_TYPES.MAP) {
87                     this.mapKey = parentKey;
88                 }
89                 this.mapKeyError = null;
90                 this.type = property.schema.property.type;
91                 if (this.type == PROPERTY_TYPES.MAP){
92                     this.mapInlist = true;
93                     this.parentMapKey = parentKey;
94                 }
95                 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
96             } else { //map
97                 if (key) {
98                     this.mapKey = key;
99                     this.mapKeyError = null;
100                 } else {
101                     this.mapKey = '';
102                     this.mapKeyError = 'Key cannot be empty.';
103                 }
104                 this.type = property.type;
105                 if (property.schema.property.type == PROPERTY_TYPES.MAP){
106                     const schProp = new SchemaProperty();
107                     schProp.isSimpleType = true;
108                     schProp.isDataType = false;
109                     schProp.simpleType = PROPERTY_TYPES.STRING;
110                     this.schema = new SchemaPropertyGroupModel(schProp);
111                     this.schemaType = PROPERTY_TYPES.STRING;
112                     if (property instanceof DerivedFEProperty) {
113                         this.parentMapKey = property.parentMapKey;
114                         if (value != null && typeof value == 'object') {
115                             this.toscaFunction = property.toscaFunction;
116                         }
117                     }
118                 } else {
119                     this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
120                 }
121         
122             }
123             this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
124             if (value != null) {
125                 this.value = typeof value == 'object' ? JSON.stringify(value) : value;
126             }
127             this.updateValueObjOrig();
128         }
129         // this.constraints = property ? property.constraints : null;
130         this.valueObjIsValid = true;
131         this.derivedDataType = this.getDerivedPropertyType();
132         this.inputName = property.inputName;
133     }
134
135     public getActualMapKey() {
136         return (this.mapKeyError) ? this.name : this.mapKey;
137     }
138
139     public updateValueObj(valueObj:any, isValid:boolean) {
140         this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
141         this.valueObjIsValid = isValid;
142         this.valueObjIsChanged = this.hasValueObjChanged();
143     }
144
145     public updateValueObjOrig() {
146         this.valueObjOrig = _.cloneDeep(this.valueObj);
147         this.valueObjIsChanged = false;
148     }
149
150     public hasValueObjChanged() {
151         return !_.isEqual(this.valueObj, this.valueObjOrig);
152     }
153
154 }
155 export class DerivedFEPropertyMap {
156     [parentPath: string]: Array<DerivedFEProperty>;
157 }
158