From 750ff20fffd50ee25570eb074236689d61d61d0f Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Wed, 14 Oct 2009 08:41:17 +0000
Subject: [PATCH] [LMS-1229] fixed dataset search with multiple db instances

SVN: 12961
---
 .../ExperimentProjectGroupCodeRecord.java     |  5 +++-
 .../entity/ISecondaryEntityListingQuery.java  |  2 +-
 .../bo/common/entity/SecondaryEntityDAO.java  | 10 +++++---
 .../bo/datasetlister/DatasetLister.java       | 25 ++++++++++++++++---
 .../bo/samplelister/SampleListingWorker.java  |  2 +-
 .../shared/basic/dto/DatabaseInstance.java    | 14 ++++++++++-
 .../DatabaseInstanceTranslator.java           |  2 ++
 .../SecondaryEntityListingQueryTest.java      |  2 +-
 8 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ExperimentProjectGroupCodeRecord.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ExperimentProjectGroupCodeRecord.java
index f9c9fbe3510..8db027a7562 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ExperimentProjectGroupCodeRecord.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ExperimentProjectGroupCodeRecord.java
@@ -3,7 +3,8 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo.common.entity;
 import ch.rinn.restrictions.Private;
 
 /**
- * A class representing an experiment, project and group code.
+ * A class representing an experiment, project and group code. It contains also code of experiment
+ * type and database instance id.
  */
 @Private
 public class ExperimentProjectGroupCodeRecord
@@ -15,4 +16,6 @@ public class ExperimentProjectGroupCodeRecord
     public String p_code;
 
     public String g_code;
+
+    public Long dbin_id;
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ISecondaryEntityListingQuery.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ISecondaryEntityListingQuery.java
index ecae1aeafc3..0c5d253f357 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ISecondaryEntityListingQuery.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/ISecondaryEntityListingQuery.java
@@ -49,7 +49,7 @@ public interface ISecondaryEntityListingQuery extends TransactionQuery
      * 
      * @param experimentId The id of the experiment to get the code for.
      */
