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 1f0bf2a..6be201d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -165,14 +165,15 @@ public abstract class ERManagement { } public void setElementType(String erType) throws ResourceRegistryException { - if(this.elementType==null) { + if(this.elementType == null) { if(erType == null || erType.compareTo("") == 0) { erType = accessType.getName(); } this.elementType = erType; - }else { - if(elementType.compareTo(erType)!=0) { - throw new ResourceRegistryException("Provided type " + erType + " does not match with the one already known " + this.accessType); + } else { + if(elementType.compareTo(erType) != 0) { + throw new ResourceRegistryException( + "Provided type " + erType + " does not match with the one already known " + this.accessType); } } @@ -185,7 +186,6 @@ public abstract class ERManagement { return elementType; } - protected void checkJSON() throws ResourceRegistryException { if(uuid == null) { try { @@ -307,9 +307,11 @@ public abstract class ERManagement { return reallyDelete(); } - protected abstract boolean reallyAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException; + protected abstract boolean reallyAddToContext(SecurityContext targetSecurityContext) + throws ContextException, ResourceRegistryException; - public boolean internalAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { + public boolean internalAddToContext(SecurityContext targetSecurityContext) + throws ContextException, ResourceRegistryException { try { boolean ret = reallyAddToContext(targetSecurityContext); HeaderUtility.updateModifiedByAndLastUpdate(element); @@ -318,13 +320,16 @@ public abstract class ERManagement { } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Adding " + elementType + " to " + targetSecurityContext.toString(), e.getCause()); + throw new ResourceRegistryException( + "Error Adding " + elementType + " to " + targetSecurityContext.toString(), e.getCause()); } } - protected abstract boolean reallyRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException; + protected abstract boolean reallyRemoveFromContext(SecurityContext targetSecurityContext) + throws ContextException, ResourceRegistryException; - public boolean internalRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException { + public boolean internalRemoveFromContext(SecurityContext targetSecurityContext) + throws ContextException, ResourceRegistryException { try { boolean ret = reallyRemoveFromContext(targetSecurityContext); HeaderUtility.updateModifiedByAndLastUpdate(element); @@ -333,7 +338,8 @@ public abstract class ERManagement { } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new ResourceRegistryException("Error Removing " + elementType + " from " + targetSecurityContext.toString(), e.getCause()); + throw new ResourceRegistryException( + "Error Removing " + elementType + " from " + targetSecurityContext.toString(), e.getCause()); } } @@ -400,7 +406,8 @@ public abstract class ERManagement { public El retrieveElementFromAnyContext() throws NotFoundException, ResourceRegistryException { try { - return Utility.getElementByUUIDAsAdmin(elementType == null ? accessType.getName() : elementType, uuid, elementClass); + return Utility.getElementByUUIDAsAdmin(elementType == null ? accessType.getName() : elementType, uuid, + elementClass); } catch(NotFoundException e) { throw getSpecificElementNotFoundException(e); } catch(ResourceRegistryException e) { @@ -429,23 +436,9 @@ public abstract class ERManagement { } } - public boolean exists() - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { - return exists(false); - } - - public boolean exists(boolean writer) - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { - if(writer) { - // Exists is used to understand if it is a create or update. - // In any case the we need writing right on this instance - orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); - orientGraph.setAutoStartTx(false); - orientGraph.begin(); - }else { - orientGraph = getWorkingContext().getGraph(PermissionMode.READER); - } + orientGraph = getWorkingContext().getGraph(PermissionMode.READER); getElement(); @@ -463,18 +456,58 @@ public abstract class ERManagement { } } - public String create() throws AlreadyPresentException, ResourceRegistryException { - return create(false); + public String createOrUpdate() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + try { + orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); + orientGraph.setAutoStartTx(false); + orientGraph.begin(); + + boolean update = false; + try { + getElement(); + update = true; + element = internalUpdate(); + }catch (NotFoundException e) { + element = internalCreate(); + } + + orientGraph.commit(); + + if(update) { + setReload(true); + } + + // TODO Notify to subscriptionNotification + + return serialize(); + + } catch(ResourceRegistryException e) { + logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid); + if(orientGraph != null) { + orientGraph.rollback(); + } + throw e; + } catch(Exception e) { + logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid, e); + if(orientGraph != null) { + orientGraph.rollback(); + } + throw new ResourceRegistryException(e); + } finally { + if(orientGraph != null) { + orientGraph.shutdown(); + } + } } - public String create(boolean transactionStarted) throws AlreadyPresentException, ResourceRegistryException { + + + public String create() throws AlreadyPresentException, ResourceRegistryException { try { - if(!transactionStarted) { - orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); - orientGraph.setAutoStartTx(false); - orientGraph.begin(); - } + orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); + orientGraph.setAutoStartTx(false); + orientGraph.begin(); element = internalCreate(); @@ -524,16 +557,10 @@ public abstract class ERManagement { } public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { - return update(false); - } - - public String update(boolean transactionStarted) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { try { - if(!transactionStarted) { - orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); - orientGraph.setAutoStartTx(false); - orientGraph.begin(); - } + orientGraph = getWorkingContext().getGraph(PermissionMode.WRITER); + orientGraph.setAutoStartTx(false); + orientGraph.begin(); element = internalUpdate(); @@ -564,8 +591,7 @@ public abstract class ERManagement { } } - public boolean delete() - throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + public boolean delete() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid); try { @@ -605,7 +631,8 @@ public abstract class ERManagement { } } - public boolean addToContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { + public boolean addToContext(UUID contextUUID) + throws NotFoundException, ContextException, ResourceRegistryException { logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID); try { @@ -640,7 +667,8 @@ public abstract class ERManagement { } } - public boolean removeFromContext(UUID contextUUID) throws NotFoundException, ContextException, ResourceRegistryException { + public boolean removeFromContext(UUID contextUUID) + throws NotFoundException, ContextException, ResourceRegistryException { logger.debug("Going to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID); try { @@ -654,7 +682,8 @@ public abstract class ERManagement { boolean removed = internalRemoveFromContext(targetSecurityContext); orientGraph.commit(); - logger.info("{} with UUID {} successfully removed from Context with UUID {}", elementType, uuid, contextUUID); + logger.info("{} with UUID {} successfully removed from Context with UUID {}", elementType, uuid, + contextUUID); return removed; } catch(ResourceRegistryException e) { @@ -664,7 +693,8 @@ public abstract class ERManagement { } throw e; } catch(Exception e) { - logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID, e); + logger.error("Unable to remove {} with UUID {} from Context with UUID {}", elementType, uuid, contextUUID, + e); if(orientGraph != null) { orientGraph.rollback(); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java index ef8e811..d87a713 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java @@ -157,23 +157,11 @@ public class InstancesManager { @SuppressWarnings("rawtypes") ERManagement erManagement = ERManagementUtility.getERManagement(type); - erManagement.setUUID(UUID.fromString(uuid)); - - boolean create = false; - try { - erManagement.exists(false); - } catch(NotFoundException e) { - create = true; - } - erManagement.setUUID(UUID.fromString(uuid)); erManagement.setElementType(type); erManagement.setJSON(json); - if(create) { - return erManagement.create(true); - } - return erManagement.update(true); + return erManagement.createOrUpdate(); } /*