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 3cc952d..3729d90 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -78,6 +78,7 @@ import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientElement; import com.tinkerpop.blueprints.impls.orient.OrientGraph; +import com.tinkerpop.blueprints.impls.orient.OrientVertex; import com.tinkerpop.blueprints.util.StringFactory; /** @@ -325,28 +326,102 @@ public abstract class ERManagement { public abstract JSONObject serializeAsJson() throws ResourceRegistryException; - public abstract El reallyCreate() throws ERAlreadyPresentException, + protected abstract El reallyCreate() throws ERAlreadyPresentException, ResourceRegistryException; - public abstract El reallyUpdate() throws ERNotFoundException, - ResourceRegistryException; - - public El reallyCreateOrUdate() throws ResourceRegistryException { + public El internalCreate() throws ERAlreadyPresentException, ResourceRegistryException { try { - return reallyUpdate(); - }catch (ERNotFoundException e) { - return reallyCreate(); + reallyCreate(); + Header entityHeader = HeaderUtility.getHeader(jsonNode, true); + if (entityHeader != null) { + element.setProperty(Entity.HEADER_PROPERTY, entityHeader); + } else { + entityHeader = HeaderUtility.addHeader(element, null); + } + ContextUtility.addToActualContext(orientGraph, element); + + ((OrientVertex) element).save(); + + return element; + }catch (ResourceRegistryException e) { + throw e; + } catch (Exception e) { + throw new ResourceRegistryException("Error Creating " + erType + " with " + jsonNode, e.getCause()); } } - public abstract boolean reallyDelete() throws ERNotFoundException, + protected abstract El reallyUpdate() throws ERNotFoundException, ResourceRegistryException; - public abstract boolean reallyAddToContext() throws ContextException, + public El internalUpdate() throws ERNotFoundException, ResourceRegistryException { + try { + reallyUpdate(); + + // TODO update lastUpdater and lastUpdatetime + + ((OrientVertex) element).save(); + + return element; + }catch (ResourceRegistryException e) { + throw e; + } catch (Exception e) { + throw new ResourceRegistryException("Error Updating " + erType + " with " + jsonNode, e.getCause()); + } + } + + public El internalCreateOrUdate() throws ResourceRegistryException { + try { + return internalUpdate(); + }catch (ERNotFoundException e) { + return internalCreate(); + } + } + + protected abstract boolean reallyDelete() throws ERNotFoundException, ResourceRegistryException; - public abstract boolean reallyRemoveFromContext() throws ContextException, + public boolean internalDelete() throws ERNotFoundException, ResourceRegistryException { + // Added for consistency with create and update addToContext removeFromContext. + return reallyDelete(); + } + + protected abstract boolean reallyAddToContext() throws ContextException, ResourceRegistryException; + + public boolean internalAddToContext() throws ContextException, ResourceRegistryException { + try { + boolean ret = reallyAddToContext(); + + // TODO update lastUpdater and lastUpdatetime + + ((OrientVertex) element).save(); + + return ret && true; + }catch (ResourceRegistryException e) { + throw e; + } catch (Exception e) { + throw new ResourceRegistryException("Error Adding " + erType + " to Current Context ", e.getCause()); + } + } + + protected abstract boolean reallyRemoveFromContext() throws ContextException, + ResourceRegistryException; + + public boolean internalRemoveFromContext() throws ContextException, ResourceRegistryException { + try { + boolean ret = reallyRemoveFromContext(); + + // TODO update lastUpdater and lastUpdatetime + + ((OrientVertex) element).save(); + + return ret && true; + }catch (ResourceRegistryException e) { + throw e; + } catch (Exception e) { + throw new ResourceRegistryException("Error Removing " + erType + " from Current Context ", e.getCause()); + } + } public void setElement(El element) throws ResourceRegistryException { if (element == null) { @@ -498,10 +573,12 @@ public abstract class ERManagement { try { orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER); - element = reallyCreate(); + element = internalCreate(); orientGraph.commit(); + // TODO Notify to subscriptionNotification + return serialize(); } catch (ResourceRegistryException e) { @@ -548,12 +625,16 @@ public abstract class ERManagement { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - element = reallyUpdate(); + element = internalUpdate(); orientGraph.commit(); - + + // TODO Notify to subscriptionNotification + return serialize(); + // TODO Serialized resource is the old version. This really strange and should be an orient bug + } catch (ResourceRegistryException e) { if (orientGraph != null) { orientGraph.rollback(); 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 566cb1d..dbe6a17 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 @@ -6,7 +6,6 @@ package org.gcube.informationsystem.resourceregistry.er.entity; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import org.gcube.informationsystem.model.AccessType; -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; @@ -19,7 +18,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFound import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; -import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,10 +60,6 @@ public abstract class EntityManagement extends this.orientGraph = orientGraph; } - @Override - public abstract Vertex reallyCreate() throws EntityAlreadyPresentException, - ResourceRegistryException; - protected Vertex createVertex() throws EntityAlreadyPresentException, ResourceRegistryException { @@ -110,25 +104,25 @@ public abstract class EntityManagement extends this.element = vertexEntity; - Header entityHeader = HeaderUtility.getHeader(jsonNode, true); - if (entityHeader != null) { - element.setProperty(Entity.HEADER_PROPERTY, entityHeader); - } else { - entityHeader = HeaderUtility.addHeader(element, null); - } - - - if (accessType.compareTo(AccessType.RESOURCE)==0) { + if (accessType==AccessType.RESOURCE) { // Facet and relation are created in calling method } else { ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); } + /* This code has been moved in ERManagement.internalCreate() + Header entityHeader = HeaderUtility.getHeader(jsonNode, true); + if (entityHeader != null) { + element.setProperty(Entity.HEADER_PROPERTY, entityHeader); + } else { + entityHeader = HeaderUtility.addHeader(element, null); + } ContextUtility.addToActualContext(orientGraph, element); - + ((OrientVertex) element).save(); - + */ + logger.info("Created {} is {}", Vertex.class.getSimpleName(), Utility.toJsonString((OrientVertex) element, true)); @@ -144,7 +138,7 @@ public abstract class EntityManagement extends } @Override - public boolean reallyAddToContext() throws ContextException, + protected boolean reallyAddToContext() throws ContextException, ResourceRegistryException { ContextUtility.addToActualContext(orientGraph, getElement()); @@ -155,14 +149,14 @@ public abstract class EntityManagement extends @SuppressWarnings("rawtypes") RelationManagement relationManagement = RelationManagement .getRelationManagement(orientGraph, edge); - relationManagement.reallyAddToContext(); + relationManagement.internalAddToContext(); } return true; } @Override - public boolean reallyRemoveFromContext() throws ContextException, + protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException { ContextUtility.removeFromActualContext(orientGraph, getElement()); @@ -173,7 +167,7 @@ public abstract class EntityManagement extends @SuppressWarnings("rawtypes") RelationManagement relationManagement = RelationManagement .getRelationManagement(orientGraph, edge); - relationManagement.reallyRemoveFromContext(); + relationManagement.internalRemoveFromContext(); } return true; 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 e4d9d53..92f3571 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 @@ -13,7 +13,6 @@ import org.gcube.informationsystem.resourceregistry.er.ERManagement; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; -import com.tinkerpop.blueprints.impls.orient.OrientVertex; /** * @author Luca Frosini (ISTI - CNR) @@ -40,20 +39,19 @@ public class FacetManagement extends EntityManagement { } @Override - public Vertex reallyCreate() throws FacetAlreadyPresentException, ResourceRegistryException { + protected Vertex reallyCreate() throws FacetAlreadyPresentException, ResourceRegistryException { return createVertex(); } @Override - public Vertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException { + protected Vertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException { Vertex facet = getElement(); facet = (Vertex) ERManagement.updateProperties(oClass, facet, jsonNode, ignoreKeys, ignoreStartWithKeys); - ((OrientVertex) facet).save(); return facet; } @Override - public boolean reallyDelete() throws FacetNotFoundException, ResourceRegistryException { + protected boolean reallyDelete() throws FacetNotFoundException, ResourceRegistryException { getElement().remove(); return true; } 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 b7537a6..e1ee723 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 @@ -138,7 +138,7 @@ public class ResourceManagement extends EntityManagement { } @Override - public Vertex reallyCreate() throws ResourceAlreadyPresentException, + protected Vertex reallyCreate() throws ResourceAlreadyPresentException, ResourceRegistryException { createVertex(); @@ -168,7 +168,7 @@ public class ResourceManagement extends EntityManagement { } @Override - public Vertex reallyUpdate() throws ResourceNotFoundException, ResourceRegistryException { + protected Vertex reallyUpdate() throws ResourceNotFoundException, ResourceRegistryException { getElement(); @@ -178,7 +178,7 @@ public class ResourceManagement extends EntityManagement { for (JsonNode relationJsonNode : jsonNodeArray) { ConsistsOfManagement com = new ConsistsOfManagement(orientGraph); com.setJSON(relationJsonNode); - com.reallyCreateOrUdate(); + com.internalCreateOrUdate(); } } @@ -189,18 +189,16 @@ public class ResourceManagement extends EntityManagement { IsRelatedToManagement irtm = new IsRelatedToManagement( orientGraph); irtm.setJSON(relationJsonNode); - irtm.reallyUpdate(); + irtm.internalUpdate(); } } - ((OrientVertex) element).save(); - return element; } @SuppressWarnings("unchecked") @Override - public boolean reallyDelete() throws ResourceNotFoundException, + protected boolean reallyDelete() throws ResourceNotFoundException, ResourceRegistryException { // internalDeleteResource(orientGraph, uuid, null); @@ -226,7 +224,7 @@ public class ResourceManagement extends EntityManagement { } if (relationManagement != null) { relationManagement.setElement(edge); - relationManagement.reallyDelete(); + relationManagement.internalDelete(); } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index dc1cdb7..4e1f33d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -33,7 +33,6 @@ import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement; import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement; -import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -236,11 +235,13 @@ public abstract class RelationManagement extends ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); + /* This code has been moved in ERManagement.internalCreate() HeaderUtility.addHeader(element, null); ContextUtility.addToActualContext(orientGraph, element); ((OrientEdge) element).save(); - + */ + logger.info("{} successfully created", erType); return element; @@ -258,7 +259,7 @@ public abstract class RelationManagement extends try { target = (Vertex) entityManagement.getElement(); } catch (Exception e) { - target = entityManagement.reallyCreate(); + target = (Vertex) entityManagement.internalCreate(); } return reallyCreate(source, target); } @@ -271,7 +272,7 @@ public abstract class RelationManagement extends } @Override - public Edge reallyCreate() throws ResourceRegistryException { + protected Edge reallyCreate() throws ResourceRegistryException { if(!jsonNode.has(Relation.SOURCE_PROPERTY)){ throw new ResourceRegistryException( "Error while creating relation. No source definition found"); @@ -285,7 +286,7 @@ public abstract class RelationManagement extends @Override - public Edge reallyUpdate() throws ResourceRegistryException { + protected Edge reallyUpdate() throws ResourceRegistryException { logger.debug("Trying to update {} : {}", erType, jsonNode); @@ -298,7 +299,7 @@ public abstract class RelationManagement extends if (target != null) { FacetManagement fm = new FacetManagement(orientGraph); fm.setJSON(target); - fm.reallyUpdate(); + fm.internalUpdate(); } } @@ -309,7 +310,7 @@ public abstract class RelationManagement extends } @Override - public boolean reallyAddToContext() throws ContextException, + protected boolean reallyAddToContext() throws ContextException, ResourceRegistryException { getElement(); @@ -352,7 +353,7 @@ public abstract class RelationManagement extends */ EntityManagement entityManagement = EntityManagement .getEntityManagement(orientGraph, target); - entityManagement.reallyAddToContext(); + entityManagement.internalAddToContext(); ContextUtility.addToActualContext(orientGraph, getElement()); break; @@ -376,13 +377,13 @@ public abstract class RelationManagement extends Vertex source = element.getVertex(Direction.OUT); EntityManagement entityManagement = EntityManagement .getEntityManagement(orientGraph, source); - entityManagement.reallyAddToContext(); + entityManagement.internalAddToContext(); /* Adding target to Context */ Vertex target = element.getVertex(Direction.IN); entityManagement = EntityManagement .getEntityManagement(orientGraph, target); - entityManagement.reallyAddToContext(); + entityManagement.internalAddToContext(); ContextUtility.addToActualContext(orientGraph, getElement()); @@ -394,7 +395,7 @@ public abstract class RelationManagement extends EntityManagement entityManagement = EntityManagement .getEntityManagement(orientGraph, target); if (entityManagement != null) { - entityManagement.reallyRemoveFromContext(); + entityManagement.internalRemoveFromContext(); return true; } else { return false; @@ -402,7 +403,7 @@ public abstract class RelationManagement extends } @Override - public boolean reallyRemoveFromContext() throws ContextException, + protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException { getElement(); @@ -545,15 +546,14 @@ public abstract class RelationManagement extends EntityManagement entityManagement = EntityManagement .getEntityManagement(orientGraph, target); if (entityManagement != null) { - entityManagement.reallyDelete(); - return true; + return entityManagement.internalDelete(); } else { return false; } } @Override - public boolean reallyDelete() throws RelationNotFoundException, + protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException { logger.debug( "Going to remove {} with UUID {}. Related {}s will be detached.", diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderOrient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderOrient.java index 9d4f8ee..eab12b0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderOrient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderOrient.java @@ -51,6 +51,15 @@ public class HeaderOrient extends ODocument implements org.gcube.informationsyst this.field(Header.CREATION_TIME_PROPERTY, creationTime); } + @Override + public String getLastUpdater() { + return this.field(Header.LAST_UPDATER_PROPERTY); + } + + public void setLastUpdater(String lastUpdater){ + this.field(Header.LAST_UPDATER_PROPERTY, lastUpdater); + } + @Override public Date getLastUpdateTime() { return this.field(Header.LAST_UPDATE_TIME_PROPERTY); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderUtility.java index ad7d6c3..1995322 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/HeaderUtility.java @@ -26,7 +26,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.orientechnologies.orient.core.record.impl.ODocument; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Element; -import com.tinkerpop.blueprints.Vertex; /** * @author Luca Frosini (ISTI - CNR) @@ -62,7 +61,8 @@ public class HeaderUtility { logger.error("Unable to retrieve user. {} will be used", creator); } header.setCreator(creator); - + header.setLastUpdater(creator); + Date date = Calendar.getInstance().getTime(); SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); logger.trace("Setting Last Update and Creation Time to " + ft.format(date)); @@ -105,6 +105,7 @@ public class HeaderUtility { headerOrient.setUUID(header.getUUID()); headerOrient.setCreator(header.getCreator()); headerOrient.setCreationTime(header.getCreationTime()); + headerOrient.setLastUpdater(header.getLastUpdater()); headerOrient.setLastUpdateTime(header.getLastUpdateTime()); return headerOrient; } catch (Exception e) { @@ -113,9 +114,9 @@ public class HeaderUtility { } } - public static Header addHeader(Vertex vertex, UUID uuid) { + public static Header addHeader(Element element, UUID uuid) { Header header = createHeader(uuid); - vertex.setProperty(Entity.HEADER_PROPERTY, header); + element.setProperty(Entity.HEADER_PROPERTY, header); return header; }