8fddaa3ebe77cc43eaea47ec10836bf5c199ed7b
[aai/gizmo.git] / src / main / java / org / onap / crud / entity / Vertex.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.entity;
25
26 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
27
28 import com.google.common.base.CaseFormat;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.annotations.SerializedName;
32
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.Set;
37
38 import org.eclipse.persistence.dynamic.DynamicType;
39 import org.eclipse.persistence.internal.helper.DatabaseField;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.mappings.DatabaseMapping;
42 import org.json.JSONObject;
43 import org.onap.aaiutils.oxm.OxmModelLoader;
44 import org.onap.crud.exception.CrudException;
45 import org.onap.crud.util.CrudServiceUtil;
46 import org.onap.schema.OxmModelValidator;
47
48 public class Vertex {
49   private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
50       .create();
51
52   @SerializedName(value = "id", alternate = { "key" })
53   private final Optional<String> id;
54
55   private final String type;
56   private final Map<String, Object> properties;
57
58   private Vertex(Builder builder) {
59     this.id = builder.id;
60     this.type = builder.type;
61     this.properties = builder.properties;
62   }
63
64   public static class Builder {
65     private Optional<String> id = Optional.empty();
66     private final String type;
67     private final Map<String, Object> properties = new HashMap<String, Object>();
68
69     public Builder(String type) {
70       if (type == null) {
71         throw new IllegalArgumentException("Type cannot be null");
72       }
73       this.type = type;
74     }
75
76     public Builder id(String id) {
77       if (id == null) {
78         throw new IllegalArgumentException("id cannot be null");
79       }
80
81       this.id = Optional.of(id);
82       return this;
83     }
84
85     public Builder property(String key, Object value) {
86       if (key == null || value == null) {
87         throw new IllegalArgumentException("Property key/value cannot be null");
88       }
89       properties.put(key, value);
90       return this;
91     }
92
93     public Vertex build() {
94       return new Vertex(this);
95     }
96   }
97
98   public String toJson() {
99     return gson.toJson(this);
100   }
101
102   public String toJson(Gson customGson) {
103     return customGson.toJson(this);
104   }
105
106   public static Vertex fromJson(String jsonString, String version) throws CrudException {
107     Builder builder;
108
109     try {
110       JSONObject doc = new JSONObject(jsonString);
111       String type = doc.getString("type");
112       builder = new Builder(type).id(doc.getString("key"));
113       
114       type = OxmModelValidator.resolveCollectionType(version, type);
115       DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
116       String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type));
117
118       final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
119       final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
120       
121       
122       if (modelObjectType == null) {
123         throw new CrudException("Unable to load oxm version", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
124       }
125
126       if (doc.has("properties")) {
127         JSONObject jsonProps = doc.getJSONObject("properties");
128         for (String key : (Set<String>)jsonProps.keySet()) {
129           String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
130           DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
131           
132           if (mapping == null) {
133             // This might be one of the reserved properties
134             mapping = reservedType.getDescriptor().getMappingForAttributeName(keyJavaName);
135           }
136           
137           if (mapping != null) {
138             DatabaseField field = mapping.getField();
139             Object value = CrudServiceUtil.validateFieldType(jsonProps.get(key).toString(), field.getType());
140             builder.property(key, value);
141           }
142         }
143       }
144     }
145     catch (Exception ex) {
146       throw new CrudException("Unable to transform response: " + jsonString, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
147     }
148     
149     return builder.build(); 
150   }
151
152   @Override
153   public String toString() {
154     return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
155   }
156
157   public Optional<String> getId() {
158     return id;
159   }
160
161   public String getType() {
162     return type;
163   }
164
165   public Map<String, Object> getProperties() {
166     return properties;
167   }
168
169 }