From 2d93ccf9433fda47a7fc0166e01ca6dd9e0fd28d Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Thu, 25 Nov 2010 09:51:58 +0000
Subject: [PATCH] LMS-1886 Changes to make the sample listing functionality
 useful.

SVN: 18895
---
 .../registrators/ReplicaRegistrator.java      | 62 +++++++++++++-
 .../constants/BundleStructureConstants.java   |  4 +
 .../cina/shared/constants/CinaConstants.java  |  4 +
 .../metadata/ReplicaMetadataExtractor.java    | 28 +++++++
 .../bundle/CinaBundleDataSetHandlerTest.java  |  1 -
 .../CinaBundleRegistrationTest.java           | 83 ++++++++++++++++++-
 .../GridPreparationRegistratorTest.java       |  1 -
 7 files changed, 177 insertions(+), 6 deletions(-)

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 c3aa29a4cb0..2009d5f3a23 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,14 +17,25 @@
 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.constants.BundleStructureConstants;
+import ch.systemsx.cisd.cina.shared.constants.CinaConstants;
 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.TechId;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataTypeCode;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityProperty;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IEntityProperty;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewAttachment;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.PropertyType;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SampleUpdatesDTO;
+import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifier;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifier;
 
 /**
@@ -92,20 +103,54 @@ public class ReplicaRegistrator extends BundleDataSetHelper
         replicaSample = getOpenbisService().tryGetSampleWithExperiment(replicaSampleId);
         didCreateSample = false;
 
-        // Sample doesn't exist, create it
+        // Get the relevant metadata from the file
+        String sampleDescriptionOrNull = replicaMetadataExtractor.tryReplicaSampleDescription();
+        String sampleCreatorOrNull = replicaMetadataExtractor.tryReplicaSampleCreatorName();
+
+        ArrayList<IEntityProperty> properties = new ArrayList<IEntityProperty>();
+        if (null != sampleDescriptionOrNull)
+        {
+            EntityProperty prop = createProperty(CinaConstants.DESCRIPTION_PROPERTY_CODE);
+            prop.setValue(sampleDescriptionOrNull);
+            properties.add(prop);
+        }
+
+        if (null != sampleCreatorOrNull)
+        {
+            EntityProperty prop = createProperty(CinaConstants.CREATOR_EMAIL_PROPERTY_CODE);
+            prop.setValue(sampleCreatorOrNull);
+            properties.add(prop);
+        }
+
         if (replicaSample == null)
         {
+            // Sample doesn't exist, create it
             NewSample newSample =
                     NewSample.createWithParent(replicaSampleId.toString(),
                             globalState.getReplicaSampleType(), null, gridPrepSampleId.toString());
             newSample.setExperimentIdentifier(gridPrepSample.getExperiment().getIdentifier());
+            newSample.setProperties(properties.toArray(IEntityProperty.EMPTY_ARRAY));
 
             String userId = getSessionContext().getUserName();
             getOpenbisService().registerSample(newSample, userId);
-            replicaSample = getOpenbisService().tryGetSampleWithExperiment(replicaSampleId);
             didCreateSample = true;
+        } else
+        {
+            ExperimentIdentifier experimentId =
+                    new ExperimentIdentifier(replicaSample.getExperiment());
+            experimentId.setDatabaseInstanceCode(replicaSample.getExperiment().getProject()
+                    .getSpace().getInstance().getCode());
+            // Sample does exist, update the metadata
+            SampleUpdatesDTO sampleUpdate =
+                    new SampleUpdatesDTO(TechId.create(replicaSample), properties, experimentId,
+                            new ArrayList<NewAttachment>(), replicaSample.getModificationDate(),
+                            replicaSampleId, null, null);
+            getOpenbisService().updateSample(sampleUpdate);
+            didCreateSample = false;
         }
 
+        replicaSample = getOpenbisService().tryGetSampleWithExperiment(replicaSampleId);
+
         assert replicaSample != null;
     }
 
@@ -154,4 +199,17 @@ public class ReplicaRegistrator extends BundleDataSetHelper
             getDataSetInformation().addAll(registeredDataSetInfos);
         }
     }
+
+    private EntityProperty createProperty(String propertyTypeCode)
+    {
+        PropertyType propertyType = new PropertyType();
+        DataType dataType = new DataType();
+        dataType.setCode(DataTypeCode.VARCHAR);
+        propertyType.setCode(propertyTypeCode);
+        propertyType.setDataType(dataType);
+
+        EntityProperty property = new EntityProperty();
+        property.setPropertyType(propertyType);
+        return property;
+    }
 }
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/BundleStructureConstants.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/BundleStructureConstants.java
index 2715427c755..4d88ad52219 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/BundleStructureConstants.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/shared/constants/BundleStructureConstants.java
@@ -59,6 +59,10 @@ public class BundleStructureConstants
 
     public static final String REPLICA_SAMPLE_CODE_KEY = "database id (replica)";
 
+    public static final String REPLICA_SAMPLE_DESCRIPTION_KEY = "description";
+
+    public static final String REPLICA_SAMPLE_CREATOR_NAME = "creator name (e-mail)";
+
     /**
      * No reason to instantiate this class.
      */
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 1c64a8a40f3..d2a4f9416dc 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
@@ -37,6 +37,10 @@ public class CinaConstants
 
     public static final String SIZE_PREFIX = "Size";
 
