From e8a738f96f249c8d69f6b9749c5f7776fa7a6ed4 Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Wed, 13 Oct 2010 08:11:26 +0000
Subject: [PATCH] LMS-1811 Implementation of new handlers for CINA.

SVN: 18280
---
 rtd_cina/resource/db/cina_metadata.sql        |  12 +-
 .../dss/bundle/CinaBundleDataSetHandler.java  |   4 +-
 .../registrators/BundleDataSetHelper.java     |  83 +++++--
 .../GridPreparationRegistrator.java           |   2 +-
 .../ReplicaAnnotatedImagesRegistrator.java    |  48 +++-
 .../ReplicaMetadataRegistrator.java           |  71 +++++-
 .../ReplicaRawImagesRegistrator.java          |  72 ++++++
 .../registrators/ReplicaRegistrator.java      |  77 ++++++-
 .../cina/shared/constants/CinaConstants.java  |   6 +
 .../metadata/BundleStructureConstants.java    |  69 ++++++
 .../metadata/IMetadataExtractor.java}         |  19 +-
 .../metadata/ImageMetadataExtractor.java      |   2 +-
 .../metadata/ReplicaMetadataExtractor.java    |   8 +-
 .../GridPreparationRegistratorTest.java       | 205 ++++++++++++------
 14 files changed, 545 insertions(+), 133 deletions(-)
 create mode 100644 rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRawImagesRegistrator.java
 create mode 100644 rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/BundleStructureConstants.java
 rename rtd_cina/source/java/ch/systemsx/cisd/cina/{dss/bundle/registrators/ReplicaOriginalImagesRegistrator.java => shared/metadata/IMetadataExtractor.java} (60%)

diff --git a/rtd_cina/resource/db/cina_metadata.sql b/rtd_cina/resource/db/cina_metadata.sql
index 9f8e6fd3bfb..8248f296549 100644
--- a/rtd_cina/resource/db/cina_metadata.sql
+++ b/rtd_cina/resource/db/cina_metadata.sql
@@ -18,7 +18,7 @@ SET search_path = public, pg_catalog;
 -- Dependencies: 1704
 -- Data for Name: experiment_types; Type: TABLE DATA; Schema: public; Owner: cramakri
 --
-INSERT INTO experiment_types (id, code, description, dbin_id, modification_timestamp) VALUES (1, 'CINA_EXP_TYPE', 'CINA Generic Experiment Type', 1, '2010-03-18 08:37:38.173356+01');
+INSERT INTO experiment_types (id, code, description, dbin_id, modification_timestamp) VALUES (1, 'CINA_EXP_TYPE', 'Generic Experiment Type', 1, '2010-03-18 08:37:38.173356+01');
 -- Completed on 2010-08-18 10:19:08 CEST
 
 --
