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;
}
protected void setAsEntryPoint() {
this.entryPoint = true;
}

View File

@ -301,9 +301,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
protected void reallyAddToContext()
throws ContextException, ResourceRegistryException {
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
/*
* DO NOT UNCOMMENT
@ -331,10 +329,8 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
try {
setOperation(Operation.ADD_TO_CONTEXT);
reallyAddToContext();
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
affectedInstances.put(uuid, serializeSelfAsJsonNode());
sanityCheck();
} catch(ResourceRegistryException e) {
@ -359,7 +355,11 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
internalAddToContext();
oDatabaseDocument.commit();
if(!dryRun) {
oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully added to Context with UUID {}", typeName, uuid, contextUUID);
} catch(ResourceRegistryException e) {
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 {
setOperation(Operation.REMOVE_FROM_CONTEXT);
reallyRemoveFromContext();
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
affectedInstances.put(uuid, serializeSelfAsJsonNode());
sanityCheck();
} 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 {
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
* the instance is added internalAddToContext() function after
* the update of Header metadata i.e. modifiedBy, lastUpdateTime
* affectedInstances.put(uuid, serializeSelfOnly());
*/
return affectedInstances;
}
public void removeFromContext(UUID contextUUID)
@ -448,7 +443,11 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
internalRemoveFromContext();
oDatabaseDocument.commit();
if(!dryRun) {
oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully removed from Context with UUID {}", typeName, uuid, contextUUID);
} catch(ResourceRegistryException e) {
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;
@ -585,7 +584,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
skip = true;
break;
}else {
skip = !propertyMatchRequestedVlaue(v, key, value, o);
skip = !propertyMatchRequestedValue(v, key, value, o);
if(skip) {
break;
}

View File

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

View File

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

View File

@ -421,9 +421,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
targetEntityManagement.internalAddToContext();
affectedInstances.putAll(targetEntityManagement.getAffectedInstances());
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
/*
* DO NOT UNCOMMENT
* // affectedInstances.put(uuid, serializeSelfOnly());
@ -440,9 +439,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
break;
}
}else {
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
/*
* DO NOT UNCOMMENT
* // affectedInstances.put(uuid, serializeSelfOnly());
@ -461,10 +458,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
operation = Operation.ADD_TO_CONTEXT;
reallyAddToContext();
if(propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) {
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
affectedInstances.put(uuid, serializeSelfAsJsonNode());
}
} catch(ResourceRegistryException e) {
@ -493,9 +488,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
targetEntityManagement.internalAddToContext();
affectedInstances.putAll(targetEntityManagement.getAffectedInstances());
if(!dryRun) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
affectedInstances.put(uuid, serializeSelfAsJsonNode());
return affectedInstances;
@ -516,7 +510,11 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
sanityCheck();
oDatabaseDocument.commit();
if(!dryRun) {
oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully added to Context with UUID {}", accessType.getName(), uuid,
contextUUID);
} 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
* 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());
T targetEntityManagement = getTargetEntityManagement();
@ -632,10 +629,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
try {
setOperation(Operation.REMOVE_FROM_CONTEXT);
reallyRemoveFromContext();
if(!dryRun) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
affectedInstances.put(uuid, serializeSelfAsJsonNode());
} catch(ResourceRegistryException e) {
throw e;
@ -662,7 +657,11 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
sanityCheck();
oDatabaseDocument.commit();
if(!dryRun) {
oDatabaseDocument.commit();
}else {
oDatabaseDocument.rollback();
}
logger.info("{} with UUID {} successfully removed from Context with UUID {}", typeName, uuid, contextUUID);
} catch(ResourceRegistryException e) {
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);
}
OVertex target = getTargetEntityManagement().getElement();
affectedInstances.put(uuid, serializeAsJsonNode());
// pre-loading target entity because after deleting the relation we will not be able to get it
T t = getTargetEntityManagement();
affectedInstances.put(uuid, serializeSelfAsJsonNode());
element.delete();
switch(removeConstraint) {
case cascade:
getTargetEntityManagement().internalDelete();
t.internalDelete();
break;
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();
if(iterator.hasNext()) {
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:
break;
}
affectedInstances.putAll(t.getAffectedInstances());
}
protected Collection<JsonNode> serializeEdges(Iterable<ODocument> edges, boolean postFilterPolymorphic)