From 2aa8d54390a67c21a9cad1dd32da271d678aaa13 Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Tue, 14 Dec 2010 14:57:27 +0000
Subject: [PATCH] LMS-1909 Fixed minor inconsistencies in the Sample/Data Set
 registration processes. Fixed tests.

SVN: 19140
---
 .../SampleAndDataSetControlFileProcessor.java | 41 +------------------
 .../SampleAndDataSetRegistrator.java          | 30 ++++++++++++++
 .../entityregistration/SampleDataSetPair.java | 15 +++----
 ...mpleAndDatasetRegistrationHandlerTest.java | 10 ++---
 .../test-data/basic-example/control.tsv       |  9 ++--
 .../test-data/basic-example/ds1/dataset.txt   |  0
 .../test-data/basic-example/ds2/dataset.txt   |  0
 .../test-data/basic-example/ds3/dataset.txt   |  0
 8 files changed, 48 insertions(+), 57 deletions(-)
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds1/dataset.txt
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds2/dataset.txt
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds3/dataset.txt

diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetControlFileProcessor.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetControlFileProcessor.java
index 4284e2124b0..24403c55957 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetControlFileProcessor.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetControlFileProcessor.java
@@ -37,8 +37,6 @@ import ch.systemsx.cisd.common.utilities.UnicodeUtils;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetType;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Person;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType;
-import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.DatabaseInstanceIdentifier;
-import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SpaceIdentifier;
 import ch.systemsx.cisd.openbis.generic.shared.parser.BisTabFileLoader;
 import ch.systemsx.cisd.openbis.generic.shared.parser.GlobalProperties;
 import ch.systemsx.cisd.openbis.generic.shared.parser.GlobalPropertiesLoader;
@@ -74,21 +72,6 @@ class SampleAndDataSetControlFileProcessor extends AbstractSampleAndDataSetProce
             properties = GlobalPropertiesLoader.load(controlFile);
         }
 
-        public String trySpaceCode()
-        {
-            return properties.get(SampleAndDataSetRegistrationHandler.DATA_SPACE_CONTROL_FILE_KEY);
-        }
-
-        public SpaceIdentifier trySpaceIdentifier()
-        {
-            String spaceCode = trySpaceCode();
-            if (null == spaceCode)
-            {
-                return null;
-            }
-            return new SpaceIdentifier(DatabaseInstanceIdentifier.createHome(), spaceCode);
-        }
-
         public SampleType trySampleType()
         {
             String sampleTypeCode =
@@ -168,19 +151,6 @@ class SampleAndDataSetControlFileProcessor extends AbstractSampleAndDataSetProce
             return globalProperties;
         }
 