@@ -27,8 +27,8 @@ INSERT INTO experiment_types (id, code, description, dbin_id, modification_times
 -- Data for Name: sample_types; Type: TABLE DATA; Schema: public; Owner: cramakri
 --
 INSERT INTO sample_types (id, code, description, dbin_id, is_listable, generated_from_depth, part_of_depth, modification_timestamp, is_auto_generated_code, generated_code_prefix, is_subcode_unique) VALUES (1, 'ORIGINAL', 'The sample for an experiment', 1, true, 1, 0, '2010-03-18 10:09:48.073644+01', false, 'ORIGINAL', false);
+INSERT INTO sample_types (id, code, description, dbin_id, is_listable, generated_from_depth, part_of_depth, modification_timestamp, is_auto_generated_code, generated_code_prefix, is_subcode_unique) VALUES (2, 'GRID_PREP', 'Grid biochemistry and preparation', 1, true, 1, 0, '2010-06-04 14:44:53.706263+02', false, 'EM-PREP', false);
 INSERT INTO sample_types (id, code, description, dbin_id, is_listable, generated_from_depth, part_of_depth, modification_timestamp, is_auto_generated_code, generated_code_prefix, is_subcode_unique) VALUES (3, 'GRID_REPLICA', 'A replica of a grid template', 1, true, 1, 0, '2010-06-04 14:46:38.886039+02', false, 'REPLICA', false);
-INSERT INTO sample_types (id, code, description, dbin_id, is_listable, generated_from_depth, part_of_depth, modification_timestamp, is_auto_generated_code, generated_code_prefix, is_subcode_unique) VALUES (2, 'GRID_TEMPLATE', 'Grid biochemistry and preparation', 1, true, 1, 0, '2010-06-04 14:44:53.706263+02', false, 'EM-PREP', false);
 -- Completed on 2010-08-18 10:13:10 CEST
 
 --
@@ -37,9 +37,11 @@ INSERT INTO sample_types (id, code, description, dbin_id, is_listable, generated
 -- Data for Name: data_set_types; Type: TABLE DATA; Schema: public; Owner: cramakri
 --
 INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (1, 'UNKNOWN', 'Unknown', 1, '2010-03-18 08:33:32.470674+01', NULL, NULL);
-INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (3, 'BUNDLE', 'CINA Bundle Data Set', 1, '2010-06-04 15:40:58.882161+02', '.*.bundle', NULL);
-INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (5, 'ANALYSIS', 'CINA Analysis', 1, '2010-06-04 16:07:04.556718+02', NULL, NULL);
-INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (4, 'IMAGE', 'CINA Image', 1, '2010-06-04 16:06:40.980157+02', '.*.png', 'original/Representations');
+INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (2, 'BUNDLE', 'Bundle Data Set', 1, '2010-06-04 15:40:58.882161+02', '.*.bundle', NULL);
+INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (3, 'RAW_IMAGES', 'Raw Images', 1, '2010-06-04 16:08:40.980157+02', NULL, NULL);
+INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (4, 'METADATA', 'Metadata', 1, '2010-06-04 16:08:40.980157+02', 'metadata.xml', 'original');
+INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (5, 'IMAGE', 'Image', 1, '2010-06-04 16:06:40.980157+02', '.*.png', 'original/Representations');
+INSERT INTO data_set_types (id, code, description, dbin_id, modification_timestamp, main_ds_pattern, main_ds_path) VALUES (6, 'ANALYSIS', 'Analysis', 1, '2010-06-04 16:07:04.556718+02', NULL, NULL);
 -- Completed on 2010-08-18 10:19:39 CEST
 
 
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandler.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandler.java
index a3312a48e3c..fe4ce92f5d6 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandler.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandler.java
@@ -58,7 +58,7 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType;
  * <li>Individual Image Thumbnails</li>
  * </ul>
  * </li>
- * <li>Replica Original Images</li>
+ * <li>Replica Raw Images</li>
  * </ul>
  * </li>
  * </ul>
@@ -68,7 +68,7 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType;
  * <li>Create a grid preparation sample if one does not exist</li>
  * <li>For each replica:
  * <ul>
- * <li>If the replica sample does not exist, create it and register an original images data set</li>
+ * <li>If the replica sample does not exist, create it and register a raw images data set</li>
  * <li>Create a metadata data set and associate it with the replica; update the replica's metadata</li>
  * <li>Create datasets for each of the annotated images in the data set</li>
  * </ul>
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/BundleDataSetHelper.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/BundleDataSetHelper.java
index e3fa02d22d4..bf184665e60 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/BundleDataSetHelper.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/BundleDataSetHelper.java
@@ -20,11 +20,15 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
+import ch.systemsx.cisd.cina.shared.constants.CinaConstants;
+import ch.systemsx.cisd.cina.shared.metadata.IMetadataExtractor;
 import ch.systemsx.cisd.etlserver.IDataSetHandlerRpc;
 import ch.systemsx.cisd.openbis.dss.generic.shared.IEncapsulatedOpenBISService;
 import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetTypeWithVocabularyTerms;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.PropertyTypeWithVocabulary;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SessionContextDTO;
 
 /**
@@ -34,6 +38,8 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.SessionContextDTO;
  */
 abstract class BundleDataSetHelper
 {
+    private static final String MISC_DATA_SET_PROPERTY_CODE = "MISC";
+
     static class BundleRegistrationState
     {
         private final IDataSetHandlerRpc delegator;
@@ -42,19 +48,32 @@ abstract class BundleDataSetHelper
 
         private final IEncapsulatedOpenBISService openbisService;
 
+        private final SampleType gridPrepSampleType;
+
         private final SampleType replicaSampleType;
 
+        private final DataSetTypeWithVocabularyTerms rawImagesDataSetType;
+
+        private final DataSetTypeWithVocabularyTerms metadataDataSetType;
+
         private final DataSetTypeWithVocabularyTerms imageDataSetType;
 
         BundleRegistrationState(IDataSetHandlerRpc delegator,
-                IEncapsulatedOpenBISService openbisService, SampleType replicaSampleType,
-                DataSetTypeWithVocabularyTerms imageDataSetType)
+                IEncapsulatedOpenBISService openbisService)
         {
             this.delegator = delegator;
             sessionContext = delegator.getSessionContext();
             this.openbisService = openbisService;
-            this.replicaSampleType = replicaSampleType;
-            this.imageDataSetType = imageDataSetType;
+            this.gridPrepSampleType =
+                    openbisService.getSampleType(CinaConstants.GRID_PREP_SAMPLE_TYPE_CODE);
+            this.replicaSampleType =
+                    openbisService.getSampleType(CinaConstants.REPLICA_SAMPLE_TYPE_CODE);
+            this.rawImagesDataSetType =
+                    openbisService.getDataSetType(CinaConstants.RAW_IMAGES_DATA_SET_TYPE_CODE);
+            this.metadataDataSetType =
+                    openbisService.getDataSetType(CinaConstants.METADATA_DATA_SET_TYPE_CODE);
+            this.imageDataSetType =
+                    openbisService.getDataSetType(CinaConstants.IMAGE_DATA_SET_TYPE_CODE);
         }
 
         IDataSetHandlerRpc getDelegator()
@@ -72,11 +91,26 @@ abstract class BundleDataSetHelper
             return openbisService;
         }
 
+        SampleType getGridPrepSampleType()
+        {
+            return gridPrepSampleType;
+        }
+
         SampleType getReplicaSampleType()
         {
             return replicaSampleType;
         }
 
+        DataSetTypeWithVocabularyTerms getRawImagesDataSetType()
+        {
+            return rawImagesDataSetType;
+        }
+
+        DataSetTypeWithVocabularyTerms getMetadataDataSetType()
+        {
+            return metadataDataSetType;
+        }
+
         DataSetTypeWithVocabularyTerms getImageDataSetType()
         {
             return imageDataSetType;
@@ -96,16 +130,27 @@ abstract class BundleDataSetHelper
         this.registeredDataSets = new ArrayList<DataSetInformation>();
     }
 
+    /**
+     * Get all the data set information that has been created as a result of
+     * {@link #registerDataSet}. Only makes sense to invoke after registerDataSet has been called
+     */
+    public ArrayList<DataSetInformation> getDataSetInformation()
+    {
+        return registeredDataSets;
+    }
+
     /**
      * Register the provided file as a data set and add it to the dataSetInformation collection.
      * 
      * @param dataSetFile The file to register as a data set
      * @return A collection of data set information objects for each data set just registered
      */
-    public List<DataSetInformation> registerDataSet(File dataSetFile)
+    protected List<DataSetInformation> registerDataSet(File dataSetFile,
+            DataSetInformation dataSetInfo)
     {
         // Register the given file
-        List<DataSetInformation> bigDataSet = getDelegator().handleDataSet(dataSetFile);
+        List<DataSetInformation> bigDataSet =
+                getDelegator().handleDataSet(dataSetFile, dataSetInfo);
         registeredDataSets.addAll(bigDataSet);
         return bigDataSet;
     }
@@ -125,13 +170,25 @@ abstract class BundleDataSetHelper
         return globalState.getSessionContext();
     }
 
-    /**
-     * Get all the data set information that has been created as a result of
-     * {@link #registerDataSet}. Only makes sense to invoke after registerDataSet has been called
-     */
-    public ArrayList<DataSetInformation> getDataSetInformation()
+    protected ArrayList<NewProperty> createDataSetProperties(IMetadataExtractor metadata)
     {
-        return registeredDataSets;
-    }
+        List<PropertyTypeWithVocabulary> propertyTypes =
+                globalState.getImageDataSetType().getPropertyTypes();
+        ArrayList<NewProperty> properties = new ArrayList<NewProperty>();
+        for (PropertyTypeWithVocabulary propertyType : propertyTypes)
+        {
+            String value = metadata.getMetadataMap().get(propertyType.getCode().toLowerCase());
+            if (null != value)
+            {
+                NewProperty prop;
+                prop = new NewProperty(propertyType.getCode(), value);
+                properties.add(prop);
+            }
+        }
 
+        // Add a property with everything
+        properties.add(new NewProperty(MISC_DATA_SET_PROPERTY_CODE, metadata.getMetadataMap()
+                .toString()));
+        return properties;
+    }
 }
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistrator.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistrator.java
index d080c5723e0..0d0fa3feab4 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistrator.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistrator.java
@@ -95,7 +95,7 @@ public class GridPreparationRegistrator extends BundleDataSetHelper
         {
             NewSample newSample =
                     NewSample.createWithParent(gridPrepSampleId.toString(),
-                            globalState.getReplicaSampleType(), null, null);
+                            globalState.getGridPrepSampleType(), null, null);
             newSample.setExperimentIdentifier(expId.toString());
 
             String userId = getSessionContext().getUserName();
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaAnnotatedImagesRegistrator.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaAnnotatedImagesRegistrator.java
index d4d7668768f..5553a4bb188 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaAnnotatedImagesRegistrator.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaAnnotatedImagesRegistrator.java
@@ -16,7 +16,14 @@
 
 package ch.systemsx.cisd.cina.dss.bundle.registrators;
 
