From a78e2dc5055b28ea4129202276375d34aa154436 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Fri, 28 Apr 2017 15:43:30 +0000 Subject: [PATCH] Fixing specific exception management git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@147257 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../resourceregistry/er/ERManagement.java | 50 +++++++++++++------ .../er/entity/EntityManagement.java | 3 +- .../er/entity/FacetManagement.java | 2 +- .../er/entity/ResourceManagement.java | 6 +-- 4 files changed, 41 insertions(+), 20 deletions(-) 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 0cd299e..0d41206 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -31,12 +31,16 @@ 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; @@ -75,6 +79,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientElement; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.util.StringFactory; +import ch.qos.logback.core.Context; + /** * @author Luca Frosini (ISTI - CNR) */ @@ -341,36 +347,52 @@ public abstract class ERManagement { this.uuid = HeaderUtility.getHeader(element).getUUID(); } - protected ERNotFoundException getRightElementNotFoundException(ERNotFoundException e) { + protected ERNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { switch (accessType) { case RESOURCE: - return new ResourceNotFoundException(e); + return new ResourceNotFoundException(e.getMessage(), e.getCause()); case FACET: - return new FacetNotFoundException(e); + return new FacetNotFoundException(e.getMessage(), e.getCause()); case IS_RELATED_TO: - return new RelationNotFoundException(e); + return new RelationNotFoundException(e.getMessage(), e.getCause()); case CONSISTS_OF: - return new RelationNotFoundException(e); + return new RelationNotFoundException(e.getMessage(), e.getCause()); default: return e; } } - protected ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(){ + protected ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message){ switch (accessType) { case RESOURCE: - return new ResourceAvailableInAnotherContextException(""); + return new ResourceAvailableInAnotherContextException(message); case FACET: - return new FacetAvailableInAnotherContextException(""); + return new FacetAvailableInAnotherContextException(message); case IS_RELATED_TO: - return new RelationAvailableInAnotherContextException(""); + return new RelationAvailableInAnotherContextException(message); case CONSISTS_OF: - return new RelationAvailableInAnotherContextException(""); + return new RelationAvailableInAnotherContextException(message); default: - return new ERAvailableInAnotherContextException(""); + 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); + } + } + + public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { if (element == null) { try { @@ -378,7 +400,7 @@ public abstract class ERManagement { }catch (ERNotFoundException e) { try { retrieveElementFromAnyContext(); - throw getSpecificERAvailableInAnotherContextException(); + throw getSpecificERAvailableInAnotherContextException(erType == null ? accessType.getName() : erType + " with UUID " + uuid + " is available in another " + Context.class.getSimpleName()); } catch (ERAvailableInAnotherContextException e1) { throw e1; }catch (Exception e1) { @@ -399,7 +421,7 @@ public abstract class ERManagement { return Utility.getElementByUUID(orientGraph, erType == null ? accessType.getName() : erType, uuid, elementClass); } catch (ERNotFoundException e) { - throw getRightElementNotFoundException(e); + throw getSpecificElementNotFoundException(e); } catch (ResourceRegistryException e) { throw e; } catch (Exception e) { @@ -411,7 +433,7 @@ public abstract class ERManagement { try{ return Utility.getElementByUUIDAsAdmin(erType == null ? accessType.getName() : erType, uuid, elementClass); }catch (ERNotFoundException e) { - throw getRightElementNotFoundException(e); + throw getSpecificElementNotFoundException(e); } catch (ResourceRegistryException e) { throw e; } catch (Exception e) { 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 0f8ab80..05c35bc 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 @@ -14,7 +14,6 @@ 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.EntityAlreadyPresentException; -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.context.ContextUtility; @@ -97,7 +96,7 @@ public abstract class EntityManagement extends String error = String.format( "UUID %s is already used by another %s. This is not allowed.", uuid.toString(), (el instanceof Vertex) ? Entity.NAME : Relation.NAME); - throw new ERAlreadyPresentException(error); + throw getSpecificERAlreadyPresentException(error); }catch (ERNotFoundException e1) { // OK the UUID is not already used. 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 f3a3d46..e4d9d53 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 @@ -45,7 +45,7 @@ public class FacetManagement extends EntityManagement { } @Override - public Vertex reallyUpdate() throws ResourceRegistryException { + public Vertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException { Vertex facet = getElement(); facet = (Vertex) ERManagement.updateProperties(oClass, facet, jsonNode, ignoreKeys, ignoreStartWithKeys); ((OrientVertex) facet).save(); 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 272ef3e..751dcbd 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 @@ -12,7 +12,7 @@ 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.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException; +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.er.relation.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement; @@ -130,7 +130,7 @@ public class ResourceManagement extends EntityManagement { } @Override - public Vertex reallyCreate() throws EntityAlreadyPresentException, + public Vertex reallyCreate() throws ResourceAlreadyPresentException, ResourceRegistryException { createVertex(); @@ -160,7 +160,7 @@ public class ResourceManagement extends EntityManagement { } @Override - public Vertex reallyUpdate() throws ResourceRegistryException { + public Vertex reallyUpdate() throws ResourceNotFoundException, ResourceRegistryException { getElement();