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