From d640638088c64966639472135574ccede816c8c5 Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Tue, 6 Aug 2013 14:21:26 +0000
Subject: [PATCH] SP-781 / BIS-496: data-set-uploader fails to start on
 biozentrum - fix a bug where listing experiments didn't work properly with a
 mixture of project identifiers with and without database instance codes

SVN: 29606
---
 .../server/business/bo/ExperimentTable.java   |  7 +--
 .../server/dataaccess/db/ProjectDAO.java      |  7 +--
 .../dto/identifier/IdentifierHelper.java      | 50 ++++++++++++++-----
 .../api/v1/GeneralInformationServiceTest.java |  2 +-
 4 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/ExperimentTable.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/ExperimentTable.java
index 029b661a1e5..8a871f4dca1 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/ExperimentTable.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/ExperimentTable.java
@@ -50,6 +50,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.Session;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SpacePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifier;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifierFactory;
+import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.IdentifierHelper;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ProjectIdentifier;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SpaceIdentifier;
 import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind;
@@ -174,7 +175,7 @@ public final class ExperimentTable extends AbstractBusinessObject implements IEx
 
     private void checkNotNull(final List<ProjectIdentifier> projectIdentifiers, final List<ProjectPE> projects)
     {
-        Set<String> unknownProjectIdentifiers = new HashSet<String>();
+        Set<ProjectIdentifier> unknownProjectIdentifiers = new HashSet<ProjectIdentifier>();
 
         if (projectIdentifiers != null)
         {
@@ -182,7 +183,7 @@ public final class ExperimentTable extends AbstractBusinessObject implements IEx
             {
                 if (projectIdentifier != null)
                 {
-                    unknownProjectIdentifiers.add(projectIdentifier.toString());
+                    unknownProjectIdentifiers.add(IdentifierHelper.createFullProjectIdentifier(projectIdentifier, getHomeDatabaseInstance()));
                 }
             }
         }
@@ -193,7 +194,7 @@ public final class ExperimentTable extends AbstractBusinessObject implements IEx
             {
                 if (project != null)
                 {
-                    unknownProjectIdentifiers.remove(project.getIdentifier());
+                    unknownProjectIdentifiers.remove(IdentifierHelper.createFullProjectIdentifier(project));
                 }
             }
         }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ProjectDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ProjectDAO.java
