From 555736da33ec74c7b6f4af4a7427e1907a27c0d7 Mon Sep 17 00:00:00 2001
From: jakubs <jakubs>
Date: Wed, 12 Sep 2012 13:49:34 +0000
Subject: [PATCH] BIS-173 regain entities lost by clearing hibernate template
 in batch updates

SVN: 26615
---
 .../db/EntityValidationInterceptor.java       | 59 +++++++++++++++----
 .../plugin/generic/EntityValidationTest.java  | 12 +++-
 2 files changed, 58 insertions(+), 13 deletions(-)

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 3e2d6e7100a..de36230703d 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
@@ -37,8 +37,13 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calcu
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.EntityValidationCalculator;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.EntityValidationCalculator.IValidationRequestDelegate;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.dynamic_property.calculator.api.IEntityAdaptor;
+import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ServiceVersionHolder;
+import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.IEntityInformationWithPropertiesHolder;
+import ch.systemsx.cisd.openbis.generic.shared.dto.MaterialPE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.ScriptPE;
 
 /**
@@ -99,6 +104,11 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
 
     int entitiesValidatedCount;
 
+    private void updateListener()
+    {
+        progressListener = ServiceConversationsThreadContext.getProgressListener();
+    }
+
     @Override
     public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames,
             Type[] types)
@@ -112,11 +122,6 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
         return false;
     }
 
-    private void updateListener()
-    {
-        progressListener = ServiceConversationsThreadContext.getProgressListener();
-    }
-
     @Override
     public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
             Object[] previousState, String[] propertyNames, Type[] types)
@@ -156,6 +161,28 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
         }
     }
 
+    private IEntityInformationWithPropertiesHolder regainEntity(
+            IEntityInformationWithPropertiesHolder entity)
+    {
+        if (entity instanceof SamplePE)
+        {
+            return daoFactory.getSampleDAO().tryGetByTechId(TechId.create(entity));
+        } else if (entity instanceof DataPE)
+        {
+            return daoFactory.getDataDAO().tryGetByTechId(TechId.create(entity));
+        } else if (entity instanceof ExperimentPE)
+        {
+            return daoFactory.getExperimentDAO().tryGetByTechId(TechId.create(entity));
+        } else if (entity instanceof MaterialPE)
+        {
+            return daoFactory.getMaterialDAO().tryGetByTechId(TechId.create(entity));
+        } else
+        {
+            throw new IllegalArgumentException("Unsupported entity type " + entity.getClass());
+        }
+
+    }
+
     private void validateEntity(Transaction tx, IEntityInformationWithPropertiesHolder entity,
             boolean isNewEntity)
     {
@@ -163,7 +190,8 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
         ScriptPE validationScript = entity.getEntityType().getValidationScript();
         if (validationScript != null)
         {
-            validateEntityWithScript(tx, validationScript, entity, isNewEntity);
+            IEntityInformationWithPropertiesHolder regained = regainEntity(entity);
+            validateEntityWithScript(tx, validationScript, regained, isNewEntity);
         }
     }
 
@@ -212,22 +240,30 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
 
     private void newEntity(IEntityInformationWithPropertiesHolder entity)
     {
-        totalEntitiesToValidateCount++;
-        newEntities.add(entity);
+        if (newEntities.add(entity))
+        {
+            totalEntitiesToValidateCount++;
+        }
     }
 
     private void validatedEntity(IEntityInformationWithPropertiesHolder entity)
     {
         entitiesValidatedCount++;
-        validatedEntities.add(entity);
+        if (false == validatedEntities.add(entity))
+        {
+            throw new IllegalStateException(
+                    "Programming error - trying to validate the same entity twice!");
+        }
     }
 
     private void modifiedEntity(IEntityInformationWithPropertiesHolder entity)
     {
         if (false == newEntities.contains(entity))
         {
-            totalEntitiesToValidateCount++;
-            modifiedEntities.add(entity);
+            if (modifiedEntities.add(entity))
+            {
+                totalEntitiesToValidateCount++;
+            }
         }
     }
 
@@ -255,6 +291,7 @@ public class EntityValidationInterceptor extends EmptyInterceptor implements
             modifiedEntities.add(typedEntity);
             // we add to the actual validation queue
             entitiesToValidate.add(typedEntity);
+            totalEntitiesToValidateCount++;
         }
     }
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityValidationTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityValidationTest.java
index d8154735348..d4fb931ffac 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityValidationTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/plugin/generic/EntityValidationTest.java
@@ -163,13 +163,21 @@ public class EntityValidationTest extends GenericSystemTestCase
     }
 
     @Test
-    public void testPerformEntityOperation()
+    public void testRegisterSampleWithPerformEntityOperation()
     {
-        NewSample sample = prepareNewSample("/TEST-SPACE/NEV-TEST", "NORMAL", null);
+        NewSample sample = prepareNewSample("/TEST-SPACE/NEV-TEST-PE", "NORMAL", null);
         sample.setParents("EV-PARENT-NORMAL");
         performSampleCreation(sample);
     }
 
+    @Test
+    public void testRegisterSampleWithETL()
+    {
+        NewSample sample = prepareNewSample("/TEST-SPACE/NEV-TEST_ETL", "NORMAL", null);
+        sample.setParents("EV-PARENT-NORMAL");
+        etlService.registerSample(systemSessionToken, sample, null);
+    }
+
     private void performSampleCreation(NewSample sampleToCreate)
     {
         List<NewSpace> spaceRegistrations = Collections.emptyList();
-- 
GitLab