From b9a5ebe072edecdab1963526df80b5e32ec9c2a7 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Fri, 17 Nov 2017 10:42:37 +0000 Subject: [PATCH] Refs #10238: Refactor Context Port Type Task-Url: https://support.d4science.org/issues/10238 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158595 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../context/ContextManagement.java | 68 ++++- .../context/IsParentOfManagement.java | 52 +++- .../resourceregistry/er/ERManagement.java | 75 ++--- .../er/entity/FacetManagement.java | 19 +- .../er/entity/ResourceManagement.java | 35 ++- .../er/relation/ConsistsOfManagement.java | 24 +- .../er/relation/IsRelatedToManagement.java | 25 +- .../er/relation/RelationManagement.java | 262 +++++++++++------- .../context/ContextManagementTest.java | 15 + .../resourceregistry/er/ERManagementTest.java | 46 ++- 10 files changed, 420 insertions(+), 201 deletions(-) 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 9ec61a7..3d0261b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java @@ -1,13 +1,20 @@ package org.gcube.informationsystem.resourceregistry.context; +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.entity.Context; +import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; +import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,14 +29,36 @@ public class ContextManagement extends EntityManagement { private static Logger logger = LoggerFactory.getLogger(ContextManagement.class); + private void init() { + forceAdmin = true; + + this.ignoreStartWithKeys.add(Context.PARENT_PROPERTY); + this.ignoreStartWithKeys.add(Context.CHILDREN_PROPERTY); + } + public ContextManagement() { super(AccessType.CONTEXT); - forceAdmin = true; + init(); } public ContextManagement(OrientGraph orientGraph) { super(AccessType.CONTEXT, orientGraph); - forceAdmin = true; + init(); + } + + @Override + protected ContextNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { + return new ContextNotFoundException(e.getMessage(), e.getCause()); + } + + @Override + protected EntityAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) { + return new EntityAvailableInAnotherContextException(message); + } + + @Override + protected ContextAlreadyPresentException getSpecificERAlreadyPresentException(String message) { + return new ContextAlreadyPresentException(message); } @Override @@ -86,13 +115,17 @@ public class ContextManagement extends EntityManagement { String propertyName = Context.PARENT_PROPERTY; if (jsonNode.has(propertyName)) { - JsonNode parentJsonNode = jsonNode.get(propertyName); + JsonNode isParentOfJsonNode = jsonNode.get(propertyName); + + JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY); ContextManagement contextManagement = new ContextManagement(orientGraph); contextManagement.setJSON(parentJsonNode); - Vertex parent = contextManagement.getElement(); IsParentOfManagement isParentOfManagement = new IsParentOfManagement(orientGraph); - isParentOfManagement.reallyCreate(parent, this.element); + isParentOfManagement.setJSON(isParentOfJsonNode); + isParentOfManagement.setSourceEntityManagement(contextManagement); + isParentOfManagement.setTargetEntityManagement(this); + isParentOfManagement.internalCreate(); } @@ -101,17 +134,28 @@ public class ContextManagement extends EntityManagement { @Override protected Vertex reallyUpdate() throws ERNotFoundException, ResourceRegistryException { - // TODO Auto-generated method stub - return null; + + Vertex context = getElement(); + + // TODO check if parent changed + + context = (Vertex) ERManagement.updateProperties(oClass, context, jsonNode, ignoreKeys, ignoreStartWithKeys); + + return context; } @Override protected boolean reallyDelete() throws ERNotFoundException, ResourceRegistryException { - // TODO Auto-generated method stub - return false; + Iterable iterable = getElement().getEdges(Direction.OUT); + Iterator iterator = iterable.iterator(); + while (iterator.hasNext()) { + throw new ContextException("Cannot remove a " + Context.NAME + " having children"); + } + + element.remove(); + + return true; + } - - - } 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 a461d7c..4a404e2 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java @@ -8,10 +8,14 @@ import org.gcube.informationsystem.model.AccessType; 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.ContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfNotFoundException; import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; import com.tinkerpop.blueprints.Direction; -import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; @@ -19,8 +23,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph; * @author Luca Frosini (ISTI - CNR) */ @SuppressWarnings("rawtypes") -public class IsParentOfManagement extends RelationManagement { - +public class IsParentOfManagement extends RelationManagement { + public IsParentOfManagement() { super(AccessType.IS_PARENT_OF); } @@ -28,12 +32,28 @@ public class IsParentOfManagement extends RelationManagement { public IsParentOfManagement(OrientGraph orientGraph) { super(AccessType.IS_PARENT_OF, orientGraph); } - + + @Override + protected IsParentOfNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { + return new IsParentOfNotFoundException(e.getMessage(), e.getCause()); + } + + @Override + protected RelationAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException( + String message) { + return new RelationAvailableInAnotherContextException(message); + } + + @Override + protected IsParentOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) { + return new IsParentOfAlreadyPresentException(message); + } + @Override public JSONObject serializeAsJson() throws ResourceRegistryException { return serializeAsJson(false, true); } - + public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException { JSONObject relation = serializeSelfOnly(); @@ -41,17 +61,17 @@ public class IsParentOfManagement extends RelationManagement { Vertex source = element.getVertex(Direction.OUT); ContextManagement sourceContextManagement = new ContextManagement(orientGraph); sourceContextManagement.setElement(source); - if(includeSource) { + if (includeSource) { relation.put(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly()); } - + Vertex target = element.getVertex(Direction.IN); ContextManagement targetContextManagement = new ContextManagement(orientGraph); targetContextManagement.setElement(target); - if(includeTarget) { + if (includeTarget) { relation.put(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly()); } - + } catch (ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e); throw e; @@ -62,9 +82,15 @@ public class IsParentOfManagement extends RelationManagement { return relation; } - - public Edge reallyCreate(Vertex source, Vertex target) throws ResourceRegistryException { - return super.reallyCreate(source, target); + + @Override + public boolean addToContext() throws ERNotFoundException, ContextException { + throw new UnsupportedOperationException(); } - + + @Override + public boolean removeFromContext() throws ERNotFoundException, ContextException { + throw new UnsupportedOperationException(); + } + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java index 995e697..4ef5e40 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -32,20 +32,13 @@ 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; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAlreadyPresentException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; +import org.gcube.informationsystem.resourceregistry.context.ContextManagement; import org.gcube.informationsystem.resourceregistry.context.ContextUtility; +import org.gcube.informationsystem.resourceregistry.context.IsParentOfManagement; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseIntializator; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; @@ -96,7 +89,7 @@ public abstract class ERManagement { protected final Set ignoreStartWithKeys; protected Class elementClass; - protected AccessType accessType; + protected final AccessType accessType; protected OrientGraph orientGraph; @@ -108,10 +101,14 @@ public abstract class ERManagement { protected El element; /** - * Yhis boolean is used to force the use of ADMIN user instead of the user of the context + * This boolean is used to force the use of ADMIN user instead of the user of the context */ protected boolean forceAdmin; + public AccessType getAccessType() { + return accessType; + } + public boolean isForceAdmin() { return forceAdmin; } @@ -346,15 +343,19 @@ public abstract class ERManagement { public El internalCreate() throws ERAlreadyPresentException, ResourceRegistryException { try { reallyCreate(); + Header entityHeader = HeaderUtility.getHeader(jsonNode, true); if (entityHeader != null) { element.setProperty(Entity.HEADER_PROPERTY, entityHeader); } else { entityHeader = HeaderUtility.addHeader(element, null); } - ContextUtility.addToActualContext(orientGraph, element); - - ((OrientVertex) element).save(); + + if(!(this instanceof ContextManagement || this instanceof IsParentOfManagement)){ + ContextUtility.addToActualContext(orientGraph, element); + } + + ((OrientElement) element).save(); return element; }catch (ResourceRegistryException e) { @@ -438,51 +439,11 @@ public abstract class ERManagement { this.uuid = HeaderUtility.getHeader(element).getUUID(); } - protected ERNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { - switch (accessType) { - case RESOURCE: - return new ResourceNotFoundException(e.getMessage(), e.getCause()); - case FACET: - return new FacetNotFoundException(e.getMessage(), e.getCause()); - case IS_RELATED_TO: - return new RelationNotFoundException(e.getMessage(), e.getCause()); - case CONSISTS_OF: - return new RelationNotFoundException(e.getMessage(), e.getCause()); - default: - return e; - } - } + protected abstract ERNotFoundException getSpecificElementNotFoundException(ERNotFoundException e); - protected ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message){ - switch (accessType) { - case RESOURCE: - return new ResourceAvailableInAnotherContextException(message); - case FACET: - return new FacetAvailableInAnotherContextException(message); - case IS_RELATED_TO: - return new RelationAvailableInAnotherContextException(message); - case CONSISTS_OF: - return new RelationAvailableInAnotherContextException(message); - default: - return new ERAvailableInAnotherContextException(message); - } - } - - protected ERAlreadyPresentException getSpecificERAlreadyPresentException(String message){ - switch (accessType) { - case RESOURCE: - return new ResourceAlreadyPresentException(message); - case FACET: - return new FacetAlreadyPresentException(message); - case IS_RELATED_TO: - return new RelationAlreadyPresentException(message); - case CONSISTS_OF: - return new RelationAlreadyPresentException(message); - default: - return new ERAlreadyPresentException(message); - } - } + protected abstract ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message); + protected abstract ERAlreadyPresentException getSpecificERAlreadyPresentException(String message); public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { if (element == null) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java index 92f3571..6795b5f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/FacetManagement.java @@ -8,7 +8,9 @@ import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.er.ERManagement; import com.tinkerpop.blueprints.Vertex; @@ -27,7 +29,22 @@ public class FacetManagement extends EntityManagement { public FacetManagement(OrientGraph orientGraph) { super(AccessType.FACET, orientGraph); } - + + @Override + protected FacetNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { + return new FacetNotFoundException(e.getMessage(), e.getCause()); + } + + @Override + protected FacetAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) { + return new FacetAvailableInAnotherContextException(message); + } + + @Override + protected FacetAlreadyPresentException getSpecificERAlreadyPresentException(String message) { + return new FacetAlreadyPresentException(message); + } + @Override public String serialize() throws ResourceRegistryException { return serializeSelfOnly().toString(); 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 23de068..49155b1 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 @@ -14,7 +14,9 @@ import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.model.relation.IsRelatedTo; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; @@ -48,12 +50,28 @@ public class ResourceManagement extends EntityManagement { public ResourceManagement(OrientGraph orientGraph) { super(AccessType.RESOURCE, orientGraph); } - + + @Override + protected ResourceNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { + return new ResourceNotFoundException(e.getMessage(), e.getCause()); + } + + @Override + protected ResourceAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) { + return new ResourceAvailableInAnotherContextException(message); + } + + @Override + protected ResourceAlreadyPresentException getSpecificERAlreadyPresentException(String message) { + return new ResourceAlreadyPresentException(message); + } + @Override public String serialize() throws ResourceRegistryException { return serializeAsJson().toString(); } + @SuppressWarnings("unchecked") @Override public JSONObject serializeAsJson() throws ResourceRegistryException { @@ -69,12 +87,12 @@ 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 = RelationManagement.getRelationManagement(orientGraph, edge); + relationManagement.setSourceEntityManagement(this); + if (relationManagement instanceof ConsistsOfManagement) { try { - JSONObject consistsOf = relationManagement - .serializeAsJson(); + JSONObject consistsOf = relationManagement.serializeAsJson(true, true); sourceResource = addConsistsOf(sourceResource, consistsOf); }catch (ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", edge); @@ -122,7 +140,8 @@ public class ResourceManagement extends EntityManagement { for (JsonNode consistOfJsonNode : jsonNodeArray) { ConsistsOfManagement com = new ConsistsOfManagement(orientGraph); com.setJSON(consistOfJsonNode); - com.reallyCreate(element); + com.setSourceEntityManagement(this); + com.internalCreate(); } } @@ -133,7 +152,8 @@ public class ResourceManagement extends EntityManagement { IsRelatedToManagement irtm = new IsRelatedToManagement( orientGraph); irtm.setJSON(relationJsonNode); - irtm.reallyCreate(element); + irtm.setSourceEntityManagement(this); + irtm.internalCreate(); } } @@ -180,6 +200,7 @@ public class ResourceManagement extends EntityManagement { Iterable iterable = element.getEdges(Direction.OUT); Iterator iterator = iterable.iterator(); while (iterator.hasNext()) { + Edge edge = iterator.next(); OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType(); @SuppressWarnings("rawtypes") 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 de3f811..29e34a8 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 @@ -5,6 +5,12 @@ package org.gcube.informationsystem.resourceregistry.er.relation; import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.relation.ConsistsOf; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfNotFoundException; +import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement; +import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement; import com.tinkerpop.blueprints.impls.orient.OrientGraph; @@ -12,7 +18,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph; * @author Luca Frosini (ISTI - CNR) */ @SuppressWarnings("rawtypes") -public class ConsistsOfManagement extends RelationManagement { +public class ConsistsOfManagement extends RelationManagement { public ConsistsOfManagement() { super(AccessType.CONSISTS_OF); @@ -21,5 +27,21 @@ public class ConsistsOfManagement extends RelationManagement { public ConsistsOfManagement(OrientGraph orientGraph) { super(AccessType.CONSISTS_OF, orientGraph); } + + @Override + protected ConsistsOfNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { + return new ConsistsOfNotFoundException(e.getMessage(), e.getCause()); + } + + @Override + protected ConsistsOfAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException( + String message) { + return new ConsistsOfAvailableInAnotherContextException(message); + } + @Override + protected ConsistsOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) { + return new ConsistsOfAlreadyPresentException(message); + } + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/IsRelatedToManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/IsRelatedToManagement.java index b6c0c2a..b8ce6a3 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/IsRelatedToManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/IsRelatedToManagement.java @@ -5,6 +5,11 @@ package org.gcube.informationsystem.resourceregistry.er.relation; import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.relation.IsRelatedTo; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToNotFoundException; +import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement; import com.tinkerpop.blueprints.impls.orient.OrientGraph; @@ -12,14 +17,30 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph; * @author Luca Frosini (ISTI - CNR) */ @SuppressWarnings("rawtypes") -public class IsRelatedToManagement extends RelationManagement { +public class IsRelatedToManagement extends RelationManagement { public IsRelatedToManagement() { super(AccessType.IS_RELATED_TO); } - + public IsRelatedToManagement(OrientGraph orientGraph) { super(AccessType.IS_RELATED_TO, orientGraph); } + @Override + protected IsRelatedToNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { + return new IsRelatedToNotFoundException(e.getMessage(), e.getCause()); + } + + @Override + protected IsRelatedToAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException( + String message) { + return new IsRelatedToAvailableInAnotherContextException(message); + } + + @Override + protected IsRelatedToAlreadyPresentException getSpecificERAlreadyPresentException(String message) { + return new IsRelatedToAlreadyPresentException(message); + } + } 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 6aa312b..5c12234 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 @@ -3,6 +3,7 @@ */ package org.gcube.informationsystem.resourceregistry.er.relation; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -25,7 +26,9 @@ 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; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.er.ERManagement; @@ -45,13 +48,15 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph; /** * @author Luca Frosini (ISTI - CNR) - * */ @SuppressWarnings("rawtypes") -public abstract class RelationManagement extends +public abstract class RelationManagement extends ERManagement { protected final Class targetEntityClass; + + protected S sourceEntityManagement; + protected T targetEntityManagement; protected RelationManagement(AccessType accessType) { super(accessType); @@ -77,6 +82,9 @@ public abstract class RelationManagement extends this.targetEntityClass = Resource.class; break; } + + sourceEntityManagement = null; + targetEntityManagement = null; } @@ -84,7 +92,7 @@ public abstract class RelationManagement extends this(accessType); this.orientGraph = orientGraph; } - + @Override public String serialize() throws ResourceRegistryException { return serializeAsJson().toString(); @@ -92,19 +100,30 @@ public abstract class RelationManagement extends @Override public JSONObject serializeAsJson() throws ResourceRegistryException { + return serializeAsJson(true, true); + } + + @SuppressWarnings("unchecked") + public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException { JSONObject relation = serializeSelfOnly(); try { - Vertex source = element.getVertex(Direction.OUT); - EntityManagement sourceEntityManagement = EntityManagement - .getEntityManagement(orientGraph, source); - relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly()); + if(includeSource) { + if(sourceEntityManagement == null) { + Vertex source = element.getVertex(Direction.OUT); + sourceEntityManagement = (S) EntityManagement.getEntityManagement(orientGraph, source); + } + relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly()); + } - Vertex target = element.getVertex(Direction.IN); - EntityManagement targetEntityManagement = EntityManagement - .getEntityManagement(orientGraph, target); - relation.put(Relation.TARGET_PROPERTY, - targetEntityManagement.serializeAsJson()); + if(includeTarget) { + if(targetEntityManagement == null) { + Vertex target = element.getVertex(Direction.IN); + targetEntityManagement = (T) EntityManagement + .getEntityManagement(orientGraph, target); + } + relation.put(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson()); + } } catch (ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e); @@ -161,69 +180,112 @@ public abstract class RelationManagement extends return visitedSourceResources; } - - private Edge reallyCreate(UUID sourceUUID, UUID targetUUID) - throws ResourceRegistryException { - ResourceManagement srmSource = new ResourceManagement(orientGraph); - srmSource.setUUID(sourceUUID); - Vertex source = srmSource.getElement(); - - EntityManagement entityManagement = getEntityManagement(); - entityManagement.setUUID(targetUUID); - Vertex target = (Vertex) entityManagement.getElement(); - - return reallyCreate(source, target); - - } - protected Edge reallyCreate(Vertex source, Vertex target) - throws ResourceRegistryException { + @Override + protected Edge reallyCreate() throws ResourceRegistryException { - EntityManagement sourceEntityManagement = EntityManagement - .getEntityManagement(orientGraph, source); - EntityManagement targetEntityManagement = EntityManagement - .getEntityManagement(orientGraph, target); - 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); + if(sourceEntityManagement==null) { + + if(!jsonNode.has(Relation.SOURCE_PROPERTY)){ + throw new ResourceRegistryException( + "Error while creating relation. No source definition found"); } - } 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, + + UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility + .getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY)); + + setSourceUUID(sourceUUID); + } + + if(targetEntityManagement == null) { + targetEntityManagement = getTargetEntityManagement(); + + if (!jsonNode.has(Relation.TARGET_PROPERTY)) { + throw new ResourceRegistryException( + "Error while creating " + erType + ". No target definition found"); + } + + try { + targetEntityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY)); + }catch (SchemaException e) { + StringWriter errorMessage = new StringWriter(); + errorMessage.append("A "); + errorMessage.append(erType); + errorMessage.append(" can be only created beetween "); + errorMessage.append(sourceEntityManagement.getAccessType().getName()); + errorMessage.append(" and "); + errorMessage.append(targetEntityManagement.getAccessType().getName()); + throw new ResourceRegistryException(errorMessage.toString(), e); + } + + try { + targetEntityManagement.getElement(); + } catch (Exception e) { + targetEntityManagement.internalCreate(); + } + } + + // Revisit this if-else because should be not needed anymore. + /* + 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 { - 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 (!(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(); + element = orientGraph.addEdge(null, source, target, erType); ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, @@ -241,43 +303,24 @@ public abstract class RelationManagement extends return element; } - public Edge reallyCreate(Vertex source) throws ResourceRegistryException { - Vertex target = null; - EntityManagement entityManagement = getEntityManagement(); - - if (!jsonNode.has(Relation.TARGET_PROPERTY)) { - throw new ResourceRegistryException( - "Error while creating relation. No target definition found"); - } - entityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY)); - try { - target = (Vertex) entityManagement.getElement(); - } catch (Exception e) { - target = (Vertex) entityManagement.internalCreate(); - } - return reallyCreate(source, target); - } - - public Edge reallyCreate(UUID sourceUUID) throws ResourceRegistryException { - ResourceManagement srmSource = new ResourceManagement(orientGraph); - srmSource.setUUID(sourceUUID); - Vertex source = srmSource.getElement(); - return reallyCreate(source); - } - - @Override - protected Edge reallyCreate() throws ResourceRegistryException { - if(!jsonNode.has(Relation.SOURCE_PROPERTY)){ - throw new ResourceRegistryException( - "Error while creating relation. No source definition found"); - } - - UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility - .getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY)); - - return reallyCreate(sourceUUID); + + public void setSourceEntityManagement(S sourceEntityManagement) { + this.sourceEntityManagement = sourceEntityManagement; } + public void setTargetEntityManagement(T targetEntityManagement) { + this.targetEntityManagement = targetEntityManagement; + } + + @SuppressWarnings("unchecked") + public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException { + this.sourceEntityManagement = (S) ERManagement.getERManagementFromUUID(orientGraph, sourceUUID); + } + + @SuppressWarnings("unchecked") + public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException { + this.targetEntityManagement = (T) ERManagement.getERManagementFromUUID(orientGraph, targetUUID); + } @Override protected Edge reallyUpdate() throws ResourceRegistryException { @@ -483,17 +526,20 @@ public abstract class RelationManagement extends return true; } - protected EntityManagement getEntityManagement() + + @SuppressWarnings("unchecked") + protected T getTargetEntityManagement() throws ResourceRegistryException { - EntityManagement entityManagement; + T entityManagement; switch (accessType) { case CONSISTS_OF: - entityManagement = new FacetManagement(orientGraph); + entityManagement = (T) new FacetManagement(orientGraph); break; case IS_RELATED_TO: - entityManagement = new ResourceManagement(orientGraph); + entityManagement = (T) new ResourceManagement(orientGraph); break; + default: String error = String.format("{%s is not a %s nor a %s. " + "This is really strange ad should not occur. " @@ -537,8 +583,7 @@ public abstract class RelationManagement extends protected boolean deleteTargetVertex(Vertex target) throws ResourceRegistryException { - EntityManagement entityManagement = EntityManagement - .getEntityManagement(orientGraph, target); + EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, target); if (entityManagement != null) { return entityManagement.internalDelete(); } else { @@ -617,8 +662,11 @@ public abstract class RelationManagement extends try { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin); - - element = reallyCreate(sourceUUID, targetUUID); + + setSourceUUID(sourceUUID); + setTargetUUID(targetUUID); + + element = reallyCreate(); orientGraph.commit(); @@ -713,7 +761,7 @@ public abstract class RelationManagement extends } @Override - public boolean addToContext() throws ContextException { + public boolean addToContext() throws ERNotFoundException, ContextException { logger.debug("Going to add {} with UUID {} to actual Context", accessType.getName(), uuid); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java index 5f232c6..a7eaac3 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java @@ -53,6 +53,21 @@ public class ContextManagementTest { } + @Test + public void test() throws Exception { + UUID uuid = UUID.fromString("34395f1c-ecb3-4d21-8abe-986ba1650919"); + + Context myTest = new ContextImpl("myTest"); + myTest.setParent(uuid); + + String contextJsonString = ISMapper.marshal(myTest); + logger.debug("myTest : {}", contextJsonString); + + ContextManagement contextManagement = new ContextManagement(); + contextManagement.setJSON(contextJsonString); + contextManagement.create(); + } + @Test public void testJava() throws Exception { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java index 3fbbe59..bdbf8f0 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/er/ERManagementTest.java @@ -16,6 +16,7 @@ import java.util.Map; import java.util.UUID; import org.gcube.common.authorization.library.provider.AuthorizationProvider; +import org.gcube.informationsystem.impl.embedded.HeaderImpl; import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl; import org.gcube.informationsystem.impl.entity.facet.AccessPointFacetImpl; import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl; @@ -164,7 +165,7 @@ public class ERManagementTest extends ScopedTest { } @Test(expected=ResourceRegistryException.class) - public void testCreateAbstarctEntity() throws Exception { + public void testCreateAbstractEntity() throws Exception { StateFacet stateFacet = new StateFacetImpl(); stateFacet.setValue("READY"); @@ -383,6 +384,49 @@ public class ERManagementTest extends ScopedTest { Assert.assertTrue(deleted); } + + @Test + public void testCreateConsistsOfBeetweenResources() throws Exception { + Map map = createHostingNodeAndEService(); + + UUID hostingNodeUUID = map.get(HostingNode.NAME).getHeader().getUUID(); + UUID eServiceUUID = map.get(EService.NAME).getHeader().getUUID(); + + HostingNode hostingNode = new HostingNodeImpl(); + hostingNode.setHeader(new HeaderImpl(hostingNodeUUID)); + + SimpleFacet fakeEServiceAsSimpleFacet = new SimpleFacetImpl(); + fakeEServiceAsSimpleFacet.setHeader(new HeaderImpl(eServiceUUID)); + + ConsistsOf consistsOf = new ConsistsOfImpl(hostingNode, fakeEServiceAsSimpleFacet, null); + + try { + ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement(); + String json = ISMapper.marshal(consistsOf); + json = json.replaceAll(SimpleFacet.NAME, EService.NAME); + + consistsOfManagement.setJSON(json); + + consistsOfManagement.create(); + logger.debug("The creation terminated correctly. This should not happen"); + + + } catch (ResourceRegistryException e) { + logger.error("Sounds good. A {} cannot be created between two resources", ConsistsOf.NAME, e); + } finally { + ResourceManagement resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(eServiceUUID); + boolean deleted = resourceManagement.delete(); + Assert.assertTrue(deleted); + + resourceManagement = new ResourceManagement(); + resourceManagement.setUUID(hostingNodeUUID); + deleted = resourceManagement.delete(); + Assert.assertTrue(deleted); + } + + } + @Test public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception { Map map = createHostingNodeAndEService();