2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.crud.entity;
23 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
25 import com.google.common.base.CaseFormat;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.annotations.SerializedName;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Optional;
37 import org.eclipse.persistence.dynamic.DynamicType;
38 import org.eclipse.persistence.internal.helper.DatabaseField;
39 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
40 import org.eclipse.persistence.mappings.DatabaseMapping;
41 import org.json.JSONArray;
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;
49 private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
52 @SerializedName(value = "id", alternate = { "key" })
53 private final Optional<String> id;
55 private final String type;
56 private final Map<String, Object> properties;
58 private Vertex(Builder builder) {
60 this.type = builder.type;
61 this.properties = builder.properties;
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>();
69 public Builder(String type) {
71 throw new IllegalArgumentException("Type cannot be null");
76 public Builder id(String id) {
78 throw new IllegalArgumentException("id cannot be null");
81 this.id = Optional.of(id);
85 public Builder property(String key, Object value) {
86 if (key == null || value == null) {
87 throw new IllegalArgumentException("Property key/value cannot be null");
89 properties.put(key, value);
93 public Vertex build() {
94 return new Vertex(this);
98 public String toJson() {
99 return gson.toJson(this);
102 public String toJson(Gson customGson) {
103 return customGson.toJson(this);
106 public static Vertex fromJson(String jsonString, String version) throws CrudException {
107 JSONObject doc = new JSONObject(jsonString);
108 return fromJson(doc, version);
111 public static Vertex fromJson(JSONObject jsonObject, String version) throws CrudException {
115 String type = jsonObject.getString("type");
116 builder = new Builder(type).id(jsonObject.getString("key"));
118 type = OxmModelValidator.resolveCollectionType(version, type);
119 DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
120 String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type));
122 final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
123 final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
126 if (modelObjectType == null) {
127 throw new CrudException("Unable to load oxm version", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
130 if (jsonObject.has("properties")) {
131 JSONObject jsonProps = jsonObject.getJSONObject("properties");
132 for (String key : (Set<String>)jsonProps.keySet()) {
133 String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
134 DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
136 if (mapping == null) {
137 // This might be one of the reserved properties
138 mapping = reservedType.getDescriptor().getMappingForAttributeName(keyJavaName);
141 if (mapping != null) {
142 DatabaseField field = mapping.getField();
143 Object value = CrudServiceUtil.validateFieldType(jsonProps.get(key).toString(), field.getType());
144 builder.property(key, value);
149 catch (Exception ex) {
150 throw new CrudException("Unable to transform response: " + jsonObject.toString(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
153 return builder.build();
156 public static List<Vertex> collectionFromJson(String jsonString, String version) throws CrudException {
157 List<Vertex> result = new ArrayList<>();
158 JSONArray array = new JSONArray(jsonString);
160 for (Object jsonObject : array) {
161 result.add(Vertex.fromJson((JSONObject)jsonObject, version));
168 public String toString() {
169 return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
172 public Optional<String> getId() {
176 public String getType() {
180 public Map<String, Object> getProperties() {