From fdfa8c70e64e319c79c0bfd6ef483d9183e2acdc Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Wed, 3 Oct 2012 22:56:46 +0000
Subject: [PATCH] Add method IDataSetLister.getDataStoreBaseURLs() for a list
 of data sets.

SVN: 26985
---
 .../datasetlister/DataSetDownloadRecord.java  | 29 +++++++++
 .../datasetlister/DataSetLister.java          | 20 ++++++
 .../datasetlister/IDataSetLister.java         | 14 +++++
 .../datasetlister/IDataSetListingQuery.java   |  6 ++
 .../datasetlister/DataSetListerTest.java      | 42 +++++++++++++
 .../api/v1/dto/DataStoreForDataSets.java      | 61 +++++++++++++++++++
 6 files changed, 172 insertions(+)
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetDownloadRecord.java
 create mode 100644 openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataStoreForDataSets.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetDownloadRecord.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetDownloadRecord.java
new file mode 100644
index 00000000000..ec71b786a1b
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetDownloadRecord.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2012 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.openbis.generic.server.business.bo.fetchoptions.datasetlister;
+
+/**
+ * Type for storing the download ULR for a set of data sets;
+ * 
+ * @author Bernd Rinn
+ */
+class DataSetDownloadRecord
+{
+    String download_url;
+
+    String data_set_codes;
+}
\ No newline at end of file
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetLister.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetLister.java
index bc1aab89116..523c1aa289d 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetLister.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetLister.java
@@ -25,6 +25,8 @@ import java.util.Map;
 
 import net.lemnik.eodsql.QueryTool;
 
+import org.apache.commons.lang.StringUtils;
+
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.common.DatabaseContextUtils;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
@@ -32,6 +34,7 @@ import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet.DataSetInitializer;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSetFetchOption;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSetFetchOptions;
+import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataStoreForDataSets;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.EntityRegistrationDetails;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.EntityRegistrationDetails.EntityRegistrationDetailsInitializer;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetKind;
@@ -312,4 +315,21 @@ public class DataSetLister implements IDataSetLister
         }
     }
 
+    @Override
+    public List<DataStoreForDataSets> getDataStoreBaseURLs(List<String> dataSetCodes)
+    {
+        final String[] dataSetCodesArray = dataSetCodes.toArray(new String[dataSetCodes.size()]);
+        final List<DataSetDownloadRecord> records = query.getDownloadInfos(dataSetCodesArray);
+        final List<DataStoreForDataSets> result =
+                new ArrayList<DataStoreForDataSets>(records.size());
+        for (DataSetDownloadRecord r : records)
+        {
+            final String[] dataSetCodeArray =
+                    StringUtils.split(r.data_set_codes.substring(1, r.data_set_codes.length() - 1),
+                            ',');
+            result.add(new DataStoreForDataSets(r.download_url, dataSetCodeArray));
+        }
+        return result;
+    }
+
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetLister.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetLister.java
index f8ada5c4980..d24f99e3ff0 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetLister.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetLister.java
@@ -20,6 +20,7 @@ import java.util.List;
 
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSetFetchOptions;
+import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataStoreForDataSets;
 
 /**
  * @author pkupczyk
@@ -27,7 +28,20 @@ import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSetFetchOptions;
 public interface IDataSetLister
 {
 
+    /**
+     * Returns the data set meta data for the given <var>dataSetCodes</var> with the given
+     * <var>dataSetFetchOptions</var>.
+     * 
+     * @return One data set meta data object for each entry in <var>dataSetCodes</var>.
+     */
     public List<DataSet> getDataSetMetaData(List<String> dataSetCodes,
             DataSetFetchOptions dataSetFetchOptions);
 
+    /**
+     * Returns the base URLs (download URLs) for a set of <var>dataSetCodes</var>.
+     * 
+     * @return The list of data stores, each with the list of data set codes it has stored.
+     */
+    public List<DataStoreForDataSets> getDataStoreBaseURLs(List<String> dataSetCodes);
+
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetListingQuery.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetListingQuery.java
index 1870ea93c6d..fae3a6d9d92 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetListingQuery.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/IDataSetListingQuery.java
@@ -69,4 +69,10 @@ public interface IDataSetListingQuery extends BaseQuery
         { StringArrayMapper.class })
     public List<DataSetRelationRecord> getDataSetChildrenCodes(String[] dataSetCodes);
 