-import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import ch.systemsx.cisd.cina.shared.metadata.ImageMetadataExtractor;
+import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
+import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier;
 
 /**
  * Registers annotated image data sets.
@@ -25,15 +32,40 @@ import java.io.File;
  */
 public class ReplicaAnnotatedImagesRegistrator extends BundleDataSetHelper
 {
+    private final ImageMetadataExtractor imageMetadataExtractor;
+
+    private final SampleIdentifier replicaSampleId;
 
-    /**
-     * @param globalState
-     * @param dataSet
-     */
-    ReplicaAnnotatedImagesRegistrator(BundleRegistrationState globalState, File dataSet)
+    ReplicaAnnotatedImagesRegistrator(BundleRegistrationState globalState,
+            ImageMetadataExtractor imageMetadataExtractor, Sample replicaSample,
+            SampleIdentifier replicaSampleId)
     {
-        super(globalState, dataSet);
-        // TODO Auto-generated constructor stub
+        super(globalState, imageMetadataExtractor.getFolder());
+        this.imageMetadataExtractor = imageMetadataExtractor;
+        this.replicaSampleId = replicaSampleId;
     }
 
+    public List<DataSetInformation> register()
+    {
+        // Create a DataSetInformation
+        DataSetInformation metadataDataSetInfo = createDataSetInformation();
+
+        // Import the metadata
+        ArrayList<NewProperty> properties = createDataSetProperties(imageMetadataExtractor);
+        metadataDataSetInfo.setDataSetProperties(properties);
+        registerDataSet(dataSet, metadataDataSetInfo);
+
+        return getDataSetInformation();
+    }
+
+    private DataSetInformation createDataSetInformation()
+    {
+        DataSetInformation metadataDataSetInfo = new DataSetInformation();
+        metadataDataSetInfo.setSampleCode(replicaSampleId.getSampleCode());
+        metadataDataSetInfo.setSpaceCode(replicaSampleId.getSpaceLevel().getSpaceCode());
+        metadataDataSetInfo.setInstanceCode(replicaSampleId.getSpaceLevel()
+                .getDatabaseInstanceCode());
+        metadataDataSetInfo.setDataSetType(globalState.getImageDataSetType().getDataSetType());
+        return metadataDataSetInfo;
+    }
 }
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaMetadataRegistrator.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaMetadataRegistrator.java
index ed3ec060c2a..f3120000b60 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaMetadataRegistrator.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaMetadataRegistrator.java
@@ -17,6 +17,15 @@
 package ch.systemsx.cisd.cina.dss.bundle.registrators;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import ch.systemsx.cisd.cina.shared.metadata.ReplicaMetadataExtractor;
