Checking if an entity with the same UUID already exist. Throwing an exception in such a case

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@135011 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-28 17:17:14 +00:00
parent a576d3d8fa
commit ee136fb6a6
7 changed files with 89 additions and 20 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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<? extends Entity> 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<? extends Entity> 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();
}
}

View File

@ -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{

View File

@ -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<EntityManagement> {
@ -129,6 +129,22 @@ public class EntityManagementFactory implements Factory<EntityManagement> {
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;
}
};
}

View File

@ -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;

View File

@ -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;