-        /**
-         * Returns a space identifier. Call checkValidity first.
-         */
-        public SpaceIdentifier getSpaceIdentifier()
-        {
-            SpaceIdentifier spaceIdentifier = overrideProperties.trySpaceIdentifier();
-            if (null == spaceIdentifier)
-            {
-                spaceIdentifier = globalProperties.trySpaceIdentifier();
-            }
-            return spaceIdentifier;
-        }
-
         public SampleType getSampleType()
         {
             SampleType sampleType = overrideProperties.trySampleType();
@@ -208,18 +178,12 @@ class SampleAndDataSetControlFileProcessor extends AbstractSampleAndDataSetProce
 
         public void checkValidity()
         {
-            SpaceIdentifier spaceIdentifier = getSpaceIdentifier();
             SampleType sampleType = getSampleType();
             DataSetType dataSetType = getDataSetType();
             Person theUser = getUser();
 
             StringBuilder sb = new StringBuilder();
             boolean hasError = false;
-            if (null == spaceIdentifier)
-            {
-                hasError = true;
-                sb.append("\tNo default space identifier has been specified, and no space identifier was specified in the control file.");
-            }
             if (null == sampleType)
             {
                 hasError = true;
@@ -340,10 +304,9 @@ class SampleAndDataSetControlFileProcessor extends AbstractSampleAndDataSetProce
     {
         String message =
                 String.format(
-                        "Global properties extracted from file '%s': SAMPLE_TYPE(%s) DATA_SET_TYPE(%s) DEFAULT_SPACE(%s) USER(%s)",
+                        "Global properties extracted from file '%s': SAMPLE_TYPE(%s) DATA_SET_TYPE(%s) USER(%s)",
                         controlFile.getName(), properties.trySampleType(),
-                        properties.tryDataSetType(), properties.trySpaceCode(),
-                        properties.tryUserString());
+                        properties.tryDataSetType(), properties.tryUserString());
         logInfo(message);
     }
 
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetRegistrator.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetRegistrator.java
index dbd23f6024d..b6ce51d9b92 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetRegistrator.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDataSetRegistrator.java
@@ -18,10 +18,12 @@ package ch.systemsx.cisd.etlserver.entityregistration;
 
 import java.io.File;
 
+import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.etlserver.IExtensibleDataSetHandler;
 import ch.systemsx.cisd.etlserver.TransferredDataSetHandler;
 import ch.systemsx.cisd.etlserver.entityregistration.SampleAndDataSetControlFileProcessor.ControlFileRegistrationProperties;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.FileFormatType;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
 import ch.systemsx.cisd.openbis.generic.shared.dto.NewExternalData;
 
@@ -58,6 +60,12 @@ class SampleAndDataSetRegistrator extends AbstractSampleAndDataSetProcessor impl
     public Exception register()
     {
         File dataSetFile = new File(folder, sampleDataSetPair.getFolderName());
+        Exception isEmptyException = checkDataSetFileNotEmpty(dataSetFile);
+        if (null != isEmptyException)
+        {
+            return isEmptyException;
+        }
+
         if (globalState.getDelegator() instanceof IExtensibleDataSetHandler)
         {
             IExtensibleDataSetHandler handler =
@@ -71,6 +79,15 @@ class SampleAndDataSetRegistrator extends AbstractSampleAndDataSetProcessor impl
         return failureException;
     }
 
+    private Exception checkDataSetFileNotEmpty(File dataSetFile)
+    {
+        if (0 == dataSetFile.list().length)
+        {
+            return new UserFailureException("The data set folder cannot be empty");
+        }
+        return null;
+    }
+
     private void logDataRegistered()
     {
         String message = String.format("Registered sample/data set pair %s", sampleDataSetPair);
@@ -79,6 +96,14 @@ class SampleAndDataSetRegistrator extends AbstractSampleAndDataSetProcessor impl
 
     public void registerDataSetInApplicationServer(NewExternalData data)
     {
+        data.setDataSetType(properties.getDataSetType());
+        if (null != sampleDataSetPair.getFileFormatTypeCode())
+        {
+            FileFormatType fileFormatType =
+                    new FileFormatType(sampleDataSetPair.getFileFormatTypeCode());
+
+            data.setFileFormatType(fileFormatType);
+        }
         try
         {
             Sample sample =
@@ -93,6 +118,11 @@ class SampleAndDataSetRegistrator extends AbstractSampleAndDataSetProcessor impl
             didSucceed = false;
             failureException = e;
             throw e;
+        } catch (Exception e)
+        {
+            didSucceed = false;
+            failureException = e;
+            throw new CheckedExceptionTunnel(e);
         }
     }
 
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleDataSetPair.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleDataSetPair.java
index 32a30518f52..b94344a616a 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleDataSetPair.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/entityregistration/SampleDataSetPair.java
@@ -44,8 +44,6 @@ public class SampleDataSetPair
 
     private final static String DATA_SET_CODE = "D_code";
 
-    private final static String DATA_SET_TYPE = "D_data_set_type";
-
     private final static String DATA_SET_FILE_TYPE = "D_file_type";
 
     private final static String FOLDER = "FOLDER";
@@ -58,6 +56,8 @@ public class SampleDataSetPair
 
     private String folderName;
 
+    private String fileFormatTypeCode;
+
     public SampleDataSetPair()
     {
         newSample = new NewSample();
@@ -84,7 +84,7 @@ public class SampleDataSetPair
         return dataSetInformation;
     }
 
-    @BeanProperty(label = SAMPLE_IDENTIFIER, optional = true)
+    @BeanProperty(label = SAMPLE_IDENTIFIER, optional = false)
     public void setSampleIdentifier(String sampleIdentifier)
     {
         this.newSample.setIdentifier(sampleIdentifier);
@@ -114,14 +114,15 @@ public class SampleDataSetPair
         dataSetInformation.setDataSetCode(code);
     }
 
-    @BeanProperty(label = DATA_SET_TYPE, optional = true)
-    public void setDataSetType(String parent)
+    public String getFileFormatTypeCode()
     {
+        return fileFormatTypeCode;
     }
 
     @BeanProperty(label = DATA_SET_FILE_TYPE, optional = true)
-    public void setDataSetFileType(String experimentIdentifier)
+    public void setFileFormatTypeCode(String fileFormatType)
     {
+        this.fileFormatTypeCode = fileFormatType;
     }
 
     public String getFolderName()
@@ -129,7 +130,7 @@ public class SampleDataSetPair
         return folderName;
     }
 
-    @BeanProperty(label = FOLDER, optional = true)
+    @BeanProperty(label = FOLDER, optional = false)
     public void setFolderName(String folderName)
     {
         this.folderName = folderName;
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDatasetRegistrationHandlerTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDatasetRegistrationHandlerTest.java
index ff71b372cc3..40c4414e760 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDatasetRegistrationHandlerTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/SampleAndDatasetRegistrationHandlerTest.java
@@ -95,8 +95,6 @@ public class SampleAndDatasetRegistrationHandlerTest extends AbstractFileSystemT
 
         setupOpenBisExpectations();
         setupDataSetHandlerExpectations(dataSetInfoMatcher, newSampleMatcher);
-        // setupSessionContextExpectations();
-        // setupCallerDataSetInfoExpectations();
 
         File workingCopy = createWorkingCopyOfTestFolder("basic-example");
 
@@ -104,10 +102,10 @@ public class SampleAndDatasetRegistrationHandlerTest extends AbstractFileSystemT
         handler.handleDataSet(workingCopy);
 
         String logText =
-                "Global properties extracted from file 'control.tsv': SAMPLE_TYPE(MY_SAMPLE_TYPE) DATA_SET_TYPE(MY_DATA_SET_TYPE) DEFAULT_SPACE(MYSPACE) USER(test@test.test)\n"
-                        + "Registered sample/data set pair SampleDataSetPair[sampleIdentifier=<null>,sampleProperties={prop1: VAL10,prop2: VAL20,prop3: VAL30},dataSetInformation=DataSetInformation{sampleCode=<null>,properties={},dataSetType=MY_DATA_SET_TYPE,instanceUUID=<null>,instanceCode=<null>,spaceCode=<null>,experimentIdentifier=<null>,isCompleteFlag=U,extractableData=ExtractableData{productionDate=<null>,dataProducerCode=<null>,parentDataSetCodes=[],dataSetProperties=[NewProperty{property=prop1,value=VAL40}, NewProperty{property=prop2,value=VAL50}],code=<null>},uploadingUserEmailOrNull=<null>,uploadingUserIdOrNull=test}]\n"
-                        + "Registered sample/data set pair SampleDataSetPair[sampleIdentifier=<null>,sampleProperties={prop1: VAL11,prop2: VAL21,prop3: VAL31},dataSetInformation=DataSetInformation{sampleCode=<null>,properties={},dataSetType=MY_DATA_SET_TYPE,instanceUUID=<null>,instanceCode=<null>,spaceCode=<null>,experimentIdentifier=<null>,isCompleteFlag=U,extractableData=ExtractableData{productionDate=<null>,dataProducerCode=<null>,parentDataSetCodes=[],dataSetProperties=[NewProperty{property=prop1,value=VAL41}, NewProperty{property=prop2,value=VAL51}],code=<null>},uploadingUserEmailOrNull=<null>,uploadingUserIdOrNull=test}]\n"
-                        + "Registered sample/data set pair SampleDataSetPair[sampleIdentifier=<null>,sampleProperties={prop1: VAL12,prop2: VAL22,prop3: VAL32},dataSetInformation=DataSetInformation{sampleCode=<null>,properties={},dataSetType=MY_DATA_SET_TYPE,instanceUUID=<null>,instanceCode=<null>,spaceCode=<null>,experimentIdentifier=<null>,isCompleteFlag=U,extractableData=ExtractableData{productionDate=<null>,dataProducerCode=<null>,parentDataSetCodes=[],dataSetProperties=[NewProperty{property=prop1,value=VAL42}, NewProperty{property=prop2,value=VAL52}],code=<null>},uploadingUserEmailOrNull=<null>,uploadingUserIdOrNull=test}]";
+                "Global properties extracted from file 'control.tsv': SAMPLE_TYPE(MY_SAMPLE_TYPE) DATA_SET_TYPE(MY_DATA_SET_TYPE) USER(test@test.test)\n"
+                        + "Registered sample/data set pair SampleDataSetPair[sampleIdentifier=/MYSPACE/S1,sampleProperties={prop1: VAL10,prop2: VAL20,prop3: VAL30},dataSetInformation=DataSetInformation{sampleCode=<null>,properties={},dataSetType=MY_DATA_SET_TYPE,instanceUUID=<null>,instanceCode=<null>,spaceCode=<null>,experimentIdentifier=<null>,isCompleteFlag=U,extractableData=ExtractableData{productionDate=<null>,dataProducerCode=<null>,parentDataSetCodes=[],dataSetProperties=[NewProperty{property=prop1,value=VAL40}, NewProperty{property=prop2,value=VAL50}],code=<null>},uploadingUserEmailOrNull=<null>,uploadingUserIdOrNull=test}]\n"
+                        + "Registered sample/data set pair SampleDataSetPair[sampleIdentifier=/MYSPACE/S2,sampleProperties={prop1: VAL11,prop2: VAL21,prop3: VAL31},dataSetInformation=DataSetInformation{sampleCode=<null>,properties={},dataSetType=MY_DATA_SET_TYPE,instanceUUID=<null>,instanceCode=<null>,spaceCode=<null>,experimentIdentifier=<null>,isCompleteFlag=U,extractableData=ExtractableData{productionDate=<null>,dataProducerCode=<null>,parentDataSetCodes=[],dataSetProperties=[NewProperty{property=prop1,value=VAL41}, NewProperty{property=prop2,value=VAL51}],code=<null>},uploadingUserEmailOrNull=<null>,uploadingUserIdOrNull=test}]\n"
+                        + "Registered sample/data set pair SampleDataSetPair[sampleIdentifier=/MYSPACE/S3,sampleProperties={prop1: VAL12,prop2: VAL22,prop3: VAL32},dataSetInformation=DataSetInformation{sampleCode=<null>,properties={},dataSetType=MY_DATA_SET_TYPE,instanceUUID=<null>,instanceCode=<null>,spaceCode=<null>,experimentIdentifier=<null>,isCompleteFlag=U,extractableData=ExtractableData{productionDate=<null>,dataProducerCode=<null>,parentDataSetCodes=[],dataSetProperties=[NewProperty{property=prop1,value=VAL42}, NewProperty{property=prop2,value=VAL52}],code=<null>},uploadingUserEmailOrNull=<null>,uploadingUserIdOrNull=test}]";
         checkAppenderContent(logText, "basic-example");
 
         context.assertIsSatisfied();
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/control.tsv b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/control.tsv
index 22f20fc3b70..a8d70ccf592 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/control.tsv
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/control.tsv
@@ -1,12 +1,11 @@
 # Control Parameters
 #! GLOBAL_PROPERTIES_START
-#!   DEFAULT_SPACE = MYSPACE
 #!   SAMPLE_TYPE = MY_SAMPLE_TYPE
 #!   DATA_SET_TYPE = MY_DATA_SET_TYPE
 #!   USERID = test@test.test
 #! GLOBAL_PROPERTIES_END
 # Data
-S_experiment	S_PROP1	S_PROP2	S_PROP3	D_file_type	D_PROP1	D_PROP2	FOLDER
-/MYSPACE/MYPROJ/EXP1	VAL10	VAL20	VAL30	FILE_TYPE	VAL40	VAL50	ds1/
-/MYSPACE/MYPROJ/EXP2	VAL11	VAL21	VAL31	FILE_TYPE	VAL41	VAL51	ds2/
-/MYSPACE/MYPROJ/EXP3	VAL12	VAL22	VAL32	FILE_TYPE	VAL42	VAL52	ds3/
+S_identifier	S_experiment	S_PROP1	S_PROP2	S_PROP3	D_file_type	D_PROP1	D_PROP2	FOLDER
+/MYSPACE/S1	/MYSPACE/MYPROJ/EXP1	VAL10	VAL20	VAL30	FILE_TYPE	VAL40	VAL50	ds1/
+/MYSPACE/S2	/MYSPACE/MYPROJ/EXP2	VAL11	VAL21	VAL31	FILE_TYPE	VAL41	VAL51	ds2/
+/MYSPACE/S3	/MYSPACE/MYPROJ/EXP3	VAL12	VAL22	VAL32	FILE_TYPE	VAL42	VAL52	ds3/
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds1/dataset.txt b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds1/dataset.txt
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds2/dataset.txt b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds2/dataset.txt
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds3/dataset.txt b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/entityregistration/test-data/basic-example/ds3/dataset.txt
new file mode 100644
index 00000000000..e69de29bb2d
-- 
GitLab