Support for Nested/Hierarchical Services
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / category / CategoryData.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.category;
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.datatypes.category.CategoryDataDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
28
29 import java.lang.reflect.Type;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 public class CategoryData extends GraphNode {
35         private CategoryDataDefinition categoryDataDefinition;
36
37         public CategoryData(NodeTypeEnum label) {
38                 super(label);
39                 categoryDataDefinition = new CategoryDataDefinition();
40         }
41
42         public CategoryData(NodeTypeEnum label, CategoryDataDefinition categoryDataDefinition) {
43                 super(label);
44                 this.categoryDataDefinition = categoryDataDefinition;
45         }
46
47         public CategoryData(Map<String, Object> properties) {
48                 this(NodeTypeEnum.getByName((String) properties.get(GraphPropertiesDictionary.LABEL.getProperty())));
49
50                 categoryDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
51                 categoryDataDefinition
52                                 .setNormalizedName((String) properties.get(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty()));
53                 categoryDataDefinition.setName((String) properties.get(GraphPropertiesDictionary.NAME.getProperty()));
54                 final Object useServiceSubstitutionForNestedServicesProperty = properties.get(GraphPropertiesDictionary.USE_SERVICE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty());
55                 final boolean useServiceSubstitutionForNestedServices = useServiceSubstitutionForNestedServicesProperty != null && (boolean) useServiceSubstitutionForNestedServicesProperty;
56                 categoryDataDefinition.setUseServiceSubstitutionForNestedServices(useServiceSubstitutionForNestedServices);
57
58                 Type listType = new TypeToken<List<String>>() {
59                 }.getType();
60                 List<String> iconsfromJson = getGson()
61                                 .fromJson((String) properties.get(GraphPropertiesDictionary.ICONS.getProperty()), listType);
62                 categoryDataDefinition.setIcons(iconsfromJson);
63         }
64
65         @Override
66         public String getUniqueId() {
67                 return categoryDataDefinition.getUniqueId();
68         }
69
70         public CategoryDataDefinition getCategoryDataDefinition() {
71                 return categoryDataDefinition;
72         }
73
74         @Override
75         public Map<String, Object> toGraphMap() {
76                 Map<String, Object> map = new HashMap<>();
77
78                 addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, categoryDataDefinition.getUniqueId());
79                 addIfExists(map, GraphPropertiesDictionary.NAME, categoryDataDefinition.getName());
80                 addIfExists(map, GraphPropertiesDictionary.NORMALIZED_NAME, categoryDataDefinition.getNormalizedName());
81                 // String icons=getGson().toJson(categoryDataDefinition.getIcons());
82                 // addIfExists(map, GraphPropertiesDictionary.ICONS, icons);
83         addIfExists(map, GraphPropertiesDictionary.ICONS, categoryDataDefinition.getIcons());
84         addIfExists(map, GraphPropertiesDictionary.USE_SERVICE_SUBSTITUTION_FOR_NESTED_SERVICES, categoryDataDefinition.isUseServiceSubstitutionForNestedServices());
85                 return map;
86         }
87
88         @Override
89         public String toString() {
90                 return "CategoryData [categoryDataDefinition=" + categoryDataDefinition + "]";
91         }
92
93 }