Implementing get all relation from a provided resource

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@141522 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-01-12 10:13:09 +00:00
parent 2b55c43477
commit 41b112670e
2 changed files with 125 additions and 11 deletions

View File

@ -3,7 +3,9 @@
*/ */
package org.gcube.informationsystem.resourceregistry.er.relation; package org.gcube.informationsystem.resourceregistry.er.relation;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
@ -81,19 +83,34 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
this.orientGraph = orientGraph; this.orientGraph = orientGraph;
} }
@Override
public String serialize() throws ResourceRegistryException { public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString(); return serializeAsJson().toString();
} }
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(false);
}
public JSONObject serializeAsJson(boolean inverse) throws ResourceRegistryException {
JSONObject ret = Utility.toJsonObject((OrientEdge) getElement(), false); JSONObject ret = Utility.toJsonObject((OrientEdge) getElement(), false);
Vertex vertex = element.getVertex(Direction.IN); Direction direction = Direction.IN;
String property = Relation.TARGET_PROPERTY;
if(inverse){
direction = Direction.OUT;
property = Relation.SOURCE_PROPERTY;
}
Vertex vertex = element.getVertex(direction);
EntityManagement entityManagement = EntityManagement EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, vertex); .getEntityManagement(orientGraph, vertex);
try { try {
ret.put(Relation.TARGET_PROPERTY, ret.put(property,
entityManagement.serializeAsJson()); entityManagement.serializeAsJson());
} catch (JSONException e) { } catch (JSONException e) {
new ResourceRegistryException(e); new ResourceRegistryException(e);
@ -102,6 +119,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
return ret; return ret;
} }
public Edge reallyCreate(UUID sourceUUID, UUID targetUUID) public Edge reallyCreate(UUID sourceUUID, UUID targetUUID)
throws ResourceRegistryException { throws ResourceRegistryException {
ResourceManagement srmSource = new ResourceManagement(orientGraph); ResourceManagement srmSource = new ResourceManagement(orientGraph);
@ -473,20 +491,30 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
} }
protected String serializeEdges(Iterable<Edge> edges) throws ResourceRegistryException{ protected List<JSONObject> serializeEdges(Iterable<Edge> edges, boolean postFilterPolymorphic, boolean inverse) throws ResourceRegistryException{
JSONArray jsonArray = new JSONArray(); List<JSONObject> list = new ArrayList<>();
for(Edge edge : edges){ for(Edge edge : edges){
if(postFilterPolymorphic && edge.getLabel().compareTo(erType)!=0){
continue;
}
RelationManagement relationManagement = getRelationManagement(orientGraph, edge); RelationManagement relationManagement = getRelationManagement(orientGraph, edge);
JSONObject jsonObject = relationManagement.serializeAsJson(); JSONObject jsonObject = relationManagement.serializeAsJson(inverse);
jsonArray.put(jsonObject); list.add(jsonObject);
} }
return list;
}
protected String serializeJSONObjectList(List<JSONObject> list){
JSONArray jsonArray = new JSONArray(list);
return jsonArray.toString(); return jsonArray.toString();
} }
@Override @Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic); Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
return serializeEdges(edges); List<JSONObject> list = serializeEdges(edges, false, false);
return serializeJSONObjectList(list);
} }
public String reallyGetAllFrom(UUID uuid, Direction direction, boolean polymorphic) throws ResourceRegistryException { public String reallyGetAllFrom(UUID uuid, Direction direction, boolean polymorphic) throws ResourceRegistryException {
@ -500,9 +528,22 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
Vertex vertex = (Vertex) entityManagement.getElement(); Vertex vertex = (Vertex) entityManagement.getElement();
Iterable<Edge> edges = vertex.getEdges(direction, erType);
return serializeEdges(edges); List<JSONObject> list = new ArrayList<>();
if(direction.equals(Direction.BOTH) || direction.equals(Direction.OUT)){
Iterable<Edge> edges = vertex.getEdges(Direction.OUT, erType);
list.addAll(serializeEdges(edges, !polymorphic, false));
}
if(direction.equals(Direction.BOTH) || direction.equals(Direction.IN)){
Iterable<Edge> edges = vertex.getEdges(Direction.IN, erType);
list.addAll(serializeEdges(edges, !polymorphic, true));
}
return serializeJSONObjectList(list);
} }
public String allFrom(UUID uuid, Direction direction, boolean polymorphic) throws ResourceRegistryException { public String allFrom(UUID uuid, Direction direction, boolean polymorphic) throws ResourceRegistryException {

View File

@ -55,6 +55,8 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.tinkerpop.blueprints.Direction;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
* *
@ -505,6 +507,77 @@ public class ERManagementTest extends ScopedTest {
} }
}
@Test
public void testGetAllFrom() throws Exception{
Map<String, Resource> map = createHostingNodeAndEService();
EService eService = (EService) map.get(EService.NAME);
HostingNode hostingNode = (HostingNode) map.get(HostingNode.NAME);
IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement();
isRelatedToManagement.setElementType(IsRelatedTo.NAME);
String json = isRelatedToManagement.allFrom(eService.getHeader().getUUID(), Direction.BOTH, true);
@SuppressWarnings("rawtypes")
List<IsRelatedTo> isRelatedToList = Entities.unmarshalList(IsRelatedTo.class, json);
Assert.assertTrue(isRelatedToList.size()==1);
Assert.assertTrue(isRelatedToList.get(0).getSource().getHeader().getUUID().compareTo(hostingNode.getHeader().getUUID())==0);
json = isRelatedToManagement.allFrom(eService.getHeader().getUUID(), Direction.IN, true);
isRelatedToList = Entities.unmarshalList(IsRelatedTo.class, json);
Assert.assertTrue(isRelatedToList.size()==1);
Assert.assertTrue(isRelatedToList.get(0).getSource().getHeader().getUUID().compareTo(hostingNode.getHeader().getUUID())==0);
json = isRelatedToManagement.allFrom(eService.getHeader().getUUID(), Direction.OUT, true);
isRelatedToList = Entities.unmarshalList(IsRelatedTo.class, json);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(eService.getHeader().getUUID(), Direction.BOTH, false);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(eService.getHeader().getUUID(), Direction.IN, false);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(eService.getHeader().getUUID(), Direction.OUT, false);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(hostingNode.getHeader().getUUID(), Direction.BOTH, true);
Assert.assertTrue(isRelatedToList.size()==1);
Assert.assertTrue(isRelatedToList.get(0).getTarget().getHeader().getUUID().compareTo(eService.getHeader().getUUID())==0);
json = isRelatedToManagement.allFrom(hostingNode.getHeader().getUUID(), Direction.IN, true);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(hostingNode.getHeader().getUUID(), Direction.OUT, true);
Assert.assertTrue(isRelatedToList.size()==1);
Assert.assertTrue(isRelatedToList.get(0).getTarget().getHeader().getUUID().compareTo(eService.getHeader().getUUID())==0);
json = isRelatedToManagement.allFrom(hostingNode.getHeader().getUUID(), Direction.BOTH, false);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(hostingNode.getHeader().getUUID(), Direction.IN, false);
Assert.assertTrue(isRelatedToList.size()==0);
json = isRelatedToManagement.allFrom(hostingNode.getHeader().getUUID(), Direction.OUT, false);
Assert.assertTrue(isRelatedToList.size()==0);
/* Removing created Entity and Relation to have a clean DB */
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNode.getHeader().getUUID());
resourceManagement.delete();
} }
} }