Sync Integ to Master
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / graph / datatype / GraphNode.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.dao.graph.datatype;
22
23 import com.google.gson.Gson;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
26 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
27
28 import java.util.List;
29 import java.util.Map;
30
31 public abstract class GraphNode extends GraphElement {
32
33         private static final Gson gson = new Gson();
34
35         private NodeTypeEnum label;
36
37         protected Gson getGson() {
38                 return gson;
39         }
40
41         protected GraphNode(NodeTypeEnum label) {
42                 super(GraphElementTypeEnum.Node);
43
44                 this.label = label;
45         }
46
47         public String getLabel() {
48                 return label.getName();
49         }
50
51         public ImmutablePair<String, Object> getKeyValueId() {
52                 ImmutablePair<String, Object> keyValue = new ImmutablePair<String, Object>(getUniqueIdKey(), getUniqueId());
53                 return keyValue;
54         }
55
56         protected void addIfExists(Map<String, Object> map, GraphPropertiesDictionary property, Object value) {
57                 if (value != null) {
58                         if (value instanceof List || value instanceof Map) {
59                                 value = getGson().toJson(value);
60                         }
61                         map.put(property.getProperty(), value);
62                 }
63         }
64
65         public String getUniqueIdKey() {
66                 return GraphPropertiesDictionary.UNIQUE_ID.getProperty();
67         }
68
69         public abstract String getUniqueId();
70
71         @Override
72         public String toString() {
73                 return "GraphNode [label=" + label + ", parent: " + super.toString() + "]";
74         }
75
76 }