Allow multiple entry for map/list when tosca function is selected
[sdc.git] / catalog-ui / src / app / models / properties-inputs / input-fe-model.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 {PropertyFEModel} from "../../models";
24 import {PROPERTY_DATA} from "../../utils/constants";
25 import {InputBEModel} from "./input-be-model";
26 import {DerivedPropertyType} from "./property-be-model";
27 import { Metadata } from "app/models/metadata";
28 import { MetadataEntry } from "app/models/metadataEntry";
29
30 export class InputFEModel extends InputBEModel {
31     isSimpleType: boolean;
32     relatedPropertyValue: any;
33     relatedPropertyName: string;
34     defaultValueObj:any;
35     defaultValueObjIsValid:boolean;
36     defaultValueObjOrig:any;
37     defaultValueObjIsChanged:boolean;
38     derivedDataType: DerivedPropertyType;
39     requiredOrig: boolean;
40     metadataOrig: Metadata;
41     metadataIsValid:boolean;
42     metadataEntries: MetadataEntry[] = [];
43     metadataMapKeyError: string;
44
45     constructor(input?: InputBEModel) {
46         super(input);
47         if (input) {
48             this.isSimpleType = PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1;
49             let relatedProperty = input.properties && input.properties[0] || input.inputs && input.inputs[0];
50             if (relatedProperty) {
51                 this.relatedPropertyValue = relatedProperty.value;
52                 this.relatedPropertyName = relatedProperty.name;
53             }
54             this.derivedDataType = this.getDerivedPropertyType();
55             this.resetDefaultValueObjValidation();
56             this.updateDefaultValueObjOrig();
57
58             this.requiredOrig = this.required;
59             this.metadataOrig = _.cloneDeep(input.metadata);
60             this.metadataIsValid = true;
61                         
62             if (input.metadata){
63                 for (let key of Object.keys(input.metadata)){
64                     this.metadataEntries.push(new MetadataEntry(key, input.metadata[key]));
65                 }
66             }
67         }
68     }
69
70     public updateDefaultValueObj(defaultValueObj:any, isValid:boolean) {
71         this.defaultValueObj = PropertyFEModel.cleanValueObj(defaultValueObj);
72         this.defaultValueObjIsValid = isValid;
73         this.defaultValueObjIsChanged = this.hasDefaultValueChanged();
74     }
75
76     public updateDefaultValueObjOrig() {
77         this.defaultValueObjOrig = _.cloneDeep(this.defaultValueObj);
78         this.defaultValueObjIsChanged = false;
79     }
80
81     public getJSONDefaultValue(): string {
82         return PropertyFEModel.stringifyValueObj(this.defaultValueObj, this.schema.property.type, this.derivedDataType);
83     }
84
85     public getDefaultValueObj(): any {
86         return PropertyFEModel.parseValueObj(this.defaultValue, this.type, this.derivedDataType, this.isToscaFunction());
87     }
88
89     public resetDefaultValueObjValidation() {
90         this.defaultValueObjIsValid = true;
91     }
92
93     hasDefaultValueChanged(): boolean {
94         return !_.isEqual(this.defaultValueObj, this.defaultValueObjOrig);
95     }
96
97     hasRequiredChanged(): boolean {
98         return this.required !== this.requiredOrig;
99     }
100
101     public updateMetadataKey(metadataEntry: MetadataEntry, newKey){
102         if (!newKey){
103                 this.metadataIsValid = false;
104             this.metadataMapKeyError = 'Key cannot be empty.';
105             metadataEntry.key = newKey;
106             return;
107         } else if (metadataEntry.metaDataMapKey != newKey && this.metadata[newKey]){
108             this.metadataIsValid = false;
109             this.metadataMapKeyError = 'This key already exists!!.';
110             metadataEntry.key = newKey;
111             return;
112         }
113         this.metadataIsValid = true;
114         this.metadataMapKeyError = null;
115
116         if(metadataEntry.metaDataMapKey != newKey){
117             this.metadata[newKey] = _.cloneDeep(this.metadata[metadataEntry.metaDataMapKey]);
118             delete this.metadata[metadataEntry.metaDataMapKey];
119             metadataEntry.metaDataMapKey = newKey;
120         }
121         metadataEntry.key = newKey;  
122     }
123
124         public updateMetadataValue(metadataEntry: MetadataEntry, value: string){
125         metadataEntry.value = value;
126         this.metadata[metadataEntry.key] = value;
127         }
128
129     public addMetadataEntry(metadataEntry: MetadataEntry){
130         this.metadataEntries.push(metadataEntry);
131         if (!this.metadata){
132             this.metadata = new Metadata;
133         }
134         this.metadata[metadataEntry.key] = metadataEntry.value;
135     }
136         
137     public deleteMetadataEntry(metadataEntry: MetadataEntry){
138         let metadataEntryIndex = this.metadataEntries.findIndex(item => item.key === metadataEntry.key);
139         if (metadataEntryIndex != -1){
140             this.metadataEntries.splice(metadataEntryIndex, 1);
141         }
142         delete this.metadata[metadataEntry.key];
143     }
144
145     public resetMetadata = (): void => {
146         if (!this.metadataOrig){
147           return;
148         }
149
150         this.metadata =  _.cloneDeep(this.metadataOrig);
151         this.metadataIsValid = true;
152
153         this.metadataEntries = [];
154         for (let key of Object.keys(this.metadata)){
155             this.metadataEntries.push(new MetadataEntry(key, this.metadata[key]));
156         }
157     }
158     
159     hasMetadataChanged(): boolean {
160         return !_.isEqual(this.metadata, this.metadataOrig);
161     }
162
163     hasChanged(): boolean {
164         return this.hasDefaultValueChanged() || this.hasRequiredChanged() || this.hasMetadataChanged();
165     }
166
167 }