diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java index 109b5c9..0d35f5c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -292,6 +292,14 @@ public abstract class ERManagement { } } + public JSONObject serializeSelfOnly() throws ResourceRegistryException { + try { + return Utility.toJsonObject((OrientElement) getElement(), false); + }catch(Exception e){ + throw new ResourceRegistryException(e); + } + } + public abstract String serialize() throws ResourceRegistryException; public abstract JSONObject serializeAsJson() diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java index 6dc0b04..833311d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java @@ -9,7 +9,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.er.ERManagement; -import org.gcube.informationsystem.resourceregistry.utils.Utility; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; @@ -31,12 +30,12 @@ public class FacetManagement extends EntityManagement { @Override public String serialize() throws ResourceRegistryException { - return Utility.toJsonString((OrientVertex) element, true); + return serializeSelfOnly().toString(); } @Override public JSONObject serializeAsJson() throws ResourceRegistryException { - return Utility.toJsonObject((OrientVertex) element, true); + return serializeSelfOnly(); } @Override diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java index 8106eed..800102f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java @@ -6,7 +6,6 @@ package org.gcube.informationsystem.resourceregistry.er.entity; import java.util.Iterator; import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.relation.ConsistsOf; @@ -38,7 +37,7 @@ public class ResourceManagement extends EntityManagement { private static Logger logger = LoggerFactory .getLogger(ResourceManagement.class); - + public ResourceManagement() { super(Resource.class); } @@ -51,69 +50,80 @@ public class ResourceManagement extends EntityManagement { public String serialize() throws ResourceRegistryException { return serializeAsJson().toString(); } - + @Override public JSONObject serializeAsJson() throws ResourceRegistryException { - Vertex vertex = getElement(); - JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, - true); + + JSONObject sourceResource = serializeSelfOnly(); - JSONArray consistsOfArray = new JSONArray(); /* - * JSONArray isRelatedToArray = new JSONArray(); - */ - - /* - * Due to orientdb bug cannot get ConsistsOf edge only because - * are filtered in hasNext() function + * Cannot get ConsistsOf edge only because is not polymorphic for a + * com.tinkerpop.blueprints.Vertex * vertex.getEdges(Direction.OUT, ConsistsOf.NAME); + * TODO Looks for a different query */ - - Iterable edges = vertex.getEdges(Direction.OUT); - for(Edge edge : edges){ + Iterable edges = getElement().getEdges(Direction.OUT); + for (Edge edge : edges) { @SuppressWarnings("rawtypes") - RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); - if(relationManagement instanceof ConsistsOfManagement){ - JSONObject relationJsonObject = relationManagement.serializeAsJson(); - consistsOfArray.put(relationJsonObject); + RelationManagement relationManagement = RelationManagement + .getRelationManagement(orientGraph, edge); + if (relationManagement instanceof ConsistsOfManagement) { + JSONObject consistsOf = relationManagement + .serializeAsJson(); + sourceResource = addConsistsOf(sourceResource, consistsOf); + } /* + * This comment is just to show that IsRelatedTo is not serialized + * by default as desing choice and not because forget + * * else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){ - * isRelatedToArray.put(jsonObjectEdge); } + * JSONObject isRelatedTo = relationManagement + .serializeAsJson(); + * sourceResource = addIsRelatedTo(sourceResource, isRelatedTo); + * } */ } + return sourceResource; + } + + protected static JSONObject addRelation(JSONObject sourceResource, + JSONObject relation, AccessType accessType) + throws ResourceRegistryException { + String arrayKey = accessType.lowerCaseFirstCharacter(); + JSONArray relationArray = null; + try { - jsonObject.put(AccessType.CONSISTS_OF.lowerCaseFirstCharacter(), - consistsOfArray); - /* - * jsonObject.put(lowerCaseFirstCharacter(IsRelatedTo.NAME), - * isRelatedToArray); - */ - } catch (JSONException e) { + if (sourceResource.has(arrayKey)) { + relationArray = sourceResource.getJSONArray(arrayKey); + } else { + relationArray = new JSONArray(); + } + + relationArray.put(relation); + sourceResource.putOpt(arrayKey, relationArray); + } catch (Exception e) { throw new ResourceRegistryException(e); } - return jsonObject; + return sourceResource; } - - public static JSONObject addIsRelatedTo(JSONObject entityJsonObject, JSONObject isRealtedToJsonObject) throws ResourceRegistryException{ - JSONArray isRelatedToArray = new JSONArray(); - try { - isRelatedToArray.put(isRealtedToJsonObject); - entityJsonObject.put(AccessType.IS_RELATED_TO.lowerCaseFirstCharacter(), - isRelatedToArray); - }catch (JSONException e) { - throw new ResourceRegistryException(e); - } - - return entityJsonObject; + + public static JSONObject addConsistsOf(JSONObject sourceResource, + JSONObject consistsOf) throws ResourceRegistryException { + return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF); } - - + + public static JSONObject addIsRelatedTo(JSONObject sourceResource, + JSONObject isRelatedTo) throws ResourceRegistryException { + return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO); + } + @Override public Vertex reallyCreate() throws EntityAlreadyPresentException, ResourceRegistryException { + createVertex(); String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter(); @@ -175,37 +185,37 @@ public class ResourceManagement extends EntityManagement { @Override public boolean reallyDelete() throws ResourceNotFoundException, ResourceRegistryException { - //internalDeleteResource(orientGraph, uuid, null); - + // internalDeleteResource(orientGraph, uuid, null); + getElement(); - + Iterable iterable = element.getEdges(Direction.OUT); Iterator iterator = iterable.iterator(); - while(iterator.hasNext()){ + while (iterator.hasNext()) { Edge edge = iterator.next(); OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType(); @SuppressWarnings("rawtypes") RelationManagement relationManagement = null; - if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){ + if (orientEdgeType.isSubClassOf(IsRelatedTo.NAME)) { relationManagement = new IsRelatedToManagement(orientGraph); - }else if(orientEdgeType.isSubClassOf(ConsistsOf.NAME)){ + } else if (orientEdgeType.isSubClassOf(ConsistsOf.NAME)) { relationManagement = new ConsistsOfManagement(orientGraph); - }else{ + } else { logger.warn("{} is not a {} nor a {}. " + "This is really strange ad should not occur. " + "Please Investigate it.", - Utility.toJsonString(edge, true), - IsRelatedTo.NAME, ConsistsOf.NAME); + Utility.toJsonString(edge, true), IsRelatedTo.NAME, + ConsistsOf.NAME); } - if(relationManagement!=null){ + if (relationManagement != null) { relationManagement.setElement(edge); relationManagement.reallyDelete(); } - + } - + element.remove(); - + return true; } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index 33ba7c8..cfa24ea 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -4,8 +4,11 @@ package org.gcube.informationsystem.resourceregistry.er.relation; import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.UUID; import org.codehaus.jettison.json.JSONArray; @@ -91,35 +94,65 @@ public abstract class RelationManagement extends @Override public JSONObject serializeAsJson() throws ResourceRegistryException { - JSONObject ret = Utility.toJsonObject((OrientEdge) getElement(), false); + JSONObject relation = serializeSelfOnly(); Vertex target = element.getVertex(Direction.IN); EntityManagement entityManagement = EntityManagement .getEntityManagement(orientGraph, target); try { - ret.put(Relation.TARGET_PROPERTY, + relation.put(Relation.TARGET_PROPERTY, entityManagement.serializeAsJson()); } catch (JSONException e) { throw new ResourceRegistryException(e); } - return ret; + return relation; } - protected JSONObject fullSerialize() throws ResourceRegistryException { + protected Map fullSerialize( + Map visitedSourceResources) + throws ResourceRegistryException { Vertex source = element.getVertex(Direction.OUT); - ResourceManagement resourceManagement = (ResourceManagement) EntityManagement - .getEntityManagement(orientGraph, source); - JSONObject entityJsonObject = resourceManagement.serializeAsJson(); + String id = source.getId().toString(); - if (this instanceof IsRelatedToManagement) { - entityJsonObject = ResourceManagement.addIsRelatedTo( - entityJsonObject, serializeAsJson()); + JSONObject sourceResource = visitedSourceResources.get(id); + if (sourceResource == null) { + ResourceManagement resourceManagement = (ResourceManagement) EntityManagement + .getEntityManagement(orientGraph, source); + if (this instanceof IsRelatedToManagement) { + sourceResource = resourceManagement.serializeAsJson(); + } else if (this instanceof ConsistsOfManagement) { + sourceResource = resourceManagement.serializeSelfOnly(); + } else { + String error = String.format("{%s is not a %s nor a %s. " + + "This is really strange and should not occur. " + + "Please Investigate it.", this, + IsRelatedToManagement.class.getSimpleName(), + ConsistsOfManagement.class.getSimpleName()); + throw new ResourceRegistryException(error); + } } - return entityJsonObject; + if (this instanceof IsRelatedToManagement) { + sourceResource = ResourceManagement.addIsRelatedTo(sourceResource, + serializeAsJson()); + } else if (this instanceof ConsistsOfManagement) { + sourceResource = ResourceManagement.addConsistsOf(sourceResource, + serializeAsJson()); + } else { + String error = String.format("{%s is not a %s nor a %s. " + + "This is really strange and should not occur. " + + "Please Investigate it.", this, + IsRelatedToManagement.class.getSimpleName(), + ConsistsOfManagement.class.getSimpleName()); + throw new ResourceRegistryException(error); + } + + visitedSourceResources.put(id, sourceResource); + + return visitedSourceResources; } public Edge reallyCreate(UUID sourceUUID, UUID targetUUID) @@ -246,7 +279,7 @@ public abstract class RelationManagement extends return edge; } - + @Override public boolean reallyAddToContext() throws ContextException, ResourceRegistryException { @@ -305,7 +338,7 @@ public abstract class RelationManagement extends return false; } } - + @Override public boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException { @@ -412,7 +445,7 @@ public abstract class RelationManagement extends return false; } } - + @Override public boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException { @@ -498,22 +531,23 @@ public abstract class RelationManagement extends } } - protected List serializeEdges(Iterable edges, + @SuppressWarnings("unchecked") + protected Collection serializeEdges(Iterable edges, boolean postFilterPolymorphic) throws ResourceRegistryException { - List list = new ArrayList<>(); + Map visitedSourceResources = new HashMap<>(); for (Edge edge : edges) { if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) { continue; } RelationManagement relationManagement = getRelationManagement( orientGraph, edge); - JSONObject jsonObject = relationManagement.fullSerialize(); - list.add(jsonObject); + visitedSourceResources = relationManagement + .fullSerialize(visitedSourceResources); } - return list; + return visitedSourceResources.values(); } - protected String serializeJSONObjectList(List list) { + protected String serializeJSONObjectList(Collection list) { JSONArray jsonArray = new JSONArray(list); return jsonArray.toString(); } @@ -522,8 +556,8 @@ public abstract class RelationManagement extends public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { Iterable edges = orientGraph.getEdgesOfClass(erType, polymorphic); - List list = serializeEdges(edges, false); - return serializeJSONObjectList(list); + Collection collection = serializeEdges(edges, false); + return serializeJSONObjectList(collection); } public String reallyGetAllFrom(UUID uuid, Direction direction,