diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/Access.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/Access.java index bd0ff36..e5cd142 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/Access.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/Access.java @@ -14,8 +14,8 @@ import org.gcube.informationsystem.resourceregistry.api.Query; import org.gcube.informationsystem.resourceregistry.api.SchemaManagement; import org.gcube.informationsystem.resourceregistry.api.exceptions.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.resources.impl.EntityManagementImpl; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java index e5fbab0..5ca23db 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java @@ -19,8 +19,8 @@ import org.gcube.informationsystem.resourceregistry.api.EntityManagement; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.rest.EntityPath; import org.gcube.informationsystem.resourceregistry.resources.impl.EntityManagementImpl; import org.gcube.informationsystem.resourceregistry.resources.utils.ContextUtility; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java index b5b96d9..f961a52 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java @@ -30,8 +30,12 @@ import org.gcube.informationsystem.resourceregistry.api.EntityManagement; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.FacetNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; +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.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; @@ -145,23 +149,30 @@ public class EntityManagementImpl implements EntityManagement { protected Vertex getEntity(OrientGraph orientGraph, JsonNode jsonNode, UUID uuid, Class entityClass) throws JsonParseException, JsonMappingException, IOException, - FacetNotFoundException, ResourceNotFoundException, - ResourceRegistryException { + EntityNotFoundException, ResourceRegistryException { String classProperty = getClassProperty(jsonNode); try { SchemaManagementImpl.getTypeSchema(orientGraph, classProperty, - classProperty); + entityClass.getSimpleName()); } catch (SchemaNotFoundException e) { throw e; } Header header = HeaderUtility.getHeader(jsonNode, false); - UUID resourceUUID = header.getUUID(); + if (header != null) { + UUID resourceUUID = header.getUUID(); + if (resourceUUID.compareTo(uuid) != 0) { + String error = String + .format("UUID provided in header (%s) differs from the one (%s) used to identify the %s instance", + resourceUUID.toString(), uuid.toString(), + classProperty); + throw new ResourceRegistryException(error); - Vertex vertex = getEntity(orientGraph, resourceUUID, classProperty, - entityClass); + } + } + Vertex vertex = getEntity(orientGraph, uuid, classProperty, entityClass); return vertex; } @@ -536,7 +547,8 @@ public class EntityManagementImpl implements EntityManagement { private Vertex createVertexEntity(OrientGraph orientGraph, String entityType, Class entity, - String jsonRepresentation) throws ResourceRegistryException { + String jsonRepresentation) throws EntityAlreadyPresentException, + ResourceRegistryException { logger.trace("Going to create {} for {} ({}) using {}", Vertex.class.getSimpleName(), @@ -566,6 +578,29 @@ public class EntityManagementImpl implements EntityManagement { OrientVertex vertex = orientGraph.addVertex("class:" + entityType); + try { + UUID uuid = org.gcube.informationsystem.impl.utils.Utility + .getUUIDFromJsonNode(jsonNode); + + Vertex v = getEntity(orientGraph, jsonNode, uuid, entity); + if(v!=null){ + String error = String.format("An %s with UUID %s already exist", entity.getSimpleName(), uuid.toString()); + if (Facet.class.isAssignableFrom(entity)) { + throw new FacetAlreadyPresentException(error); + } + if (Resource.class.isAssignableFrom(entity)) { + throw new ResourceAlreadyPresentException(error); + } + + throw new EntityAlreadyPresentException(error); + } + + } catch (EntityAlreadyPresentException e) { + throw e; + } catch (Exception e) { + // no header or no header with uuid is provided and it is fine + } + Header entityHeader = HeaderUtility.getHeader(jsonNode, true); if (entityHeader != null) { vertex.setProperty(Entity.HEADER_PROPERTY, entityHeader); @@ -600,7 +635,8 @@ public class EntityManagementImpl implements EntityManagement { Utility.toJsonString((OrientVertex) vertex, true)); return vertex; - + } catch (ResourceRegistryException e) { + throw e; } catch (Exception e) { logger.trace("Error while creating {} for {} ({}) using {}", Vertex.class.getSimpleName(), entity.getClass() @@ -787,7 +823,7 @@ public class EntityManagementImpl implements EntityManagement { @Override public String createFacet(String facetType, String jsonRepresentation) - throws ResourceRegistryException { + throws FacetAlreadyPresentException, ResourceRegistryException { OrientGraph orientGraph = null; try { @@ -1161,7 +1197,7 @@ public class EntityManagementImpl implements EntityManagement { @Override public String createResource(String resourceType, String jsonRepresentation) - throws ResourceRegistryException { + throws ResourceAlreadyPresentException, ResourceRegistryException { logger.debug("Trying to create {} using {}", resourceType, jsonRepresentation); @@ -1415,15 +1451,30 @@ public class EntityManagementImpl implements EntityManagement { } } + @Override public boolean addResourceToContext(UUID uuid) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { return addEntityToContext(Resource.class, uuid); } + @Override public boolean addFacetToContext(UUID uuid) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { return addEntityToContext(Facet.class, uuid); } + @Override + public boolean removeResourceFromContext(UUID uuid) + throws ResourceNotFoundException, ContextNotFoundException, + ResourceRegistryException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeFacetFromContext(UUID uuid) + throws FacetNotFoundException, ContextNotFoundException, + ResourceRegistryException { + throw new UnsupportedOperationException(); + } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java index fc07703..3aee908 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java @@ -78,6 +78,8 @@ public class HeaderUtility { } HeaderOrient header = null; if(creation){ + // If an header is provided MUST contains and UUID otherwise is + // an invalid request so that let that an exception is raised UUID uuid = UUID.fromString(headerNode.get(Header.UUID_PROPERTY).asText()); header = (HeaderOrient) createHeader(uuid); }else{ diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java b/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java index ef443c6..bf920a9 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java @@ -5,8 +5,8 @@ import java.util.UUID; import org.gcube.informationsystem.resourceregistry.api.EntityManagement; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.glassfish.hk2.api.Factory; public class EntityManagementFactory implements Factory { @@ -129,6 +129,22 @@ public class EntityManagementFactory implements Factory { return null; } + @Override + public boolean removeResourceFromContext(UUID uuid) + throws ResourceNotFoundException, ContextNotFoundException, + ResourceRegistryException { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean removeFacetFromContext(UUID uuid) + throws FacetNotFoundException, ContextNotFoundException, + ResourceRegistryException { + // TODO Auto-generated method stub + return false; + } + }; } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java index c4e6d0b..0d6f7c3 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java @@ -43,7 +43,7 @@ import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.model.relation.IsIdentifiedBy; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.isrelatedto.Hosts; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/MultiContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/MultiContextTest.java index 2258dff..1ee6327 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/MultiContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/MultiContextTest.java @@ -42,7 +42,7 @@ import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory; import org.gcube.informationsystem.model.relation.isrelatedto.Hosts; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger;