Implementing strategy for add/remove to/from context no follow

This commit is contained in:
Luca Frosini 2021-02-02 16:51:40 +01:00
parent b18ebe7427
commit d4f4ac9468
2 changed files with 97 additions and 0 deletions

View File

@ -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<UUID, JsonNode> affectedInstances) throws ResourceRegistryException;
}

View File

@ -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<UUID, JsonNode> expectedInstaces;
protected final String operation;
public SharingOperationValidator(Map<UUID, JsonNode> expectedInstaces, String operation) {
this.expectedInstaces = expectedInstaces;
this.operation = operation;
}
@Override
public boolean isValidOperation(Map<UUID, JsonNode> affectedInstances) throws ResourceRegistryException {
Set<UUID> expectedUUIDSet = expectedInstaces.keySet();
Set<UUID> 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;
}
}