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);
}
} else {
String error = String.format("Error while updating {} properties",
String error = String.format("Error while updating %s properties",
element.toString());
throw new ResourceRegistryException(error);
}

View File

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

View File

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

View File

@ -48,7 +48,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
*
*/
@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
.getLogger(RelationManagement.class);
@ -90,36 +91,37 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(false);
}
public JSONObject serializeAsJson(boolean inverse) throws ResourceRegistryException {
JSONObject ret = Utility.toJsonObject((OrientEdge) getElement(), false);
Direction direction = Direction.IN;
String property = Relation.TARGET_PROPERTY;
if(inverse){
direction = Direction.OUT;
property = Relation.SOURCE_PROPERTY;
}
Vertex vertex = element.getVertex(direction);
Vertex target = element.getVertex(Direction.IN);
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, vertex);
.getEntityManagement(orientGraph, target);
try {
ret.put(property,
ret.put(Relation.TARGET_PROPERTY,
entityManagement.serializeAsJson());
} catch (JSONException e) {
new ResourceRegistryException(e);
throw new ResourceRegistryException(e);
}
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)
throws ResourceRegistryException {
ResourceManagement srmSource = new ResourceManagement(orientGraph);
@ -221,12 +223,14 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
return reallyCreate(source);
}
@Override
public Edge reallyUpdate() throws ResourceRegistryException {
logger.debug("Trying to update {} : {}", erType, jsonNode);
Edge edge = getElement();
ERManagement.updateProperties(edge, jsonNode, ignoreKeys, ignoreStartWithKeys);
ERManagement.updateProperties(edge, jsonNode, ignoreKeys,
ignoreStartWithKeys);
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
@ -242,11 +246,12 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
return edge;
}
@Override
public boolean reallyAddToContext() throws ContextException,
ResourceRegistryException {
getElement();
AddConstraint addConstraint = AddConstraint.unpropagate;
try {
@ -267,23 +272,23 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
Vertex target = element.getVertex(Direction.IN);
switch (addConstraint) {
case propagate:
/*
* The relation must be added only in the case the target vertex
* must be added. Otherwise we have a relation which point
* to an entity outside of the context.
*/
ContextUtility.addToActualContext(orientGraph, getElement());
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
entityManagement.reallyAddToContext();
break;
case unpropagate:
break;
default:
break;
case propagate:
/*
* The relation must be added only in the case the target vertex
* must be added. Otherwise we have a relation which point to an
* entity outside of the context.
*/
ContextUtility.addToActualContext(orientGraph, getElement());
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
entityManagement.reallyAddToContext();
break;
case unpropagate:
break;
default:
break;
}
return true;
@ -300,7 +305,8 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
return false;
}
}
@Override
public boolean reallyRemoveFromContext() throws ContextException,
ResourceRegistryException {
getElement();
@ -323,7 +329,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
}
Vertex target = element.getVertex(Direction.IN);
/*
* In any removeConstraint value the relation MUSt be removed from
* context to avoid to have edge having a source outside of the context.
@ -331,27 +337,27 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
ContextUtility.removeFromActualContext(orientGraph, element);
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);
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);
}
break;
case keep:
break;
default:
break;
}
break;
case keep:
break;
default:
break;
}
return true;
@ -406,13 +412,14 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
return false;
}
}
@Override
public boolean reallyDelete() throws RelationNotFoundException,
ResourceRegistryException {
logger.debug(
"Going to remove {} with UUID {}. Related {}s will be detached.",
baseType, uuid, targetEntityClass.getSimpleName());
getElement();
RemoveConstraint removeConstraint = RemoveConstraint.keep;
@ -436,27 +443,27 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
element.remove();
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);
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);
}
break;
case keep:
break;
default:
break;
}
break;
case keep:
break;
default:
break;
}
return true;
@ -490,63 +497,61 @@ 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<>();
for(Edge edge : edges){
if(postFilterPolymorphic && edge.getLabel().compareTo(erType)!=0){
for (Edge edge : edges) {
if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) {
continue;
}
RelationManagement relationManagement = getRelationManagement(orientGraph, edge);
JSONObject jsonObject = relationManagement.serializeAsJson(inverse);
RelationManagement relationManagement = getRelationManagement(
orientGraph, edge);
JSONObject jsonObject = relationManagement.fullSerialize();
list.add(jsonObject);
}
return list;
}
protected String serializeJSONObjectList(List<JSONObject> list){
protected String serializeJSONObjectList(List<JSONObject> list) {
JSONArray jsonArray = new JSONArray(list);
return jsonArray.toString();
}
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
public String reallyGetAll(boolean polymorphic)
throws ResourceRegistryException {
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
List<JSONObject> list = serializeEdges(edges, false, false);
List<JSONObject> list = serializeEdges(edges, 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 {
EntityManagement entityManagement = null;
try {
entityManagement = (EntityManagement) ERManagement.getERManagementFromUUID(orientGraph, uuid);
}catch(ResourceRegistryException e) {
entityManagement = (EntityManagement) ERManagement
.getERManagementFromUUID(orientGraph, uuid);
} catch (ResourceRegistryException e) {
throw e;
}catch (Exception e) {
throw new ResourceRegistryException(String.format("Provided UUID %s does not belogn to any %s", uuid.toString(), Entity.NAME));
} catch (Exception e) {
throw new ResourceRegistryException(String.format(
"Provided UUID %s does not belogn to any %s",
uuid.toString(), Entity.NAME));
}
Vertex vertex = (Vertex) entityManagement.getElement();
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));
}
Vertex vertex = (Vertex) entityManagement.getElement();
List<JSONObject> list = new ArrayList<>();
Iterable<Edge> edges = vertex.getEdges(Direction.BOTH, erType);
list.addAll(serializeEdges(edges, !polymorphic));
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 {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER);
@ -562,6 +567,5 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
}
}
}
}

View File

@ -1,5 +1,6 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.Arrays;
import java.util.UUID;
import javax.ws.rs.GET;
@ -132,7 +133,7 @@ public class Access {
uuid = UUID.fromString(reference);
} catch (Exception e) {
String errror = String.format(
"Provided %s (%s) is not a valid %",
"Provided %s (%s) is not a valid %s",
AccessPath.REFERENCE, reference,
UUID.class.getSimpleName());
throw new ResourceRegistryException(errror);
@ -149,8 +150,7 @@ public class Access {
String errror = String
.format("Provided %s (%s) is not valid. Allowed values are %s",
AccessPath.DIRECTION, direction,
Direction.values().toString()
.toLowerCase());
Arrays.toString(Direction.values()).toLowerCase());
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.Resource;
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.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
@ -49,7 +50,8 @@ public class SchemaManagementImpl implements SchemaManagement {
private static Logger logger = LoggerFactory
.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);
}
@ -124,7 +126,7 @@ public class SchemaManagementImpl implements SchemaManagement {
throw new SchemaException(e);
}
}
protected List<OClass> getSuperclassesAndCheckCompliancy(
OrientGraphNoTx orientGraphNoTx, TypeDefinition typeDefinition,
String baseType) throws SchemaException {
@ -182,7 +184,6 @@ public class SchemaManagementImpl implements SchemaManagement {
OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema();
OClass oClass = null;
if (Entity.class.isAssignableFrom(baseType.getTypeClass())) {
@ -193,10 +194,17 @@ public class SchemaManagementImpl implements SchemaManagement {
.getName());
} else if (Embedded.class.isAssignableFrom(baseType.getTypeClass())) {
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);
}
if (typeDefinition.getDescription() != null) {
oClass.setDescription(typeDefinition.getDescription());
}
oClass.setDescription(typeDefinition.getDescription());
try {
oClass.setAbstract(typeDefinition.isAbstract());
} catch (Exception e) {
@ -207,8 +215,7 @@ public class SchemaManagementImpl implements SchemaManagement {
if (typeDefinition.getName().compareTo(Embedded.NAME) != 0) {
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(
orientGraphNoTx, typeDefinition,
baseType.getName());
orientGraphNoTx, typeDefinition, baseType.getName());
oClass.setSuperClasses(oSuperclasses);
}
@ -259,10 +266,11 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
}
OClass toBeSerializedOClass = oClass;
if (oClass instanceof OrientElementType) {
toBeSerializedOClass = getOClass(oSchema, typeDefinition.getName());
toBeSerializedOClass = getOClass(oSchema,
typeDefinition.getName());
}
String ret = getTypeDefinitionAsString(toBeSerializedOClass);
@ -294,7 +302,6 @@ public class SchemaManagementImpl implements SchemaManagement {
OSchema oSchema = oMetadata.getSchema();
OClass baseOClass = getTypeSchema(oSchema, type, null);
List<TypeDefinition> typeDefinitions = new ArrayList<>();
Collection<OClass> oClasses = oSchema.getClasses();
for (OClass oClass : oClasses) {
@ -315,21 +322,22 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
@Override
public String create(String jsonSchema, AccessType accessType) throws SchemaException {
public String create(String jsonSchema, AccessType accessType)
throws SchemaException {
return registerTypeSchema(jsonSchema, accessType);
}
@Override
public String read(String entityType, boolean includeSubtypes) throws SchemaNotFoundException,
SchemaException {
public String read(String entityType, boolean includeSubtypes)
throws SchemaNotFoundException, SchemaException {
return getSchema(entityType, includeSubtypes);
}
@Override
public String update(String entityType, AccessType accessType, String jsonSchema)
throws SchemaNotFoundException, SchemaException {
public String update(String entityType, AccessType accessType,
String jsonSchema) throws SchemaNotFoundException, SchemaException {
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.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;