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