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)); 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") @SuppressWarnings("rawtypes")
public static ERManagement getERManagementFromUUID(OrientGraph orientGraph, public static ERManagement getERManagementFromUUID(OrientGraph orientGraph,
UUID uuid) throws ResourceRegistryException { UUID uuid) throws ResourceRegistryException {
Element element; Element element;
try { try {
element = Utility.getElementByUUID(orientGraph, null, uuid, element = getAnyElementByUUID(uuid);
Vertex.class); return getERManagement(orientGraph, element);
} catch (Exception e) { } catch (Exception e) {
try {
element = Utility.getElementByUUID(orientGraph, null, uuid,
Edge.class);
} catch (Exception ex) {
throw new ResourceRegistryException(String.format( throw new ResourceRegistryException(String.format(
"%s does not belong to an %s nor to a %s", "%s does not belong to an %s nor to a %s",
uuid.toString(), Entity.NAME, Relation.NAME)); uuid.toString(), Entity.NAME, Relation.NAME));
} }
} }
return getERManagement(orientGraph, element);
}
protected ERManagement(AccessType accessType) { protected ERManagement(AccessType accessType) {
this.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(); this.uuid = HeaderUtility.getHeader(element).getUUID();
} }
protected void throwElementNotFoundException(ERNotFoundException e) protected ERNotFoundException getRightElementNotFoundException(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 {
switch (accessType) { switch (accessType) {
case RESOURCE: case RESOURCE:
throw new ResourceAvailableInAnotherContextException(e); return new ResourceNotFoundException(e);
case FACET: case FACET:
throw new FacetAvailableInAnotherContextException(e); return new FacetNotFoundException(e);
case IS_RELATED_TO: case IS_RELATED_TO:
throw new RelationAvailableInAnotherContextException(e); return new RelationNotFoundException(e);
case CONSISTS_OF: case CONSISTS_OF:
throw new RelationAvailableInAnotherContextException(e); return new RelationNotFoundException(e);
default: 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 { public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException {
try {
if (element == null) { if (element == null) {
element = Utility.getElementByUUID(orientGraph, try {
erType == null ? accessType.getName() : erType, uuid, elementClass); element = retrieveElement();
}
}catch (ERNotFoundException e) { }catch (ERNotFoundException e) {
try { try {
Utility.getElementByUUIDAsAdmin(erType == null ? accessType.getName() : erType, uuid, elementClass); retrieveElementFromAnyContext();
throwERAvailableInAnotherContextException(e); throw getSpecificERAvailableInAnotherContextException();
}catch (ERNotFoundException e1) { } catch (ERAvailableInAnotherContextException e1) {
// Using e not e1
throwElementNotFoundException(e);
// TODO Check if another ER has the same UUID
} catch (ResourceRegistryException e1) {
throw e1; throw e1;
}catch (Exception e1) { }catch (Exception e1) {
throw new ResourceRegistryException(e); throw e;
} }
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
@ -412,9 +391,35 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
throw new ResourceRegistryException(e); throw new ResourceRegistryException(e);
} }
}
return element; 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) public abstract String reallyGetAll(boolean polymorphic)
throws ResourceRegistryException; throws ResourceRegistryException;
@ -440,11 +445,9 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try { try {
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER); orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER);
if(getElement()==null){ getElement();
return false;
}else{
return true; return true;
}
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
@ -516,10 +519,15 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
boolean deleted = reallyDelete(); boolean deleted = reallyDelete();
if(deleted){
orientGraph.commit(); orientGraph.commit();
logger.info("{} with UUID {} was successfully deleted.", accessType.getName(), logger.info("{} with UUID {} was successfully deleted.", accessType.getName(),
uuid); uuid);
}else{
logger.info("{} with UUID {} was NOT deleted.", accessType.getName(),
uuid);
orientGraph.rollback();
}
return deleted; 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.Entity;
import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource; 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.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; 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.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.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagement;
@ -24,6 +28,7 @@ import org.slf4j.LoggerFactory;
import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex; 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); Vertex vertexEntity = orientGraph.addVertex("class:" + erType);
try { try {
if(uuid!=null){
Vertex v = getElement(); Vertex v = getElement();
if (v != null) { if (v != null) {
String error = String.format( String error = String.format(
@ -84,12 +89,27 @@ public abstract class EntityManagement<E extends Entity> extends
uuid.toString()); uuid.toString());
throw new EntityAlreadyPresentException(error); 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; 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 // no header or no header with uuid is provided and it is fine
} }
*/
this.element = vertexEntity; this.element = vertexEntity;