Changed the way to serialize relations

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@141547 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-01-13 11:21:35 +00:00
parent ac2c827022
commit b853a799f9
7 changed files with 181 additions and 156 deletions

View File

@ -638,7 +638,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
throw new ResourceRegistryException(e); throw new ResourceRegistryException(e);
} }
} else { } else {
String error = String.format("Error while updating {} properties", String error = String.format("Error while updating %s properties",
element.toString()); element.toString());
throw new ResourceRegistryException(error); throw new ResourceRegistryException(error);
} }

View File

@ -39,6 +39,7 @@ public class FacetManagement extends EntityManagement<Facet> {
return Utility.toJsonObject((OrientVertex) element, true); return Utility.toJsonObject((OrientVertex) element, true);
} }
@Override
public Vertex reallyCreate() throws FacetAlreadyPresentException, ResourceRegistryException { public Vertex reallyCreate() throws FacetAlreadyPresentException, ResourceRegistryException {
return createVertex(); return createVertex();
} }
@ -51,6 +52,7 @@ public class FacetManagement extends EntityManagement<Facet> {
return facet; return facet;
} }
@Override
public boolean reallyDelete() throws FacetNotFoundException, ResourceRegistryException { public boolean reallyDelete() throws FacetNotFoundException, ResourceRegistryException {
getElement().remove(); getElement().remove();
return true; return true;

View File

@ -47,8 +47,14 @@ public class ResourceManagement extends EntityManagement<Resource> {
super(Resource.class, orientGraph); super(Resource.class, orientGraph);
} }
private static JSONObject marshallResource(OrientGraph orientGraph, @Override
Vertex vertex) throws ResourceRegistryException { public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString();
}
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
Vertex vertex = getElement();
JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex,
true); true);
@ -80,26 +86,30 @@ public class ResourceManagement extends EntityManagement<Resource> {
try { try {
jsonObject.put(AccessType.CONSISTS_OF.lowerCaseFirstCharacter(), jsonObject.put(AccessType.CONSISTS_OF.lowerCaseFirstCharacter(),
consistsOfArray); consistsOfArray);
/*
* jsonObject.put(lowerCaseFirstCharacter(IsRelatedTo.NAME),
* isRelatedToArray);
*/
} catch (JSONException e) { } catch (JSONException e) {
throw new ResourceRegistryException(e); throw new ResourceRegistryException(e);
} }
/*
* jsonObject.put(lowerCaseFirstCharacter(IsRelatedTo.NAME),
* isRelatedToArray);
*/
return jsonObject; return jsonObject;
} }
@Override public static JSONObject addIsRelatedTo(JSONObject entityJsonObject, JSONObject isRealtedToJsonObject) throws ResourceRegistryException{
public String serialize() throws ResourceRegistryException { JSONArray isRelatedToArray = new JSONArray();
return marshallResource(orientGraph, getElement()).toString(); try {
isRelatedToArray.put(isRealtedToJsonObject);
entityJsonObject.put(AccessType.IS_RELATED_TO.lowerCaseFirstCharacter(),
isRelatedToArray);
}catch (JSONException e) {
throw new ResourceRegistryException(e);
}
return entityJsonObject;
} }
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
return marshallResource(orientGraph, getElement());
}
@Override @Override
public Vertex reallyCreate() throws EntityAlreadyPresentException, public Vertex reallyCreate() throws EntityAlreadyPresentException,

View File

