summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/onap/crud/service/AbstractGraphDataService.java162
-rw-r--r--src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java193
-rw-r--r--src/main/java/org/onap/crud/service/CrudGraphDataService.java169
3 files changed, 238 insertions, 286 deletions
diff --git a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java
index 013845e..654ebfb 100644
--- a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java
+++ b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java
@@ -23,10 +23,13 @@
*/
package org.onap.crud.service;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response.Status;
import org.onap.crud.dao.GraphDao;
import org.onap.crud.entity.Edge;
@@ -37,6 +40,8 @@ import org.onap.crud.util.CrudServiceUtil;
import org.onap.schema.OxmModelValidator;
import org.onap.schema.RelationshipSchemaValidator;
+import com.google.gson.JsonElement;
+
public abstract class AbstractGraphDataService {
protected GraphDao dao;
@@ -73,6 +78,154 @@ public abstract class AbstractGraphDataService {
return CrudResponseBuilder.buildGetVerticesResponse(items, version);
}
+ public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException {
+ HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
+ HashMap<String, Edge> edges = new HashMap<String, Edge>();
+
+ String txId = dao.openTransaction();
+
+ try {
+ // Step 1. Handle edge deletes (must happen before vertex deletes)
+ for (JsonElement v : payload.getRelationships()) {
+ List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
+ v.getAsJsonObject().entrySet());
+
+ if (entries.size() != 2) {
+ throw new CrudException("", Status.BAD_REQUEST);
+ }
+ Map.Entry<String, JsonElement> opr = entries.get(0);
+ Map.Entry<String, JsonElement> item = entries.get(1);
+ EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
+
+ if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
+ RelationshipSchemaValidator.validateType(version, edgePayload.getType());
+ deleteBulkEdge(edgePayload.getId(), version, edgePayload.getType(), txId);
+ }
+ }
+
+ // Step 2: Handle vertex deletes
+ for (JsonElement v : payload.getObjects()) {
+ List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
+ v.getAsJsonObject().entrySet());
+
+ if (entries.size() != 2) {
+ throw new CrudException("", Status.BAD_REQUEST);
+ }
+
+ Map.Entry<String, JsonElement> opr = entries.get(0);
+ Map.Entry<String, JsonElement> item = entries.get(1);
+ VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
+
+ if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
+ String type = OxmModelValidator.resolveCollectionType(version, vertexPayload.getType());
+ deleteBulkVertex(vertexPayload.getId(), version, type, txId);
+ }
+ }
+
+ // Step 3: Handle vertex add/modify (must happen before edge adds)
+ for (JsonElement v : payload.getObjects()) {
+ List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
+ v.getAsJsonObject().entrySet());
+
+ if (entries.size() != 2) {
+ throw new CrudException("", Status.BAD_REQUEST);
+ }
+ Map.Entry<String, JsonElement> opr = entries.get(0);
+ Map.Entry<String, JsonElement> item = entries.get(1);
+ VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
+
+ // Add vertex
+ if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
+ vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
+ headers, true));
+ Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
+ vertexPayload.getProperties());
+ Vertex persistedVertex = addBulkVertex(validatedVertex, version, txId);
+ Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
+ vertices.put(item.getKey(), outgoingVertex);
+ }
+
+ // Update vertex
+ else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
+ vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
+ headers, false));
+ Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version,
+ vertexPayload.getType(), vertexPayload.getProperties());
+ Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId);
+ Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
+ vertices.put(item.getKey(), outgoingVertex);
+ }
+ }
+
+ // Step 4: Handle edge add/modify
+ for (JsonElement v : payload.getRelationships()) {
+ List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
+ v.getAsJsonObject().entrySet());
+
+ if (entries.size() != 2) {
+ throw new CrudException("", Status.BAD_REQUEST);
+ }
+ Map.Entry<String, JsonElement> opr = entries.get(0);
+ Map.Entry<String, JsonElement> item = entries.get(1);
+ EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
+
+ // Add/Update edge
+ if (opr.getValue().getAsString().equalsIgnoreCase("add")
+ || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
+ Edge validatedEdge;
+ Edge persistedEdge;
+ if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
+ // Fix the source/destination
+ if (edgePayload.getSource().startsWith("$")) {
+ Vertex source = vertices.get(edgePayload.getSource().substring(1));
+ if (source == null) {
+ throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1),
+ Status.INTERNAL_SERVER_ERROR);
+ }
+ edgePayload
+ .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
+ }
+ if (edgePayload.getTarget().startsWith("$")) {
+ Vertex target = vertices.get(edgePayload.getTarget().substring(1));
+ if (target == null) {
+ throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1),
+ Status.INTERNAL_SERVER_ERROR);
+ }
+ edgePayload
+ .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
+ }
+ validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(),
+ edgePayload);
+ persistedEdge = addBulkEdge(validatedEdge, version, txId);
+ } else {
+ Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
+ validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
+ persistedEdge = updateBulkEdge(validatedEdge, version, txId);
+ }
+
+ Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
+ edges.put(item.getKey(), outgoingEdge);
+ }
+ }
+
+ // commit transaction
+ dao.commitTransaction(txId);
+ } catch (CrudException ex) {
+ dao.rollbackTransaction(txId);
+ throw ex;
+ } catch (Exception ex) {
+ dao.rollbackTransaction(txId);
+ throw ex;
+ } finally {
+ if (dao.transactionExists(txId)) {
+ dao.rollbackTransaction(txId);
+ }
+ }
+
+ return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
+ }
+
+
public abstract String addVertex(String version, String type, VertexPayload payload) throws CrudException;
public abstract String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException;
public abstract String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException;
@@ -81,5 +234,12 @@ public abstract class AbstractGraphDataService {
public abstract String deleteEdge(String version, String id, String type) throws CrudException;
public abstract String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException;
public abstract String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException;
- public abstract String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException;
+
+ protected abstract Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException;
+ protected abstract Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException;
+ protected abstract void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException;
+
+ protected abstract Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
+ protected abstract Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
+ protected abstract void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException;
}
diff --git a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java
index d825d79..60a18d3 100644
--- a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java
+++ b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java
@@ -25,7 +25,6 @@ package org.onap.crud.service;
import com.att.ecomp.event.api.EventConsumer;
import com.att.ecomp.event.api.EventPublisher;
-import com.google.gson.JsonElement;
import org.onap.aai.cl.api.LogFields;
import org.onap.aai.cl.api.Logger;
@@ -45,15 +44,10 @@ import org.onap.crud.logging.CrudServiceMsgs;
import org.onap.crud.parser.CrudResponseBuilder;
import org.onap.crud.util.CrudProperties;
import org.onap.crud.util.CrudServiceConstants;
-import org.onap.crud.util.CrudServiceUtil;
import org.onap.schema.OxmModelValidator;
import org.onap.schema.RelationshipSchemaValidator;
import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
import java.util.Timer;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
@@ -64,7 +58,6 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.PreDestroy;
-import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response.Status;
public class CrudAsyncGraphDataService extends AbstractGraphDataService {
@@ -372,146 +365,54 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService {
timer.cancel();
}
-
+
@Override
- public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException {
- HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
- HashMap<String, Edge> edges = new HashMap<String, Edge>();
- String txId = dao.openTransaction();
-
- try {
- // Handle vertices
- for (JsonElement v : payload.getObjects()) {
- List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
- v.getAsJsonObject().entrySet());
-
- if (entries.size() != 2) {
- throw new CrudException("", Status.BAD_REQUEST);
- }
- Map.Entry<String, JsonElement> opr = entries.get(0);
- Map.Entry<String, JsonElement> item = entries.get(1);
-
- VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
-
- if (opr.getValue().getAsString().equalsIgnoreCase("add")
- || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
- Vertex validatedVertex;
- GraphEvent event;
-
- if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
- vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
- headers, true));
- // Publish add-vertex event
- validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
- vertexPayload.getProperties());
- event = GraphEvent.builder(GraphEventOperation.CREATE)
- .vertex(GraphEventVertex.fromVertex(validatedVertex, version)).build();
- } else {
- vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
- headers, false));
- // Publish update-vertex event
- validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version,
- vertexPayload.getType(), vertexPayload.getProperties());
- event = GraphEvent.builder(GraphEventOperation.UPDATE)
- .vertex(GraphEventVertex.fromVertex(validatedVertex, version)).build();
- }
-
- event.setDbTransactionId(txId);
- GraphEvent response = publishEvent(event);
- Vertex persistedVertex = response.getVertex().toVertex();
- Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
- vertices.put(item.getKey(), outgoingVertex);
- } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
- // Publish delete-vertex event
- String type = OxmModelValidator.resolveCollectionType(version, vertexPayload.getType());
- GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
- .vertex(new GraphEventVertex(vertexPayload.getId(), version, type, null)).build();
- event.setDbTransactionId(txId);
- publishEvent(event);
- }
- }
-
- // Handle Edges
- for (JsonElement v : payload.getRelationships()) {
- List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
- v.getAsJsonObject().entrySet());
-
- if (entries.size() != 2) {
- throw new CrudException("", Status.BAD_REQUEST);
- }
- Map.Entry<String, JsonElement> opr = entries.get(0);
- Map.Entry<String, JsonElement> item = entries.get(1);
-
- EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
-
- if (opr.getValue().getAsString().equalsIgnoreCase("add")
- || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
- Edge validatedEdge;
- Edge persistedEdge;
- if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
- // Fix the source/destination
- if (edgePayload.getSource().startsWith("$")) {
- Vertex source = vertices.get(edgePayload.getSource().substring(1));
- if (source == null) {
- throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1),
- Status.INTERNAL_SERVER_ERROR);
- }
- edgePayload
- .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
- }
- if (edgePayload.getTarget().startsWith("$")) {
- Vertex target = vertices.get(edgePayload.getTarget().substring(1));
- if (target == null) {
- throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1),
- Status.INTERNAL_SERVER_ERROR);
- }
- edgePayload
- .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
- }
- validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(),
- edgePayload);
- GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE)
- .edge(GraphEventEdge.fromEdge(validatedEdge, version)).build();
- event.setDbTransactionId(txId);
- GraphEvent response = publishEvent(event);
- persistedEdge = response.getEdge().toEdge();
- } else {
- Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
- validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
- GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE)
- .edge(GraphEventEdge.fromEdge(validatedEdge, version)).build();
- event.setDbTransactionId(txId);
- GraphEvent response = publishEvent(event);
- persistedEdge = response.getEdge().toEdge();
- }
-
- Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
- edges.put(item.getKey(), outgoingEdge);
- } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
- RelationshipSchemaValidator.validateType(version, edgePayload.getType());
- // Publish delete-vertex event
- GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
- .edge(new GraphEventEdge(edgePayload.getId(), version, edgePayload.getType(), null, null, null)).build();
- event.setDbTransactionId(txId);
- publishEvent(event);
- }
- }
-
- // commit transaction
- dao.commitTransaction(txId);
- } catch (CrudException ex) {
- dao.rollbackTransaction(txId);
- throw ex;
- } catch (Exception ex) {
- dao.rollbackTransaction(txId);
- throw ex;
- } finally {
- if (dao.transactionExists(txId)) {
- dao.rollbackTransaction(txId);
- }
- }
-
- return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
+ protected Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException {
+ GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE)
+ .vertex(GraphEventVertex.fromVertex(vertex, version)).build();
+ event.setDbTransactionId(dbTransId);
+ GraphEvent response = publishEvent(event);
+ return response.getVertex().toVertex();
+ }
+
+ @Override
+ protected Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException {
+ GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE)
+ .vertex(GraphEventVertex.fromVertex(vertex, version)).build();
+ event.setDbTransactionId(dbTransId);
+ GraphEvent response = publishEvent(event);
+ return response.getVertex().toVertex();
+ }
+
+ @Override
+ protected void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException {
+ GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE).vertex(new GraphEventVertex(id, version, type, null)).build();
+ event.setDbTransactionId(dbTransId);
+ publishEvent(event);
+ }
+
+ @Override
+ protected Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
+ GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE).edge(GraphEventEdge.fromEdge(edge, version)).build();
+ event.setDbTransactionId(dbTransId);
+ GraphEvent response = publishEvent(event);
+ return response.getEdge().toEdge();
+ }
+
+ @Override
+ protected Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
+ GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE).edge(GraphEventEdge.fromEdge(edge, version)).build();
+ event.setDbTransactionId(dbTransId);
+ GraphEvent response = publishEvent(event);
+ return response.getEdge().toEdge();
+ }
+
+ @Override
+ protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
+ GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE)
+ .edge(new GraphEventEdge(id, version, type, null, null, null)).build();
+ event.setDbTransactionId(dbTransId);
+ publishEvent(event);
}
private GraphEvent publishEvent(GraphEvent event) throws CrudException {
diff --git a/src/main/java/org/onap/crud/service/CrudGraphDataService.java b/src/main/java/org/onap/crud/service/CrudGraphDataService.java
index cd72533..a18ab3b 100644
--- a/src/main/java/org/onap/crud/service/CrudGraphDataService.java
+++ b/src/main/java/org/onap/crud/service/CrudGraphDataService.java
@@ -23,13 +23,6 @@
*/
package org.onap.crud.service;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.Response.Status;
import org.onap.crud.dao.GraphDao;
import org.onap.crud.entity.Edge;
@@ -37,11 +30,9 @@ import org.onap.crud.entity.Edge;
import org.onap.crud.entity.Vertex;
import org.onap.crud.exception.CrudException;
import org.onap.crud.parser.CrudResponseBuilder;
-import org.onap.crud.util.CrudServiceUtil;
import org.onap.schema.OxmModelValidator;
import org.onap.schema.RelationshipSchemaValidator;
-import com.google.gson.JsonElement;
public class CrudGraphDataService extends AbstractGraphDataService {
@@ -54,131 +45,6 @@ public class CrudGraphDataService extends AbstractGraphDataService {
return addVertex(version, vertex);
}
- public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException {
- HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
- HashMap<String, Edge> edges = new HashMap<String, Edge>();
- String txId = dao.openTransaction();
- try {
- // Handle vertices
- for (JsonElement v : payload.getObjects()) {
- List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
- v.getAsJsonObject().entrySet());
-
- if (entries.size() != 2) {
- throw new CrudException("", Status.BAD_REQUEST);
- }
- Map.Entry<String, JsonElement> opr = entries.get(0);
- Map.Entry<String, JsonElement> item = entries.get(1);
-
- VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
-
- if (opr.getValue().getAsString().equalsIgnoreCase("add")
- || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
- Vertex validatedVertex;
- Vertex persistedVertex;
- if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
- vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
- headers, true));
-
- validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
- vertexPayload.getProperties());
-
- // Call champDAO to add the vertex
- persistedVertex = dao.addVertex(validatedVertex.getType(), validatedVertex.getProperties(), txId);
- } else {
- vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
- headers, false));
- validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version,
- vertexPayload.getType(), vertexPayload.getProperties());
- // Call champDAO to update the vertex
- persistedVertex = dao.updateVertex(vertexPayload.getId(), validatedVertex.getType(),
- validatedVertex.getProperties(), txId);
- }
-
- Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
-
- vertices.put(item.getKey(), outgoingVertex);
-
- } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
- dao.deleteVertex(vertexPayload.getId(),
- OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), txId);
- }
-
- }
- // Handle Edges
- for (JsonElement v : payload.getRelationships()) {
- List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
- v.getAsJsonObject().entrySet());
-
- if (entries.size() != 2) {
- throw new CrudException("", Status.BAD_REQUEST);
- }
- Map.Entry<String, JsonElement> opr = entries.get(0);
- Map.Entry<String, JsonElement> item = entries.get(1);
-
- EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
-
- if (opr.getValue().getAsString().equalsIgnoreCase("add")
- || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
- Edge validatedEdge;
- Edge persistedEdge;
- if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
- // Fix the source/detination
- if (edgePayload.getSource().startsWith("$")) {
- Vertex source = vertices.get(edgePayload.getSource().substring(1));
- if (source == null) {
- throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1),
- Status.INTERNAL_SERVER_ERROR);
- }
- edgePayload
- .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
- }
- if (edgePayload.getTarget().startsWith("$")) {
- Vertex target = vertices.get(edgePayload.getTarget().substring(1));
- if (target == null) {
- throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1),
- Status.INTERNAL_SERVER_ERROR);
- }
- edgePayload
- .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
- }
- validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(),
- edgePayload);
- persistedEdge = dao.addEdge(validatedEdge.getType(), validatedEdge.getSource(), validatedEdge.getTarget(),
- validatedEdge.getProperties(), txId);
- } else {
- Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
- validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
- persistedEdge = dao.updateEdge(edge, txId);
- }
-
- Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
-
- edges.put(item.getKey(), outgoingEdge);
-
- } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
- RelationshipSchemaValidator.validateType(version, edgePayload.getType());
- dao.deleteEdge(edgePayload.getId(), edgePayload.getType(), txId);
- }
-
- }
- // close champ TX
- dao.commitTransaction(txId);
- } catch (CrudException ex) {
- dao.rollbackTransaction(txId);
- throw ex;
- } catch (Exception ex) {
- dao.rollbackTransaction(txId);
- throw ex;
- } finally {
- if (dao.transactionExists(txId)) {
- dao.rollbackTransaction(txId);
- }
- }
-
- return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
- }
-
private String addVertex(String version, Vertex vertex) throws CrudException {
Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties());
return CrudResponseBuilder
@@ -213,28 +79,24 @@ public class CrudGraphDataService extends AbstractGraphDataService {
Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, payload.getProperties(),
existingVertex);
return updateVertex(version, vertex);
-
}
public String deleteVertex(String version, String id, String type) throws CrudException {
type = OxmModelValidator.resolveCollectionType(version, type);
dao.deleteVertex(id, type);
return "";
-
}
public String deleteEdge(String version, String id, String type) throws CrudException {
RelationshipSchemaValidator.validateType(version, type);
dao.deleteEdge(id, type);
return "";
-
}
public String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException {
Edge edge = dao.getEdge(id, type);
Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
return updateEdge(version, validatedEdge);
-
}
private String updateEdge(String version, Edge edge) throws CrudException {
@@ -242,7 +104,7 @@ public class CrudGraphDataService extends AbstractGraphDataService {
return CrudResponseBuilder
.buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version);
}
-
+
public String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException {
Edge edge = dao.getEdge(id, type);
Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
@@ -254,4 +116,33 @@ public class CrudGraphDataService extends AbstractGraphDataService {
return dao.getVertex(id);
}
+ @Override
+ protected Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException {
+ return dao.addVertex(vertex.getType(), vertex.getProperties(), dbTransId);
+ }
+
+ @Override
+ protected Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException {
+ return dao.updateVertex(id, vertex.getType(), vertex.getProperties(), dbTransId);
+ }
+
+ @Override
+ protected void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException {
+ dao.deleteVertex(id, type, dbTransId);
+ }
+
+ @Override
+ protected Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
+ return dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), dbTransId);
+ }
+
+ @Override
+ protected Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
+ return dao.updateEdge(edge, dbTransId);
+ }
+
+ @Override
+ protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
+ dao.deleteEdge(id, type, dbTransId);
+ }
}