re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / AttributeData.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.Map;
34
35 public class AttributeData extends GraphNode {
36         PropertyDataDefinition attributeDataDefinition;
37
38         public AttributeData() {
39                 super(NodeTypeEnum.Attribute);
40                 attributeDataDefinition = new PropertyDataDefinition();
41         }
42
43         public AttributeData(PropertyDataDefinition attributeDataDefinition) {
44                 super(NodeTypeEnum.Attribute);
45                 this.attributeDataDefinition = attributeDataDefinition;
46         }
47
48         @Override
49         public String toString() {
50                 return "AttributeData [attributeDataDefinition=" + attributeDataDefinition + "]";
51         }
52
53         @Override
54         public String getUniqueId() {
55                 return attributeDataDefinition.getUniqueId();
56         }
57
58         public PropertyDataDefinition getAttributeDataDefinition() {
59                 return attributeDataDefinition;
60         }
61
62         public void setAttributeDataDefinition(PropertyDataDefinition attributeDataDefinition) {
63                 this.attributeDataDefinition = attributeDataDefinition;
64         }
65
66         public AttributeData(Map<String, Object> properties) {
67
68                 this();
69
70                 attributeDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
71
72                 attributeDataDefinition.setType((String) properties.get(GraphPropertiesDictionary.TYPE.getProperty()));
73
74                 attributeDataDefinition
75                                 .setDescription((String) properties.get(GraphPropertiesDictionary.DESCRIPTION.getProperty()));
76
77                 String defaultValue = (String) properties.get(GraphPropertiesDictionary.DEFAULT_VALUE.getProperty());
78                 if (Constants.GRAPH_EMPTY_VALUE.equals(defaultValue)) {
79                         attributeDataDefinition.setDefaultValue(null);
80                 } else {
81                         attributeDataDefinition.setDefaultValue(defaultValue);
82                 }
83
84                 attributeDataDefinition.setStatus((String) properties.get(GraphPropertiesDictionary.STATUS.getProperty()));
85
86                 attributeDataDefinition.setName((String) properties.get(GraphPropertiesDictionary.NAME.getProperty()));
87
88                 attributeDataDefinition.setValue((String) properties.get(GraphPropertiesDictionary.VALUE.getProperty()));
89
90                 Type schemaType = new TypeToken<SchemaDefinition>() {
91                 }.getType();
92                 SchemaDefinition schema = getGson()
93                                 .fromJson((String) properties.get(GraphPropertiesDictionary.ENTRY_SCHEMA.getProperty()), schemaType);
94                 attributeDataDefinition.setSchema(schema);
95         }
96
97         @Override
98         public Map<String, Object> toGraphMap() {
99
100                 Map<String, Object> map = new HashMap<>();
101
102                 addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, attributeDataDefinition.getUniqueId());
103
104                 addIfExists(map, GraphPropertiesDictionary.TYPE, attributeDataDefinition.getType());
105
106                 addIfExists(map, GraphPropertiesDictionary.DESCRIPTION, attributeDataDefinition.getDescription());
107
108                 String defaultValue = attributeDataDefinition.getDefaultValue();
109                 if (defaultValue == null) {
110                         defaultValue = Constants.GRAPH_EMPTY_VALUE;
111                 }
112
113                 addIfExists(map, GraphPropertiesDictionary.DEFAULT_VALUE, defaultValue);
114
115                 addIfExists(map, GraphPropertiesDictionary.STATUS, attributeDataDefinition.getStatus());
116
117                 addIfExists(map, GraphPropertiesDictionary.NAME, attributeDataDefinition.getName());
118
119                 addIfExists(map, GraphPropertiesDictionary.VALUE, attributeDataDefinition.getValue());
120
121                 SchemaDefinition entrySchema = attributeDataDefinition.getSchema();
122                 if (entrySchema != null) {
123                         String entrySchemaStr = getGson().toJson(entrySchema);
124                         addIfExists(map, GraphPropertiesDictionary.ENTRY_SCHEMA, entrySchemaStr);
125                 }
126
127                 return map;
128         }
129
130 }