From 263edfde3642bbc918f360a69d50d24085251d7b Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Thu, 26 May 2016 08:06:11 +0000
Subject: [PATCH] SSDM-3543 : V3 AS API - improve context information in
 exceptions - cover setting data set linked data and physical data

SVN: 36584
---
 .../dataset/SetDataSetLinkedDataExecutor.java | 47 +++++++++++--------
 .../SetDataSetPhysicalDataExecutor.java       | 47 +++++++++++--------
 .../asapi/v3/AbstractSampleTest.java          | 14 ------
 .../systemtest/asapi/v3/AbstractTest.java     | 14 ++++++
 .../asapi/v3/CreateDataSetTest.java           |  4 +-
 5 files changed, 72 insertions(+), 54 deletions(-)

diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetLinkedDataExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetLinkedDataExecutor.java
index b8dd2494fa4..f5a0297dbd4 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetLinkedDataExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetLinkedDataExecutor.java
@@ -16,15 +16,16 @@
 
 package ch.ethz.sis.openbis.generic.server.asapi.v3.executor.dataset;
 
-import java.util.Map;
-
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.create.DataSetCreation;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.create.LinkedDataCreation;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.context.IProgress;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.MapBatch;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.MapBatchProcessor;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.entity.progress.SetRelationProgress;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.LinkDataPE;
@@ -40,29 +41,37 @@ public class SetDataSetLinkedDataExecutor implements ISetDataSetLinkedDataExecut
     private ISetDataSetExternalDmsExecutor setDataSetExternalDmsExecutor;
 
     @Override
-    public void set(IOperationContext context, MapBatch<DataSetCreation, DataPE> batch)
+    public void set(final IOperationContext context, final MapBatch<DataSetCreation, DataPE> batch)
     {
-        for (Map.Entry<DataSetCreation, DataPE> entry : batch.getObjects().entrySet())
-        {
-            DataSetCreation creation = entry.getKey();
-            LinkedDataCreation linkedCreation = creation.getLinkedData();
-            DataPE entity = entry.getValue();
-
-            if (entity instanceof LinkDataPE)
+        new MapBatchProcessor<DataSetCreation, DataPE>(context, batch)
             {
-                if (linkedCreation == null)
+                @Override
+                public void process(DataSetCreation creation, DataPE entity)
                 {
-                    throw new UserFailureException("Linked data cannot be null for a link data set.");
+                    LinkedDataCreation linkedCreation = creation.getLinkedData();
+
+                    if (entity instanceof LinkDataPE)
+                    {
+                        if (linkedCreation == null)
+                        {
+                            throw new UserFailureException("Linked data cannot be null for a link data set.");
+                        }
+                        set(context, linkedCreation, (LinkDataPE) entity);
+                    } else
+                    {
+                        if (linkedCreation != null)
+                        {
+                            throw new UserFailureException("Linked data cannot be set for a non-link data set.");
+                        }
+                    }
                 }
-                set(context, linkedCreation, (LinkDataPE) entity);
-            } else
-            {
-                if (linkedCreation != null)
+
+                @Override
+                public IProgress createProgress(DataSetCreation creation, DataPE entity, int objectIndex, int totalObjectCount)
                 {
-                    throw new UserFailureException("Linked data cannot be set for a non-link data set.");
+                    return new SetRelationProgress(entity, creation, "dataset-linkeddata", objectIndex, totalObjectCount);
                 }
-            }
-        }
+            };
 
         setDataSetExternalDmsExecutor.set(context, batch);
     }
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetPhysicalDataExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetPhysicalDataExecutor.java
index b313394b0e3..e4118bebf2c 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetPhysicalDataExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/dataset/SetDataSetPhysicalDataExecutor.java
@@ -16,16 +16,17 @@
 
 package ch.ethz.sis.openbis.generic.server.asapi.v3.executor.dataset;
 
-import java.util.Map;
-
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.Complete;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.create.DataSetCreation;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.create.PhysicalDataCreation;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.context.IProgress;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.MapBatch;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.common.batch.MapBatchProcessor;
+import ch.ethz.sis.openbis.generic.server.asapi.v3.helper.entity.progress.SetRelationProgress;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.common.types.BooleanOrUnknown;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE;
@@ -48,29 +49,37 @@ public class SetDataSetPhysicalDataExecutor implements ISetDataSetPhysicalDataEx
     private ISetDataSetLocatorTypeExecutor setDataSetLocatorTypeExecutor;
 
     @Override
