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