+import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
+import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier;
 
 /**
  * Registers a metadata data set.
@@ -25,15 +34,67 @@ import java.io.File;
  */
 public class ReplicaMetadataRegistrator extends BundleDataSetHelper
 {
+    private final ReplicaMetadataExtractor replicaMetadataExtractor;
+
+    private final SampleIdentifier replicaSampleId;
+
+    private File metadataDataSetFile = null;
+
+    ReplicaMetadataRegistrator(BundleRegistrationState globalState,
+            ReplicaMetadataExtractor replicaMetadataExtractor, Sample replicaSample,
+            SampleIdentifier replicaSampleId)
+    {
+        super(globalState, replicaMetadataExtractor.getFolder());
+        this.replicaMetadataExtractor = replicaMetadataExtractor;
+        this.replicaSampleId = replicaSampleId;
+    }
 
     /**
-     * @param globalState
-     * @param dataSet
+     * Register the metadata data set. Initialize the file object for the data set in the store.
      */
-    ReplicaMetadataRegistrator(BundleRegistrationState globalState, File dataSet)
+    public List<DataSetInformation> register()
+    {
+        String dataSetFileName = dataSet.getName();
+
+        // Create a DataSetInformation
+        DataSetInformation metadataDataSetInfo = createDataSetInformation();
+
+        // Import the metadata
+        ArrayList<NewProperty> properties = createDataSetProperties(replicaMetadataExtractor);
+        metadataDataSetInfo.setDataSetProperties(properties);
+        registerDataSet(dataSet, metadataDataSetInfo);
+
+        // Get the data set information for the data set we just registered
+        DataSetInformation registeredDataSetInformation = getDataSetInformation().get(0);
+        initializeMetadataDataSetFile(registeredDataSetInformation, dataSetFileName);
+
+        return getDataSetInformation();
+    }
+
+    public File getMetadataDataSetFile()
     {
-        super(globalState, dataSet);
-        // TODO Auto-generated constructor stub
+        assert metadataDataSetFile != null : "Can get metadataDataSetFile only after calling register";
+        return metadataDataSetFile;
     }
 
+    private DataSetInformation createDataSetInformation()
+    {
+        DataSetInformation metadataDataSetInfo = new DataSetInformation();
+        metadataDataSetInfo.setSampleCode(replicaSampleId.getSampleCode());
+        metadataDataSetInfo.setSpaceCode(replicaSampleId.getSpaceLevel().getSpaceCode());
+        metadataDataSetInfo.setInstanceCode(replicaSampleId.getSpaceLevel()
+                .getDatabaseInstanceCode());
+        metadataDataSetInfo.setDataSetType(globalState.getMetadataDataSetType().getDataSetType());
+        return metadataDataSetInfo;
+    }
+
+    private void initializeMetadataDataSetFile(DataSetInformation registeredDataSetInformation,
+            String dataSetFileName)
+    {
+        ExternalData metadataExternalData =
+                getOpenbisService().tryGetDataSet(getSessionContext().getSessionToken(),
+                        registeredDataSetInformation.getDataSetCode());
+        File containerFile = getDelegator().getFileForExternalData(metadataExternalData);
+        metadataDataSetFile = new File(containerFile, dataSetFileName);
+    }
 }
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRawImagesRegistrator.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRawImagesRegistrator.java
new file mode 100644
index 00000000000..0c5361aa6f6
--- /dev/null
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRawImagesRegistrator.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.cina.dss.bundle.registrators;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import ch.systemsx.cisd.cina.shared.metadata.ReplicaMetadataExtractor;
+import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
+import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier;
+
+/**
+ * Registers a replica's raw image data set.
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class ReplicaRawImagesRegistrator extends BundleDataSetHelper
+{
+    private final ReplicaMetadataExtractor replicaMetadataExtractor;
+
+    private final SampleIdentifier replicaSampleId;
+
+    ReplicaRawImagesRegistrator(BundleRegistrationState globalState,
+            ReplicaMetadataExtractor replicaMetadataExtractor, Sample replicaSample,
+            SampleIdentifier replicaSampleId, File dataSet)
+    {
+        super(globalState, dataSet);
+        this.replicaMetadataExtractor = replicaMetadataExtractor;
+        this.replicaSampleId = replicaSampleId;
+    }
+
+    public List<DataSetInformation> register()
+    {
+        // Create a DataSetInformation
+        DataSetInformation imagesDataSetInfo = createDataSetInformation();
+
+        // Import the metadata
+        ArrayList<NewProperty> properties = createDataSetProperties(replicaMetadataExtractor);
+        imagesDataSetInfo.setDataSetProperties(properties);
+        registerDataSet(dataSet, imagesDataSetInfo);
+
+        return getDataSetInformation();
+    }
+
+    private DataSetInformation createDataSetInformation()
+    {
+        DataSetInformation imagesDataSetInfo = new DataSetInformation();
+        imagesDataSetInfo.setSampleCode(replicaSampleId.getSampleCode());
+        imagesDataSetInfo.setSpaceCode(replicaSampleId.getSpaceLevel().getSpaceCode());
+        imagesDataSetInfo
+                .setInstanceCode(replicaSampleId.getSpaceLevel().getDatabaseInstanceCode());
+        imagesDataSetInfo.setDataSetType(globalState.getRawImagesDataSetType().getDataSetType());
+        return imagesDataSetInfo;
+    }
+}
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRegistrator.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRegistrator.java
index b45fdb329d2..d06500e51a3 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRegistrator.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaRegistrator.java
@@ -17,19 +17,24 @@
 package ch.systemsx.cisd.cina.dss.bundle.registrators;
 
 import java.io.File;
+import java.util.List;
 
+import ch.systemsx.cisd.cina.shared.metadata.ImageMetadataExtractor;
 import ch.systemsx.cisd.cina.shared.metadata.ReplicaMetadataExtractor;
+import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier;
 
 /**
- * Registers/Updates a replica, its metadata, the original images and the annotated images.
+ * Registers/Updates a replica, its metadata, the raw images and the annotated images.
  * 
  * @author Chandrasekhar Ramakrishnan
  */
 public class ReplicaRegistrator extends BundleDataSetHelper
 {
+    private static final String RAW_IMAGES_FOLDER_NAME = "RawData";
+
     private final ReplicaMetadataExtractor replicaMetadataExtractor;
 
     private final Sample gridPrepSample;
@@ -41,10 +46,8 @@ public class ReplicaRegistrator extends BundleDataSetHelper
 
     private Sample replicaSample;
 
-    /**
-     * @param globalState
-     * @param dataSet
-     */
+    private boolean didCreateSample = false;
+
     ReplicaRegistrator(BundleRegistrationState globalState,
             ReplicaMetadataExtractor replicaMetadataExtractor, Sample gridPrepSample,
             SampleIdentifier gridPrepSampleId, File dataSet)
@@ -60,17 +63,27 @@ public class ReplicaRegistrator extends BundleDataSetHelper
      * Create the replica sample (if necessary). Register the original images (if the replica sample
      * did not exist), register the metadata dataset, and update the sample metadata, register the
      * annotated images.
+     * 
+     * @return DataSetInformation objects for each of the data sets registered
      */
-    public void register()
+    public List<DataSetInformation> register()
     {
-        retrieveOrCreateGridPrepSample();
+        retrieveOrCreateReplicaSample();
+        if (didCreateSample)
+        {
+            registerRawImages();
+        }
+        registerMetadata();
+
+        return getDataSetInformation();
     }
 
-    private void retrieveOrCreateGridPrepSample()
+    private void retrieveOrCreateReplicaSample()
     {
         String sampleCode = replicaMetadataExtractor.tryReplicaSampleCode();
         replicaSampleId = new SampleIdentifier(gridPrepSampleId.getSpaceLevel(), sampleCode);
         replicaSample = getOpenbisService().tryGetSampleWithExperiment(replicaSampleId);
+        didCreateSample = false;
 
         // Sample doesn't exist, create it
         if (replicaSample == null)
@@ -83,8 +96,56 @@ public class ReplicaRegistrator extends BundleDataSetHelper
             String userId = getSessionContext().getUserName();
             getOpenbisService().registerSample(newSample, userId);
             replicaSample = getOpenbisService().tryGetSampleWithExperiment(replicaSampleId);
+            didCreateSample = true;
         }
 
         assert replicaSample != null;
     }
