Skip to content
Snippets Groups Projects
Commit 97e28573 authored by jakubs's avatar jakubs
Browse files

BIS-173 refactor checking arguments of validateEntity function to an interceptor

SVN: 26533
parent 0303fc3e
No related branches found
No related tags found
No related merge requests found
......@@ -2499,8 +2499,6 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
{
final List<String> objectsWhichValidationWouldBeForced = new LinkedList<String>();
// TODO: refactor the check for type of entity requested for validation to the code of
// the caller to avoid duplication beetween here and EntityValidationInterceptor
EntityValidationCalculator calculator =
EntityValidationCalculator.create(info.getScript(),
new IValidationRequestDelegate()
......@@ -2508,16 +2506,6 @@ public final class CommonServer extends AbstractCommonServer<ICommonServerForInt
@Override
public void requestValidation(Object o)
{
if (o == null)
{
return;
}
if (!(o instanceof IEntityInformationWithPropertiesHolder))
{
throw new IllegalArgumentException(
"Would try to force validation if illegal object "
+ o.getClass());
}
objectsWhichValidationWouldBeForced
.add(((IEntityInformationWithPropertiesHolder) o)
.getIdentifier());
......
......@@ -209,29 +209,19 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
@Override
public void requestValidation(Object entity)
{
if (entity == null)
{
return;
}
if (false == entity instanceof IEntityInformationWithPropertiesHolder)
{
throw new IllegalArgumentException(
"Trying to force the validation of an object of invalid type "
+ entity.getClass());
}
if (validatedEntities.contains(entity) || newEntities.contains(entity)
|| modifiedEntities.contains(entity))
{
// forcing validation of entity already listed for validation
} else
{
IEntityInformationWithPropertiesHolder typedEntity =
(IEntityInformationWithPropertiesHolder) entity;
// we update modified entities to know that we will validate this entity
modifiedEntities.add((IEntityInformationWithPropertiesHolder) entity);
modifiedEntities.add(typedEntity);
// we add to the actual validation queue
entitiesToValidate.add((IEntityInformationWithPropertiesHolder) entity);
entitiesToValidate.add(typedEntity);
}
}
}
......@@ -19,6 +19,7 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calc
import ch.systemsx.cisd.common.evaluator.Evaluator;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor;
import ch.systemsx.cisd.openbis.generic.shared.calculator.AbstractCalculator;
import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityInformationWithPropertiesHolder;
/**
* @author Jakub Straszewski
......@@ -32,10 +33,10 @@ public class EntityValidationCalculator extends AbstractCalculator
private static final String INVOKE_CALCULATE_EXPR = "validate(" + ENTITY_VARIABLE_NAME + ", "
+ IS_NEW_ENTITY_VARIABLE_NAME + ")";
private static final String REQUEST_DELEGATE_VARIABLE = "__entityValidationRequestDelegate";
private static final String CALCULATOR_VARIABLE = "__calculator";
private static final String VALIDATION_REQUEST_FUNCTION = "def requestValidation(entity):\n "
+ REQUEST_DELEGATE_VARIABLE + ".requestValidation(entity)\n";
+ CALCULATOR_VARIABLE + ".requestValidation(entity)\n";
public interface IValidationRequestDelegate
{
......@@ -49,22 +50,47 @@ public class EntityValidationCalculator extends AbstractCalculator
* "isNewEntity"
*/
public static EntityValidationCalculator create(String expression,
IValidationRequestDelegate validationRequestedDelegate)
final IValidationRequestDelegate validationRequestedDelegate)
{
String initialScript = getBasicInitialScript();
initialScript += importFunctions(EntityValidationCalculator.class) + NEWLINE;
initialScript += VALIDATION_REQUEST_FUNCTION + NEWLINE;
initialScript += expression;
String calculatedExpression = INVOKE_CALCULATE_EXPR;
return new EntityValidationCalculator(new Evaluator(calculatedExpression, Math.class,
initialScript), validationRequestedDelegate);
}
public EntityValidationCalculator(Evaluator evaluator,
IValidationRequestDelegate validationRequested)
final IValidationRequestDelegate validationRequested)
{
super(evaluator);
evaluator.set(REQUEST_DELEGATE_VARIABLE, validationRequested);
// wrap the request validation with argument checking, so that the implementators of the
// interface can focus on logic
IValidationRequestDelegate wrappedValidationRequestedDelegate =
new IValidationRequestDelegate()
{
@Override
public void requestValidation(Object entity)
{
if (entity == null)
{
return;
}
if (false == entity instanceof IEntityInformationWithPropertiesHolder)
{
throw new IllegalArgumentException(
"Trying to force the validation of an object of invalid type "
+ entity.getClass());
}
validationRequested.requestValidation(entity);
}
};
evaluator.set(CALCULATOR_VARIABLE, wrappedValidationRequestedDelegate);
}
public void setEntity(IEntityAdaptor entity)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment