2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.service;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
32 import org.onap.crud.exception.CrudException;
34 import java.util.ArrayList;
35 import java.util.List;
39 import javax.ws.rs.core.Response.Status;
41 public class BulkPayload {
42 public enum OperationType {
43 CREATE, UPDATE, DELETE
46 private List<JsonElement> objects = new ArrayList<JsonElement>();
47 private List<JsonElement> relationships = new ArrayList<JsonElement>();
49 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
51 public String toJson() {
52 return gson.toJson(this);
55 public static BulkPayload fromJson(String payload) throws CrudException {
57 if (payload == null || payload.isEmpty()) {
58 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
60 return gson.fromJson(payload, BulkPayload.class);
61 } catch (Exception ex) {
62 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
66 public List<JsonElement> getObjects() {
70 public void setObjects(List<JsonElement> objects) {
71 this.objects = objects;
74 public List<JsonElement> getRelationships() {
78 public void setRelationships(List<JsonElement> relationships) {
79 this.relationships = relationships;
83 public String toString() {
84 return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]";
87 public static void main(String[] args) throws Exception {
88 BulkPayload p = new BulkPayload();
89 JsonObject root = new JsonObject();
90 JsonArray vertices = new JsonArray();
91 JsonObject v1 = new JsonObject();
92 JsonObject v2 = new JsonObject();
93 JsonObject prop = new JsonObject();
95 prop.addProperty("p1", "value1");
96 prop.addProperty("p2", "value2");
103 root.add("objects", vertices);
105 String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
107 p = BulkPayload.fromJson(s);
109 List<JsonElement> po = p.getObjects();
110 List<String> ids = new ArrayList<String>();
111 for (JsonElement e : po) {
112 Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet();
114 for (Map.Entry<String, JsonElement> entry : entries) {
115 ids.add(entry.getKey());
119 System.out.println("root: " + root.toString());
120 System.out.println("payload ids: " + ids.toString());