-    public void set(IOperationContext context, MapBatch<DataSetCreation, DataPE> batch)
+    public void set(final IOperationContext context, final MapBatch<DataSetCreation, DataPE> batch)
     {
-        for (Map.Entry<DataSetCreation, DataPE> entry : batch.getObjects().entrySet())
-        {
-            DataSetCreation creation = entry.getKey();
-            PhysicalDataCreation physicalCreation = creation.getPhysicalData();
-            DataPE entity = entry.getValue();
-
-            if (entity instanceof ExternalDataPE)
+        new MapBatchProcessor<DataSetCreation, DataPE>(context, batch)
             {
-                if (physicalCreation == null)
+                @Override
+                public void process(DataSetCreation creation, DataPE entity)
                 {
-                    throw new UserFailureException("Physical data cannot be null for a physical data set.");
+                    PhysicalDataCreation physicalCreation = creation.getPhysicalData();
+
+                    if (entity instanceof ExternalDataPE)
+                    {
+                        if (physicalCreation == null)
+                        {
+                            throw new UserFailureException("Physical data cannot be null for a physical data set.");
+                        }
+                        set(context, physicalCreation, (ExternalDataPE) entity);
+                    } else
+                    {
+                        if (physicalCreation != null)
+                        {
+                            throw new UserFailureException("Physical data cannot be set for a non-physical data set.");
+                        }
+                    }
                 }
-                set(context, physicalCreation, (ExternalDataPE) entity);
-            } else
-            {
-                if (physicalCreation != null)
+
+                @Override
+                public IProgress createProgress(DataSetCreation creation, DataPE entity, int objectIndex, int totalObjectCount)
                 {
-                    throw new UserFailureException("Physical data cannot be set for a non-physical data set.");
+                    return new SetRelationProgress(entity, creation, "dataset-physicaldata", objectIndex, totalObjectCount);
                 }
-            }
-        }
+            };
 
         setDataSetStorageFormatExecutor.set(context, batch);
         setDataSetFileFormatTypeExecutor.set(context, batch);
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractSampleTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractSampleTest.java
index 16bb75cea84..f453bd72e35 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractSampleTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractSampleTest.java
@@ -1,7 +1,5 @@
 package ch.ethz.sis.openbis.systemtest.asapi.v3;
 
-import java.util.regex.Pattern;
-
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.id.EntityTypePermId;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.create.SampleCreation;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.id.SpacePermId;
@@ -50,16 +48,4 @@ public class AbstractSampleTest extends AbstractTest
         commonServer.registerAndAssignPropertyType(sessionToken, propertyType, assignment);
     }
 
-    protected static String patternContains(String... parts)
-    {
-        StringBuilder pattern = new StringBuilder();
-        pattern.append(".*");
-        for (String part : parts)
-        {
-            pattern.append(Pattern.quote(part));
-            pattern.append(".*");
-        }
-        return pattern.toString();
-    }
-
 }
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractTest.java
index 7cd3e92ed26..65683ab353b 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/AbstractTest.java
@@ -36,6 +36,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
@@ -994,5 +995,18 @@ public class AbstractTest extends SystemTestCase
 
         assertCollectionContainsOnly(actualIds, expectedIds.toArray(new Long[] {}));
     }
+    
+    protected static String patternContains(String... parts)
+    {
+        StringBuilder pattern = new StringBuilder();
+        pattern.append(".*");
+        for (String part : parts)
+        {
+            pattern.append(Pattern.quote(part));
+            pattern.append(".*");
+        }
+        return pattern.toString();
+    }
+
 
 }
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/CreateDataSetTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/CreateDataSetTest.java
index 990be82ec3e..520dc06c665 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/CreateDataSetTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/CreateDataSetTest.java
@@ -1057,7 +1057,7 @@ public class CreateDataSetTest extends AbstractDataSetTest
                 {
                     createDataSet(sessionToken, creation, new DataSetFetchOptions());
                 }
-            }, "Linked data cannot be set for a non-link data set.");
+            }, "Linked data cannot be set for a non-link data set.", patternContains("setting relation dataset-linkeddata (1/1)"));
     }
 
     @Test
@@ -1075,7 +1075,7 @@ public class CreateDataSetTest extends AbstractDataSetTest
                 {
                     createDataSet(sessionToken, creation, new DataSetFetchOptions());
                 }
-            }, "Physical data cannot be null for a physical data set.");
+            }, "Physical data cannot be null for a physical data set.", patternContains("setting relation dataset-physicaldata (1/1)"));
     }
 
     @Test
-- 
GitLab