@ -48,7 +48,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* *
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public abstract class RelationManagement<R extends Relation> extends ERManagement<R, Edge> { public abstract class RelationManagement<R extends Relation> extends
ERManagement<R, Edge> {
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(RelationManagement.class); .getLogger(RelationManagement.class);
@ -90,35 +91,36 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
@Override @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);
Direction direction = Direction.IN; Vertex target = element.getVertex(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, target);
try { try {
ret.put(property, ret.put(Relation.TARGET_PROPERTY,
entityManagement.serializeAsJson()); entityManagement.serializeAsJson());
} catch (JSONException e) { } catch (JSONException e) {
new ResourceRegistryException(e); throw new ResourceRegistryException(e);
} }
return ret; return ret;
} }
protected JSONObject fullSerialize() throws ResourceRegistryException {
Vertex source = element.getVertex(Direction.OUT);
ResourceManagement resourceManagement = (ResourceManagement) EntityManagement
.getEntityManagement(orientGraph, source);
JSONObject entityJsonObject = resourceManagement.serializeAsJson();
if (this instanceof IsRelatedToManagement) {
entityJsonObject = ResourceManagement.addIsRelatedTo(
entityJsonObject, serializeAsJson());
}
return entityJsonObject;
}
public Edge reallyCreate(UUID sourceUUID, UUID targetUUID) public Edge reallyCreate(UUID sourceUUID, UUID targetUUID)
throws ResourceRegistryException { throws ResourceRegistryException {
@ -221,12 +223,14 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
return reallyCreate(source); return reallyCreate(source);
} }
@Override
public Edge reallyUpdate() throws ResourceRegistryException { public Edge reallyUpdate() throws ResourceRegistryException {
logger.debug("Trying to update {} : {}", erType, jsonNode); logger.debug("Trying to update {} : {}", erType, jsonNode);
Edge edge = getElement(); Edge edge = getElement();
ERManagement.updateProperties(edge, jsonNode, ignoreKeys, ignoreStartWithKeys); ERManagement.updateProperties(edge, jsonNode, ignoreKeys,
ignoreStartWithKeys);
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) { if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY); JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
@ -243,6 +247,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
@Override
public boolean reallyAddToContext() throws ContextException, public boolean reallyAddToContext() throws ContextException,
ResourceRegistryException { ResourceRegistryException {
getElement(); getElement();
@ -267,23 +272,23 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
Vertex target = element.getVertex(Direction.IN); Vertex target = element.getVertex(Direction.IN);
switch (addConstraint) { switch (addConstraint) {
case propagate: case propagate:
/* /*
* The relation must be added only in the case the target vertex * The relation must be added only in the case the target vertex
* must be added. Otherwise we have a relation which point * must be added. Otherwise we have a relation which point to an
* to an entity outside of the context. * entity outside of the context.
*/ */
ContextUtility.addToActualContext(orientGraph, getElement()); ContextUtility.addToActualContext(orientGraph, getElement());
EntityManagement entityManagement = EntityManagement EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target); .getEntityManagement(orientGraph, target);
entityManagement.reallyAddToContext(); entityManagement.reallyAddToContext();
break; break;
case unpropagate: case unpropagate:
break; break;
default: default:
break; break;
} }
return true; return true;
@ -301,6 +306,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
} }
@Override
public boolean reallyRemoveFromContext() throws ContextException, public boolean reallyRemoveFromContext() throws ContextException,
ResourceRegistryException { ResourceRegistryException {
getElement(); getElement();
@ -331,27 +337,27 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
ContextUtility.removeFromActualContext(orientGraph, element); ContextUtility.removeFromActualContext(orientGraph, element);
switch (removeConstraint) { switch (removeConstraint) {
case cascade: case cascade:
removeFromContextTargetVertex(target);
break;
case cascadeWhenOrphan:
Iterable<Edge> iterable = target.getEdges(Direction.IN);
Iterator<Edge> iterator = iterable.iterator();
if (iterator.hasNext()) {
logger.trace(
"{} point to {} which is not orphan. Giving {} directive, it will be not remove from current context.",
element, target, removeConstraint);
} else {
removeFromContextTargetVertex(target); removeFromContextTargetVertex(target);
break; }
break;
case cascadeWhenOrphan: case keep:
Iterable<Edge> iterable = target.getEdges(Direction.IN); break;
Iterator<Edge> iterator = iterable.iterator();
if (iterator.hasNext()) {
logger.trace(
"{} point to {} which is not orphan. Giving {} directive, it will be not remove from current context.",
element, target, removeConstraint);
} else {
removeFromContextTargetVertex(target);
}
break;
case keep: default:
break; break;
default:
break;
} }
return true; return true;
@ -407,6 +413,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
} }
@Override
public boolean reallyDelete() throws RelationNotFoundException, public boolean reallyDelete() throws RelationNotFoundException,
ResourceRegistryException { ResourceRegistryException {
logger.debug( logger.debug(
@ -436,27 +443,27 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
element.remove(); element.remove();
switch (removeConstraint) { switch (removeConstraint) {
case cascade: case cascade:
deleteTargetVertex(target);
break;
case cascadeWhenOrphan:
Iterable<Edge> iterable = target.getEdges(Direction.IN);
Iterator<Edge> iterator = iterable.iterator();
if (iterator.hasNext()) {
logger.trace(
"{} point to {} which is not orphan. Giving {} directive, it will be keep.",
element, target, removeConstraint);
} else {
deleteTargetVertex(target); deleteTargetVertex(target);
break; }
break;
case cascadeWhenOrphan: case keep:
Iterable<Edge> iterable = target.getEdges(Direction.IN); break;
Iterator<Edge> iterator = iterable.iterator();
if (iterator.hasNext()) {
logger.trace(
"{} point to {} which is not orphan. Giving {} directive, it will be keep.",
element, target, removeConstraint);
} else {
deleteTargetVertex(target);
}
break;
case keep: default:
break; break;
default:
break;
} }
return true; return true;
@ -491,62 +498,60 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
} }
protected List<JSONObject> serializeEdges(Iterable<Edge> edges, boolean postFilterPolymorphic, boolean inverse) throws ResourceRegistryException{ protected List<JSONObject> serializeEdges(Iterable<Edge> edges,
boolean postFilterPolymorphic) throws ResourceRegistryException {
List<JSONObject> list = new ArrayList<>(); List<JSONObject> list = new ArrayList<>();
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(orientGraph, edge); RelationManagement relationManagement = getRelationManagement(
JSONObject jsonObject = relationManagement.serializeAsJson(inverse); orientGraph, edge);
JSONObject jsonObject = relationManagement.fullSerialize();
list.add(jsonObject); list.add(jsonObject);
} }
return list; return list;
} }
protected String serializeJSONObjectList(List<JSONObject> list){ protected String serializeJSONObjectList(List<JSONObject> list) {
JSONArray jsonArray = new JSONArray(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);
List<JSONObject> list = serializeEdges(edges, false, false); List<JSONObject> list = serializeEdges(edges, false);
return serializeJSONObjectList(list); 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 {
EntityManagement entityManagement = null; EntityManagement entityManagement = null;
try { try {
entityManagement = (EntityManagement) ERManagement.getERManagementFromUUID(orientGraph, uuid); entityManagement = (EntityManagement) ERManagement
}catch(ResourceRegistryException e) { .getERManagementFromUUID(orientGraph, uuid);
} catch (ResourceRegistryException e) {
throw e; throw e;
}catch (Exception e) { } catch (Exception e) {
throw new ResourceRegistryException(String.format("Provided UUID %s does not belogn to any %s", uuid.toString(), Entity.NAME)); throw new ResourceRegistryException(String.format(
"Provided UUID %s does not belogn to any %s",
uuid.toString(), Entity.NAME));
} }
Vertex vertex = (Vertex) entityManagement.getElement(); Vertex vertex = (Vertex) entityManagement.getElement();
List<JSONObject> list = new ArrayList<>(); List<JSONObject> list = new ArrayList<>();
Iterable<Edge> edges = vertex.getEdges(Direction.BOTH, erType);
if(direction.equals(Direction.BOTH) || direction.equals(Direction.OUT)){ list.addAll(serializeEdges(edges, !polymorphic));
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); 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 {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER); .getActualSecurityContextGraph(PermissionMode.READER);
@ -563,5 +568,4 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
} }
} }
} }