+
+    private void registerRawImages()
+    {
+        String replicaName = replicaMetadataExtractor.getFolder().getName();
+        File replicaOriginalImages =
+                new File(new File(dataSet, RAW_IMAGES_FOLDER_NAME), replicaName);
+        ReplicaRawImagesRegistrator registrator =
+                new ReplicaRawImagesRegistrator(globalState, replicaMetadataExtractor,
+                        replicaSample, replicaSampleId, replicaOriginalImages);
+        List<DataSetInformation> registeredDataSetInfos = registrator.register();
+        getDataSetInformation().addAll(registeredDataSetInfos);
+    }
+
+    /**
+     * Register the metadata data set and return the File object for the registered data set.
+     * 
+     * @return The File object for the registered data set
+     */
+    private File registerMetadata()
+    {
+        ReplicaMetadataRegistrator registrator =
+                new ReplicaMetadataRegistrator(globalState, replicaMetadataExtractor,
+                        replicaSample, replicaSampleId);
+        List<DataSetInformation> registeredDataSetInfos = registrator.register();
+        getDataSetInformation().addAll(registeredDataSetInfos);
+        return registrator.getMetadataDataSetFile();
+    }
+
+    @SuppressWarnings("unused")
+    private void registerAnnotatedImages(File registeredMetadataFile)
+    {
+        // Create a metadata extractor on the data set in the store (the ivar is a replica metadata
+        // extractor on the data set in the incoming directory, so the paths it has are not
+        // persistent)
+        ReplicaMetadataExtractor storeReplicaMetadataExtractor =
+                new ReplicaMetadataExtractor(registeredMetadataFile);
+        storeReplicaMetadataExtractor.prepare();
+        for (ImageMetadataExtractor imageMetadataExtractor : storeReplicaMetadataExtractor
+                .getImageMetadataExtractors())
+        {
+            ReplicaAnnotatedImagesRegistrator registrator =
+                    new ReplicaAnnotatedImagesRegistrator(globalState, imageMetadataExtractor,
+                            replicaSample, replicaSampleId);
+            List<DataSetInformation> registeredDataSetInfos = registrator.register();
+            getDataSetInformation().addAll(registeredDataSetInfos);
+        }
+    }
 }
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/CinaConstants.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/CinaConstants.java
index ffdc760ba7b..1c64a8a40f3 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/CinaConstants.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/CinaConstants.java
@@ -23,8 +23,14 @@ public class CinaConstants
 {
     public static final String BUNDLE_DATA_SET_TYPE_CODE = "BUNDLE";
 
+    public static final String RAW_IMAGES_DATA_SET_TYPE_CODE = "RAW_IMAGES";
+
+    public static final String METADATA_DATA_SET_TYPE_CODE = "METADATA";
+
     public static final String IMAGE_DATA_SET_TYPE_CODE = "IMAGE";
 
+    public static final String GRID_PREP_SAMPLE_TYPE_CODE = "GRID_PREP";
+
     public final static String REPLICA_SAMPLE_TYPE_CODE = "GRID_REPLICA";
 
     public static final String DIMENSION_PREFIX = "Dimension";
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/BundleStructureConstants.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/BundleStructureConstants.java
new file mode 100644
index 00000000000..2ddf9d74d5a
--- /dev/null
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/BundleStructureConstants.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.cina.shared.metadata;
+
+/**
+ * Constants that describe the structure fo the CINA bundle files.
+ * <p>
+ * Root.bundle/
+ * <ul>
+ * <li>BUNDLE_METADATA_FILE_NAME</li>
+ * <li>METADATA_FOLDER_NAME
+ * <ul>
+ * <li>Replica Folder
+ * <ul>
+ * <li>REPLICA_METADATA_FILE_NAME</li>
+ * <li>ANNOTATED_IMAGES_FOLDER_NAME
+ * <ul>
+ * <li>IMAGE_METADATA_FILE_NAME</li>
+ * </ul>
+ * </li>
+ * </ul>
+ * </li>
+ * </ul>
+ * </li>
+ * <li>RAW_IMAGES_FOLDER_NAME</li>
+ * </ul>
+ * 
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class BundleStructureConstants
+{
+    public static final String METADATA_FOLDER_NAME = "Annotations";
+
+    public static final String RAW_IMAGES_FOLDER_NAME = "RawData";
+
+    public static final String ANNOTATED_IMAGES_FOLDER_NAME = "Representations";
+
+    public static final String BUNDLE_METADATA_FILE_NAME = "BundleMetadata.xml";
+
+    public static final String REPLICA_METADATA_FILE_NAME = "ReplicaMetadata.xml";
+
+    public final static String IMAGE_METADATA_FILE_NAME = "metadata.xml";
+
+    public static final String GRID_PREP_SAMPLE_CODE_KEY = "database link information";
+
+    public static final String REPLICA_SAMPLE_CODE_KEY = "database id (replica)";
+
+    /**
+     * No reason to instantiate this class.
+     */
+    private BundleStructureConstants()
+    {
+
+    }
+}
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaOriginalImagesRegistrator.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/IMetadataExtractor.java
similarity index 60%
rename from rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaOriginalImagesRegistrator.java
rename to rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/IMetadataExtractor.java
index 22c8f354930..8dfeef4a94a 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/dss/bundle/registrators/ReplicaOriginalImagesRegistrator.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/IMetadataExtractor.java
@@ -14,26 +14,17 @@
  * limitations under the License.
  */
 
