From 6e35fce0778be247683c35c5f908fc231716dc2b Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 6 Apr 2017 07:20:43 +0000
Subject: [PATCH] SSDM-4724: DataStoreServerApi.createDataSets improved and bug
 fixed.

SVN: 38021
---
 .../server/dssapi/v3/DataStoreServerApi.java  | 66 ++++++++++++++++---
 .../dssapi/v3/pathinfo/PathInfoFeeder.java    | 20 ++++--
 .../dss/api/v3/PathInfoFeederTest.java        |  2 +-
 3 files changed, 70 insertions(+), 18 deletions(-)

diff --git a/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/DataStoreServerApi.java b/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/DataStoreServerApi.java
index 39b05f1863d..95395f63725 100644
--- a/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/DataStoreServerApi.java
+++ b/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/DataStoreServerApi.java
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.commons.collections.iterators.IteratorChain;
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -42,6 +43,8 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.create.DataSetCreation;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.fetchoptions.DataSetFetchOptions;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.DataSetPermId;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.search.DataSetSearchCriteria;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.datastore.id.DataStorePermId;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.datastore.id.IDataStoreId;
 import ch.ethz.sis.openbis.generic.dssapi.v3.IDataStoreServerApi;
 import ch.ethz.sis.openbis.generic.dssapi.v3.dto.dataset.create.FullDataSetCreation;
 import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.DataSetFile;
@@ -76,6 +79,7 @@ import ch.systemsx.cisd.openbis.dss.generic.shared.utils.PathInfoDataSourceProvi
 import ch.systemsx.cisd.openbis.generic.server.authorization.annotation.RolesAllowed;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.RoleWithHierarchy;
 import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
