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 9239058..735d42d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagementUtility.java @@ -32,35 +32,25 @@ 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) throws ResourceRegistryException { + * switch (querableType) { case FACET: return new FacetManagement(); + * + * case RESOURCE: return new ResourceManagement(); + * + * case IS_RELATED_TO: return new IsRelatedToManagement(); + * + * case CONSISTS_OF: return new ConsistsOfManagement(); + * + * default: throw new ResourceRegistryException(String.format( + * "%s is not querable", querableType.toString())); } } + */ + @SuppressWarnings("rawtypes") - public static ERManagement getERManagement(AccessType querableType) - throws ResourceRegistryException { - switch (querableType) { - case FACET: - return new FacetManagement(); - - case RESOURCE: - return new ResourceManagement(); - - case IS_RELATED_TO: - return new IsRelatedToManagement(); - - case CONSISTS_OF: - return new ConsistsOfManagement(); - - default: - throw new ResourceRegistryException(String.format( - "%s is not querable", querableType.toString())); - } - } - */ - - @SuppressWarnings("rawtypes") - public static ERManagement getERManagement(String type) - throws ResourceRegistryException { + public static ERManagement getERManagement(String type) throws ResourceRegistryException { OClass oClass = SchemaManagementImpl.getTypeSchema(type, null); ERManagement erManagement = null; @@ -76,8 +66,7 @@ public class ERManagementUtility { } if (erManagement == null) { - throw new ResourceRegistryException(String.format( - "%s is not querable", type.toString())); + throw new ResourceRegistryException(String.format("%s is not querable", type.toString())); } erManagement.setElementType(type); @@ -85,24 +74,21 @@ public class ERManagementUtility { } @SuppressWarnings("rawtypes") - private static ERManagement getERManagement(OrientGraph orientGraph, - Element element) throws ResourceRegistryException { + private static ERManagement getERManagement(OrientGraph orientGraph, Element element) + throws ResourceRegistryException { if (element instanceof Vertex) { - return getEntityManagement(orientGraph, - (Vertex) element); + return getEntityManagement(orientGraph, (Vertex) element); } else if (element instanceof Edge) { - return getRelationManagement(orientGraph, - (Edge) element); + return getRelationManagement(orientGraph, (Edge) element); } - throw new ResourceRegistryException(String.format( - "%s is not a %s nor a %s", element.getClass().getSimpleName(), + throw new ResourceRegistryException(String.format("%s is not a %s nor a %s", element.getClass().getSimpleName(), Entity.NAME, Relation.NAME)); } - + public static Element getAnyElementByUUID(UUID uuid) throws ERNotFoundException, ResourceRegistryException { - try{ + try { return Utility.getElementByUUIDAsAdmin(null, uuid, Vertex.class); - }catch (ERNotFoundException e) { + } catch (ERNotFoundException e) { return Utility.getElementByUUIDAsAdmin(null, uuid, Edge.class); } catch (ResourceRegistryException e) { throw e; @@ -110,12 +96,12 @@ public class ERManagementUtility { throw new ResourceRegistryException(e); } } - - - private static Element getAnyElementByUUID(OrientGraph orientGraph, UUID uuid) throws ERNotFoundException, ResourceRegistryException { - try{ + + private static Element getAnyElementByUUID(OrientGraph orientGraph, UUID uuid) + throws ERNotFoundException, ResourceRegistryException { + try { return Utility.getElementByUUID(orientGraph, null, uuid, Vertex.class); - }catch (ERNotFoundException e) { + } catch (ERNotFoundException e) { return Utility.getElementByUUID(orientGraph, null, uuid, Edge.class); } catch (ResourceRegistryException e) { throw e; @@ -125,57 +111,57 @@ public class ERManagementUtility { } @SuppressWarnings("rawtypes") - public static ERManagement getERManagementFromUUID(OrientGraph orientGraph, - UUID uuid) throws ResourceRegistryException { + public static ERManagement getERManagementFromUUID(OrientGraph orientGraph, UUID uuid) + throws ResourceRegistryException { Element element; try { element = getAnyElementByUUID(orientGraph, uuid); return getERManagement(orientGraph, element); } catch (Exception e) { - throw new ResourceRegistryException(String.format( - "%s does not belong to an %s nor to a %s", - uuid.toString(), Entity.NAME, Relation.NAME)); + throw new ResourceRegistryException(String.format("%s does not belong to an %s nor to a %s", + uuid.toString(), Entity.NAME, Relation.NAME)); } } - - + @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."); + 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."); + + 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()); + } 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); + 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 { @@ -204,5 +190,5 @@ public class ERManagementUtility { 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 6f24e3d..2748cd5 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 @@ -66,7 +66,7 @@ public abstract class EntityManagement extends String id = edge.getId().toString(); RelationManagement relationManagement = relationManagements.get(id); if(relationManagement==null) { - relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); + relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge); } return relationManagement; } 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 2b0ead8..15f73e0 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 @@ -88,7 +88,7 @@ public class ResourceManagement extends EntityManagement { Iterable edges = getElement().getEdges(Direction.OUT); for (Edge edge : edges) { @SuppressWarnings("rawtypes") - RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge); + RelationManagement relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge); relationManagement.setSourceEntityManagement(this); if (relationManagement instanceof ConsistsOfManagement) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index 748f5e7..e768140 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -21,8 +21,6 @@ import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveCo 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.ConsistsOf; -import org.gcube.informationsystem.model.relation.IsRelatedTo; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; @@ -44,7 +42,6 @@ import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph; import com.tinkerpop.blueprints.impls.orient.OrientEdge; -import com.tinkerpop.blueprints.impls.orient.OrientEdgeType; import com.tinkerpop.blueprints.impls.orient.OrientGraph; /** @@ -103,7 +100,7 @@ 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) getElement(); - if (edge.compareTo(thisOrientEdge) != 0) { - if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) { - count++; - break; - } - /* - * else{ ContextUtility.removeFromActualContext(orientGraph, edge); } - */ + case cascade: + getTargetEntityManagemen().internalRemoveFromContext(); + break; + + case cascadeWhenOrphan: + Vertex target = (Vertex) getTargetEntityManagemen().getElement(); + + Iterable 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) getElement(); + if (edge.compareTo(thisOrientEdge) != 0) { + if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) { + count++; + break; } + /* + * else{ ContextUtility.removeFromActualContext(orientGraph, edge); } + */ } - - 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 { - getTargetEntityManagemen().internalRemoveFromContext(); - } - break; - - case keep: - break; - - default: - break; + } + + 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 { + getTargetEntityManagemen().internalRemoveFromContext(); + } + break; + + case keep: + break; + + default: + break; } return true; } - - - @SuppressWarnings("unchecked") - 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; - } - @Override protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException { logger.debug("Going to remove {} with UUID {}. Related {}s will be detached.", accessType.getName(), uuid, @@ -567,26 +528,26 @@ 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); + } else { getTargetEntityManagemen().internalDelete(); - break; - - case cascadeWhenOrphan: - Iterable 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); - } else { - getTargetEntityManagemen().internalDelete(); - } - break; - - case keep: - break; - - default: - break; + } + break; + + case keep: + break; + + default: + break; } return true; @@ -631,7 +592,7 @@ public abstract class RelationManagement