diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java index cfba67300d866531936db9a2ec3fb4f9b4c64579..b436b358135363bd66209a595ffb99b691420574 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonServer.java @@ -425,7 +425,8 @@ public final class CommonServer extends AbstractCommonServer<ICommonServer> impl { final Session session = getSession(sessionToken); final IDatasetLister datasetLister = createDatasetLister(session); - final List<ExternalData> datasets = datasetLister.listByExperimentTechId(experimentId); + final List<ExternalData> datasets = + datasetLister.listByExperimentTechIds(Collections.singleton(experimentId)); Collections.sort(datasets); return datasets; } 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 d42d617dec9d2ad967ea78fb5ea7a21c59030f11..896c09f739b5a473637b4512a2a491e4c9e6dab8 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 @@ -26,10 +26,15 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Map.Entry; +import net.lemnik.eodsql.DataIterator; + import org.apache.commons.lang.time.DateUtils; import ch.rinn.restrictions.Friend; @@ -162,9 +167,36 @@ public class DatasetLister implements IDatasetLister } } - public List<ExternalData> listByExperimentTechId(TechId experimentId) + public List<ExternalData> listByExperimentTechIds(Collection<TechId> experimentIds) + { + LongSet ids = new LongOpenHashSet(); + for (TechId techId : experimentIds) + { + ids.add(techId.getId()); + } + return enrichDatasets(query.getDatasetsForExperiment(ids)); + } + + public Map<Long, Set<Long>> listParentIds(Collection<Long> dataSetIDs) { - return enrichDatasets(query.getDatasetsForExperiment(experimentId.getId())); + LongOpenHashSet ids = new LongOpenHashSet(); + for (Long id : dataSetIDs) + { + ids.add(id); + } + DataIterator<DatasetRelationRecord> relationships = query.listParentDataSetIds(ids); + Map<Long, Set<Long>> map = new LinkedHashMap<Long, Set<Long>>(); + for (DatasetRelationRecord relationship : relationships) + { + Set<Long> parents = map.get(relationship.data_id_child); + if (parents == null) + { + parents = new LinkedHashSet<Long>(); + map.put(relationship.data_id_child, parents); + } + parents.add(relationship.data_id_parent); + } + return map; } public List<ExternalData> listByChildTechId(TechId childDatasetId) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetLister.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetLister.java index 52a6466538655976ba53e3e7956d51aefccf0bf4..d6e20c26c1f446f7a76cf4d44b94116d4d3706e8 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetLister.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/IDatasetLister.java @@ -18,6 +18,8 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo.datasetlister; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Set; import ch.systemsx.cisd.openbis.generic.shared.basic.TechId; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ArchiverDataSetCriteria; @@ -31,8 +33,8 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.TrackingDataSetCriteria */ public interface IDatasetLister { - /** @return datasets connected to the experiment with the specified id */ - List<ExternalData> listByExperimentTechId(TechId experimentId); + /** @return datasets connected to experiments with the specified ids */ + List<ExternalData> listByExperimentTechIds(Collection<TechId> experimentIds); /** * @return datasets connected to the sample with the specified id @@ -46,6 +48,12 @@ public interface IDatasetLister /** @return datasets that are parents of a dataset with the specified id */ List<ExternalData> listByParentTechId(TechId parentDatasetId); + + /** + * Returns a map with all parent data set IDs of specified data set IDs. The keys of the map + * are IDs from the argument. A value of the map contains at least one alement. + */ + Map<Long, Set<Long>> listParentIds(Collection<Long> dataSetIDs); // 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 1f04b0f1aa244699b702a6464203b01e6bb03ddd..308982b76f0bd070b557b4a989b983d653a5c96a 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 @@ -52,8 +52,10 @@ public interface IDatasetListingQuery extends TransactionQuery, IPropertyListing /** * Returns the datasets for the given experiment id. */ - @Select(sql = "select * from data join external_data on data.id = external_data.data_id where data.expe_id=?{1}", fetchSize = FETCH_SIZE) - public DataIterator<DatasetRecord> getDatasetsForExperiment(long experimentId); + @Select(sql = "select * from data join external_data on data.id = external_data.data_id" + + " where data.expe_id = any(?{1})", parameterBindings = + { LongSetMapper.class }, fetchSize = FETCH_SIZE) + public DataIterator<DatasetRecord> getDatasetsForExperiment(LongSet experimentIds); /** * Returns the directly connected datasets for the given sample id. @@ -95,7 +97,11 @@ 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); - + + @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); + /** * Returns the datasets that are children of a dataset with given id. */ diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/CommonServerTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/CommonServerTest.java index e8104f60f2f012cc5fcf6a7af291e07576b052ae..8c8e2789b268ff601ee178eb2ca76ed2357ae47e 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/CommonServerTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/CommonServerTest.java @@ -568,8 +568,7 @@ public final class CommonServerTest extends AbstractServerTestCase one(commonBusinessObjectFactory).createDatasetLister(SESSION, DATA_STORE_BASE_URL); will(returnValue(datasetLister)); - - one(datasetLister).listByExperimentTechId(experimentId); + one(datasetLister).listByExperimentTechIds(Collections.singleton(experimentId)); will(returnValue(Arrays.asList(externalData))); } }); diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListerTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListerTest.java index dd7208ca9d4256fbb15fe9208358b134adaba368..323a2d22a8ba01f504a7340d72cb09abd8c33593 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListerTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListerTest.java @@ -20,7 +20,12 @@ import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Set; import org.testng.AssertJUnit; import org.testng.annotations.BeforeClass; @@ -71,5 +76,18 @@ public class DatasetListerTest extends AbstractDAOTest assertFalse(externalData.getProperties().isEmpty()); AssertJUnit.assertNotNull(externalData.getExperiment()); } + + @Test + public void testListParents() + { + Map<Long, Set<Long>> map = lister.listParentIds(Arrays.<Long>asList(2L, 4L, 9L)); + System.out.println(map); + + assertEquals(null, map.get(2L)); + assertEquals("[2]", map.get(4L).toString()); + List<Long> list = new ArrayList<Long>(map.get(9L)); + Collections.sort(list); + assertEquals("[2, 5, 6, 7]", list.toString()); + } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListingQueryTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListingQueryTest.java index 97804e9e4415a18005bda5b3390997b84a7f024b..0fa3d56670d4fc463cda242b59ffdf686498605c 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListingQueryTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/datasetlister/DatasetListingQueryTest.java @@ -22,9 +22,13 @@ import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertTrue; +import it.unimi.dsi.fastutil.longs.LongArraySet; + import java.sql.SQLException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; import org.apache.commons.lang.builder.EqualsBuilder; @@ -124,19 +128,34 @@ public class DatasetListingQueryTest extends AbstractDAOTest } @Test - public void testDatasetsForExperiment() + public void testDatasetsForExperiments() { - ExperimentPE experiment = + LongArraySet experimentIds = new LongArraySet(); + ExperimentPE experiment1 = getExperiment(dbInstance.getCode(), "CISD", "NEMO", "EXP-TEST-1", daoFactory); - long expId = experiment.getId(); - List<DatasetRecord> datasets = asList(query.getDatasetsForExperiment(expId)); + ExperimentPE experiment2 = + getExperiment(dbInstance.getCode(), "CISD", "NEMO", "EXP-TEST-2", daoFactory); + experimentIds.add((long) experiment1.getId()); + experimentIds.add((long) experiment2.getId()); + List<DatasetRecord> datasets = asList(query.getDatasetsForExperiment(experimentIds)); assertTrue(datasets.size() > 0); + Map<Long, Integer> dataSetCounters = new HashMap<Long, Integer>(); for (DatasetRecord record : datasets) { assertDatasetCorrect(record); assertEqualWithFetchedById(record); - assertEquals(expId, record.expe_id); + Integer counter = dataSetCounters.get(record.expe_id); + if (counter == null) + { + counter = 0; + } + counter++; + dataSetCounters.put(record.expe_id, counter); } + + assertEquals(1, dataSetCounters.get(experiment1.getId()).intValue()); + assertEquals(1, dataSetCounters.get(experiment2.getId()).intValue()); + assertEquals(2, dataSetCounters.size()); } @Test