diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/OperationValidator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/OperationValidator.java new file mode 100644 index 0000000..54bf0c1 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/OperationValidator.java @@ -0,0 +1,16 @@ +package org.gcube.informationsystem.resourceregistry.instances.model; + +import java.util.Map; +import java.util.UUID; + +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public interface OperationValidator { + + public boolean isValidOperation(Map affectedInstances) throws ResourceRegistryException; + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java new file mode 100644 index 0000000..3beea67 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java @@ -0,0 +1,81 @@ +package org.gcube.informationsystem.resourceregistry.instances.model; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SharingOperationValidator implements OperationValidator { + + protected static Logger logger = LoggerFactory.getLogger(SharingOperationValidator.class); + + protected final Map expectedInstaces; + protected final String operation; + + public SharingOperationValidator(Map expectedInstaces, String operation) { + this.expectedInstaces = expectedInstaces; + this.operation = operation; + } + + @Override + public boolean isValidOperation(Map affectedInstances) throws ResourceRegistryException { + Set expectedUUIDSet = expectedInstaces.keySet(); + Set affectedUUIDSet = new HashSet<>(affectedInstances.keySet()); + + affectedUUIDSet.removeAll(expectedUUIDSet); + if(affectedUUIDSet.size()>0) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("To keep the integrity of the IS the operation "); + stringBuffer.append(operation); + stringBuffer.append(" affects more instances than the requested by the client. The operation will be aborted. "); + stringBuffer.append("Additional affected instances are "); + stringBuffer.append(affectedUUIDSet.toString()); + String errorMessage = stringBuffer.toString(); + logger.error(errorMessage); + throw new ResourceRegistryException(errorMessage); + } + + for(UUID expectedUUID : expectedUUIDSet) { + JsonNode expectedJsonNode = expectedInstaces.get(expectedUUID); + JsonNode affectedJsonNode = affectedInstances.get(expectedUUID); + if(affectedJsonNode==null) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("The operation "); + stringBuffer.append(operation); + stringBuffer.append(" did not affected the instance with UUID "); + stringBuffer.append(expectedUUID.toString()); + stringBuffer.append(" "); + stringBuffer.append(expectedJsonNode); + stringBuffer.append(". This should not occur. Please contact the administrator."); + String errorMessage = stringBuffer.toString(); + logger.error(errorMessage); + throw new ResourceRegistryException(errorMessage); + } + + String expectedType = expectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + if(expectedType.compareTo(affectedType)!=0) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append("The expected type of the instance with UUID "); + stringBuffer.append(expectedUUID); + stringBuffer.append(" is "); + stringBuffer.append(expectedType); + stringBuffer.append(". Instead the effective instance with such UUID is"); + stringBuffer.append(affectedType); + stringBuffer.append(". The operation has been aborted."); + String errorMessage = stringBuffer.toString(); + logger.error(errorMessage); + throw new ResourceRegistryException(errorMessage); + } + } + + return true; + } + +}