From 39dc72e1211e59b875910ea69aea3b4b9b36735d Mon Sep 17 00:00:00 2001
From: juanf <juanf@bsse-bs-dock-dhcp-358.ethz.ch>
Date: Tue, 25 Sep 2018 12:45:40 +0200
Subject: [PATCH] SSDM-7235 : Continuous sample codes, project samples support

---
 .../parser/SampleUploadSectionsParser.java    | 27 ++++++---
 .../web/server/GenericClientService.java      | 14 ++++-
 .../plugin/generic/server/GenericServer.java  | 57 +++++++++++++++----
 3 files changed, 78 insertions(+), 20 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java
index ba0386f3962..45319af47c0 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/SampleUploadSectionsParser.java
@@ -25,6 +25,8 @@ import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
 
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.id.ProjectIdentifier;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.id.SampleIdentifier;
 import ch.systemsx.cisd.common.io.DelegatedReader;
 import ch.systemsx.cisd.common.parser.ExcelFileLoader;
 import ch.systemsx.cisd.common.parser.IParserObjectFactory;
@@ -85,7 +87,9 @@ public class SampleUploadSectionsParser
         List<String> generateCodes(int size);
     }
 
-    public static BatchSamplesOperation prepareSamples(final SampleType sampleType,
+    public static BatchSamplesOperation prepareSamples(
+    			final boolean projectSamplesEnabled,
+    			final SampleType sampleType,
             final String spaceIdentifierSilentOverrideOrNull,
             final String experimentIdentifierSilentOverrideOrNull,
             final Collection<NamedInputStream> files, String defaultGroupIdentifier,
@@ -107,7 +111,7 @@ public class SampleUploadSectionsParser
                 case REGISTRATION:
                     if (isAutoGenerateCodes)
                     {
-                        generateIdentifiers(defaultGroupIdentifier, sampleCodeGeneratorOrNull,
+                        generateIdentifiers(projectSamplesEnabled, defaultGroupIdentifier, sampleCodeGeneratorOrNull,
                                 isAutoGenerateCodes, newSamples);
                     }
                     break;
@@ -332,7 +336,8 @@ public class SampleUploadSectionsParser
         return results;
     }
 
-    private static void generateIdentifiers(String defaultGroupIdentifier,
+    private static void generateIdentifiers(boolean projectSamplesEnabled,
+    		String defaultGroupIdentifier,
             SampleCodeGenerator sampleCodeGenerator, boolean isAutoGenerateCodes,
             List<NewSamplesWithTypes> newSamplesWithTypes)
     {
@@ -344,14 +349,22 @@ public class SampleUploadSectionsParser
             List<String> codes = sampleCodeGenerator.generateCodes(newSamples.size());
             for (int i = 0; i < newSamples.size(); i++)
             {
-                if (newSamples.get(i).getDefaultSpaceIdentifier() == null || newSamples.get(i).getDefaultSpaceIdentifier().isEmpty())
+            		String spaceCodeOrNull = null;
+            		if (newSamples.get(i).getDefaultSpaceIdentifier() == null || newSamples.get(i).getDefaultSpaceIdentifier().isEmpty())
                 {
-                    newSamples.get(i).setIdentifier(defaultGroupIdentifier + "/" + codes.get(i));
+            			spaceCodeOrNull = defaultGroupIdentifier;
                 } else
                 {
-                    newSamples.get(i).setIdentifier(
-                            newSamples.get(i).getDefaultSpaceIdentifier() + "/" + codes.get(i));
+                		spaceCodeOrNull = newSamples.get(i).getDefaultSpaceIdentifier();
                 }
+            		spaceCodeOrNull = spaceCodeOrNull.substring(1);
+            		String projectCodeOrNull = null;
+            		if(projectSamplesEnabled && newSamples.get(i).getExperimentIdentifier() != null && !newSamples.get(i).getExperimentIdentifier().isEmpty()) {
+            			String[] experimentIdentifierParts = newSamples.get(i).getExperimentIdentifier().split("/");
+            			projectCodeOrNull = experimentIdentifierParts[experimentIdentifierParts.length - 2];
+            		}
+            		SampleIdentifier identifier = new SampleIdentifier(spaceCodeOrNull, projectCodeOrNull, null, codes.get(i));
+            		newSamples.get(i).setIdentifier(identifier.getIdentifier());
             }
         }
     }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java
index 2e25dc08210..880f76470a7 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java
@@ -26,6 +26,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
 import javax.annotation.Resource;
@@ -39,6 +40,7 @@ import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.common.exceptions.ExceptionUtils;
 import ch.systemsx.cisd.common.properties.PropertyUtils;
 import ch.systemsx.cisd.common.servlet.IRequestContextProvider;
+import ch.systemsx.cisd.common.spring.ExposablePropertyPlaceholderConfigurer;
 import ch.systemsx.cisd.openbis.common.spring.IUncheckedMultipartFile;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.DataSetUpdates;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SampleUpdates;
@@ -114,6 +116,9 @@ import ch.systemsx.cisd.openbis.plugin.generic.shared.ResourceNames;
 public class GenericClientService extends AbstractClientService implements IGenericClientService, IEntityImportService
 {
 
+	@Resource(name = ExposablePropertyPlaceholderConfigurer.PROPERTY_CONFIGURER_BEAN_NAME)
+    private ExposablePropertyPlaceholderConfigurer configurer;
+	
     @Resource(name = ResourceNames.GENERIC_PLUGIN_SERVER)
     private IGenericServerInternal genericServer;
 
@@ -592,9 +597,11 @@ public class GenericClientService extends AbstractClientService implements IGene
         {
             files.add(new NamedInputStream(f.getInputStream(), f.getOriginalFilename()));
         }
-
+        	
+        boolean projectSamplesEnabled = PropertyUtils.getBoolean(configurer.getResolvedProps(), Constants.PROJECT_SAMPLES_ENABLED_KEY, false);
+        
         BatchSamplesOperation batchSamplesOperation =
-                SampleUploadSectionsParser.prepareSamples(sampleType, spaceIdentifierSilentOverrideOrNull, experimentIdentifierSilentOverrideOrNull,
+                SampleUploadSectionsParser.prepareSamples(projectSamplesEnabled, sampleType, spaceIdentifierSilentOverrideOrNull, experimentIdentifierSilentOverrideOrNull,
                         files,
                         defaultGroupIdentifier, sampleCodeGeneratorOrNull, allowExperiments,
                         excelSheetName, operationKind);
@@ -1372,7 +1379,10 @@ public class GenericClientService extends AbstractClientService implements IGene
         Map<String, Object> info = new HashMap<String, Object>();
         try
         {
+        		boolean projectSamplesEnabled = PropertyUtils.getBoolean(configurer.getResolvedProps(), Constants.PROJECT_SAMPLES_ENABLED_KEY, false);
+        	
             BatchSamplesOperation batchSamplesOperation = SampleUploadSectionsParser.prepareSamples(
+            			projectSamplesEnabled,
                     sampleType,
                     null,
                     null,
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericServer.java
index fdb3181bd20..02fc32bdf6a 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericServer.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/server/GenericServer.java
@@ -68,6 +68,7 @@ import ch.systemsx.cisd.openbis.generic.server.business.bo.SampleCodeGeneratorBy
 import ch.systemsx.cisd.openbis.generic.server.business.bo.SampleCodeGeneratorForTempCodes;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.samplelister.ISampleLister;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.IProjectDAO;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.ISampleDAO;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.ISpaceDAO;
 import ch.systemsx.cisd.openbis.generic.server.plugin.IDataSetTypeSlaveServerPlugin;
@@ -111,6 +112,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentTypePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentUpdatesDTO;
 import ch.systemsx.cisd.openbis.generic.shared.dto.MetaprojectPE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.ProjectPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SampleBatchUpdatesDTO;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SampleTypePE;
@@ -1041,9 +1043,12 @@ public final class GenericServer extends AbstractServer<IGenericServerInternal>
     public void updateTemporaryCodes(String sessionToken, Map<String, List<String>> sampleTypeCodeToTemporaryIdentifiers)
     {
         ISpaceDAO spaceDAO = getDAOFactory().getSpaceDAO();
+        IProjectDAO projectDAO = getDAOFactory().getProjectDAO();
         ISampleDAO sampleDAO = getDAOFactory().getSampleDAO();
         Map<String, SpacePE> spaceCache = new HashMap<String, SpacePE>();
+        Map<String, ProjectPE> projectCache = new HashMap<String, ProjectPE>();
         Map<String, List<String>> spaceToOldSampleCodes = new HashMap<String, List<String>>();
+        Map<String, List<String>> projectToOldSampleCodes = new HashMap<String, List<String>>();
         Map<String, String> identifiersToNewSampleCodes = new HashMap<String, String>();
 
         // Generate Updates for each space and build helper maps
@@ -1060,23 +1065,48 @@ public final class GenericServer extends AbstractServer<IGenericServerInternal>
                 String tempIdentifier = temporaryIdentifiers.get(tIdx);
                 String spaceCode = tempIdentifier.split("/")[1];
                 String tempSampleCode = tempIdentifier.substring(tempIdentifier.lastIndexOf('/') + 1, tempIdentifier.length());
-                SpacePE spacePE = spaceCache.get(spaceCode);
-                if (spacePE == null)
-                {
-                    spacePE = spaceDAO.tryFindSpaceByCode(spaceCode);
-                    spaceCache.put(spaceCode, spacePE);
+                String[] tempIdentifierParts = tempIdentifier.split("/");
+                String projectIdentifier = null;
+                String projectCode = null;
+                if(tempIdentifierParts.length == 4) {
+                		projectCode = tempIdentifierParts[2];
+                		projectIdentifier = "/" + spaceCode + "/" + projectCode;
                 }
-                List<String> oldSampleCodes = spaceToOldSampleCodes.get(spaceCode);
-                if (oldSampleCodes == null)
-                {
-                    oldSampleCodes = new ArrayList<String>();
-                    spaceToOldSampleCodes.put(spaceCode, oldSampleCodes);
+                
+                List<String> oldSampleCodes = null;
+                if(projectIdentifier != null) {
+                		ProjectPE projectPE = projectCache.get(projectIdentifier);
+                    if(projectPE == null) {
+                    		projectPE = projectDAO.tryFindProject(spaceCode, projectCode);
+                    		projectCache.put(projectIdentifier, projectPE);
+                    }
+                    
+                    oldSampleCodes = projectToOldSampleCodes.get(projectIdentifier);
+                    if (oldSampleCodes == null)
+                    {
+                        oldSampleCodes = new ArrayList<String>();
+                        projectToOldSampleCodes.put(projectIdentifier, oldSampleCodes);
+                    }
+                } else {
+                		SpacePE spacePE = spaceCache.get(spaceCode);
+                    if (spacePE == null)
+                    {
+                        spacePE = spaceDAO.tryFindSpaceByCode(spaceCode);
+                        spaceCache.put(spaceCode, spacePE);
+                    }
+                    
+                    oldSampleCodes = spaceToOldSampleCodes.get(spaceCode);
+                    if (oldSampleCodes == null)
+                    {
+                        oldSampleCodes = new ArrayList<String>();
+                        spaceToOldSampleCodes.put(spaceCode, oldSampleCodes);
+                    }
                 }
                 oldSampleCodes.add(tempSampleCode);
                 identifiersToNewSampleCodes.put(tempIdentifier, newCodes.get(tIdx));
             }
         }
-
+        
         // Retrieve all sample PE objects
         List<SamplePE> allSamplesPE = new ArrayList<SamplePE>();
         for (String spaceCode : spaceToOldSampleCodes.keySet())
@@ -1084,6 +1114,11 @@ public final class GenericServer extends AbstractServer<IGenericServerInternal>
             List<SamplePE> samples = sampleDAO.listByCodesAndSpace(spaceToOldSampleCodes.get(spaceCode), null, spaceCache.get(spaceCode));
             allSamplesPE.addAll(samples);
         }
+        for (String projectIdentifier : projectToOldSampleCodes.keySet())
+        {
+            List<SamplePE> samples = sampleDAO.listByCodesAndProject(projectToOldSampleCodes.get(projectIdentifier), null, projectCache.get(projectIdentifier));
+            allSamplesPE.addAll(samples);
+        }
         // Update all codes in sample PE objects
         for (SamplePE samplePE : allSamplesPE)
         {
-- 
GitLab