diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java index 1596ff2..9679ad3 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java @@ -4,6 +4,7 @@ package org.gcube.informationsystem.resourceregistry.resources.impl; import java.io.IOException; +import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -16,6 +17,8 @@ import java.util.Set; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; +import org.codehaus.jettison.json.JSONStringer; +import org.codehaus.jettison.json.JSONWriter; import org.gcube.informationsystem.impl.utils.Entities; import org.gcube.informationsystem.model.embedded.Embedded; import org.gcube.informationsystem.model.embedded.Header; @@ -412,8 +415,7 @@ public class EntityManagementImpl implements EntityManagement { logger.trace("Created {} is {} orientVertexToJsonString", Vertex.class.getSimpleName(), Utility - .orientVertexToJsonString( - (OrientVertex) vertex, true)); + .toJsonString((OrientVertex) vertex, true)); } return vertex; @@ -556,7 +558,7 @@ public class EntityManagementImpl implements EntityManagement { public String createFacet(String facetType, String jsonRepresentation) throws ResourceRegistryException { Vertex vertex = createVertexEntity(facetType, Facet.class, jsonRepresentation, false); - return Utility.orientVertexToJsonString((OrientVertex) vertex, false); + return Utility.toJsonString((OrientVertex) vertex, false); } @Override @@ -577,10 +579,9 @@ public class EntityManagementImpl implements EntityManagement { Vertex facet = getEntity(orientGraph, uuid, facetType, Facet.class); logger.trace("{} of type {} with UUID {} is {}", Facet.NAME, - facetType, uuid, Utility.orientVertexToJsonString( - (OrientVertex) facet, true)); + facetType, uuid, Utility.toJsonString((OrientVertex) facet, true)); - return Utility.orientVertexToJsonString((OrientVertex) facet, true); + return Utility.toJsonString((OrientVertex) facet, true); } catch (FacetNotFoundException fnfe) { throw fnfe; } catch (Exception e) { @@ -644,11 +645,9 @@ public class EntityManagementImpl implements EntityManagement { orientGraph.commit(); logger.trace("{} with UUID {} has been updated {}", Facet.NAME, - uuid, Utility.orientVertexToJsonString( - (OrientVertex) facet, true)); + uuid, Utility.toJsonString((OrientVertex) facet, true)); - return Utility - .orientVertexToJsonString((OrientVertex) facet, false); + return Utility.toJsonString((OrientVertex) facet, false); } catch (FacetNotFoundException fnfe) { if (orientGraph != null) { @@ -709,7 +708,7 @@ public class EntityManagementImpl implements EntityManagement { facetUUID, Facet.class, consistOfType, ConsistsOf.class, jsonProperties); - return Utility.orientEdgeToJsonString((OrientEdge) edge, false); + return Utility.toJsonString((OrientEdge) edge, false); } @Override @@ -755,7 +754,7 @@ public class EntityManagementImpl implements EntityManagement { targetResourceUuid, Resource.class, relatedToType, IsRelatedTo.class, jsonProperties); - return Utility.orientEdgeToJsonString((OrientEdge) edge, false); + return Utility.toJsonString((OrientEdge) edge, false); } @Override @@ -792,23 +791,34 @@ public class EntityManagementImpl implements EntityManagement { } private static String marshallResource(Vertex vertex) throws JSONException{ - JSONObject jsonObject = Utility.toJsonObject(vertex, false); + JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, true); - JSONArray jsonArray = new JSONArray(); + JSONArray consistsOfArray = new JSONArray(); + + Iterable edges = vertex.getEdges(Direction.OUT); - Iterable edges = vertex.getEdges(Direction.OUT, ConsistsOf.NAME); for(Edge edge : edges){ - JSONObject jsonObjectEdge = Utility.toJsonObject(edge, false); - Vertex facetVertex = edge.getVertex(Direction.OUT); - jsonObjectEdge.put(Relation.TARGET_PROPERTY, Utility.toJsonObject(facetVertex, false)); + String edgeType = edge.getLabel(); + + try { + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); + schemaManagement.getTypeSchema(edgeType, ConsistsOf.NAME); + + JSONObject jsonObjectEdge = Utility.toJsonObject((OrientEdge) edge, true); + Vertex facetVertex = edge.getVertex(Direction.IN); + jsonObjectEdge.put(Relation.TARGET_PROPERTY, Utility.toJsonObject((OrientVertex) facetVertex, true)); + consistsOfArray.put(jsonObjectEdge); + + } catch (SchemaNotFoundException e) { + // This not an ConsistsOf Edge. it will be skipped + } - jsonArray.put(jsonObjectEdge); } - jsonObject.append(lowerCaseFirstCharacter(ConsistsOf.NAME), jsonArray); + jsonObject.put(lowerCaseFirstCharacter(ConsistsOf.NAME), consistsOfArray); - return jsonObject.toString(); + return jsonObject.toString(); } @@ -881,8 +891,7 @@ public class EntityManagementImpl implements EntityManagement { Resource.class); logger.trace("{} of type {} with UUID {} is {}", Resource.NAME, - resourceType, uuid, Utility.orientVertexToJsonString( - (OrientVertex) resource, true)); + resourceType, uuid, Utility.toJsonString((OrientVertex) resource, true)); return marshallResource(resource); } catch (ResourceNotFoundException rnfe) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java index fdfa201..0f9d671 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java @@ -23,9 +23,8 @@ import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; -import com.tinkerpop.blueprints.impls.orient.OrientEdge; +import com.tinkerpop.blueprints.impls.orient.OrientElement; import com.tinkerpop.blueprints.impls.orient.OrientGraph; -import com.tinkerpop.blueprints.impls.orient.OrientVertex; import com.tinkerpop.blueprints.util.io.graphson.GraphSONMode; import com.tinkerpop.blueprints.util.io.graphson.GraphSONUtility; @@ -37,16 +36,12 @@ public class Utility { private static final Logger logger = LoggerFactory.getLogger(Utility.class); - public static String orientVertexToJsonString(OrientVertex orientVertex, boolean raw) { - ORecord oRecord = orientVertex.getRecord(); - if (raw) { - return oRecord.toJSON(); - } - return oRecord.toJSON("class"); + public static JSONObject toJsonObject(OrientElement element, boolean raw) throws JSONException { + return new JSONObject(toJsonString(element, raw)); } - public static String orientEdgeToJsonString(OrientEdge orientEdge, boolean raw) { - ORecord oRecord = orientEdge.getRecord(); + public static String toJsonString(OrientElement element, boolean raw) { + ORecord oRecord = element.getRecord(); if (raw) { return oRecord.toJSON(); } @@ -68,8 +63,6 @@ public class Utility { GraphSONMode.EXTENDED); } } - - public static String toJsonString(Element element) { try {