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.service;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
29 import org.onap.crud.exception.CrudException;
31 import java.util.ArrayList;
32 import java.util.List;
36 import javax.ws.rs.core.Response.Status;
38 public class BulkPayload {
39 public enum OperationType {
40 CREATE, UPDATE, DELETE
43 private List<JsonElement> objects = new ArrayList<JsonElement>();
44 private List<JsonElement> relationships = new ArrayList<JsonElement>();
46 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
48 public String toJson() {
49 return gson.toJson(this);
52 public static BulkPayload fromJson(String payload) throws CrudException {
54 if (payload == null || payload.isEmpty()) {
55 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
57 return gson.fromJson(payload, BulkPayload.class);
58 } catch (Exception ex) {
59 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
63 public List<JsonElement> getObjects() {
67 public void setObjects(List<JsonElement> objects) {
68 this.objects = objects;
71 public List<JsonElement> getRelationships() {
75 public void setRelationships(List<JsonElement> relationships) {
76 this.relationships = relationships;
80 public String toString() {
81 return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]";
84 public static void main(String[] args) throws Exception {
85 BulkPayload p = new BulkPayload();
86 JsonObject root = new JsonObject();
87 JsonArray vertices = new JsonArray();
88 JsonObject v1 = new JsonObject();
89 JsonObject v2 = new JsonObject();
90 JsonObject prop = new JsonObject();
92 prop.addProperty("p1", "value1");
93 prop.addProperty("p2", "value2");
100 root.add("objects", vertices);
102 String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
104 p = BulkPayload.fromJson(s);
106 List<JsonElement> po = p.getObjects();
107 List<String> ids = new ArrayList<String>();
108 for (JsonElement e : po) {
109 Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet();
111 for (Map.Entry<String, JsonElement> entry : entries) {
112 ids.add(entry.getKey());
116 System.out.println("root: " + root.toString());
117 System.out.println("payload ids: " + ids.toString());