From 97e2857332a916fbe6981385ee4dbc47cfae3782 Mon Sep 17 00:00:00 2001
From: jakubs <jakubs>
Date: Thu, 6 Sep 2012 14:28:55 +0000
Subject: [PATCH] BIS-173 refactor checking arguments of validateEntity
 function to an interceptor

SVN: 26533
---
 .../openbis/generic/server/CommonServer.java  | 12 -------
 .../db/EntityValidationInterceptor.java       | 20 +++--------
 .../EntityValidationCalculator.java           | 36 ++++++++++++++++---
 3 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java
index c01b9a07f10..1f08764ec9b 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java
@@ -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());
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java
index 84bc81bad25..0fea59db33c 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EntityValidationInterceptor.java
@@ -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);
         }
     }
-
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityValidationCalculator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityValidationCalculator.java
index 16a64f31299..908e39f1ecd 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityValidationCalculator.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/dynamic_property/calculator/EntityValidationCalculator.java
@@ -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)
-- 
GitLab