2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
30 export class InputFEModel extends InputBEModel {
31 isSimpleType: boolean;
32 relatedPropertyValue: any;
33 relatedPropertyName: string;
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;
45 constructor(input?: InputBEModel) {
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;
54 this.derivedDataType = this.getDerivedPropertyType();
55 this.resetDefaultValueObjValidation();
56 this.updateDefaultValueObjOrig();
58 this.requiredOrig = this.required;
59 this.metadataOrig = _.cloneDeep(input.metadata);
60 this.metadataIsValid = true;
63 for (let key of Object.keys(input.metadata)){
64 this.metadataEntries.push(new MetadataEntry(key, input.metadata[key]));
70 public updateDefaultValueObj(defaultValueObj:any, isValid:boolean) {
71 this.defaultValueObj = PropertyFEModel.cleanValueObj(defaultValueObj);
72 this.defaultValueObjIsValid = isValid;
73 this.defaultValueObjIsChanged = this.hasDefaultValueChanged();
76 public updateDefaultValueObjOrig() {
77 this.defaultValueObjOrig = _.cloneDeep(this.defaultValueObj);
78 this.defaultValueObjIsChanged = false;
81 public getJSONDefaultValue(): string {
82 return PropertyFEModel.stringifyValueObj(this.defaultValueObj, this.schema.property.type, this.derivedDataType);
85 public getDefaultValueObj(): any {
86 return PropertyFEModel.parseValueObj(this.defaultValue, this.type, this.derivedDataType);
89 public resetDefaultValueObjValidation() {
90 this.defaultValueObjIsValid = true;
93 hasDefaultValueChanged(): boolean {
94 return !_.isEqual(this.defaultValueObj, this.defaultValueObjOrig);
97 hasRequiredChanged(): boolean {
98 return this.required !== this.requiredOrig;
101 public updateMetadataKey(metadataEntry: MetadataEntry, newKey){
103 this.metadataIsValid = false;
104 this.metadataMapKeyError = 'Key cannot be empty.';
105 metadataEntry.key = newKey;
107 } else if (metadataEntry.metaDataMapKey != newKey && this.metadata[newKey]){
108 this.metadataIsValid = false;
109 this.metadataMapKeyError = 'This key already exists!!.';
110 metadataEntry.key = newKey;
113 this.metadataIsValid = true;
114 this.metadataMapKeyError = null;
116 if(metadataEntry.metaDataMapKey != newKey){
117 this.metadata[newKey] = _.cloneDeep(this.metadata[metadataEntry.metaDataMapKey]);
118 delete this.metadata[metadataEntry.metaDataMapKey];
119 metadataEntry.metaDataMapKey = newKey;
121 metadataEntry.key = newKey;
124 public updateMetadataValue(metadataEntry: MetadataEntry, value: string){
125 metadataEntry.value = value;
126 this.metadata[metadataEntry.key] = value;
129 public addMetadataEntry(metadataEntry: MetadataEntry){
130 this.metadataEntries.push(metadataEntry);
132 this.metadata = new Metadata;
134 this.metadata[metadataEntry.key] = metadataEntry.value;
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);
142 delete this.metadata[metadataEntry.key];
145 public resetMetadata = (): void => {
146 this.metadata = _.cloneDeep(this.metadataOrig);
147 this.metadataIsValid = true;
149 this.metadataEntries = [];
150 for (let key of Object.keys(this.metadata)){
151 this.metadataEntries.push(new MetadataEntry(key, this.metadata[key]));
155 hasMetadataChanged(): boolean {
156 return !_.isEqual(this.metadata, this.metadataOrig);
159 hasChanged(): boolean {
160 return this.hasDefaultValueChanged() || this.hasRequiredChanged() || this.hasMetadataChanged();