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

@ -39,6 +39,7 @@ public class FacetManagement extends EntityManagement<Facet> {
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,27 +86,31 @@ public class ResourceManagement extends EntityManagement<Resource> {
try {
jsonObject.put(AccessType.CONSISTS_OF.lowerCaseFirstCharacter(),
consistsOfArray);
} catch (JSONException e) {
throw new ResourceRegistryException(e);
}
/*
* jsonObject.put(lowerCaseFirstCharacter(IsRelatedTo.NAME),
* isRelatedToArray);
*/
} catch (JSONException e) {
throw new ResourceRegistryException(e);
}
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);
}
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
return marshallResource(orientGraph, getElement());
return entityJsonObject;
}
@Override
public Vertex reallyCreate() throws EntityAlreadyPresentException,
ResourceRegistryException {

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,35 +91,36 @@ 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 {
@ -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);
@ -243,6 +247,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
}
@Override
public boolean reallyAddToContext() throws ContextException,
ResourceRegistryException {
getElement();
@ -270,8 +275,8 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
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.
* must be added. Otherwise we have a relation which point to an
* entity outside of the context.
*/
ContextUtility.addToActualContext(orientGraph, getElement());
EntityManagement entityManagement = EntityManagement
@ -301,6 +306,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
}
}
@Override
public boolean reallyRemoveFromContext() throws ContextException,
ResourceRegistryException {
getElement();
@ -407,6 +413,7 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
}
}
@Override
public boolean reallyDelete() throws RelationNotFoundException,
ResourceRegistryException {
logger.debug(
@ -491,14 +498,16 @@ 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) {
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;
@ -509,44 +518,40 @@ public abstract class RelationManagement<R extends Relation> extends ERManagemen
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);
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));
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));
}
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);
@ -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;
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);
}
@ -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,9 +194,16 @@ 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());
}
try {
oClass.setAbstract(typeDefinition.isAbstract());
@ -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);
}
@ -262,7 +269,8 @@ 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) {
@ -317,19 +324,20 @@ 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;