Fixing entity creation throwing the proper exception

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@146551 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-04-04 13:03:25 +00:00
parent a95d369ba2
commit 8a985b16f3
2 changed files with 119 additions and 91 deletions

View File

@ -164,29 +164,32 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
Entity.NAME, Relation.NAME));
}
public static Element getAnyElementByUUID(UUID uuid) throws ERNotFoundException, ResourceRegistryException {
try{
return Utility.getElementByUUIDAsAdmin(null, uuid, Vertex.class);
}catch (ERNotFoundException e) {
return Utility.getElementByUUIDAsAdmin(null, uuid, Edge.class);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
@SuppressWarnings("rawtypes")
public static ERManagement getERManagementFromUUID(OrientGraph orientGraph,
UUID uuid) throws ResourceRegistryException {
Element element;
try {
element = Utility.getElementByUUID(orientGraph, null, uuid,
Vertex.class);
element = getAnyElementByUUID(uuid);
return getERManagement(orientGraph, element);
} catch (Exception e) {
try {
element = Utility.getElementByUUID(orientGraph, null, uuid,
Edge.class);
} catch (Exception ex) {
throw new ResourceRegistryException(String.format(
"%s does not belong to an %s nor to a %s",
uuid.toString(), Entity.NAME, Relation.NAME));
}
}
return getERManagement(orientGraph, element);
}
protected ERManagement(AccessType accessType) {
this.accessType = accessType;
@ -339,72 +342,48 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
this.uuid = HeaderUtility.getHeader(element).getUUID();
}
protected void throwElementNotFoundException(ERNotFoundException e)
throws ERNotFoundException {
switch (accessType) {
case RESOURCE:
throw new ResourceNotFoundException(e);
case FACET:
throw new FacetNotFoundException(e);
case IS_RELATED_TO:
throw new RelationNotFoundException(e);
case CONSISTS_OF:
throw new RelationNotFoundException(e);
default:
break;
}
throw e;
}
protected void throwERAvailableInAnotherContextException(ERNotFoundException e)
throws ERAvailableInAnotherContextException {
protected ERNotFoundException getRightElementNotFoundException(ERNotFoundException e) {
switch (accessType) {
case RESOURCE:
throw new ResourceAvailableInAnotherContextException(e);
return new ResourceNotFoundException(e);
case FACET:
throw new FacetAvailableInAnotherContextException(e);
return new FacetNotFoundException(e);
case IS_RELATED_TO:
throw new RelationAvailableInAnotherContextException(e);
return new RelationNotFoundException(e);
case CONSISTS_OF:
throw new RelationAvailableInAnotherContextException(e);
return new RelationNotFoundException(e);
default:
break;
return e;
}
}
throw new ERAvailableInAnotherContextException(e);
protected ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(){
switch (accessType) {
case RESOURCE:
return new ResourceAvailableInAnotherContextException("");
case FACET:
return new FacetAvailableInAnotherContextException("");
case IS_RELATED_TO:
return new RelationAvailableInAnotherContextException("");
case CONSISTS_OF:
return new RelationAvailableInAnotherContextException("");
default:
return new ERAvailableInAnotherContextException("");
}
}
public El getElement() throws ResourceRegistryException {
try {
public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException {
if (element == null) {
element = Utility.getElementByUUID(orientGraph,
erType == null ? accessType.getName() : erType, uuid, elementClass);
}
} catch (ERNotFoundException e) {
try{
Utility.getElementByUUIDAsAdmin(erType == null ? accessType.getName() : erType, uuid, elementClass);
throwERAvailableInAnotherContextException(e);
}catch (ERNotFoundException e1) {
// Using e not e1
throwElementNotFoundException(e);
// TODO Check if another ER has the same UUID
} catch (ResourceRegistryException e1) {
try {
element = retrieveElement();
}catch (ERNotFoundException e) {
try {
retrieveElementFromAnyContext();
throw getSpecificERAvailableInAnotherContextException();
} catch (ERAvailableInAnotherContextException e1) {
throw e1;
} catch (Exception e1) {
throw new ResourceRegistryException(e);
}catch (Exception e1) {
throw e;
}
} catch (ResourceRegistryException e) {
throw e;
@ -412,9 +391,35 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
throw new ResourceRegistryException(e);
}
}
return element;
}
public El retrieveElement() throws ERNotFoundException, ResourceRegistryException {
try {
return Utility.getElementByUUID(orientGraph,
erType == null ? accessType.getName() : erType, uuid, elementClass);
} catch (ERNotFoundException e) {
throw getRightElementNotFoundException(e);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
public El retrieveElementFromAnyContext() throws ERNotFoundException, ResourceRegistryException {
try{
return Utility.getElementByUUIDAsAdmin(erType == null ? accessType.getName() : erType, uuid, elementClass);
}catch (ERNotFoundException e) {
throw getRightElementNotFoundException(e);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
public abstract String reallyGetAll(boolean polymorphic)
throws ResourceRegistryException;
@ -440,11 +445,9 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try {
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER);
if(getElement()==null){
return false;
}else{
getElement();
return true;
}
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
@ -516,10 +519,15 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
boolean deleted = reallyDelete();
if(deleted){
orientGraph.commit();
logger.info("{} with UUID {} was successfully deleted.", accessType.getName(),
uuid);
}else{
logger.info("{} with UUID {} was NOT deleted.", accessType.getName(),
uuid);
orientGraph.rollback();
}
return deleted;

View File

@ -10,9 +10,13 @@ import org.gcube.informationsystem.model.embedded.Header;
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.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;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
@ -24,6 +28,7 @@ import org.slf4j.LoggerFactory;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
@ -76,7 +81,7 @@ public abstract class EntityManagement<E extends Entity> extends
Vertex vertexEntity = orientGraph.addVertex("class:" + erType);
try {
if(uuid!=null){
Vertex v = getElement();
if (v != null) {
String error = String.format(
@ -84,12 +89,27 @@ public abstract class EntityManagement<E extends Entity> extends
uuid.toString());
throw new EntityAlreadyPresentException(error);
}
}
} catch (EntityAlreadyPresentException e) {
} catch (ERNotFoundException e) {
try {
Element el = getAnyElementByUUID(uuid);
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);
}catch (ERNotFoundException e1) {
// OK the UUID is not already used.
}
} catch (ERAvailableInAnotherContextException e) {
throw e;
} catch (Exception e) {
}
/* Removed because the check if(uudi!=null) has beeen added
catch (Exception e) {
// no header or no header with uuid is provided and it is fine
}
*/
this.element = vertexEntity;