From 5598bd1ed13e6b27855a3297b764ae92ece7b6c9 Mon Sep 17 00:00:00 2001
From: Viktor Kovtun <viktor.kovtun@id.ethz.ch>
Date: Mon, 4 Jan 2021 13:43:18 +0100
Subject: [PATCH] SSDM-10566 Fixing error thrown when searching by code in some
 cases.

---
 .../CodeSearchConditionTranslator.java        | 110 +++++++++++-------
 .../systemtest/asapi/v3/SearchSampleTest.java |   9 ++
 2 files changed, 75 insertions(+), 44 deletions(-)

diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/search/translator/condition/CodeSearchConditionTranslator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/search/translator/condition/CodeSearchConditionTranslator.java
index 22cdd757fcd..514ced0f16d 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/search/translator/condition/CodeSearchConditionTranslator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/search/translator/condition/CodeSearchConditionTranslator.java
@@ -28,6 +28,8 @@ import ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.condition.u
 import ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.condition.utils.JoinInformation;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.condition.utils.TranslatorUtils;
 
+import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.mapper.TableMapper.SAMPLE;
+import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.mapper.TableMapper.TAG;
 import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.AND;
 import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.EQ;
 import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.FROM;
@@ -38,13 +40,9 @@ import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLL
 import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.SELECT;
 import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.SP;
 import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.WHERE;
-import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.CODE_COLUMN;
-import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.ID_COLUMN;
-import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.NAME_COLUMN;
-import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.OWNER_COLUMN;
-import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.PART_OF_SAMPLE_COLUMN;
-import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.USER_COLUMN;
+import static ch.systemsx.cisd.openbis.generic.shared.dto.ColumnNames.*;
 import static ch.systemsx.cisd.openbis.generic.shared.dto.TableNames.PERSONS_TABLE;
+import static ch.systemsx.cisd.openbis.generic.shared.dto.TableNames.SPACES_TABLE;
 
 public class CodeSearchConditionTranslator implements IConditionTranslator<StringFieldSearchCriteria>
 {
@@ -71,48 +69,72 @@ public class CodeSearchConditionTranslator implements IConditionTranslator<Strin
                 if (value != null && value.getValue() != null)
                 {
                     final String innerValue = value.getValue();
-
-                    final FullEntityIdentifier fullObjectIdentifier = new FullEntityIdentifier(innerValue, null);
-                    final SampleIdentifierParts identifierParts = fullObjectIdentifier.getParts();
-
-                    if (identifierParts.getProjectCodeOrNull() != null)
-                    {
-                        throw new IllegalArgumentException("There cannot be project code for this entity.");
-                    }
-
-                    final String entityCode = fullObjectIdentifier.getEntityCode();
-                    final String spaceCode = identifierParts.getSpaceCodeOrNull();
-                    final String containerCode = identifierParts.getContainerCodeOrNull();
-
-                    if (spaceCode != null)
+                    if (tableMapper == TAG || tableMapper == SAMPLE)
                     {
-                        sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(OWNER_COLUMN).append(SP).append(EQ).append(SP).
-                                append(LP);
-                        sqlBuilder.append(SELECT).append(SP).append(ID_COLUMN).append(SP).
-                                append(FROM).append(SP).append(PERSONS_TABLE).append(SP).
-                                append(WHERE).append(SP).append(USER_COLUMN).append(SP);
-                        TranslatorUtils.appendStringComparatorOp(value.getClass(), spaceCode.toLowerCase(), sqlBuilder, args);
-                        sqlBuilder.append(RP);
-                        sqlBuilder.append(SP).append(AND).append(SP);
+                        final FullEntityIdentifier fullObjectIdentifier = new FullEntityIdentifier(innerValue, null);
+                        final SampleIdentifierParts identifierParts = fullObjectIdentifier.getParts();
+
+                        if (identifierParts.getProjectCodeOrNull() != null)
+                        {
+                            throw new IllegalArgumentException("There cannot be project code for this entity.");
+                        }
+
+                        final String entityCode = fullObjectIdentifier.getEntityCode();
+                        final String spaceCode = identifierParts.getSpaceCodeOrNull();
+                        final String containerCode = identifierParts.getContainerCodeOrNull();
+
+                        if (spaceCode != null)
+                        {
+                            final String finalValue;
+                            if (tableMapper == TAG)
+                            {
+                                sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD)
+                                        .append(OWNER_COLUMN).append(SP).append(EQ).append(SP).append(LP);
+                                sqlBuilder.append(SELECT).append(SP).append(ID_COLUMN).append(SP).
+                                        append(FROM).append(SP).append(PERSONS_TABLE).append(SP).
+                                        append(WHERE).append(SP).append(USER_COLUMN).append(SP);
+                                finalValue = spaceCode.toLowerCase();
+                            } else
+                            {
+                                sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD)
+                                        .append(SPACE_COLUMN).append(SP).append(EQ).append(SP).append(LP);
+                                sqlBuilder.append(SELECT).append(SP).append(ID_COLUMN).append(SP).
+                                        append(FROM).append(SP).append(SPACES_TABLE).append(SP).
+                                        append(WHERE).append(SP).append(CODE_COLUMN).append(SP);
+                                finalValue = spaceCode;
+                            }
+
+                            TranslatorUtils.appendStringComparatorOp(value.getClass(), finalValue, sqlBuilder, args);
+                            sqlBuilder.append(RP);
+                            sqlBuilder.append(SP).append(AND).append(SP);
+                        }
+
+                        if (containerCode != null)
+                        {
+                            sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).
+                                    append(PART_OF_SAMPLE_COLUMN).append(SP).
+                                    append(EQ).append(SP).append(LP).
+                                    append(SELECT).append(SP).append(ID_COLUMN).append(SP).append(FROM).append(SP).
+                                    append(tableMapper.getEntitiesTable()).append(SP).
+                                    append(WHERE).append(SP).append(columnName).append(SP);
+                            TranslatorUtils.appendStringComparatorOp(value.getClass(), containerCode, sqlBuilder, args);
+
+                            sqlBuilder.append(RP).append(SP).append(AND).append(SP);
+                        }
+
+                        sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(columnName)
+                                .append(SP);
+                        TranslatorUtils.appendStringComparatorOp(value.getClass(), entityCode, sqlBuilder, args);
+                    } else {
+                        sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(columnName)
+                                .append(SP);
+                        TranslatorUtils.appendStringComparatorOp(value.getClass(), innerValue.toUpperCase(), sqlBuilder,
+                                args);
                     }
