Changed relations serialization

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@141608 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-01-17 15:53:24 +00:00
parent 1ea16a59bc
commit 4e5426e283
4 changed files with 132 additions and 81 deletions

View File

@ -292,6 +292,14 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
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 String serialize() throws ResourceRegistryException;
public abstract JSONObject serializeAsJson() public abstract JSONObject serializeAsJson()

View File

@ -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.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -31,12 +30,12 @@ public class FacetManagement extends EntityManagement<Facet> {
@Override @Override
public String serialize() throws ResourceRegistryException { public String serialize() throws ResourceRegistryException {
return Utility.toJsonString((OrientVertex) element, true); return serializeSelfOnly().toString();
} }
@Override @Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
return Utility.toJsonObject((OrientVertex) element, true); return serializeSelfOnly();
} }
@Override @Override

View File

@ -6,7 +6,6 @@ package org.gcube.informationsystem.resourceregistry.er.entity;
import java.util.Iterator; import java.util.Iterator;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.model.relation.ConsistsOf;
@ -38,7 +37,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(ResourceManagement.class); .getLogger(ResourceManagement.class);
public ResourceManagement() { public ResourceManagement() {
super(Resource.class); super(Resource.class);
} }
@ -51,69 +50,80 @@ public class ResourceManagement extends EntityManagement<Resource> {
public String serialize() throws ResourceRegistryException { public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString(); return serializeAsJson().toString();
} }
@Override @Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
Vertex vertex = getElement();
JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, JSONObject sourceResource = serializeSelfOnly();
true);
JSONArray consistsOfArray = new JSONArray();
/* /*
* JSONArray isRelatedToArray = new JSONArray(); * Cannot get ConsistsOf edge only because is not polymorphic for a
*/ * com.tinkerpop.blueprints.Vertex
/*
* Due to orientdb bug cannot get ConsistsOf edge only because
* are filtered in hasNext() function
* vertex.getEdges(Direction.OUT, ConsistsOf.NAME); * vertex.getEdges(Direction.OUT, ConsistsOf.NAME);
* TODO Looks for a different query
*/ */
Iterable<Edge> edges = getElement().getEdges(Direction.OUT);
Iterable<Edge> edges = vertex.getEdges(Direction.OUT); for (Edge edge : edges) {
for(Edge edge : edges){
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); RelationManagement relationManagement = RelationManagement
if(relationManagement instanceof ConsistsOfManagement){ .getRelationManagement(orientGraph, edge);
JSONObject relationJsonObject = relationManagement.serializeAsJson(); if (relationManagement instanceof ConsistsOfManagement) {
consistsOfArray.put(relationJsonObject); 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)){ * 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 { try {
jsonObject.put(AccessType.CONSISTS_OF.lowerCaseFirstCharacter(), if (sourceResource.has(arrayKey)) {
consistsOfArray); relationArray = sourceResource.getJSONArray(arrayKey);
/* } else {
* jsonObject.put(lowerCaseFirstCharacter(IsRelatedTo.NAME), relationArray = new JSONArray();
* isRelatedToArray); }
*/
} catch (JSONException e) { relationArray.put(relation);
sourceResource.putOpt(arrayKey, relationArray);
} catch (Exception e) {
throw new ResourceRegistryException(e); throw new ResourceRegistryException(e);
} }
return jsonObject; return sourceResource;
} }
public static JSONObject addIsRelatedTo(JSONObject entityJsonObject, JSONObject isRealtedToJsonObject) throws ResourceRegistryException{ public static JSONObject addConsistsOf(JSONObject sourceResource,
JSONArray isRelatedToArray = new JSONArray(); JSONObject consistsOf) throws ResourceRegistryException {
try { return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF);
isRelatedToArray.put(isRealtedToJsonObject);
entityJsonObject.put(AccessType.IS_RELATED_TO.lowerCaseFirstCharacter(),
isRelatedToArray);
}catch (JSONException e) {
throw new ResourceRegistryException(e);
}
return entityJsonObject;
} }
public static JSONObject addIsRelatedTo(JSONObject sourceResource,
JSONObject isRelatedTo) throws ResourceRegistryException {
return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO);
}
@Override @Override
public Vertex reallyCreate() throws EntityAlreadyPresentException, public Vertex reallyCreate() throws EntityAlreadyPresentException,
ResourceRegistryException { ResourceRegistryException {
createVertex(); createVertex();
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter(); String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
@ -175,37 +185,37 @@ public class ResourceManagement extends EntityManagement<Resource> {
@Override @Override
public boolean reallyDelete() throws ResourceNotFoundException, public boolean reallyDelete() throws ResourceNotFoundException,
ResourceRegistryException { ResourceRegistryException {
//internalDeleteResource(orientGraph, uuid, null); // internalDeleteResource(orientGraph, uuid, null);
getElement(); getElement();
Iterable<Edge> iterable = element.getEdges(Direction.OUT); Iterable<Edge> iterable = element.getEdges(Direction.OUT);
Iterator<Edge> iterator = iterable.iterator(); Iterator<Edge> iterator = iterable.iterator();
while(iterator.hasNext()){ while (iterator.hasNext()) {
Edge edge = iterator.next(); Edge edge = iterator.next();
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType(); OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
RelationManagement relationManagement = null; RelationManagement relationManagement = null;
if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){ if (orientEdgeType.isSubClassOf(IsRelatedTo.NAME)) {
relationManagement = new IsRelatedToManagement(orientGraph); relationManagement = new IsRelatedToManagement(orientGraph);
}else if(orientEdgeType.isSubClassOf(ConsistsOf.NAME)){ } else if (orientEdgeType.isSubClassOf(ConsistsOf.NAME)) {
relationManagement = new ConsistsOfManagement(orientGraph); relationManagement = new ConsistsOfManagement(orientGraph);
}else{ } else {
logger.warn("{} is not a {} nor a {}. " logger.warn("{} is not a {} nor a {}. "
+ "This is really strange ad should not occur. " + "This is really strange ad should not occur. "
+ "Please Investigate it.", + "Please Investigate it.",
Utility.toJsonString(edge, true), Utility.toJsonString(edge, true), IsRelatedTo.NAME,
IsRelatedTo.NAME, ConsistsOf.NAME); ConsistsOf.NAME);
} }
if(relationManagement!=null){ if (relationManagement != null) {
relationManagement.setElement(edge); relationManagement.setElement(edge);
relationManagement.reallyDelete(); relationManagement.reallyDelete();
} }
} }
element.remove(); element.remove();
return true; return true;
} }

View File

@ -4,8 +4,11 @@
package org.gcube.informationsystem.resourceregistry.er.relation; package org.gcube.informationsystem.resourceregistry.er.relation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
@ -91,35 +94,65 @@ public abstract class RelationManagement<R extends Relation> extends
@Override @Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
JSONObject ret = Utility.toJsonObject((OrientEdge) getElement(), false); JSONObject relation = serializeSelfOnly();
Vertex target = element.getVertex(Direction.IN); Vertex target = element.getVertex(Direction.IN);
EntityManagement entityManagement = EntityManagement EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target); .getEntityManagement(orientGraph, target);
try { try {
ret.put(Relation.TARGET_PROPERTY, relation.put(Relation.TARGET_PROPERTY,
entityManagement.serializeAsJson()); entityManagement.serializeAsJson());
} catch (JSONException e) { } catch (JSONException e) {
throw new ResourceRegistryException(e); throw new ResourceRegistryException(e);
} }
return ret; return relation;
} }
protected JSONObject fullSerialize() throws ResourceRegistryException { protected Map<String, JSONObject> fullSerialize(
Map<String, JSONObject> visitedSourceResources)
throws ResourceRegistryException {
Vertex source = element.getVertex(Direction.OUT); Vertex source = element.getVertex(Direction.OUT);
ResourceManagement resourceManagement = (ResourceManagement) EntityManagement String id = source.getId().toString();
.getEntityManagement(orientGraph, source);
JSONObject entityJsonObject = resourceManagement.serializeAsJson();
if (this instanceof IsRelatedToManagement) { JSONObject sourceResource = visitedSourceResources.get(id);
entityJsonObject = ResourceManagement.addIsRelatedTo( if (sourceResource == null) {
entityJsonObject, serializeAsJson()); 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) public Edge reallyCreate(UUID sourceUUID, UUID targetUUID)
@ -246,7 +279,7 @@ public abstract class RelationManagement<R extends Relation> extends
return edge; return edge;
} }
@Override @Override
public boolean reallyAddToContext() throws ContextException, public boolean reallyAddToContext() throws ContextException,
ResourceRegistryException { ResourceRegistryException {
@ -305,7 +338,7 @@ public abstract class RelationManagement<R extends Relation> extends
return false; return false;
} }
} }
@Override @Override
public boolean reallyRemoveFromContext() throws ContextException, public boolean reallyRemoveFromContext() throws ContextException,
ResourceRegistryException { ResourceRegistryException {
@ -412,7 +445,7 @@ public abstract class RelationManagement<R extends Relation> extends
return false; return false;
} }
} }
@Override @Override
public boolean reallyDelete() throws RelationNotFoundException, public boolean reallyDelete() throws RelationNotFoundException,
ResourceRegistryException { ResourceRegistryException {
@ -498,22 +531,23 @@ public abstract class RelationManagement<R extends Relation> extends
} }
} }
protected List<JSONObject> serializeEdges(Iterable<Edge> edges, @SuppressWarnings("unchecked")
protected Collection<JSONObject> serializeEdges(Iterable<Edge> edges,
boolean postFilterPolymorphic) throws ResourceRegistryException { boolean postFilterPolymorphic) throws ResourceRegistryException {
List<JSONObject> list = new ArrayList<>(); Map<String, JSONObject> visitedSourceResources = new HashMap<>();
for (Edge edge : edges) { for (Edge edge : edges) {
if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) { if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) {
continue; continue;
} }
RelationManagement relationManagement = getRelationManagement( RelationManagement relationManagement = getRelationManagement(
orientGraph, edge); orientGraph, edge);
JSONObject jsonObject = relationManagement.fullSerialize(); visitedSourceResources = relationManagement
list.add(jsonObject); .fullSerialize(visitedSourceResources);
} }
return list; return visitedSourceResources.values();
} }
protected String serializeJSONObjectList(List<JSONObject> list) { protected String serializeJSONObjectList(Collection<JSONObject> list) {
JSONArray jsonArray = new JSONArray(list); JSONArray jsonArray = new JSONArray(list);
return jsonArray.toString(); return jsonArray.toString();
} }
@ -522,8 +556,8 @@ public abstract class RelationManagement<R extends Relation> extends
public String reallyGetAll(boolean polymorphic) public String reallyGetAll(boolean polymorphic)
throws ResourceRegistryException { throws ResourceRegistryException {
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic); Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
List<JSONObject> list = serializeEdges(edges, false); Collection<JSONObject> collection = serializeEdges(edges, false);
return serializeJSONObjectList(list); return serializeJSONObjectList(collection);
} }
public String reallyGetAllFrom(UUID uuid, Direction direction, public String reallyGetAllFrom(UUID uuid, Direction direction,