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 02afefe6d5c3fc76312481d892e99f82c008f083..2c03489ec002267444abf9fd0f744dce7f6cfcca 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 @@ -30,16 +30,7 @@ import ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.condition.u 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; -import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.IS_NOT_NULL; -import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.LP; -import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.PERIOD; -import static ch.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.RP; -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.ethz.sis.openbis.generic.server.asapi.v3.search.translator.SQLLexemes.*; 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; @@ -71,54 +62,71 @@ public class CodeSearchConditionTranslator implements IConditionTranslator<Strin final String innerValue = value.getValue(); if (tableMapper == TAG || tableMapper == SAMPLE) { - final FullEntityIdentifier fullObjectIdentifier = new FullEntityIdentifier(innerValue, null); - final SampleIdentifierParts identifierParts = fullObjectIdentifier.getParts(); - final String entityCode = fullObjectIdentifier.getEntityCode(); - final String spaceCode = identifierParts.getSpaceCodeOrNull(); - final String containerCode = identifierParts.getContainerCodeOrNull(); + FullEntityIdentifier fullObjectIdentifier; + try + { + fullObjectIdentifier = new FullEntityIdentifier(innerValue, null); + } catch (final IllegalArgumentException e) + { + fullObjectIdentifier = null; + } - if (spaceCode != null) + if (fullObjectIdentifier != 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 + final SampleIdentifierParts identifierParts = fullObjectIdentifier.getParts(); + final String entityCode = fullObjectIdentifier.getEntityCode(); + final String spaceCode = identifierParts.getSpaceCodeOrNull(); + final String containerCode = identifierParts.getContainerCodeOrNull(); + + if (spaceCode != null) { - 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; + 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); } - 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); + } - if (containerCode != null) + 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(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(FALSE); } - - 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); 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 cd08cf96ecdd83a2bb619627f6cb83ca87d9ef3d..fc51efd8614f07a2c2b2c4cd50e4fa8e6651d3ad 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 @@ -304,10 +304,21 @@ public class SearchSampleTest extends AbstractSampleTest { final SampleSearchCriteria criteria = new SampleSearchCriteria(); criteria.withCode().thatEquals("/CISD/RP1-A2X"); - testSearch(TEST_USER, criteria, "/CISD/RP1-A2X"); } + @Test + public void testSearchWithIncorrectCode() + { + final SampleSearchCriteria criteria1 = new SampleSearchCriteria(); + criteria1.withCode().thatEquals("/CISD/RP1-A2X/ABCD"); + testSearch(TEST_USER, criteria1); + + final SampleSearchCriteria criteria2 = new SampleSearchCriteria(); + criteria2.withCode().thatEquals("/A/B/C/D/E"); + testSearch(TEST_USER, criteria2); + } + @Test public void testSearchWithCodeThatIsLessOrEqualTo() { @@ -401,6 +412,14 @@ public class SearchSampleTest extends AbstractSampleTest testSearch(TEST_USER, criteria, "/CISD/RP1-A2X", "/CISD/RP1-B1X"); } + @Test + public void testSearchWithIncorrectCodes() + { + SampleSearchCriteria criteria = new SampleSearchCriteria(); + criteria.withCodes().thatIn(Arrays.asList("/CISD/RP1-A2X/ABC", "/A/B/C/D")); + testSearch(TEST_USER, criteria); + } + @Test public void testSearchWithCodesEmpty() {