View File

@ -1,5 +1,6 @@
package org.gcube.informationsystem.resourceregistry.rest; package org.gcube.informationsystem.resourceregistry.rest;
import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -132,7 +133,7 @@ public class Access {
uuid = UUID.fromString(reference); uuid = UUID.fromString(reference);
} catch (Exception e) { } catch (Exception e) {
String errror = String.format( String errror = String.format(
"Provided %s (%s) is not a valid %", "Provided %s (%s) is not a valid %s",
AccessPath.REFERENCE, reference, AccessPath.REFERENCE, reference,
UUID.class.getSimpleName()); UUID.class.getSimpleName());
throw new ResourceRegistryException(errror); throw new ResourceRegistryException(errror);
@ -149,8 +150,7 @@ public class Access {
String errror = String String errror = String
.format("Provided %s (%s) is not valid. Allowed values are %s", .format("Provided %s (%s) is not valid. Allowed values are %s",
AccessPath.DIRECTION, direction, AccessPath.DIRECTION, direction,
Direction.values().toString() Arrays.toString(Direction.values()).toLowerCase());
.toLowerCase());
throw new ResourceRegistryException(errror); throw new ResourceRegistryException(errror);
} }
} }

View File

@ -12,6 +12,7 @@ import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Resource; import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
@ -49,7 +50,8 @@ public class SchemaManagementImpl implements SchemaManagement {
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(SchemaManagementImpl.class); .getLogger(SchemaManagementImpl.class);
protected static OClass getOClass(OSchema oSchema, String type) throws SchemaException { protected static OClass getOClass(OSchema oSchema, String type)
throws SchemaException {
return oSchema.getClass(type); return oSchema.getClass(type);
} }
@ -182,7 +184,6 @@ public class SchemaManagementImpl implements SchemaManagement {
OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();
OClass oClass = null; OClass oClass = null;
if (Entity.class.isAssignableFrom(baseType.getTypeClass())) { if (Entity.class.isAssignableFrom(baseType.getTypeClass())) {
@ -193,9 +194,16 @@ public class SchemaManagementImpl implements SchemaManagement {
.getName()); .getName());
} else if (Embedded.class.isAssignableFrom(baseType.getTypeClass())) { } else if (Embedded.class.isAssignableFrom(baseType.getTypeClass())) {
oClass = oSchema.createClass(typeDefinition.getName()); oClass = oSchema.createClass(typeDefinition.getName());
} else {
String error = String
.format("Allowed superclass are %s, %s, %s, or any subclasses of them.",
Entity.NAME, Relation.NAME, Embedded.NAME);
throw new ResourceRegistryException(error);
} }
oClass.setDescription(typeDefinition.getDescription()); if (typeDefinition.getDescription() != null) {
oClass.setDescription(typeDefinition.getDescription());
}
try { try {
oClass.setAbstract(typeDefinition.isAbstract()); oClass.setAbstract(typeDefinition.isAbstract());
@ -207,8 +215,7 @@ public class SchemaManagementImpl implements SchemaManagement {
if (typeDefinition.getName().compareTo(Embedded.NAME) != 0) { if (typeDefinition.getName().compareTo(Embedded.NAME) != 0) {
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy( List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
orientGraphNoTx, typeDefinition, orientGraphNoTx, typeDefinition, baseType.getName());
baseType.getName());
oClass.setSuperClasses(oSuperclasses); oClass.setSuperClasses(oSuperclasses);
} }
@ -262,7 +269,8 @@ public class SchemaManagementImpl implements SchemaManagement {
OClass toBeSerializedOClass = oClass; OClass toBeSerializedOClass = oClass;
if (oClass instanceof OrientElementType) { if (oClass instanceof OrientElementType) {
toBeSerializedOClass = getOClass(oSchema, typeDefinition.getName()); toBeSerializedOClass = getOClass(oSchema,
typeDefinition.getName());
} }
String ret = getTypeDefinitionAsString(toBeSerializedOClass); String ret = getTypeDefinitionAsString(toBeSerializedOClass);
@ -294,7 +302,6 @@ public class SchemaManagementImpl implements SchemaManagement {
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();
OClass baseOClass = getTypeSchema(oSchema, type, null); OClass baseOClass = getTypeSchema(oSchema, type, null);
List<TypeDefinition> typeDefinitions = new ArrayList<>(); List<TypeDefinition> typeDefinitions = new ArrayList<>();
Collection<OClass> oClasses = oSchema.getClasses(); Collection<OClass> oClasses = oSchema.getClasses();
for (OClass oClass : oClasses) { for (OClass oClass : oClasses) {
@ -317,19 +324,20 @@ public class SchemaManagementImpl implements SchemaManagement {
} }
@Override @Override
public String create(String jsonSchema, AccessType accessType) throws SchemaException { public String create(String jsonSchema, AccessType accessType)
throws SchemaException {
return registerTypeSchema(jsonSchema, accessType); return registerTypeSchema(jsonSchema, accessType);
} }
@Override @Override
public String read(String entityType, boolean includeSubtypes) throws SchemaNotFoundException, public String read(String entityType, boolean includeSubtypes)
SchemaException { throws SchemaNotFoundException, SchemaException {
return getSchema(entityType, includeSubtypes); return getSchema(entityType, includeSubtypes);
} }
@Override @Override
public String update(String entityType, AccessType accessType, String jsonSchema) public String update(String entityType, AccessType accessType,
throws SchemaNotFoundException, SchemaException { String jsonSchema) throws SchemaNotFoundException, SchemaException {
throw new UnsupportedOperationException("Not Yet implemented"); throw new UnsupportedOperationException("Not Yet implemented");
} }

View File

@ -6,6 +6,7 @@ package org.gcube.informationsystem.resourceregistry.er;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;