-
-                    if (containerCode != null)
-                    {
-                        sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(PART_OF_SAMPLE_COLUMN).append(SP).
-                                append(EQ).append(SP).append(LP).
-                                append(SELECT).append(SP).append(ID_COLUMN).append(SP).append(FROM).append(SP).
-                                append(tableMapper.getEntitiesTable()).append(SP).
-                                append(WHERE).append(SP).append(columnName).append(SP);
-                        TranslatorUtils.appendStringComparatorOp(value.getClass(), containerCode, sqlBuilder, args);
-
-                        sqlBuilder.append(RP).append(SP).append(AND).append(SP);
-                    }
-
-                    sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(columnName).append(SP);
-                    TranslatorUtils.appendStringComparatorOp(value.getClass(), entityCode, sqlBuilder, args);
                 } else
                 {
-                    sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(columnName).append(SP).append(IS_NOT_NULL);
+                    sqlBuilder.append(SearchCriteriaTranslator.MAIN_TABLE_ALIAS).append(PERIOD).append(columnName)
+                            .append(SP).append(IS_NOT_NULL);
                 }
                 break;
             }
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java
index 3c1f54352ce..cd08cf96ecd 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/asapi/v3/SearchSampleTest.java
@@ -299,6 +299,15 @@ public class SearchSampleTest extends AbstractSampleTest
         testSearch(TEST_USER, criteria, "/CISD/RP1-A2X");
     }
 
+    @Test
+    public void testSearchWithCodeWithSpace()
+    {
+        final SampleSearchCriteria criteria = new SampleSearchCriteria();
+        criteria.withCode().thatEquals("/CISD/RP1-A2X");
+
+        testSearch(TEST_USER, criteria, "/CISD/RP1-A2X");
+    }
+
     @Test
     public void testSearchWithCodeThatIsLessOrEqualTo()
     {
-- 
GitLab