+
 import net.lemnik.eodsql.QueryTool;
 
 /**
@@ -312,24 +316,21 @@ public class DataStoreServerApi extends AbstractDssServiceRpc<IDataStoreServerAp
     @RolesAllowed({ RoleWithHierarchy.INSTANCE_ADMIN })
     public List<DataSetPermId> createDataSets(String sessionToken, List<FullDataSetCreation> newDataSets)
     {
-        List<String> permIds = getOpenBISService().createPermIds(newDataSets.size());
-
         if (PathInfoDataSourceProvider.isDataSourceDefined() == false)
         {
             throw new IllegalStateException("Pathinfo DB not configured - cannot store dataset file information");
         }
-
+        injectDataStoreIdAndCodesIfNeeded(newDataSets);
         IPathsInfoDAO dao = QueryTool.getQuery(PathInfoDataSourceProvider.getDataSource(), IPathsInfoDAO.class);
 
         for (int i = 0; i < newDataSets.size(); i++)
         {
-            String permId = permIds.get(i);
-            List<DataSetFileCreation> files = newDataSets.get(i).getFileMetadata();
-
-            long dataSetId =
-                    dao.createDataSet(permId, "");
+            FullDataSetCreation fullDataSetCreation = newDataSets.get(i);
+            List<DataSetFileCreation> files = fullDataSetCreation.getFileMetadata();
 
-            PathInfoFeeder feeder = new PathInfoFeeder(dataSetId, files);
+            long dataSetId = dao.createDataSet(fullDataSetCreation.getMetadataCreation().getCode(), "");
+            String dataSetCode = fullDataSetCreation.getMetadataCreation().getCode();
+            PathInfoFeeder feeder = new PathInfoFeeder(dataSetId, dataSetCode, files);
 
             try
             {
@@ -348,10 +349,55 @@ public class DataStoreServerApi extends AbstractDssServiceRpc<IDataStoreServerAp
         for (int i = 0; i < newDataSets.size(); i++)
         {
             DataSetCreation creation = newDataSets.get(i).getMetadataCreation();
-            creation.setCode(permIds.get(i));
             metadata.add(creation);
         }
 
         return as.createDataSets(sessionToken, metadata);
     }
+    
+    private void injectDataStoreIdAndCodesIfNeeded(List<FullDataSetCreation> newDataSets)
+    {
+        String dataStoreCode = configProvider.getDataStoreCode();
+        DataStorePermId dataStoreId = new DataStorePermId(dataStoreCode);
+        int numberOfPermIdsToGenerate = 0;
+        for (int i = 0; i < newDataSets.size(); i++)
+        {
+            FullDataSetCreation fullDataSetCreation = newDataSets.get(i);
+            DataSetCreation metadataCreation = fullDataSetCreation.getMetadataCreation();
+            IDataStoreId dsid = metadataCreation.getDataStoreId();
+            if (dsid != null && dsid instanceof DataStorePermId)
+            {
+                String code = ((DataStorePermId) dsid).getPermId();
+                if (dataStoreCode.equals(code) == false)
+                {
+                    throw new UserFailureException("Data store id specified for creation object with index "
+                            + i + " is '" + code + "' instead of '" + dataStoreCode + "' or undefined.");
+                }
+            }
+            metadataCreation.setDataStoreId(dataStoreId);
+            if (metadataCreation.isAutoGeneratedCode())
+            {
+                numberOfPermIdsToGenerate++;
+            } else if (StringUtils.isBlank(metadataCreation.getCode()))
+            {
+                throw new UserFailureException("Neither code nor auto generating code specified in creation object with index " + i);
+            } else
+            {
+                metadataCreation.setCode(StringUtils.upperCase(metadataCreation.getCode()));
+            }
+        }
+        if (numberOfPermIdsToGenerate > 0)
+        {
+            List<String> permIds = getOpenBISService().createPermIds(numberOfPermIdsToGenerate);
+            for (FullDataSetCreation newDataSet : newDataSets)
+            {
+                DataSetCreation metadataCreation = newDataSet.getMetadataCreation();
+                if (metadataCreation.isAutoGeneratedCode())
+                {
+                    metadataCreation.setCode(permIds.remove(0));
+                    metadataCreation.setAutoGeneratedCode(false);
+                }
+            }
+        }
+    }
 }
diff --git a/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/pathinfo/PathInfoFeeder.java b/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/pathinfo/PathInfoFeeder.java
index fe5568fd382..82360e109b4 100644
--- a/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/pathinfo/PathInfoFeeder.java
+++ b/datastore_server/source/java/ch/ethz/sis/openbis/generic/server/dssapi/v3/pathinfo/PathInfoFeeder.java
@@ -37,8 +37,11 @@ public class PathInfoFeeder
 
     private final long dataSetId;
 
-    public PathInfoFeeder(long dataSetId, Collection<DataSetFileCreation> files)
+    private String dataSetCode;
+
+    public PathInfoFeeder(long dataSetId, String dataSetCode, Collection<DataSetFileCreation> files)
     {
+        this.dataSetCode = dataSetCode;
         checkInput(files);
         for (DataSetFileCreation file : files)
         {
@@ -55,7 +58,7 @@ public class PathInfoFeeder
 
     public void storeFilesWith(IPathsInfoDAO dao)
     {
-        storeFilesWith(dao, root, null, "", new Date());
+        storeFilesWith(dao, root, null, dataSetCode, new Date());
     }
 
     private void storeFilesWith(IPathsInfoDAO dao, DataSetContentNode node, Long parentId, String name, Date timestamp)
@@ -87,19 +90,22 @@ public class PathInfoFeeder
         for (DataSetFileCreation file : files)
         {
             checkInput(file);
-            check(paths.contains(file.getPath()), "Path " + file.getPath() + " appears twice");
+            check(paths.contains(file.getPath()), "Path '" + file.getPath() + "' appears twice");
             paths.add(file.getPath());
         }
     }
 
+    @SuppressWarnings("null")
     private static void checkInput(DataSetFileCreation file)
     {
         String path = file.getPath();
         check(path == null || path.length() == 0, "Path of " + file + " was null");
-        check(path.startsWith("/"), "Path of " + file + " is absolute");
-        check(file.isDirectory() == false && file.getFileLength() == null, "Size of " + file + " is null");
-        check(file.isDirectory() && file.getFileLength() != null, "Directory " + file + " has a size");
-        check(file.isDirectory() && file.getChecksumCRC32() != null, "Directory " + file + " has a checksum");
+        check(path.startsWith("/"), "Path '" + path + "' is absolute");
+        check(file.isDirectory() == false && file.getFileLength() == null, "Size of '" + path + "' is null");
+        check(file.isDirectory() && file.getFileLength() != null, "Directory '" + path + "' has size "
+                + file.getFileLength());
+        check(file.isDirectory() && file.getChecksumCRC32() != null, "Directory '" + path + "' has checksum "
+                + file.getChecksumCRC32());
     }
 
     private static void check(boolean condition, String message)
diff --git a/datastore_server/sourceTest/java/ch/ethz/sis/openbis/generic/dss/api/v3/PathInfoFeederTest.java b/datastore_server/sourceTest/java/ch/ethz/sis/openbis/generic/dss/api/v3/PathInfoFeederTest.java
index df849163eaf..3ab778bc7d4 100644
--- a/datastore_server/sourceTest/java/ch/ethz/sis/openbis/generic/dss/api/v3/PathInfoFeederTest.java
+++ b/datastore_server/sourceTest/java/ch/ethz/sis/openbis/generic/dss/api/v3/PathInfoFeederTest.java
@@ -240,7 +240,7 @@ public class PathInfoFeederTest
         {
             input.add(file.build());
         }
-        PathInfoFeeder creator = new PathInfoFeeder(1, input);
+        PathInfoFeeder creator = new PathInfoFeeder(1, "my-data-set", input);
 
         Mockery context = new Mockery();
         final IPathsInfoDAO mockDao = context.mock(IPathsInfoDAO.class);
-- 
GitLab