Support of get_property in property assignment
[sdc.git] / catalog-ui / src / app / models / properties-inputs / property-be-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 {PROPERTY_DATA, PROPERTY_TYPES} from 'app/utils/constants';
22 import {SchemaProperty, SchemaPropertyGroupModel} from '../schema-property';
23 import {ToscaPresentationData} from '../tosca-presentation';
24 import {PropertyInputDetail} from './property-input-detail';
25 import {Metadata} from '../metadata';
26 import {ToscaGetFunctionType} from "../tosca-get-function-type";
27 import {ToscaGetFunctionDto} from '../tosca-get-function-dto';
28 import {PropertySource} from '../property-source';
29
30 export enum DerivedPropertyType {
31     SIMPLE,
32     LIST,
33     MAP,
34     COMPLEX
35 }
36 export class PropertyPolicyDetail {
37     policyId: string;
38     propertyName: string;
39     constructor(propertyPolicy?: PropertyPolicyDetail) {
40         if(propertyPolicy) {
41             this.policyId = propertyPolicy.policyId;
42             this.propertyName = propertyPolicy.propertyName;
43         }
44     }
45 }
46
47 export class PropertyBEModel {
48
49     constraints: any[];
50     defaultValue: string;
51     definition: boolean;
52     description: string;
53     fromDerived: boolean;
54     getInputValues: PropertyInputDetail[];
55     getPolicyValues: PropertyPolicyDetail[];
56     name: string;
57     origName: string;
58     parentUniqueId: string;
59     password: boolean;
60     required: boolean;
61     schema: SchemaPropertyGroupModel;
62     schemaType: string;
63     type: string;
64     uniqueId: string;
65     value: string;
66     parentPropertyType: string;
67     subPropertyInputPath: string;
68     inputPath: string;
69     toscaPresentation: ToscaPresentationData;
70     metadata: Metadata;
71     //deprecated
72     toscaGetFunctionType: ToscaGetFunctionType;
73     toscaGetFunction: ToscaGetFunctionDto;
74
75     constructor(property?: PropertyBEModel) {
76         if (property) {
77             this.constraints = property.constraints;
78             this.defaultValue = property.defaultValue;
79             this.description = property.description;
80             this.fromDerived = property.fromDerived;
81             this.name = property.name;
82             this.origName = property.origName;
83             this.parentUniqueId = property.parentUniqueId;
84             this.password = property.password;
85             this.required = property.required;
86             this.schema = property.schema;
87             this.schemaType = property.schemaType;
88             this.type = property.type;
89             this.uniqueId = property.uniqueId;
90             this.value = property.value;
91             this.definition = property.definition;
92             this.getInputValues = property.getInputValues;
93             this.parentPropertyType = property.parentPropertyType;
94             this.subPropertyInputPath = property.subPropertyInputPath;
95             this.toscaPresentation = property.toscaPresentation;
96             this.getPolicyValues = property.getPolicyValues;
97             this.inputPath = property.inputPath;
98             this.metadata = property.metadata;
99             if (property.toscaGetFunction) {
100                 this.toscaGetFunction = property.toscaGetFunction;
101             } else if (property.toscaGetFunctionType) {
102                 this.toscaGetFunction = new ToscaGetFunctionDto();
103                 this.toscaGetFunction.functionType = property.toscaGetFunctionType;
104                 this.toscaGetFunction.propertySource = PropertySource.SELF;
105             }
106         }
107
108         if (!this.schema || !this.schema.property) {
109             this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
110         } else { // forcing creating new object, so editing different one than the object in the table
111             this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
112         }
113     }
114
115     public toJSON = (): any => {
116         const temp = angular.copy(this);
117         temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
118         temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
119         return temp;
120     }
121
122     public getDerivedPropertyType = (): DerivedPropertyType => {
123         if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
124             return DerivedPropertyType.SIMPLE;
125         }
126         if (this.type === PROPERTY_TYPES.LIST) {
127             return DerivedPropertyType.LIST;
128         }
129         if (this.type === PROPERTY_TYPES.MAP) {
130             return DerivedPropertyType.MAP;
131         }
132         return DerivedPropertyType.COMPLEX;
133     }
134
135     /**
136      * Parses default value to JSON.
137      */
138     public parseDefaultValueToJson(): any {
139         if (this.defaultValue == undefined) {
140             return undefined;
141         }
142
143         const propertyType: DerivedPropertyType = this.getDerivedPropertyType();
144         if (propertyType == DerivedPropertyType.SIMPLE) {
145             return this.parseDefaultSimpleValue();
146         }
147
148         try {
149             return JSON.parse(this.defaultValue);
150         } catch (e) {
151             console.error(`Could not parse the property of type '${this.type}' default value to JSON '${this.defaultValue}'`, e);
152         }
153
154         return undefined;
155     }
156
157     private parseDefaultSimpleValue() {
158         switch (this.type) {
159             case PROPERTY_TYPES.INTEGER:
160                 try {
161                     return parseInt(this.defaultValue);
162                 } catch (e) {
163                     console.error(`Could not parse the property of type '${this.type}' default value to int '${this.defaultValue}'`, e);
164                 }
165                 return undefined;
166             case PROPERTY_TYPES.FLOAT:
167                 try {
168                     return parseFloat(this.defaultValue);
169                 } catch (e) {
170                     console.error(`Could not parse the property of type '${this.type}' default value to float '${this.defaultValue}'`, e);
171                 }
172                 return undefined;
173             case PROPERTY_TYPES.BOOLEAN:
174                 return this.defaultValue === 'true';
175             default:
176                 return this.defaultValue;
177         }
178     }
179
180     /**
181      * Checks whether the property value is a tosca get function (e.g. get_input, get_property, get_attribute)
182      */
183     public isToscaGetFunction(): boolean {
184         return this.toscaGetFunction != null;
185     }
186 }
187