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.openecomp.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.openecomp.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
48 private List<JsonElement> objects = new ArrayList<JsonElement>();
49 private List<JsonElement> relationships = new ArrayList<JsonElement>();
51 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
53 public String toJson() {
54 return gson.toJson(this);
57 public static BulkPayload fromJson(String payload) throws CrudException {
59 if (payload == null || payload.isEmpty()) {
60 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
62 return gson.fromJson(payload, BulkPayload.class);
63 } catch (Exception ex) {
64 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
68 public List<JsonElement> getObjects() {
72 public void setObjects(List<JsonElement> objects) {
73 this.objects = objects;
76 public List<JsonElement> getRelationships() {
80 public void setRelationships(List<JsonElement> relationships) {
81 this.relationships = relationships;
85 public String toString() {
86 return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]";
89 public static void main(String[] args) throws Exception {
90 BulkPayload p = new BulkPayload();
91 JsonObject root = new JsonObject();
92 JsonArray vertices = new JsonArray();
93 JsonObject v1 = new JsonObject();
94 JsonObject v2 = new JsonObject();
95 JsonObject prop = new JsonObject();
97 prop.addProperty("p1","value1");
98 prop.addProperty("p2","value2");
106 root.add("objects", vertices);
108 String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
110 p = BulkPayload.fromJson(s);
112 List<JsonElement> po = p.getObjects();
113 List<String> ids = new ArrayList<String>();
114 for (JsonElement e : po){
115 Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet();
117 for (Map.Entry<String, JsonElement> entry : entries) {
118 ids.add(entry.getKey());
123 System.out.println("root: " + root.toString());
124 System.out.println("payload ids: " + ids.toString());