index 2bb80380db2..b06f5cd49c9 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ProjectDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/ProjectDAO.java
@@ -39,6 +39,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.ProjectPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.SpacePE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.IdentifierHelper;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ProjectIdentifier;
 
 /**
@@ -129,15 +130,15 @@ public class ProjectDAO extends AbstractGenericEntityDAO<ProjectPE> implements I
         List<ProjectPE> allProjects = listProjects();
         List<ProjectPE> matchingProjects = new LinkedList<ProjectPE>();
 
-        Set<String> projectIdentifiersSet = new HashSet<String>();
+        Set<ProjectIdentifier> projectIdentifiersSet = new HashSet<ProjectIdentifier>();
         for (ProjectIdentifier projectIdentifier : projectIdentifiers)
         {
-            projectIdentifiersSet.add(projectIdentifier.toString());
+            projectIdentifiersSet.add(IdentifierHelper.createFullProjectIdentifier(projectIdentifier, getDatabaseInstance()));
         }
 
         for (ProjectPE project : allProjects)
         {
-            if (projectIdentifiersSet.contains(project.getIdentifier()))
+            if (projectIdentifiersSet.contains(IdentifierHelper.createFullProjectIdentifier(project)))
             {
                 matchingProjects.add(project);
             }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/identifier/IdentifierHelper.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/identifier/IdentifierHelper.java
index 3a99377113f..015f68afadd 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/identifier/IdentifierHelper.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/identifier/IdentifierHelper.java
@@ -192,8 +192,8 @@ public final class IdentifierHelper
     }
 
     /**
-     * Extracts "sub" code from {@link SamplePE} that is in exactly the same as the one kept in DB
-     * (so the same one that is mapped with {@link SamplePE#getCode()}).
+     * Extracts "sub" code from {@link SamplePE} that is in exactly the same as the one kept in DB (so the same one that is mapped with
+     * {@link SamplePE#getCode()}).
      */
     public final static String extractSubCode(SamplePE samplePE)
     {
@@ -201,8 +201,7 @@ public final class IdentifierHelper
     }
 
     /**
-     * Converts "sub" code from <var>sampleCode</var> that is in exactly the same as the one kept in
-     * DB.
+     * Converts "sub" code from <var>sampleCode</var> that is in exactly the same as the one kept in DB.
      */
     public final static String convertSubCode(String sampleCode)
     {
@@ -210,9 +209,8 @@ public final class IdentifierHelper
     }
 
     /**
-     * Extracts "full" sample code from {@link SamplePE}. For contained samples has a prefix
-     * consisting of container sample DB code and a colon, otherwise it is just sample DB code,
-     * where by "sample DB code" is the code kept in the DB.
+     * Extracts "full" sample code from {@link SamplePE}. For contained samples has a prefix consisting of container sample DB code and a colon,
+     * otherwise it is just sample DB code, where by "sample DB code" is the code kept in the DB.
      */
     public final static String extractCode(SamplePE samplePE)
     {
@@ -232,11 +230,9 @@ public final class IdentifierHelper
     }
 
     /**
-     * Converts "full" sample code from the <var>sampleCode</var> and
-     * <var>containerCodeOrNull</var>. For contained samples (i.e.
-     * <code>containerCodeOrNull != null</code>) has a prefix consisting of container sample DB code
-     * and a colon, otherwise it is just sample DB code, where by "sample DB code" is the code kept
-     * in the DB.
+     * Converts "full" sample code from the <var>sampleCode</var> and <var>containerCodeOrNull</var>. For contained samples (i.e.
+     * <code>containerCodeOrNull != null</code>) has a prefix consisting of container sample DB code and a colon, otherwise it is just sample DB code,
+     * where by "sample DB code" is the code kept in the DB.
      */
     public final static String convertCode(String sampleCode, String containerCodeOrNull)
     {
@@ -269,6 +265,36 @@ public final class IdentifierHelper
         return identifier;
     }
 
+    /**
+     * Creates a full {@link ProjectIdentifier} (i.e. identifier with database instance, space and project codes specified) from given
+     * <var>project</var>.
+     */
+    public final static ProjectIdentifier createFullProjectIdentifier(final ProjectPE project)
+    {
+        assert project != null : "Unspecified project";
+        return new ProjectIdentifier(project.getSpace().getDatabaseInstance().getCode(), project.getSpace().getCode(), project.getCode());
+    }
+
+    /**
+     * Creates a full {@link ProjectIdentifier} (i.e. identifier with database instance, space and project codes specified) from given
+     * <var>projectIdentifier</var> and <var>homeDatabaseInstance</var>.
+     */
+    public final static ProjectIdentifier createFullProjectIdentifier(final ProjectIdentifier projectIdentifier,
+            DatabaseInstancePE homeDatabaseInstance)
+    {
+        assert projectIdentifier != null : "Unspecified project identifier";
+        assert homeDatabaseInstance != null : "Unspecified home database instance";
+
+        if (projectIdentifier.isHomeDatabase())
+        {
+            return new ProjectIdentifier(homeDatabaseInstance.getCode(), projectIdentifier.getSpaceCode(), projectIdentifier.getProjectCode());
+        } else
+        {
+            return new ProjectIdentifier(projectIdentifier.getDatabaseInstanceCode(), projectIdentifier.getSpaceCode(),
+                    projectIdentifier.getProjectCode());
+        }
+    }
+
     /**
      * Creates a {@link ExperimentIdentifier} from given <var>experiment</var>.
      */
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java
index bdc13d4c94c..33590cb25c4 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/api/v1/GeneralInformationServiceTest.java
@@ -581,7 +581,7 @@ public class GeneralInformationServiceTest extends AbstractServerTestCase
                     ProjectIdentifier projectIdentifier =
                             new ProjectIdentifier("SPACE-1", "PROJECT-1");
                     one(commonServer).listExperiments(SESSION_TOKEN, returnExperimentType,
-                            projectIdentifier);
+                            Collections.singletonList(projectIdentifier));
 
                     Person registrator = new Person();
                     registrator.setEmail("mail@mail.com");
-- 
GitLab