re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / PropertyData.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.resources.data;
22
23 import com.google.gson.reflect.TypeToken;
24 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
25 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
26 import org.openecomp.sdc.be.dao.utils.Constants;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
29 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
30
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 public class PropertyData extends GraphNode {
37
38         PropertyDataDefinition propertyDataDefinition;
39
40         private List<String> constraints;
41
42         public PropertyData() {
43                 super(NodeTypeEnum.Property);
44                 propertyDataDefinition = new PropertyDataDefinition();
45         }
46
47         public PropertyData(PropertyDataDefinition propertyDataDefinition, List<String> constraints) {
48                 super(NodeTypeEnum.Property);
49                 this.propertyDataDefinition = propertyDataDefinition;
50                 this.constraints = constraints;
51         }
52
53         public PropertyData(Map<String, Object> properties) {
54
55                 this();
56
57                 propertyDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
58
59                 propertyDataDefinition.setType((String) properties.get(GraphPropertiesDictionary.TYPE.getProperty()));
60
61                 propertyDataDefinition.setRequired((Boolean) properties.get(GraphPropertiesDictionary.REQUIRED.getProperty()));
62
63                 String defaultValue = (String) properties.get(GraphPropertiesDictionary.DEFAULT_VALUE.getProperty());
64                 if (Constants.GRAPH_EMPTY_VALUE.equals(defaultValue)) {
65                         propertyDataDefinition.setDefaultValue(null);
66                 } else {
67                         propertyDataDefinition.setDefaultValue(defaultValue);
68                 }
69
70                 propertyDataDefinition
71                                 .setDescription((String) properties.get(GraphPropertiesDictionary.DESCRIPTION.getProperty()));
72
73                 Type listType = new TypeToken<List<String>>() {
74                 }.getType();
75                 List<String> constraintsfromJson = getGson()
76                                 .fromJson((String) properties.get(GraphPropertiesDictionary.CONSTRAINTS.getProperty()), listType);
77                 setConstraints(constraintsfromJson);
78                 // setConstraints((List<String>)
79                 // properties.get(GraphPropertiesDictionary.CONSTRAINTS.getProperty()));
80
81                 Type schemaType = new TypeToken<SchemaDefinition>() {
82                 }.getType();
83                 SchemaDefinition schema = getGson()
84                                 .fromJson((String) properties.get(GraphPropertiesDictionary.ENTRY_SCHEMA.getProperty()), schemaType);
85                 propertyDataDefinition.setSchema(schema);
86
87         }
88
89         @Override
90         public Map<String, Object> toGraphMap() {
91
92                 Map<String, Object> map = new HashMap<>();
93
94                 addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, propertyDataDefinition.getUniqueId());
95
96                 addIfExists(map, GraphPropertiesDictionary.TYPE, propertyDataDefinition.getType());
97
98                 addIfExists(map, GraphPropertiesDictionary.DESCRIPTION, propertyDataDefinition.getDescription());
99
100                 String defaultValue = propertyDataDefinition.getDefaultValue();
101                 if (defaultValue == null) {
102                         defaultValue = Constants.GRAPH_EMPTY_VALUE;
103                 }
104                 addIfExists(map, GraphPropertiesDictionary.DEFAULT_VALUE, defaultValue);
105
106                 addIfExists(map, GraphPropertiesDictionary.REQUIRED, propertyDataDefinition.isRequired());
107
108                 addIfExists(map, GraphPropertiesDictionary.CONSTRAINTS, getConstraints());
109
110                 SchemaDefinition entrySchema = propertyDataDefinition.getSchema();
111                 if (entrySchema != null) {
112                         String entrySchemaStr = getGson().toJson(entrySchema);
113                         addIfExists(map, GraphPropertiesDictionary.ENTRY_SCHEMA, entrySchemaStr);
114                 }
115                 // String constraintsAsJson = getGson().toJson(getConstraints());
116                 // addIfExists(map, GraphPropertiesDictionary.CONSTRAINTS,
117                 // constraintsAsJson);
118
119                 return map;
120         }
121
122         public List<String> getConstraints() {
123                 return constraints;
124         }
125
126         public void setConstraints(List<String> constraints) {
127                 this.constraints = constraints;
128         }
129
130         @Override
131         public String getUniqueId() {
132                 return propertyDataDefinition.getUniqueId();
133         }
134
135         public PropertyDataDefinition getPropertyDataDefinition() {
136                 return propertyDataDefinition;
137         }
138
139         public void setPropertyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
140                 this.propertyDataDefinition = propertyDataDefinition;
141         }
142
143         @Override
144         public String toString() {
145                 return "PropertyData [propertyDataDefinition=" + propertyDataDefinition + ", constraints=" + constraints + "]";
146         }
147
148 }