+    public static final String DESCRIPTION_PROPERTY_CODE = "Description";
+
+    public static final String CREATOR_EMAIL_PROPERTY_CODE = "CREATOR_EMAIL";
+
     // No need to instantiate this class
     private CinaConstants()
     {
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 e5fc1856808..fec9ace0bc7 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
@@ -37,6 +37,12 @@ public class ReplicaMetadataExtractor implements IMetadataExtractor
     private static final String REPLICA_SAMPLE_CODE_KEY =
             BundleStructureConstants.REPLICA_SAMPLE_CODE_KEY;
 
+    private static final String REPLICA_SAMPLE_DESCRIPTION_KEY =
+            BundleStructureConstants.REPLICA_SAMPLE_DESCRIPTION_KEY;
+
+    private static final String REPLICA_SAMPLE_CREATOR_NAME =
+            BundleStructureConstants.REPLICA_SAMPLE_CREATOR_NAME;
+
     private final ArrayList<ImageMetadataExtractor> metadataExtractors;
 
     private final File folder;
@@ -124,6 +130,28 @@ public class ReplicaMetadataExtractor implements IMetadataExtractor
         return metadataMap.get(REPLICA_SAMPLE_CODE_KEY);
     }
 
+    /**
+     * Return the description for the replica sample.
+     * 
+     * @return Return the code, or null if none was found
+     */
+    public String tryReplicaSampleDescription()
+    {
+        checkPrepared();
+        return metadataMap.get(REPLICA_SAMPLE_DESCRIPTION_KEY);
+    }
+
+    /**
+     * Return the creator name for the replica sample.
+     * 
+     * @return Return the code, or null if none was found
+     */
+    public String tryReplicaSampleCreatorName()
+    {
+        checkPrepared();
+        return metadataMap.get(REPLICA_SAMPLE_CREATOR_NAME);
+    }
+
     public File getFolder()
     {
         return folder;
diff --git a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandlerTest.java b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandlerTest.java
index af8432098a5..7e4b5e3386a 100644
--- a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandlerTest.java
+++ b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/CinaBundleDataSetHandlerTest.java
@@ -47,7 +47,6 @@ public class CinaBundleDataSetHandlerTest extends CinaBundleRegistrationTest
         setupNewEntititesExpectations();
 
         setupExistingGridPrepExpectations();
-        setupExistingReplicaExpectations();
         setupHandleRawDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/RawData/ReplicTest");
         setupHandleReplicaMetadataDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/Annotations/ReplicTest");
         setupHandleBundleMetadataDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/BundleMetadata.xml");
diff --git a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/CinaBundleRegistrationTest.java b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/CinaBundleRegistrationTest.java
index 71255cd6f58..05c4838d849 100644
--- a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/CinaBundleRegistrationTest.java
+++ b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/dss/bundle/registrators/CinaBundleRegistrationTest.java
@@ -19,6 +19,7 @@ package ch.systemsx.cisd.cina.dss.bundle.registrators;
 import java.io.File;
 import java.io.IOException;
 import java.util.Collections;
+import java.util.List;
 
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
@@ -34,11 +35,16 @@ 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.DataSetType;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetTypeWithVocabularyTerms;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DatabaseInstance;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Experiment;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalData;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IEntityProperty;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewSample;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Project;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Sample;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Space;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SampleUpdatesDTO;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SessionContextDTO;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifierFactory;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SampleIdentifierFactory;
@@ -61,8 +67,12 @@ public abstract class CinaBundleRegistrationTest extends AbstractFileSystemTestC
 
     private static final String SPACE_CODE = "SPACE";
 
-    protected static final String EXPERIMENT_IDENTIFIER = DB_CODE + ":/" + SPACE_CODE
-            + "/PROJECT/EXP-1";
+    private static final String PROJECT_CODE = "PROJECT";
+
+    private static final String EXPERIMENT_CODE = "EXP-1";
+
+    protected static final String EXPERIMENT_IDENTIFIER = DB_CODE + ":/" + SPACE_CODE + "/"
+            + PROJECT_CODE + "/" + EXPERIMENT_CODE;
 
     private static final String GRID_SAMPLE_CODE = "GRID-CODE";
 
@@ -206,6 +216,17 @@ public abstract class CinaBundleRegistrationTest extends AbstractFileSystemTestC
         final Sample sample = new Sample();
         Experiment exp = new Experiment();
         exp.setIdentifier(EXPERIMENT_IDENTIFIER);
+        exp.setCode(EXPERIMENT_CODE);
+        Project project = new Project();
+        project.setCode(PROJECT_CODE);
+        Space space = new Space();
+        space.setCode(SPACE_CODE);
+        DatabaseInstance dbInstance = new DatabaseInstance();
+        dbInstance.setCode(DB_CODE);
+        space.setInstance(dbInstance);
+        project.setSpace(space);
+        exp.setProject(project);
+        sample.setId((long) 1);
         sample.setExperiment(exp);
         sample.setIdentifier(REPLICA_SAMPLE_IDENTIFIER);
 
@@ -213,6 +234,53 @@ public abstract class CinaBundleRegistrationTest extends AbstractFileSystemTestC
         context.checking(new Expectations()
             {
                 {
+                    // Get the sample
+                    one(openbisService).tryGetSampleWithExperiment(
+                            with(new SampleIdentifierFactory(REPLICA_SAMPLE_IDENTIFIER)
+                                    .createIdentifier()));
+                    will(returnValue(sample));
+
+                    // Update it with new data
+                    one(openbisService).updateSample(with(new MatcherNoDesc<SampleUpdatesDTO>()
+                        {
+                            public boolean matches(Object item)
+                            {
+                                if (item instanceof SampleUpdatesDTO)
+                                {
+                                    SampleUpdatesDTO sampleUpdatesDto = (SampleUpdatesDTO) item;
+                                    assertEquals(REPLICA_SAMPLE_IDENTIFIER, sampleUpdatesDto
+                                            .getSampleIdentifier().toString());
+                                    assertEquals(EXPERIMENT_IDENTIFIER, sampleUpdatesDto
+                                            .getExperimentIdentifierOrNull().toString());
+                                    List<IEntityProperty> properties =
+                                            sampleUpdatesDto.getProperties();
+
+                                    assertTrue("The update should include properties.",
+                                            properties.size() > 0);
+                                    for (IEntityProperty property : properties)
+                                    {
+                                        if (property.getPropertyType().getCode()
+                                                .equals(CinaConstants.DESCRIPTION_PROPERTY_CODE))
+                                        {
+                                            assertEquals(
+                                                    "This replica is a test for imported MRC files",
+                                                    property.getValue());
+                                        }
+
+                                        if (property.getPropertyType().getCode()
+                                                .equals(CinaConstants.CREATOR_EMAIL_PROPERTY_CODE))
+                                        {
+                                            assertEquals("thomas.braun@bsse.ethz.ch",
+                                                    property.getValue());
+                                        }
+                                    }
+                                    return true;
+                                }
+                                return false;
+                            }
+                        }));
+
+                    // Return the sample
                     one(openbisService).tryGetSampleWithExperiment(
                             with(new SampleIdentifierFactory(REPLICA_SAMPLE_IDENTIFIER)
                                     .createIdentifier()));
@@ -474,6 +542,17 @@ public abstract class CinaBundleRegistrationTest extends AbstractFileSystemTestC
                             }
                         }), with(TEST_USER_NAME));
                     will(returnValue(new Long(2)));
+
+                    Sample sample = new Sample();
+                    Experiment exp = new Experiment();
+                    exp.setIdentifier(EXPERIMENT_IDENTIFIER);
+                    sample.setId((long) 1);
+                    sample.setExperiment(exp);
+                    sample.setIdentifier(REPLICA_SAMPLE_IDENTIFIER);
+                    one(openbisService).tryGetSampleWithExperiment(
+                            with(new SampleIdentifierFactory(REPLICA_SAMPLE_IDENTIFIER)
+                                    .createIdentifier()));
+                    will(returnValue(sample));
                 }
             });
     }
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 408e41bc896..fea5db79363 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
@@ -73,7 +73,6 @@ public class GridPreparationRegistratorTest extends CinaBundleRegistrationTest
         setupNewEntititesExpectations();
 
         setupExistingGridPrepExpectations();
-        setupExistingReplicaExpectations();
         setupHandleRawDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/RawData/ReplicTest");
         setupHandleReplicaMetadataDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/Annotations/ReplicTest");
         setupHandleBundleMetadataDataSetExpectations("sourceTest/java/ch/systemsx/cisd/cina/shared/metadata/Test.bundle/BundleMetadata.xml");
-- 
GitLab