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 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.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<Facet> {
@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

View File

@ -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<Resource> {
private static Logger logger = LoggerFactory
.getLogger(ResourceManagement.class);
public ResourceManagement() {
super(Resource.class);
}
@ -51,69 +50,80 @@ public class ResourceManagement extends EntityManagement<Resource> {
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<Edge> edges = vertex.getEdges(Direction.OUT);
for(Edge edge : edges){
Iterable<Edge> 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<Resource> {
@Override
public boolean reallyDelete() throws ResourceNotFoundException,
ResourceRegistryException {
//internalDeleteResource(orientGraph, uuid, null);
// internalDeleteResource(orientGraph, uuid, null);
getElement();
Iterable<Edge> iterable = element.getEdges(Direction.OUT);
Iterator<Edge> 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;
}

View File

@ -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<R extends Relation> 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<String, JSONObject> fullSerialize(
Map<String, JSONObject> 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<R extends Relation> extends
return edge;
}
@Override
public boolean reallyAddToContext() throws ContextException,
ResourceRegistryException {
@ -305,7 +338,7 @@ public abstract class RelationManagement<R extends Relation> extends
return false;
}
}
@Override
public boolean reallyRemoveFromContext() throws ContextException,
ResourceRegistryException {
@ -412,7 +445,7 @@ public abstract class RelationManagement<R extends Relation> extends
return false;
}
}
@Override
public boolean reallyDelete() throws RelationNotFoundException,
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 {
List<JSONObject> list = new ArrayList<>();
Map<String, JSONObject> 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<JSONObject> list) {
protected String serializeJSONObjectList(Collection<JSONObject> list) {
JSONArray jsonArray = new JSONArray(list);
return jsonArray.toString();
}
@ -522,8 +556,8 @@ public abstract class RelationManagement<R extends Relation> extends
public String reallyGetAll(boolean polymorphic)
throws ResourceRegistryException {
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
List<JSONObject> list = serializeEdges(edges, false);
return serializeJSONObjectList(list);
Collection<JSONObject> collection = serializeEdges(edges, false);
return serializeJSONObjectList(collection);
}
public String reallyGetAllFrom(UUID uuid, Direction direction,