-package ch.systemsx.cisd.cina.dss.bundle.registrators;
+package ch.systemsx.cisd.cina.shared.metadata;
 
-import java.io.File;
+import java.util.Map;
 
 /**
- * Registers a replica's original image data set.
- * 
  * @author Chandrasekhar Ramakrishnan
  */
-public class ReplicaOriginalImagesRegistrator extends BundleDataSetHelper
+public interface IMetadataExtractor
 {
-
     /**
-     * @param globalState
-     * @param dataSet
+     * Get the metadata extracted from the file in the form of a map.
      */
-    ReplicaOriginalImagesRegistrator(BundleRegistrationState globalState, File dataSet)
-    {
-        super(globalState, dataSet);
-        // TODO Auto-generated constructor stub
-    }
-
+    public Map<String, String> getMetadataMap();
 }
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ImageMetadataExtractor.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ImageMetadataExtractor.java
index 96c8934c9e3..1a3254c4a13 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ImageMetadataExtractor.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ImageMetadataExtractor.java
@@ -29,7 +29,7 @@ import ch.systemsx.cisd.cina.shared.labview.LVDataParser;
  * 
  * @author Chandrasekhar Ramakrishnan
  */
-public class ImageMetadataExtractor
+public class ImageMetadataExtractor implements IMetadataExtractor
 {
     private final HashMap<String, String> metadataMap;
 
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ReplicaMetadataExtractor.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ReplicaMetadataExtractor.java
index 6c34c2ee7b2..efad5ad7500 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ReplicaMetadataExtractor.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/metadata/ReplicaMetadataExtractor.java
@@ -28,9 +28,9 @@ import ch.systemsx.cisd.cina.shared.labview.LVDataParser;
 /**
  * @author Chandrasekhar Ramakrishnan
  */
-public class ReplicaMetadataExtractor
+public class ReplicaMetadataExtractor implements IMetadataExtractor
 {
-    private static final String METADATA_FILE_NAME = "ReplicaMetadata.xml";
+    private static final String REPLICA_METADATA_FILE_NAME = "ReplicaMetadata.xml";
 
     private static final String REPLICA_SAMPLE_CODE_KEY = "database id (replica)";
 
@@ -44,7 +44,7 @@ public class ReplicaMetadataExtractor
 
     public static boolean doesFolderContainReplicaMetadata(File folder)
     {
-        File metadataFile = new File(folder, METADATA_FILE_NAME);
+        File metadataFile = new File(folder, REPLICA_METADATA_FILE_NAME);
         return metadataFile.exists();
     }
 
@@ -67,7 +67,7 @@ public class ReplicaMetadataExtractor
         }
 
         // First parse the metadata for the replica
-        File metadataFile = new File(folder, METADATA_FILE_NAME);
+        File metadataFile = new File(folder, REPLICA_METADATA_FILE_NAME);
         lvdata = LVDataParser.parse(metadataFile);
         if (null == lvdata)
         {
diff --git a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistratorTest.java b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistratorTest.java
index 6b7a50e8297..2feeca594d1 100644
--- a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistratorTest.java
+++ b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/GridPreparationRegistratorTest.java
@@ -18,6 +18,7 @@ package ch.systemsx.cisd.cina.dss.bundle.registrators;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
@@ -49,6 +50,14 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifierFa
  */
 public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
 {
+    private static final String TEST_USER_NAME = "test";
+
+    private static final String SESSION_TOKEN = "session-token";
+
+    private static final String RAW_IMAGES_DATA_SET_CODE = "RAW_IMAGES_DATA_SET_CODE";
+
+    private static final String METADATA_DATA_SET_CODE = "METADATA_DATA_SET_CODE";
+
     // Constants used in the test
     private static final String DB_CODE = "DB";
 
@@ -67,6 +76,16 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
     private static final String REPLICA_SAMPLE_IDENTIFIER = DB_CODE + ":/" + SPACE_CODE + "/"
             + REPLICA_SAMPLE_CODE;
 
+    private static abstract class MatcherNoDesc<T> extends BaseMatcher<T>
+    {
+
+        public void describeTo(Description description)
+        {
+
+        }
+
+    }
+
     private Mockery context;
 
     private IEncapsulatedOpenBISService openbisService;
@@ -136,21 +155,6 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
 
         context.checking(new Expectations()
             {
-                // private long uniqueId = 1;
-                //
-                // private void delegatorHandleDataSetExpectation(final String path)
-                // {
-                // final DataSetInformation dataSetInformation = new DataSetInformation();
-                // dataSetInformation.setDataSetCode("Derived");
-                // dataSetInformation.setSampleCode("" + uniqueId++);
-                // dataSetInformation.setSpaceCode(SPACE_CODE);
-                // dataSetInformation.setInstanceCode("Test");
-                //
-                // allowing(delegator).linkAndHandleDataSet(with(new File(path)),
-                // with(any(DataSetInformation.class)));
-                // will(returnValue(Collections.singletonList(dataSetInformation)));
-                // }
-
                 {
                     // The Grid Prep does not yet exist
                     one(openbisService).tryGetSampleWithExperiment(
@@ -178,7 +182,7 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
                             public void describeTo(Description description)
                             {
                             }
-                        }), with("test"));
+                        }), with(TEST_USER_NAME));
                     will(returnValue(new Long(1)));
 
                     // The Replica does not yet exist
@@ -188,7 +192,7 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
                     will(returnValue(null));
 
                     // Create the Replica
-                    one(openbisService).registerSample(with(new BaseMatcher<NewSample>()
+                    one(openbisService).registerSample(with(new MatcherNoDesc<NewSample>()
                         {
                             public boolean matches(Object item)
                             {
@@ -205,21 +209,15 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
                                 }
                                 return false;
                             }
-
-                            public void describeTo(Description description)
-                            {
-                            }
-                        }), with("test"));
+                        }), with(TEST_USER_NAME));
                     will(returnValue(new Long(2)));
