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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
28 import org.eclipse.persistence.dynamic.DynamicType;
29 import org.eclipse.persistence.internal.helper.DatabaseField;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.eclipse.persistence.mappings.DatabaseMapping;
32 import org.json.JSONArray;
33 import org.json.JSONObject;
34 import org.onap.crud.exception.CrudException;
35 import org.onap.crud.util.CrudServiceUtil;
36 import org.onap.schema.OxmModelLoader;
37 import org.onap.schema.OxmModelValidator;
39 import com.google.common.base.CaseFormat;
40 import com.google.gson.Gson;
41 import com.google.gson.GsonBuilder;
42 import com.google.gson.annotations.SerializedName;
43 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
46 private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
49 @SerializedName(value = "id", alternate = { "key" })
50 private final Optional<String> id;
52 private final String type;
53 private final Map<String, Object> properties;
55 private Vertex(Builder builder) {
57 this.type = builder.type;
58 this.properties = builder.properties;
61 public static class Builder {
62 private Optional<String> id = Optional.empty();
63 private final String type;
64 private final Map<String, Object> properties = new HashMap<String, Object>();
66 public Builder(String type) {
68 throw new IllegalArgumentException("Type cannot be null");
73 public Builder id(String id) {
75 throw new IllegalArgumentException("id cannot be null");
78 this.id = Optional.of(id);
82 public Builder property(String key, Object value) {
83 if (key == null || value == null) {
84 throw new IllegalArgumentException("Property key/value cannot be null");
86 properties.put(key, value);
90 public Vertex build() {
91 return new Vertex(this);
95 public String toJson() {
96 return gson.toJson(this);
99 public String toJson(Gson customGson) {
100 return customGson.toJson(this);
103 public static Vertex fromJson(String jsonString, String version) throws CrudException {
104 JSONObject doc = new JSONObject(jsonString);
105 return fromJson(doc, version);
108 public static Vertex fromJson(JSONObject jsonObject, String version) throws CrudException {
112 String type = jsonObject.getString("type");
113 builder = new Builder(type).id(jsonObject.getString("key"));
115 type = OxmModelValidator.resolveCollectionType(version, type);
116 DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
118 final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version, type);
119 final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
122 if (modelObjectType == null) {
123 throw new CrudException("Unable to load oxm version", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
126 if (jsonObject.has("properties")) {
127 JSONObject jsonProps = jsonObject.getJSONObject("properties");
128 for (String key : jsonProps.keySet()) {
129 String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
130 DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
132 if (mapping == null) {
133 // This might be one of the reserved properties
134 mapping = reservedType.getDescriptor().getMappingForAttributeName(keyJavaName);
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);
145 catch (Exception ex) {
146 throw new CrudException("Unable to transform response: " + jsonObject.toString(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
149 return builder.build();
152 public static List<Vertex> collectionFromJson(String jsonString, String version) throws CrudException {
153 List<Vertex> result = new ArrayList<>();
154 JSONArray array = new JSONArray(jsonString);
156 for (Object jsonObject : array) {
157 result.add(Vertex.fromJson((JSONObject)jsonObject, version));
164 public String toString() {
165 return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
168 public Optional<String> getId() {
172 public String getType() {
176 public Map<String, Object> getProperties() {