From 3f03965a1dad1b65f19b9cb3999b97ae270f2322 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Fri, 12 Nov 2010 10:46:41 +0000
Subject: [PATCH] [LMS-1720] extended showing datasets indirectly connected
 with a sample to go through both sample and data set relationships

SVN: 18651
---
 .../entity/ISecondaryEntityListingQuery.java   |  8 ++++++++
 .../bo/common/entity/SecondaryEntityDAO.java   | 18 ++++++++++++++++++
 .../bo/datasetlister/DatasetLister.java        |  7 ++++---
 .../bo/datasetlister/IDatasetListingQuery.java |  9 ++++++++-
 4 files changed, 38 insertions(+), 4 deletions(-)

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 67aa00309b0..48b4268a5cc 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
@@ -71,6 +71,14 @@ public interface ISecondaryEntityListingQuery extends TransactionQuery
         { LongSetMapper.class }, fetchSize = FETCH_SIZE)
     public DataIterator<SampleReferenceRecord> getSamples(LongSet sampleIds);
 
+    /**
+     * Returns all children sample ids of the specified samples.
+     */
+    @Select(sql = "SELECT sample_id_child FROM sample_relationships "
+            + "    WHERE sample_id_parent = any(?{1})", parameterBindings =
+        { LongSetMapper.class }, fetchSize = FETCH_SIZE)
+    public DataIterator<Long> getChildrenIds(LongSet parentSampleIds);
+
     //
     // Persons
     //
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 a6ea68c05dd..0da91ffe55a 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
@@ -18,6 +18,7 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo.common.entity;
 
 import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
 import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
 import it.unimi.dsi.fastutil.longs.LongSet;
 
 import java.sql.Connection;
@@ -172,6 +173,23 @@ public class SecondaryEntityDAO
         return result;
     }
 
+    public LongSet getSampleDescendantIdsAndSelf(Long sampleId)
+    {
+        LongSet results = new LongOpenHashSet();
+        LongSet currentLayer = new LongOpenHashSet();
+        currentLayer.add(sampleId);
+        // go layer by layer into children samples
+        LongSet nextLayer;
+        while (currentLayer.isEmpty() == false)
+        {
+            results.addAll(currentLayer);
+            nextLayer = new LongOpenHashSet(query.getChildrenIds(currentLayer));
+            nextLayer.removeAll(results); // don't go twice through the same sample
+            currentLayer = nextLayer;
+        }
+        return results;
+    }
+
     private static Sample createSample(SampleReferenceRecord record,
             DatabaseInstance 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 559d06dbb65..19c752bbdd4 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
@@ -151,10 +151,11 @@ public class DatasetLister extends AbstractLister implements IDatasetLister
             return enrichDatasets(query.getDatasetsForSample(sampleId.getId()));
         } else
         {
-            // first get directly connected datasets, then go layer by layer into children datasets
+            // get all descendands of the sample
+            LongSet sampleIds = referencedEntityDAO.getSampleDescendantIdsAndSelf(sampleId.getId());
+            // get directly connected datasets, then go layer by layer into children datasets
             LongSet results = new LongOpenHashSet();
-            LongSet currentLayer =
-                    new LongOpenHashSet(query.getDatasetIdsForSample(sampleId.getId()));
+            LongSet currentLayer = new LongOpenHashSet(query.getDatasetIdsForSamples(sampleIds));
             LongSet nextLayer;
             while (currentLayer.isEmpty() == false)
             {
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetListingQuery.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetListingQuery.java
index aa67e7e9555..62ec5f83ee9 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetListingQuery.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetListingQuery.java
@@ -105,6 +105,13 @@ public interface IDatasetListingQuery extends TransactionQuery, IPropertyListing
     @Select(sql = "select id from data where data.samp_id=?{1}", fetchSize = FETCH_SIZE)
     public DataIterator<Long> getDatasetIdsForSample(long sampleId);
 
+    /**
+     * Returns ids of datasets directly connected to samples with given ids.
+     */
+    @Select(sql = "select id from data where data.samp_id = any(?{1})", parameterBindings =
+        { LongSetMapper.class }, fetchSize = FETCH_SIZE)
+    public DataIterator<Long> getDatasetIdsForSamples(LongSet sampleIds);
+
     @Select(sql = "select * from data_set_relationships where data_id_child = any(?{1})", parameterBindings =
         { LongSetMapper.class }, fetchSize = FETCH_SIZE)
     public DataIterator<DatasetRelationRecord> listParentDataSetIds(LongSet ids);
@@ -112,7 +119,7 @@ public interface IDatasetListingQuery extends TransactionQuery, IPropertyListing
     @Select(sql = "select * from data_set_relationships where data_id_parent = any(?{1})", parameterBindings =
         { LongSetMapper.class }, fetchSize = FETCH_SIZE)
     public DataIterator<DatasetRelationRecord> listChildrenDataSetIds(LongSet ids);
-    
+
     /**
      * Returns all datasets that are children of any specified dataset id.
      */
-- 
GitLab