DryRun has been implemented as transaction rollback

This commit is contained in:
Luca Frosini 2021-03-05 16:19:55 +01:00
parent e5eedc0108
commit ada9439ab9
5 changed files with 54 additions and 56 deletions

View File

@ -165,7 +165,6 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
this.dryRun = dryRun; this.dryRun = dryRun;
} }
protected void setAsEntryPoint() { protected void setAsEntryPoint() {
this.entryPoint = true; this.entryPoint = true;
} }

View File

@ -301,9 +301,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
protected void reallyAddToContext() protected void reallyAddToContext()
throws ContextException, ResourceRegistryException { throws ContextException, ResourceRegistryException {
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument); targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
/* /*
* DO NOT UNCOMMENT * DO NOT UNCOMMENT
@ -331,10 +329,8 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
try { try {
setOperation(Operation.ADD_TO_CONTEXT); setOperation(Operation.ADD_TO_CONTEXT);
reallyAddToContext(); reallyAddToContext();
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element); HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
}
affectedInstances.put(uuid, serializeSelfAsJsonNode()); affectedInstances.put(uuid, serializeSelfAsJsonNode());
sanityCheck(); sanityCheck();
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
@ -359,7 +355,11 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
internalAddToContext(); internalAddToContext();
if(!dryRun) {
oDatabaseDocument.commit(); oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully added to Context with UUID {}", typeName, uuid, contextUUID); logger.info("{} with UUID {} successfully added to Context with UUID {}", typeName, uuid, contextUUID);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to add {} with UUID {} to Context with UUID {} - Reason is {}", typeName, uuid, contextUUID, e.getMessage()); logger.error("Unable to add {} with UUID {} to Context with UUID {} - Reason is {}", typeName, uuid, contextUUID, e.getMessage());
@ -390,10 +390,8 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
try { try {
setOperation(Operation.REMOVE_FROM_CONTEXT); setOperation(Operation.REMOVE_FROM_CONTEXT);
reallyRemoveFromContext(); reallyRemoveFromContext();
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element); HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
}
affectedInstances.put(uuid, serializeSelfAsJsonNode()); affectedInstances.put(uuid, serializeSelfAsJsonNode());
sanityCheck(); sanityCheck();
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
@ -404,7 +402,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
} }
} }
protected Map<UUID,JsonNode> reallyRemoveFromContext() protected void reallyRemoveFromContext()
throws ContextException, ResourceRegistryException { throws ContextException, ResourceRegistryException {
if(honourPropagationConstraintsInContextSharing) { if(honourPropagationConstraintsInContextSharing) {
@ -420,17 +418,14 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
} }
} }
if(!dryRun) {
targetSecurityContext.removeElement(getElement(), oDatabaseDocument); targetSecurityContext.removeElement(getElement(), oDatabaseDocument);
}
/* /*
* DO NOT UNCOMMENT * DO NOT UNCOMMENT
* the instance is added internalAddToContext() function after * the instance is added internalAddToContext() function after
* the update of Header metadata i.e. modifiedBy, lastUpdateTime * the update of Header metadata i.e. modifiedBy, lastUpdateTime
* affectedInstances.put(uuid, serializeSelfOnly()); * affectedInstances.put(uuid, serializeSelfOnly());
*/ */
return affectedInstances;
} }
public void removeFromContext(UUID contextUUID) public void removeFromContext(UUID contextUUID)
@ -448,7 +443,11 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
internalRemoveFromContext(); internalRemoveFromContext();
if(!dryRun) {
oDatabaseDocument.commit(); oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully removed from Context with UUID {}", typeName, uuid, contextUUID); logger.info("{} with UUID {} successfully removed from Context with UUID {}", typeName, uuid, contextUUID);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to remove {} with UUID {} from Context with UUID {}", typeName, uuid, contextUUID); logger.error("Unable to remove {} with UUID {} from Context with UUID {}", typeName, uuid, contextUUID);
@ -498,7 +497,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
} }
} }
public boolean propertyMatchRequestedVlaue(OVertex v, String key, String requestedValue, Object instanceValue) throws SchemaException, ResourceRegistryException { public boolean propertyMatchRequestedValue(OVertex v, String key, String requestedValue, Object instanceValue) throws SchemaException, ResourceRegistryException {
return requestedValue.compareTo(instanceValue.toString())==0; return requestedValue.compareTo(instanceValue.toString())==0;
@ -585,7 +584,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
skip = true; skip = true;
break; break;
}else { }else {
skip = !propertyMatchRequestedVlaue(v, key, value, o); skip = !propertyMatchRequestedValue(v, key, value, o);
if(skip) { if(skip) {
break; break;
} }

View File

@ -1,8 +1,5 @@
package org.gcube.informationsystem.resourceregistry.instances.model.entities; package org.gcube.informationsystem.resourceregistry.instances.model.entities;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
@ -70,14 +67,14 @@ public class FacetManagement extends EntityManagement<Facet, FacetType> {
} }
@Override @Override
protected Map<UUID,JsonNode> reallyRemoveFromContext() protected void reallyRemoveFromContext()
throws ContextException, ResourceRegistryException { throws ContextException, ResourceRegistryException {
if(entryPoint) { if(entryPoint) {
OVertex oVertex = getElement().getVertices(ODirection.IN).iterator().next(); OVertex oVertex = getElement().getVertices(ODirection.IN).iterator().next();
resourceManagement = new ResourceManagement(); resourceManagement = new ResourceManagement();
resourceManagement.setElement(oVertex); resourceManagement.setElement(oVertex);
} }
return super.reallyRemoveFromContext(); super.reallyRemoveFromContext();
} }
@Override @Override

View File

@ -249,9 +249,7 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
protected void reallyAddToContext() protected void reallyAddToContext()
throws ContextException, ResourceRegistryException { throws ContextException, ResourceRegistryException {
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument); targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
/* /*
* DO NOT UNCOMMENT * DO NOT UNCOMMENT

View File

@ -421,9 +421,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
targetEntityManagement.internalAddToContext(); targetEntityManagement.internalAddToContext();
affectedInstances.putAll(targetEntityManagement.getAffectedInstances()); affectedInstances.putAll(targetEntityManagement.getAffectedInstances());
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument); targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
/* /*
* DO NOT UNCOMMENT * DO NOT UNCOMMENT
* // affectedInstances.put(uuid, serializeSelfOnly()); * // affectedInstances.put(uuid, serializeSelfOnly());
@ -440,9 +439,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
break; break;
} }
}else { }else {
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument); targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
/* /*
* DO NOT UNCOMMENT * DO NOT UNCOMMENT
* // affectedInstances.put(uuid, serializeSelfOnly()); * // affectedInstances.put(uuid, serializeSelfOnly());
@ -461,10 +458,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
operation = Operation.ADD_TO_CONTEXT; operation = Operation.ADD_TO_CONTEXT;
reallyAddToContext(); reallyAddToContext();
if(propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) { if(propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) {
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element); HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
}
affectedInstances.put(uuid, serializeSelfAsJsonNode()); affectedInstances.put(uuid, serializeSelfAsJsonNode());
} }
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
@ -493,9 +488,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
targetEntityManagement.internalAddToContext(); targetEntityManagement.internalAddToContext();
affectedInstances.putAll(targetEntityManagement.getAffectedInstances()); affectedInstances.putAll(targetEntityManagement.getAffectedInstances());
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument); targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
affectedInstances.put(uuid, serializeSelfAsJsonNode()); affectedInstances.put(uuid, serializeSelfAsJsonNode());
return affectedInstances; return affectedInstances;
@ -516,7 +510,11 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
sanityCheck(); sanityCheck();
if(!dryRun) {
oDatabaseDocument.commit(); oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully added to Context with UUID {}", accessType.getName(), uuid, logger.info("{} with UUID {} successfully added to Context with UUID {}", accessType.getName(), uuid,
contextUUID); contextUUID);
} catch(Exception e) { } catch(Exception e) {
@ -570,9 +568,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
* In any removeConstraint value the relation MUST be removed from context to * In any removeConstraint value the relation MUST be removed from context to
* avoid to have edge having a source outside of the context. * avoid to have edge having a source outside of the context.
*/ */
if(!dryRun) {
targetSecurityContext.removeElement(getElement(), oDatabaseDocument); targetSecurityContext.removeElement(getElement(), oDatabaseDocument);
}
affectedInstances.put(uuid, serializeSelfAsJsonNode()); affectedInstances.put(uuid, serializeSelfAsJsonNode());
T targetEntityManagement = getTargetEntityManagement(); T targetEntityManagement = getTargetEntityManagement();
@ -632,10 +629,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
try { try {
setOperation(Operation.REMOVE_FROM_CONTEXT); setOperation(Operation.REMOVE_FROM_CONTEXT);
reallyRemoveFromContext(); reallyRemoveFromContext();
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element); HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save(); element.save();
}
affectedInstances.put(uuid, serializeSelfAsJsonNode()); affectedInstances.put(uuid, serializeSelfAsJsonNode());
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
@ -662,7 +657,11 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
sanityCheck(); sanityCheck();
if(!dryRun) {
oDatabaseDocument.commit(); oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully removed from Context with UUID {}", typeName, uuid, contextUUID); logger.info("{} with UUID {} successfully removed from Context with UUID {}", typeName, uuid, contextUUID);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to remove {} with UUID {} from Context with UUID {}", typeName, uuid, contextUUID); logger.error("Unable to remove {} with UUID {} from Context with UUID {}", typeName, uuid, contextUUID);
@ -716,17 +715,20 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
Utility.toJsonString(element, true), removeConstraint, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); Utility.toJsonString(element, true), removeConstraint, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
} }
OVertex target = getTargetEntityManagement().getElement(); // pre-loading target entity because after deleting the relation we will not be able to get it
affectedInstances.put(uuid, serializeAsJsonNode()); T t = getTargetEntityManagement();
affectedInstances.put(uuid, serializeSelfAsJsonNode());
element.delete(); element.delete();
switch(removeConstraint) { switch(removeConstraint) {
case cascade: case cascade:
getTargetEntityManagement().internalDelete(); t.internalDelete();
break; break;
case cascadeWhenOrphan: case cascadeWhenOrphan:
Iterable<OEdge> iterable = target.getEdges(ODirection.IN); OVertex target = t.getElement();
Iterable<OEdge> iterable = t.getElement().getEdges(ODirection.IN);
Iterator<OEdge> iterator = iterable.iterator(); Iterator<OEdge> iterator = iterable.iterator();
if(iterator.hasNext()) { if(iterator.hasNext()) {
logger.trace("{} point to {} which is not orphan. Giving {} directive, it will be keep.", element, logger.trace("{} point to {} which is not orphan. Giving {} directive, it will be keep.", element,
@ -742,6 +744,9 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
default: default:
break; break;
} }
affectedInstances.putAll(t.getAffectedInstances());
} }
protected Collection<JsonNode> serializeEdges(Iterable<ODocument> edges, boolean postFilterPolymorphic) protected Collection<JsonNode> serializeEdges(Iterable<ODocument> edges, boolean postFilterPolymorphic)