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