-
-                    // delegatorHandleDataSetExpectation("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/Annotations/Replica for MRC files/MRC for Thomas/test20090422_BacklashRef.mrc");
-                    // delegatorHandleDataSetExpectation("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/Annotations/Replica for MRC files/MRC for Thomas/test20090424_TrackAtZeroRef.mrc");
-                    // delegatorHandleDataSetExpectation("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/Annotations/Replica for STEM files/STEM/stem_134588_1.imag");
                 }
             });
 
         setupExistingGridPrepExpectations();
         setupExistingReplicaExpectations();
+        setupHandleRawDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/RawData/ReplicTest");
+        setupHandleMetadataDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/Annotations/ReplicTest");
 
         createRegistrator(dataSetFile);
         registrator.register();
@@ -229,15 +227,30 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
 
     private void setupOpenBisExpectations()
     {
-        final SampleType sampleType = new SampleType();
-        sampleType.setCode(CinaConstants.REPLICA_SAMPLE_TYPE_CODE);
-        sampleType.setAutoGeneratedCode(true);
-        sampleType.setGeneratedCodePrefix("Replica-");
+        final SampleType gridPrepSampleType = new SampleType();
+        gridPrepSampleType.setCode(CinaConstants.GRID_PREP_SAMPLE_TYPE_CODE);
+        gridPrepSampleType.setAutoGeneratedCode(true);
+        gridPrepSampleType.setGeneratedCodePrefix("GridPrep-");
+
+        final SampleType replicaSampleType = new SampleType();
+        replicaSampleType.setCode(CinaConstants.REPLICA_SAMPLE_TYPE_CODE);
+        replicaSampleType.setAutoGeneratedCode(true);
+        replicaSampleType.setGeneratedCodePrefix("Replica-");
 
         DataSetType dataSetType = new DataSetType(CinaConstants.IMAGE_DATA_SET_TYPE_CODE);
-        final DataSetTypeWithVocabularyTerms dataSetTypeWithTerms =
+        final DataSetTypeWithVocabularyTerms rawImagesDataSetTypeWithTerms =
+                new DataSetTypeWithVocabularyTerms();
+        rawImagesDataSetTypeWithTerms.setDataSetType(dataSetType);
+
+        dataSetType = new DataSetType(CinaConstants.METADATA_DATA_SET_TYPE_CODE);
+        final DataSetTypeWithVocabularyTerms metadataDataSetTypeWithTerms =
+                new DataSetTypeWithVocabularyTerms();
+        metadataDataSetTypeWithTerms.setDataSetType(dataSetType);
+
+        dataSetType = new DataSetType(CinaConstants.IMAGE_DATA_SET_TYPE_CODE);
+        final DataSetTypeWithVocabularyTerms imageDataSetTypeWithTerms =
                 new DataSetTypeWithVocabularyTerms();
-        dataSetTypeWithTerms.setDataSetType(dataSetType);
+        imageDataSetTypeWithTerms.setDataSetType(dataSetType);
 
         externalData = new ExternalData();
         externalData.setCode("1");
@@ -246,10 +259,16 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
         context.checking(new Expectations()
             {
                 {
+                    one(openbisService).getSampleType(CinaConstants.GRID_PREP_SAMPLE_TYPE_CODE);
+                    will(returnValue(gridPrepSampleType));
                     one(openbisService).getSampleType(CinaConstants.REPLICA_SAMPLE_TYPE_CODE);
-                    will(returnValue(sampleType));
+                    will(returnValue(replicaSampleType));
+                    one(openbisService).getDataSetType(CinaConstants.RAW_IMAGES_DATA_SET_TYPE_CODE);
+                    will(returnValue(rawImagesDataSetTypeWithTerms));
+                    one(openbisService).getDataSetType(CinaConstants.METADATA_DATA_SET_TYPE_CODE);
+                    will(returnValue(metadataDataSetTypeWithTerms));
                     one(openbisService).getDataSetType(CinaConstants.IMAGE_DATA_SET_TYPE_CODE);
-                    will(returnValue(dataSetTypeWithTerms));
+                    will(returnValue(imageDataSetTypeWithTerms));
                     // one(openbisService).tryGetDataSet("session-token", externalData.getCode());
                     // will(returnValue(externalData));
                 }
@@ -296,41 +315,88 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
             });
     }
 
