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