-    @Select("select e.code as e_code, et.code as et_code, p.code as p_code, g.code as g_code from experiments e "
+    @Select("select e.code as e_code, et.code as et_code, p.code as p_code, g.code as g_code, g.dbin_id as dbin_id from experiments e "
             + "join experiment_types et on e.exty_id=et.id join projects p on e.proj_id=p.id "
             + "join groups g on p.grou_id=g.id where e.id=?{1}")
     public ExperimentProjectGroupCodeRecord getExperimentAndProjectAndGroupCodeForId(
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityDAO.java
index 3a7d533a98f..7390a887b4d 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityDAO.java
@@ -90,7 +90,7 @@ public class SecondaryEntityDAO
         this.databaseInstance = DatabaseInstanceTranslator.translate(databaseInstancePE);
     }
 
-    public Experiment getExperiment(final long experimentId)
+    public Experiment tryGetExperiment(final long experimentId)
     {
         final ExperimentProjectGroupCodeRecord record =
                 query.getExperimentAndProjectAndGroupCodeForId(experimentId);
@@ -98,12 +98,16 @@ public class SecondaryEntityDAO
         {
             throw new EmptyResultDataAccessException(1);
         }
-        return createExperiment(experimentId, record);
+        return tryCreateExperiment(experimentId, record);
     }
 
-    private Experiment createExperiment(final long experimentId,
+    private Experiment tryCreateExperiment(final long experimentId,
             final ExperimentProjectGroupCodeRecord record)
     {
+        if (record.dbin_id.equals(databaseInstance.getId()) == false)
+        {
+            return null; // experiment is connected (through group) with different db instance
+        }
         final Group group = new Group();
         group.setCode(escapeHtml(record.g_code));
         group.setInstance(databaseInstance);
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetLister.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetLister.java
index 645f1796513..7f6e4a76f71 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetLister.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetLister.java
@@ -179,8 +179,9 @@ public class DatasetLister implements IDatasetLister
         loadSmallConnectedTables();
         List<DatasetRecord> datasetRecords = asList(datasets);
         final Long2ObjectMap<ExternalData> datasetMap = createPrimaryDatasets(datasetRecords);
-        enrichWithProperties(datasetMap);
         enrichWithExperiments(datasetMap);
+        filterDatasetsWithNullExperiments(datasetMap);
+        enrichWithProperties(datasetMap);
         enrichWithSamples(datasetMap);
         return asList(datasetMap);
     }
@@ -243,15 +244,32 @@ public class DatasetLister implements IDatasetLister
         {
             long experimentId = dataset.getExperiment().getId();
             Experiment experiment = experimentMap.get(experimentId);
-            if (experiment == null)
+            // null value is put if experiment is from different db instance
+            if (experimentMap.containsKey(experimentId) == false)
             {
-                experiment = referencedEntityDAO.getExperiment(experimentId);
+                experiment = referencedEntityDAO.tryGetExperiment(experimentId);
                 experimentMap.put(experimentId, experiment);
             }
             dataset.setExperiment(experiment);
         }
     }
 
+    private void filterDatasetsWithNullExperiments(Long2ObjectMap<ExternalData> datasetMap)
+    {
+        LongSet datasetsToRemove = new LongOpenHashSet();
+        for (ExternalData dataset : datasetMap.values())
+        {
+            if (dataset.getExperiment() == null)
+            {
+                datasetsToRemove.add(dataset.getId());
+            }
+        }
+        for (Long datasetId : datasetsToRemove)
+        {
+            datasetMap.remove(datasetId);
+        }
+    }
+
     private static <T> List<T> asList(Iterable<T> items)
     {
         List<T> result = new ArrayList<T>();
@@ -293,6 +311,7 @@ public class DatasetLister implements IDatasetLister
     private ExternalData createPrimaryDataset(DatasetRecord record)
     {
         ExternalData dataset = createBasicDataset(record);
+        dataset.setId(record.id);
         dataset.setComplete(resolve(record.is_complete));
         dataset.setDataProducerCode(escapeHtml(record.data_producer_code));
         dataset.setDataStore(dataStores.get(record.dast_id));
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java
index ad0d44fc2b6..9a50a5b3e3f 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java
@@ -289,7 +289,7 @@ final class SampleListingWorker
 
     private Experiment createAndSaveExperiment(final long experimentId)
     {
-        final Experiment experiment = referencedEntityDAO.getExperiment(experimentId);
+        final Experiment experiment = referencedEntityDAO.tryGetExperiment(experimentId);
         experiments.put(experimentId, experiment);
         return experiment;
     }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DatabaseInstance.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DatabaseInstance.java
index 75c0949126e..372d11523cf 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DatabaseInstance.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DatabaseInstance.java
@@ -17,12 +17,14 @@
 package ch.systemsx.cisd.openbis.generic.shared.basic.dto;
 
 /**
- * @author     Franz-Josef Elmer
+ * @author Franz-Josef Elmer
  */
 public class DatabaseInstance extends Code<DatabaseInstance>
 {
     private static final long serialVersionUID = ServiceVersionHolder.VERSION;
 
+    private Long id;
+
     private String uuid;
 
     private String identifier;
@@ -46,4 +48,14 @@ public class DatabaseInstance extends Code<DatabaseInstance>
     {
         this.identifier = identifier;
     }
+
+    public Long getId()
+    {
+        return id;
+    }
+
+    public void setId(Long id)
+    {
+        this.id = id;
+    }
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/translator/DatabaseInstanceTranslator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/translator/DatabaseInstanceTranslator.java
index 36d6f4d77d2..c142c5dddac 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/translator/DatabaseInstanceTranslator.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/translator/DatabaseInstanceTranslator.java
@@ -21,6 +21,7 @@ import org.apache.commons.lang.StringEscapeUtils;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DatabaseInstance;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.IdentifierHelper;
+import ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils;
 
 /**
  * A {@link DatabaseInstance} &lt;---&gt; {@link DatabaseInstancePE} translator.
@@ -42,6 +43,7 @@ public final class DatabaseInstanceTranslator
             return null;
         }
         final DatabaseInstance result = new DatabaseInstance();
+        result.setId(HibernateUtils.getId(databaseInstance));
         result.setCode(StringEscapeUtils.escapeHtml(databaseInstance.getCode()));
         result.setUuid(StringEscapeUtils.escapeHtml(databaseInstance.getUuid()));
         result.setIdentifier(StringEscapeUtils.escapeHtml(IdentifierHelper
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityListingQueryTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityListingQueryTest.java
index 30bb59292eb..1960c9c3d52 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityListingQueryTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/common/entity/SecondaryEntityListingQueryTest.java
@@ -71,7 +71,7 @@ public class SecondaryEntityListingQueryTest extends AbstractDAOTest
     @Test
     public void testGetExperiment()
     {
-        Experiment expFull = dao.getExperiment(firstExperiment.getId());
+        Experiment expFull = dao.tryGetExperiment(firstExperiment.getId());
         assertEquals(firstExperiment.getCode(), expFull.getCode());
         ProjectPE project = firstExperiment.getProject();
         assertEquals(project.getCode(), expFull.getProject().getCode());
-- 
GitLab