+    @Select(sql = " select ds.download_url, array_agg(d.code::text) as data_set_codes"
+            + " from data d left join data_stores ds on ds.id=d.dast_id"
+            + " where d.code = any(?{1}) group by download_url", parameterBindings =
+        { StringArrayMapper.class })
+    public List<DataSetDownloadRecord> getDownloadInfos(String[] dataSetCodes);
+
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetListerTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetListerTest.java
index 78e09c62f31..4b40bf1741a 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetListerTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/fetchoptions/datasetlister/DataSetListerTest.java
@@ -23,6 +23,7 @@ import static junit.framework.Assert.assertTrue;
 
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -31,17 +32,22 @@ import java.util.List;
 
 import javax.annotation.Resource;
 
+import net.lemnik.eodsql.QueryTool;
+
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
+import ch.systemsx.cisd.dbmigration.DatabaseConfigurationContext;
 import ch.systemsx.cisd.openbis.generic.server.api.v1.ResourceNames;
+import ch.systemsx.cisd.openbis.generic.server.business.bo.common.DatabaseContextUtils;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.common.EntityListingTestUtils;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.AbstractDAOTest;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSet;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSetFetchOption;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataSetFetchOptions;
+import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.DataStoreForDataSets;
 import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.EntityRegistrationDetails;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DatabaseInstance;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ExternalDataManagementSystem;
@@ -108,6 +114,42 @@ public class DataSetListerTest extends AbstractDAOTest
         assertEqualToDataSetMetaDataWithCodes(codes, fetchOptions, result);
     }
 
+    @Test
+    public void testGetDataStoreBaseURLs()
+    {
+        DatabaseConfigurationContext context = DatabaseContextUtils.getDatabaseContext(daoFactory);
+        QueryTool
+                .update(context.getDataSource(),
+                        "update data_stores set download_url='http://download_1' where code='STANDARD'");
+        final long newDataStoreId =
+                (Long) QueryTool
+                        .select(context.getDataSource(),
+                                "insert into data_stores (id,dbin_id,code,download_url,remote_url,session_token)"
+                                        + " values (nextval('data_store_id_seq'),1,'DSS2','http://download_2','','') returning id")
+                        .get(0).get("id");
+        QueryTool.update(context.getDataSource(),
+                "update data set dast_id = ?{1} where code = ?{2}", newDataStoreId,
+                "20081105092259000-20");
+        QueryTool.update(context.getDataSource(),
+                "update data set dast_id = ?{1} where code = ?{2}", newDataStoreId,
+                "20081105092259000-21");
+
+        List<String> codes = new ArrayList<String>();
+        codes.add("20081105092159188-3");
+        codes.add("20081105092159111-1");
+        codes.add("20081105092259000-19");
+        codes.add("20081105092259000-20");
+        codes.add("20081105092259000-21");
+        List<DataStoreForDataSets> result = lister.getDataStoreBaseURLs(codes);
+        assertEquals("http://download_1", result.get(0).getDataStoreBaseURL());
+        assertEquals(
+                Arrays.asList("20081105092159188-3", "20081105092159111-1", "20081105092259000-19"),
+                result.get(0).getDataSetCodes());
+        assertEquals("http://download_2", result.get(1).getDataStoreBaseURL());
+        assertEquals(Arrays.asList("20081105092259000-20", "20081105092259000-21"), result.get(1)
+                .getDataSetCodes());
+    }
+
     @Test
     public void testGetDataSetMetaDataForExistingDataSetCodesWithParentsShouldReturnDataSetsWithParents()
     {
diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataStoreForDataSets.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataStoreForDataSets.java
new file mode 100644
index 00000000000..5b949cb5b22
--- /dev/null
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/DataStoreForDataSets.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2012 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.openbis.generic.shared.api.v1.dto;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.List;
+
+import ch.systemsx.cisd.base.annotation.JsonObject;
+
+/**
+ * The data store URL for a list of data sets.
+ *
+ * @author Bernd Rinn
+ */
+@JsonObject("DataStoreForDataSets")
+public class DataStoreForDataSets implements Serializable
+{
+    private static final long serialVersionUID = 1L;
+    
+    private final String dataStoreBaseURL;
+    
+    private final String[] dataSetCodes;
+
+    public DataStoreForDataSets(String dataStoreBaseURL, String[] dataSetCodes)
+    {
+        this.dataStoreBaseURL = dataStoreBaseURL;
+        this.dataSetCodes = dataSetCodes;
+    }
+
+    /**
+     * The base URL of the data store (can be used for download from a client).
+     */
+    public String getDataStoreBaseURL()
+    {
+        return dataStoreBaseURL;
+    }
+
+    /**
+     * The list of data sets that can be found in this data store.
+     */
+    public List<String> getDataSetCodes()
+    {
+        return Arrays.asList(dataSetCodes);
+    }
+
+}
-- 
GitLab