Fix PropertyConvertor initialization (use spring)
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / PropertyDataDefinition.java
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 package org.openecomp.sdc.be.datatypes.elements;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
25 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
26
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34
35 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
36
37 @EqualsAndHashCode(callSuper = false)
38 @Data
39 public class PropertyDataDefinition extends ToscaDataDefinition {
40
41     private String uniqueId;
42
43     // "boolean", "string", "float", "integer", "version" })
44     private String type;
45
46     private Boolean required = Boolean.FALSE;
47
48     protected boolean definition = false;
49
50     private String defaultValue;
51
52     private String description;
53
54     private SchemaDefinition schema;
55
56     private boolean password;
57
58     private String name;
59
60     private String value;
61
62     private String label;
63     protected Boolean hidden = Boolean.FALSE;
64     private Boolean immutable = Boolean.FALSE;
65
66     private String inputPath;
67     private String status;
68     private String inputId;
69     private String instanceUniqueId;
70     private String propertyId;
71     private String parentPropertyType;
72     private String subPropertyInputPath;
73
74     private List<Annotation> annotations;
75     /**
76      * The resource id which this property belongs to
77      */
78     private String parentUniqueId;
79
80     private List<GetInputValueDataDefinition> getInputValues;
81
82     private Boolean isDeclaredListInput = Boolean.FALSE;
83
84     private List<GetPolicyValueDataDefinition> getPolicyValues;
85
86     private List<String> propertyConstraints;
87
88     public PropertyDataDefinition() {
89         super();
90     }
91
92     public PropertyDataDefinition(Map<String, Object> pr) {
93         super(pr);
94     }
95
96     public PropertyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
97         super();
98         this.setUniqueId(propertyDataDefinition.getUniqueId());
99         this.setRequired(propertyDataDefinition.isRequired());
100         this.setDefaultValue(propertyDataDefinition.getDefaultValue());
101         this.setDefinition(propertyDataDefinition.getDefinition());
102         this.setDescription(propertyDataDefinition.getDescription());
103         this.setSchema(propertyDataDefinition.getSchema());
104         this.setPassword(propertyDataDefinition.isPassword());
105         this.setType(propertyDataDefinition.getType());
106         this.setName(propertyDataDefinition.getName());
107         this.setValue(propertyDataDefinition.getValue());
108         this.setRequired(propertyDataDefinition.isRequired());
109         this.setHidden(propertyDataDefinition.isHidden());
110         this.setLabel(propertyDataDefinition.getLabel());
111         this.setImmutable(propertyDataDefinition.isImmutable());
112         this.setParentUniqueId(propertyDataDefinition.getParentUniqueId());
113         this.setOwnerId(propertyDataDefinition.getOwnerId());
114         this.setGetInputValues(propertyDataDefinition.getGetInputValues());
115         this.setGetPolicyValues(propertyDataDefinition.getGetPolicyValues());
116         this.setInputPath(propertyDataDefinition.getInputPath());
117         this.setStatus(propertyDataDefinition.getStatus());
118         this.setInputId(propertyDataDefinition.getInputId());
119         this.setInstanceUniqueId(propertyDataDefinition.getInstanceUniqueId());
120         this.setPropertyId(propertyDataDefinition.getPropertyId());
121         this.parentPropertyType = propertyDataDefinition.getParentPropertyType();
122         this.subPropertyInputPath = propertyDataDefinition.getSubPropertyInputPath();
123         if (isNotEmpty(propertyDataDefinition.annotations)) {
124             this.setAnnotations(propertyDataDefinition.annotations);
125         }
126         if(isNotEmpty(propertyDataDefinition.getPropertyConstraints())){
127             setPropertyConstraints(new ArrayList<>(propertyDataDefinition.getPropertyConstraints()));
128         }
129         this.setIsDeclaredListInput(propertyDataDefinition.getIsDeclaredListInput());
130     }
131
132     // @Override
133     public boolean isDefinition() {
134         return true;
135     }
136
137     public boolean getDefinition() {
138         return definition;
139     }
140
141     public Boolean isRequired() {
142         return required;
143     }
144
145     public void setSchemaType(String schemaType) {
146         if (schema != null && schema.getProperty() != null) {
147             schema.getProperty().setType(schemaType);
148         }
149     }
150
151     public String getSchemaType() {
152         if (schema != null && schema.getProperty() != null) {
153             return schema.getProperty().getType();
154         }
155         return null;
156     }
157
158     public PropertyDataDefinition getSchemaProperty() {
159         if (schema != null) {
160             return schema.getProperty();
161         }
162
163         return null;
164     }
165
166     public Boolean isHidden() {
167         return hidden;
168     }
169
170     public Boolean isImmutable() {
171         return immutable;
172     }
173
174     public String getParentUniqueId() {
175         return getOwnerId();
176     }
177
178     public void setParentUniqueId(String parentUniqueId) {
179         setOwnerId(parentUniqueId);
180     }
181
182     public List<GetPolicyValueDataDefinition> safeGetGetPolicyValues() {
183         return CollectionUtils.isEmpty(getPolicyValues) ? new ArrayList<>() : getPolicyValues;
184     }
185
186     public boolean typeEquals(Object obj) {
187         if (this == obj) {
188             return true;
189         }
190         if (obj == null) {
191             return false;
192         }
193         if (getClass() != obj.getClass()) {
194             return false;
195         }
196         PropertyDataDefinition other = (PropertyDataDefinition) obj;
197         if (this.getType() == null) {
198             return other.getType() == null;
199         }
200         if (!this.type.equals(other.type)) {
201             return false;
202         }
203         String thisSchemaType = this.getSchemaType();
204         String otherSchemaType = other.getSchemaType();
205         if (thisSchemaType == null) {
206             return otherSchemaType == null;
207         }
208         return thisSchemaType.equals(otherSchemaType);
209     }
210
211     @Override
212     public Object getToscaPresentationValue(JsonPresentationFields field) {
213         switch (field) {
214             case NAME:
215                 return name;
216             case UNIQUE_ID:
217                 return uniqueId;
218             case PASSWORD:
219                 return password;
220             case TYPE:
221                 return type;
222             case DEFINITION:
223                 return definition;
224             case VALUE:
225                 return value;
226             case DEFAULT_VALUE:
227                 return defaultValue;
228             default:
229                 return super.getToscaPresentationValue(field);
230         }
231     }
232
233     @Override
234     public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
235         switch (name) {
236             case NAME:
237                 setName((String) value);
238                 break;
239             case UNIQUE_ID:
240                 setUniqueId((String) value);
241                 break;
242             case PASSWORD:
243                 setPassword((Boolean) value);
244                 break;
245             case TYPE:
246                 setType((String) value);
247                 break;
248             case DEFINITION:
249                 setDefinition((Boolean) value);
250                 break;
251             case VALUE:
252                 setValue((String) value);
253                 break;
254             case DEFAULT_VALUE:
255                 setDefaultValue((String) value);
256                 break;
257             default:
258                 super.setToscaPresentationValue(name, value);
259                 break;
260         }
261     }
262
263     private <T extends ToscaDataDefinition> boolean compareSchemaType(T other) {
264         return !"list".equals(type) && !"map".equals(type) || this.getSchema().getProperty().getType().equals(((PropertyDataDefinition) other).getSchema().getProperty().getType());
265     }
266
267     @Override
268     public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
269         if (this.getType() != null
270                 && this.getType().equals(other.getToscaPresentationValue(JsonPresentationFields.TYPE))
271                 && compareSchemaType(other)) {
272             other.setOwnerId(getOwnerId());
273             if (allowDefaultValueOverride
274                     && getDefaultValue() != null
275                     && !getDefaultValue().isEmpty()) {
276                 other.setToscaPresentationValue(JsonPresentationFields.DEFAULT_VALUE, getDefaultValue());
277             }
278             return other;
279         }
280         return null;
281     }
282
283     public void convertPropertyDataToInstancePropertyData() {
284         if (null != value) {
285             defaultValue = value;
286         }
287     }
288
289     public boolean isGetInputProperty() {
290         return this.getGetInputValues() != null && !this.getGetInputValues().isEmpty();
291     }
292
293     public void setAnnotations(List<Annotation> newAnnotations) {
294         Set<Annotation> annotationSet = isNotEmpty(newAnnotations) ? new HashSet<>(newAnnotations) : new HashSet<>();
295         //We would to prioritize the new valid annotations over the old ones if the same one existed.
296         if (this.annotations != null) {
297             annotationSet.addAll(this.annotations);
298         }
299
300         this.annotations = new ArrayList<>(annotationSet);
301         setToscaPresentationValue(JsonPresentationFields.ANNOTATIONS, this.annotations);
302     }
303
304     public List<Annotation> getAnnotations() {
305         return (List<Annotation>) getToscaPresentationValue(JsonPresentationFields.ANNOTATIONS);
306     }
307 }