+    private void setupHandleRawDataSetExpectations(final String path)
+    {
+        // Create the Raw Images Data Set
+        final DataSetInformation dataSetInformation = new DataSetInformation();
+        dataSetInformation.setDataSetCode(RAW_IMAGES_DATA_SET_CODE);
+        dataSetInformation.setSampleCode(REPLICA_SAMPLE_CODE);
+        dataSetInformation.setSpaceCode(SPACE_CODE);
+        dataSetInformation.setInstanceCode(DB_CODE);
+
+        // set up the expectations
+        context.checking(new Expectations()
+            {
+                {
+                    one(delegator).handleDataSet(with(new File(path)),
+                            with(new MatcherNoDesc<DataSetInformation>()
+                                {
+                                    public boolean matches(Object item)
+                                    {
+                                        if (item instanceof DataSetInformation)
+                                        {
+                                            DataSetInformation dataSetInfo =
+                                                    (DataSetInformation) item;
+                                            assertEquals(REPLICA_SAMPLE_CODE,
+                                                    dataSetInfo.getSampleCode());
+                                            return true;
+                                        }
+                                        return false;
+                                    }
+                                }));
+                    will(returnValue(Collections.singletonList(dataSetInformation)));
+                }
+            });
+    }
+
+    private void setupHandleMetadataDataSetExpectations(final String path)
+    {
+        // Create the Raw Images Data Set
+        final DataSetInformation dataSetInformation = new DataSetInformation();
+        dataSetInformation.setDataSetCode(METADATA_DATA_SET_CODE);
+        dataSetInformation.setSampleCode(REPLICA_SAMPLE_CODE);
+        dataSetInformation.setSpaceCode(SPACE_CODE);
+        dataSetInformation.setInstanceCode(DB_CODE);
+
+        externalData = new ExternalData();
+        externalData.setCode("1");
+
+        final File dataSetFile = new File(path);
+
+        // set up the expectations
+        context.checking(new Expectations()
+            {
+                {
+                    one(delegator).handleDataSet(with(dataSetFile),
+                            with(new MatcherNoDesc<DataSetInformation>()
+                                {
+                                    public boolean matches(Object item)
+                                    {
+                                        if (item instanceof DataSetInformation)
+                                        {
+                                            DataSetInformation dataSetInfo =
+                                                    (DataSetInformation) item;
+                                            assertEquals(REPLICA_SAMPLE_CODE,
+                                                    dataSetInfo.getSampleCode());
+                                            return true;
+                                        }
+                                        return false;
+                                    }
+                                }));
+                    will(returnValue(Collections.singletonList(dataSetInformation)));
+                    one(openbisService).tryGetDataSet(SESSION_TOKEN, METADATA_DATA_SET_CODE);
+                    will(returnValue(externalData));
+                    one(delegator).getFileForExternalData(externalData);
+                    will(returnValue(dataSetFile.getParentFile()));
+                }
+            });
+    }
+
     private void createRegistrator(final File dataSet)
     {
         registrator = new GridPreparationRegistrator(createBundleRegistrationState(), dataSet);
     }
 
-    // private void setupNewSampleExpectations(final File dataSet)
-    // {
-    // setupSessionContextExpectations();
-    //
-    // final DataSetInformation dataSetInformation = new DataSetInformation();
-    // dataSetInformation.setDataSetCode(externalData.getCode());
-    // dataSetInformation.setSampleCode("GRID-1");
-    // dataSetInformation.setSpaceCode(SPACE_CODE);
-    // dataSetInformation.setInstanceCode("Test");
-    //
-    // final Sample sample = new Sample();
-    // Experiment exp = new Experiment();
-    // exp.setIdentifier("/Space/Exp-1");
-    // sample.setExperiment(exp);
-    // sample.setIdentifier(dataSetInformation.getSampleIdentifier().toString());
-    //
-    // // set up the expectations
-    // context.checking(new Expectations()
-    // {
-    // {
-    // one(delegator).handleDataSet(dataSet);
-    // will(returnValue(Collections.singletonList(dataSetInformation)));
-    // one(delegator).getFileForExternalData(externalData);
-    // will(returnValue(dataSet.getParentFile()));
-    // }
-    // });
-    //
-    // createRegistrator(dataSet);
-    // }
-
     private void setupCallerDataSetInfoExpectations()
     {
 
@@ -353,9 +419,9 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
     private void setupSessionContextExpectations()
     {
         final SessionContextDTO sessionContext = new SessionContextDTO();
-        sessionContext.setSessionToken("session-token");
+        sessionContext.setSessionToken(SESSION_TOKEN);
         sessionContext.setUserEmail("test@test.bar");
-        sessionContext.setUserName("test");
+        sessionContext.setUserName(TEST_USER_NAME);
 
         // set up the expectations
         context.checking(new Expectations()
@@ -370,12 +436,7 @@ public class GridPreparationRegistratorTest extends AbstractFileSystemTestCase
 
     private BundleRegistrationState createBundleRegistrationState()
     {
-        SampleType replicaSampleType =
-                openbisService.getSampleType(CinaConstants.REPLICA_SAMPLE_TYPE_CODE);
-        DataSetTypeWithVocabularyTerms imageDataSetType =
-                openbisService.getDataSetType(CinaConstants.IMAGE_DATA_SET_TYPE_CODE);
-        return new BundleRegistrationState(delegator, openbisService, replicaSampleType,
-                imageDataSetType);
+        return new BundleRegistrationState(delegator, openbisService);
     }
 
 }
-- 
GitLab