diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java index 3d0261b..654051c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java @@ -1,11 +1,14 @@ package org.gcube.informationsystem.resourceregistry.context; +import java.io.StringWriter; import java.util.Iterator; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.AccessType; +import org.gcube.informationsystem.model.embedded.Header; import org.gcube.informationsystem.model.entity.Context; +import org.gcube.informationsystem.model.relation.IsParentOf; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException; @@ -20,6 +23,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.JsonNode; +import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; @@ -46,6 +50,10 @@ public class ContextManagement extends EntityManagement { init(); } + public String getName() { + return jsonNode.get(Context.NAME_PROPERTY).asText(); + } + @Override protected ContextNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { return new ContextNotFoundException(e.getMessage(), e.getCause()); @@ -61,6 +69,61 @@ public class ContextManagement extends EntityManagement { return new ContextAlreadyPresentException(message); } + protected void checkContext(ContextManagement parentContext, ContextManagement childContext) throws ContextNotFoundException, + ContextAlreadyPresentException, ResourceRegistryException { + + if (parentContext != null) { + String parentId = parentContext.getElement().getId().toString(); + + // TODO Rewrite using Gremlin + String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME + + ") FROM " + parentId + " MAXDEPTH 1) WHERE " + + Context.NAME_PROPERTY + "=\"" + childContext.getName() + "\" AND " + + Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + + "<>\"" + parentContext.toString() + "\""; + + logger.trace(select); + + StringWriter message = new StringWriter(); + message.append("A context with the same name ("); + message.append(childContext.getName()); + message.append(") has been already created as child of "); + message.append(parentContext.serializeSelfOnly().toString()); + + logger.trace("Checking if {} -> {}", message, select); + + OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery( + select); + Iterable vertexes = orientGraph.command(osqlSynchQuery) + .execute(); + + if (vertexes != null && vertexes.iterator().hasNext()) { + throw new ContextAlreadyPresentException(message.toString()); + } + + } else { + // TODO Rewrite using Gremlin + String select = "SELECT FROM " + + org.gcube.informationsystem.model.entity.Context.NAME + + " WHERE " + Context.NAME_PROPERTY + " = \"" + childContext.getName() + + "\"" + " AND in(\"" + IsParentOf.NAME + "\").size() = 0"; + + OSQLSynchQuery osqlSynchQuery = new OSQLSynchQuery( + select); + Iterable vertexes = orientGraph.command(osqlSynchQuery) + .execute(); + + if (vertexes != null && vertexes.iterator().hasNext()) { + throw new ContextAlreadyPresentException( + "A root context with the same name (" + childContext.getName() + + ") already exist"); + } + + } + + } + + @Override public String serialize() throws ResourceRegistryException { return serializeAsJson().toString(); @@ -111,6 +174,9 @@ public class ContextManagement extends EntityManagement { @Override protected Vertex reallyCreate() throws ERAlreadyPresentException, ResourceRegistryException { + + + createVertex(); String propertyName = Context.PARENT_PROPERTY; @@ -139,6 +205,7 @@ public class ContextManagement extends EntityManagement { // TODO check if parent changed + // TODO check if name changes and check the feasibility context = (Vertex) ERManagement.updateProperties(oClass, context, jsonNode, ignoreKeys, ignoreStartWithKeys); return context; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java index 544a0e1..c8a5854 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java @@ -35,7 +35,7 @@ public class ContextManagementImpl implements OLDContextManagement { private static Logger logger = LoggerFactory .getLogger(ContextManagementImpl.class); - protected Vertex checkContext(OrientGraph orientGraph, UUID parentContext, + protected Vertex checkContext(OrientGraph orientGraph, UUID parentContext, String contextName) throws ContextNotFoundException, ContextException { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java index 468984f..7b4035a 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java @@ -199,7 +199,7 @@ public class ContextUtility { ScopeBean scopeBean = new ScopeBean(fullName); String name = scopeBean.name(); - // TODO Rewrite the previous query using Gremlin + // TODO Rewrite the query using Gremlin // Please note that this query works because all the scope parts has a // different name String select = "SELECT FROM " + Context.class.getSimpleName() diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java index e235db8..ff8c776 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java @@ -3,8 +3,6 @@ */ package org.gcube.informationsystem.resourceregistry.context; -import java.util.UUID; - import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.relation.IsParentOf; @@ -96,17 +94,13 @@ public class IsParentOfManagement extends RelationManagement { public El internalUpdate() throws ERNotFoundException, ResourceRegistryException { try { + reallyUpdate(); + HeaderUtility.updateModifiedByAndLastUpdate(element); ((OrientVertex) element).save(); @@ -490,6 +492,7 @@ public abstract class ERManagement { try { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin); + element = internalUpdate(); orientGraph.commit(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java index 2208bc0..9239058 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java @@ -18,15 +18,22 @@ import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManag import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; import org.gcube.informationsystem.resourceregistry.schema.SchemaManagementImpl; import org.gcube.informationsystem.resourceregistry.utils.Utility; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.orientechnologies.orient.core.metadata.schema.OClass; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; +import com.tinkerpop.blueprints.impls.orient.OrientEdge; +import com.tinkerpop.blueprints.impls.orient.OrientEdgeType; import com.tinkerpop.blueprints.impls.orient.OrientGraph; +import com.tinkerpop.blueprints.impls.orient.OrientVertex; +import com.tinkerpop.blueprints.impls.orient.OrientVertexType; public class ERManagementUtility { - + + private static Logger logger = LoggerFactory.getLogger(EntityManagement.class); /* @SuppressWarnings("rawtypes") public static ERManagement getERManagement(AccessType querableType) @@ -81,10 +88,10 @@ public class ERManagementUtility { private static ERManagement getERManagement(OrientGraph orientGraph, Element element) throws ResourceRegistryException { if (element instanceof Vertex) { - return EntityManagement.getEntityManagement(orientGraph, + return getEntityManagement(orientGraph, (Vertex) element); } else if (element instanceof Edge) { - return RelationManagement.getRelationManagement(orientGraph, + return getRelationManagement(orientGraph, (Edge) element); } throw new ResourceRegistryException(String.format( @@ -131,4 +138,71 @@ public class ERManagementUtility { } } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static EntityManagement getEntityManagement(OrientGraph orientGraph, + Vertex vertex) throws ResourceRegistryException { + + if(orientGraph==null){ + throw new ResourceRegistryException(OrientGraph.class.getSimpleName() + "instance is null. This is really strage please contact the administrator."); + } + + if(vertex==null){ + throw new ResourceRegistryException(Vertex.class.getSimpleName() + "instance is null. This is really strage please contact the administrator."); + } + + OrientVertexType orientVertexType = null; + try { + orientVertexType = ((OrientVertex) vertex).getType(); + }catch (Exception e) { + String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString()); + logger.error(error, e); + throw new ResourceRegistryException(error); + } + + EntityManagement entityManagement = null; + if (orientVertexType.isSubClassOf(Resource.NAME)) { + entityManagement = new ResourceManagement(orientGraph); + } else if (orientVertexType.isSubClassOf(Facet.NAME)) { + entityManagement = new FacetManagement(orientGraph); + } else { + String error = String.format("{%s is not a %s nor a %s. " + + "This is really strange and should not occur. " + + "Please Investigate it.", vertex, Resource.NAME, + Facet.NAME); + throw new ResourceRegistryException(error); + } + entityManagement.setElement(vertex); + return entityManagement; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static RelationManagement getRelationManagement(OrientGraph orientGraph, Edge edge) + throws ResourceRegistryException { + + if (orientGraph == null) { + throw new ResourceRegistryException(OrientGraph.class.getSimpleName() + + "instance is null. This is really strage please contact the administrator."); + } + + if (edge == null) { + throw new ResourceRegistryException(Edge.class.getSimpleName() + + "instance is null. This is really strage please contact the administrator."); + } + + OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType(); + RelationManagement relationManagement = null; + if (orientEdgeType.isSubClassOf(ConsistsOf.NAME)) { + relationManagement = new ConsistsOfManagement(orientGraph); + } else if (orientEdgeType.isSubClassOf(IsRelatedTo.NAME)) { + relationManagement = new IsRelatedToManagement(orientGraph); + } else { + String error = String.format("{%s is not a %s nor a %s. " + "This is really strange ad should not occur. " + + "Please Investigate it.", edge, ConsistsOf.NAME, IsRelatedTo.NAME); + throw new ResourceRegistryException(error); + } + relationManagement.setElement(edge); + return relationManagement; + } + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java index 8cb90cf..6f24e3d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java @@ -3,12 +3,13 @@ */ package org.gcube.informationsystem.resourceregistry.er.entity; +import java.util.HashMap; +import java.util.Map; + import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.entity.Entity; -import org.gcube.informationsystem.model.entity.Facet; -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.context.ContextException; @@ -20,8 +21,6 @@ import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; import org.gcube.informationsystem.resourceregistry.utils.Utility; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; @@ -29,7 +28,6 @@ import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientVertex; -import com.tinkerpop.blueprints.impls.orient.OrientVertexType; /** * @author Luca Frosini (ISTI - CNR) @@ -37,9 +35,14 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertexType; public abstract class EntityManagement extends ERManagement { - private static Logger staticLogger = LoggerFactory - .getLogger(EntityManagement.class); - + /** + * Provide a cache edge-internal-id -> RelationManagement + * this avoid to recreate the relationManagement of already visited + * edges + */ + @SuppressWarnings("rawtypes") + protected Map relationManagements; + protected EntityManagement(AccessType accessType) { super(accessType); @@ -53,8 +56,20 @@ public abstract class EntityManagement extends .toUpperCase()); this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_OUT_PREFIX .toUpperCase()); + + this.relationManagements = new HashMap<>(); } + + @SuppressWarnings("rawtypes") + protected RelationManagement getRelationManagement(Edge edge) throws ResourceRegistryException { + String id = edge.getId().toString(); + RelationManagement relationManagement = relationManagements.get(id); + if(relationManagement==null) { + relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); + } + return relationManagement; + } protected EntityManagement(AccessType accessType, OrientGraph orientGraph) { this(accessType); @@ -167,8 +182,7 @@ public abstract class EntityManagement extends for (Edge edge : edges) { @SuppressWarnings("rawtypes") - RelationManagement relationManagement = RelationManagement - .getRelationManagement(orientGraph, edge); + RelationManagement relationManagement = getRelationManagement(edge); relationManagement.internalAddToContext(); } @@ -185,50 +199,14 @@ public abstract class EntityManagement extends for (Edge edge : edges) { @SuppressWarnings("rawtypes") - RelationManagement relationManagement = RelationManagement - .getRelationManagement(orientGraph, edge); + RelationManagement relationManagement = getRelationManagement(edge); relationManagement.internalRemoveFromContext(); } return true; } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static EntityManagement getEntityManagement(OrientGraph orientGraph, - Vertex vertex) throws ResourceRegistryException { - - if(orientGraph==null){ - throw new ResourceRegistryException(OrientGraph.class.getSimpleName() + "instance is null. This is really strage please contact the administrator."); - } - - if(vertex==null){ - throw new ResourceRegistryException(Vertex.class.getSimpleName() + "instance is null. This is really strage please contact the administrator."); - } - - OrientVertexType orientVertexType = null; - try { - orientVertexType = ((OrientVertex) vertex).getType(); - }catch (Exception e) { - String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString()); - staticLogger.error(error, e); - throw new ResourceRegistryException(error); - } - - EntityManagement entityManagement = null; - if (orientVertexType.isSubClassOf(Resource.NAME)) { - entityManagement = new ResourceManagement(orientGraph); - } else if (orientVertexType.isSubClassOf(Facet.NAME)) { - entityManagement = new FacetManagement(orientGraph); - } else { - String error = String.format("{%s is not a %s nor a %s. " - + "This is really strange and should not occur. " - + "Please Investigate it.", vertex, Resource.NAME, - Facet.NAME); - throw new ResourceRegistryException(error); - } - entityManagement.setElement(vertex); - return entityManagement; - } + @Override public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { @@ -236,7 +214,7 @@ public abstract class EntityManagement extends Iterable iterable = orientGraph.getVerticesOfClass(erType, polymorphic); for(Vertex vertex : iterable){ @SuppressWarnings("rawtypes") - EntityManagement entityManagement = getEntityManagement(orientGraph, vertex); + EntityManagement entityManagement = ERManagementUtility.getEntityManagement(orientGraph, vertex); try { JSONObject jsonObject = entityManagement.serializeAsJson(); jsonArray.put(jsonObject); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java index 49155b1..2b0ead8 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java @@ -20,6 +20,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFound import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; +import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; @@ -330,7 +331,7 @@ public class ResourceManagement extends EntityManagement { Vertex vertex = (Vertex) element; @SuppressWarnings("rawtypes") - EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, vertex); + EntityManagement entityManagement = ERManagementUtility.getEntityManagement(orientGraph, vertex); try { JSONObject jsonObject = entityManagement.serializeAsJson(); jsonArray.put(jsonObject); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/ConsistsOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/ConsistsOfManagement.java index 7304e64..bc5bfd5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/ConsistsOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/ConsistsOfManagement.java @@ -3,8 +3,6 @@ */ package org.gcube.informationsystem.resourceregistry.er.relation; -import java.util.UUID; - import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -48,17 +46,13 @@ public class ConsistsOfManagement extends RelationManagement extends - ERManagement { +public abstract class RelationManagement + extends ERManagement { protected final Class targetEntityClass; - - protected S sourceEntityManagement; - protected T targetEntityManagement; + + private S sourceEntityManagemen; + private T targetEntityManagemen; protected RelationManagement(AccessType accessType) { super(accessType); @@ -71,21 +71,21 @@ public abstract class RelationManagement fullSerialize( - Map visitedSourceResources) + protected Map fullSerialize(Map visitedSourceResources) throws ResourceRegistryException { Vertex source = element.getVertex(Direction.OUT); @@ -146,34 +163,30 @@ public abstract class RelationManagement %s ", - IsParentOf.NAME, Context.NAME, erType, - sourceEntityManagement.serialize(), - targetEntityManagement.serialize()); - throw new ResourceRegistryException(error); - } - } else { - if (!(sourceEntityManagement instanceof ResourceManagement)) { - String error = String.format( - "Any type of %s can have only a %s as %s. " - + "Cannot instatiate %s beetween %s -> %s ", - Relation.NAME, Resource.NAME, Relation.SOURCE_PROPERTY, - erType, sourceEntityManagement.serialize(), - targetEntityManagement.serialize()); - throw new ResourceRegistryException(error); - } - - if (this instanceof IsRelatedToManagement) { - if (!(targetEntityManagement instanceof ResourceManagement)) { - String error = String.format("A %s can have only a %s as %s. " - + "Cannot instatiate %s beetween %s -> %s ", accessType.getName(), - Resource.NAME, Relation.TARGET_PROPERTY, erType, - sourceEntityManagement.serialize(), - targetEntityManagement.serialize()); - throw new ResourceRegistryException(error); - } - } else if (this instanceof ConsistsOfManagement) { - if (!(targetEntityManagement instanceof FacetManagement)) { - String error = String.format("A %s can have only a %s as %s. " - + "Cannot instatiate %s beetween %s -> %s ", accessType.getName(), - Facet.NAME, Relation.TARGET_PROPERTY, erType, - sourceEntityManagement.serialize(), - targetEntityManagement.serialize()); - throw new ResourceRegistryException(error); - } - } 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); - } - - } - */ + * if(this instanceof IsParentOfManagement) { if (!(sourceEntityManagement + * instanceof ContextManagement && targetEntityManagement instanceof + * ContextManagement)) { String error = String.format( + * "A %s can be only created beetween two %s. " + + * "Cannot instatiate %s beetween %s -> %s ", IsParentOf.NAME, Context.NAME, + * erType, sourceEntityManagement.serialize(), + * targetEntityManagement.serialize()); throw new + * ResourceRegistryException(error); } } else { if (!(sourceEntityManagement + * instanceof ResourceManagement)) { String error = String.format( + * "Any type of %s can have only a %s as %s. " + + * "Cannot instatiate %s beetween %s -> %s ", Relation.NAME, Resource.NAME, + * Relation.SOURCE_PROPERTY, erType, sourceEntityManagement.serialize(), + * targetEntityManagement.serialize()); throw new + * ResourceRegistryException(error); } + * + * if (this instanceof IsRelatedToManagement) { if (!(targetEntityManagement + * instanceof ResourceManagement)) { String error = + * String.format("A %s can have only a %s as %s. " + + * "Cannot instatiate %s beetween %s -> %s ", accessType.getName(), + * Resource.NAME, Relation.TARGET_PROPERTY, erType, + * sourceEntityManagement.serialize(), targetEntityManagement.serialize()); + * throw new ResourceRegistryException(error); } } else if (this instanceof + * ConsistsOfManagement) { if (!(targetEntityManagement instanceof + * FacetManagement)) { String error = + * String.format("A %s can have only a %s as %s. " + + * "Cannot instatiate %s beetween %s -> %s ", accessType.getName(), Facet.NAME, + * Relation.TARGET_PROPERTY, erType, sourceEntityManagement.serialize(), + * targetEntityManagement.serialize()); throw new + * ResourceRegistryException(error); } } 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); } + * + * } + */ // end of if-else to be revisited - - logger.trace("Creating {} beetween {} -> {}", erType, - sourceEntityManagement.serialize(), - targetEntityManagement.serialize()); - - Vertex source = (Vertex) sourceEntityManagement.getElement(); - Vertex target = (Vertex) targetEntityManagement.getElement(); - + + logger.trace("Creating {} beetween {} -> {}", erType, getSourceEntityManagemen().serialize(), + getTargetEntityManagemen().serialize()); + + Vertex source = (Vertex) getSourceEntityManagemen().getElement(); + Vertex target = (Vertex) getTargetEntityManagemen().getElement(); + element = orientGraph.addEdge(null, source, target, erType); - ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, - ignoreStartWithKeys); + ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); - /* This code has been moved in ERManagement.internalCreate() - HeaderUtility.addHeader(element, null); - ContextUtility.addToActualContext(orientGraph, element); + /* + * This code has been moved in ERManagement.internalCreate() + * HeaderUtility.addHeader(element, null); + * ContextUtility.addToActualContext(orientGraph, element); + * + * ((OrientEdge) element).save(); + */ - ((OrientEdge) element).save(); - */ - logger.info("{} successfully created", erType); return element; } - protected abstract S getSourceEntityManagement(UUID sourceUUID) throws ResourceRegistryException; + protected abstract S newSourceEntityManagement() throws ResourceRegistryException; - protected abstract T getTargetEntityManagement(UUID targetUUID) throws ResourceRegistryException; + protected abstract T newTargetEntityManagement() throws ResourceRegistryException; - - public void setSourceEntityManagement(S sourceEntityManagement) { - this.sourceEntityManagement = sourceEntityManagement; - } - - public void setTargetEntityManagement(T targetEntityManagement) { - this.targetEntityManagement = targetEntityManagement; + /* + public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException { + this.sourceEntityManagemen = newSourceEntityManagement(); + this.sourceEntityManagemen.setUUID(sourceUUID); } - public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException { - this.sourceEntityManagement = getSourceEntityManagement(sourceUUID); - } - public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException { - this.targetEntityManagement = getTargetEntityManagement(targetUUID); + this.targetEntityManagemen = newTargetEntityManagement(); + this.targetEntityManagemen.setUUID(targetUUID); + } - + */ + @Override protected Edge reallyUpdate() throws ResourceRegistryException { logger.debug("Trying to update {} : {}", erType, jsonNode); Edge edge = getElement(); - ERManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys, - ignoreStartWithKeys); + ERManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys); - if (accessType.compareTo(AccessType.CONSISTS_OF)==0) { + if (accessType.compareTo(AccessType.CONSISTS_OF) == 0) { JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY); if (target != null) { FacetManagement fm = new FacetManagement(orientGraph); @@ -350,52 +343,44 @@ public abstract class RelationManagement iterable = target.getEdges(Direction.IN); Iterator iterator = iterable.iterator(); int count = 0; OrientEdge edge = null; while (iterator.hasNext()) { edge = (OrientEdge) iterator.next(); - OrientEdge thisOrientEdge = (OrientEdge) element; - if(edge.compareTo(thisOrientEdge)!=0){ - if(thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex())!=0){ + OrientEdge thisOrientEdge = (OrientEdge) getElement(); + if (edge.compareTo(thisOrientEdge) != 0) { + if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) { count++; break; } /* - else{ - ContextUtility.removeFromActualContext(orientGraph, edge); - } - */ + * else{ ContextUtility.removeFromActualContext(orientGraph, edge); } + */ } } - - if (count>0) { + + if (count > 0) { logger.trace( "{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from current context.", element, target, edge, removeConstraint); } else { - removeFromContextTargetVertex(target); + getTargetEntityManagemen().internalRemoveFromContext(); } break; @@ -530,43 +503,21 @@ public abstract class RelationManagement iterable = target.getEdges(Direction.IN); Iterator iterator = iterable.iterator(); if (iterator.hasNext()) { - logger.trace( - "{} point to {} which is not orphan. Giving {} directive, it will be keep.", - element, target, removeConstraint); + logger.trace("{} point to {} which is not orphan. Giving {} directive, it will be keep.", element, + target, removeConstraint); } else { - deleteTargetVertex(target); + getTargetEntityManagemen().internalDelete(); } break; @@ -660,15 +593,13 @@ public abstract class RelationManagement serializeEdges(Iterable edges, - boolean postFilterPolymorphic) throws ResourceRegistryException { + protected Collection serializeEdges(Iterable edges, boolean postFilterPolymorphic) + throws ResourceRegistryException { Map visitedSourceResources = new HashMap<>(); for (Edge edge : edges) { if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) { continue; } - RelationManagement relationManagement = getRelationManagement( - orientGraph, edge); - visitedSourceResources = relationManagement - .fullSerialize(visitedSourceResources); + RelationManagement relationManagement = getRelationManagement(orientGraph, edge); + visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources); } return visitedSourceResources.values(); } @@ -714,24 +643,22 @@ public abstract class RelationManagement edges = orientGraph.getEdgesOfClass(erType, polymorphic); Collection collection = serializeEdges(edges, false); return serializeJSONObjectList(collection); } - 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) ERManagementUtility.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(); @@ -744,11 +671,9 @@ public abstract class RelationManagement