From 4ba8d57c42bc9955fa748d4f3678762540414699 Mon Sep 17 00:00:00 2001 From: Katarina Valalikova Date: Tue, 13 Jun 2017 11:53:30 +0200 Subject: [PATCH] fix for MID-3439 - added support for expression for substring filter. --- .../prism/marshaller/QueryConvertor.java | 63 ++++++++++++------- .../midpoint/prism/query/SubstringFilter.java | 8 ++- .../midpoint/schema/TestQueryConvertor.java | 4 +- ...-substring-anchor-start-end-expression.xml | 35 +++++++++++ .../filter-user-substring-expression.xml | 33 ++++++++++ 5 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 infra/schema/src/test/resources/queryconvertor/filter-user-substring-anchor-start-end-expression.xml create mode 100644 infra/schema/src/test/resources/queryconvertor/filter-user-substring-expression.xml diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/QueryConvertor.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/QueryConvertor.java index 4816ec2475b..3ad445d26b6 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/QueryConvertor.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/QueryConvertor.java @@ -509,7 +509,7 @@ private static RefFilter parseRefFilter(MapXNode claus private static ExpressionWrapper parseExpression(MapXNode xmap, PrismContext prismContext) throws SchemaException { Entry expressionEntry = xmap.getSingleEntryThatDoesNotMatch( - ELEMENT_VALUE, ELEMENT_MATCHING, ELEMENT_PATH); + ELEMENT_VALUE, ELEMENT_MATCHING, ELEMENT_ANCHOR_START, ELEMENT_ANCHOR_END, ELEMENT_PATH); return PrismUtil.parseExpression(expressionEntry, prismContext); } @@ -517,18 +517,12 @@ private static SubstringFilter parseSubstringFilter(Ma throws SchemaException { ItemPath itemPath = getPath(clauseXMap); - if (itemPath == null || itemPath.isEmpty()){ - throw new SchemaException("Could not convert query, because query does not contain item path."); + if (itemPath == null || itemPath.isEmpty()) { + throw new SchemaException("Could not convert query, because query does not contain item path."); } QName itemName = ItemPath.getName(itemPath.last()); QName matchingRule = getMatchingRule(clauseXMap); - XNode valueXnode = clauseXMap.get(ELEMENT_VALUE); - - ItemDefinition itemDefinition = locateItemDefinition(valueXnode, itemPath, pcd, prismContext); - - Item item = parseItem(new RootXNode(ELEMENT_VALUE, valueXnode), itemName, itemDefinition, prismContext); - Boolean anchorStart = clauseXMap.getParsedPrimitiveValue(ELEMENT_ANCHOR_START, DOMUtil.XSD_BOOLEAN); if (anchorStart == null) { anchorStart = false; @@ -539,21 +533,46 @@ private static SubstringFilter parseSubstringFilter(Ma anchorEnd = false; } - if (preliminaryParsingOnly) { - return null; - } else { - List values = item.getValues(); - Object realValue; - if (values == null || values.isEmpty()) { - realValue = null; // TODO throw an exception? - } else if (values.size() > 1) { - throw new IllegalArgumentException("Expected at most 1 value, got " + values); + XNode valueXnode = clauseXMap.get(ELEMENT_VALUE); + ItemDefinition itemDefinition = locateItemDefinition(valueXnode, itemPath, pcd, prismContext); + + if (valueXnode != null) { + + Item item = parseItem(new RootXNode(ELEMENT_VALUE, valueXnode), itemName, itemDefinition, prismContext); + + if (preliminaryParsingOnly) { + return null; } else { - realValue = ((PrismPropertyValue) values.get(0)).getValue(); + List values = item.getValues(); + Object realValue; + if (values == null || values.isEmpty()) { + realValue = null; // TODO throw an exception? + } else if (values.size() > 1) { + throw new IllegalArgumentException("Expected at most 1 value, got " + values); + } else { + realValue = ((PrismPropertyValue) values.get(0)).getValue(); + } + return SubstringFilter.createSubstring(itemPath, (PrismPropertyDefinition) itemDefinition, prismContext, + matchingRule, realValue, anchorStart, anchorEnd); + } - return SubstringFilter.createSubstring(itemPath, (PrismPropertyDefinition) itemDefinition, - prismContext, matchingRule, realValue, anchorStart, anchorEnd); - } + } else { + ExpressionWrapper expressionWrapper = parseExpression(clauseXMap, prismContext); + if (expressionWrapper != null) { + if (preliminaryParsingOnly) { + return null; + } else { + return SubstringFilter.createSubstring(itemPath, (PrismPropertyDefinition) itemDefinition, prismContext, matchingRule, expressionWrapper, anchorStart, anchorEnd); + } + } else { + if (preliminaryParsingOnly) { + return null; + } else { + return SubstringFilter.createSubstring(itemPath, (PrismPropertyDefinition) itemDefinition, prismContext, matchingRule, + (ExpressionWrapper) null, anchorStart, anchorEnd); + } + } + } } private static OrgFilter parseOrgFilter(MapXNode clauseXMap, PrismContainerDefinition pcd, boolean preliminaryParsingOnly, PrismContext prismContext) throws SchemaException { diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/query/SubstringFilter.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/query/SubstringFilter.java index 81d40863e7b..a36d916ee7b 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/query/SubstringFilter.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/query/SubstringFilter.java @@ -57,8 +57,12 @@ public static SubstringFilter createSubstring(@NotNull ItemPath path, @Nu List> values = anyValueToPropertyValueList(prismContext, anyValue); return new SubstringFilter<>(path, itemDefinition, matchingRule, values, null, anchorStart, anchorEnd); } - - // TODO expression based substring filter + + public static SubstringFilter createSubstring(@NotNull ItemPath path, @Nullable PrismPropertyDefinition itemDefinition, + @NotNull PrismContext prismContext, + @Nullable QName matchingRule, ExpressionWrapper expressionWrapper, boolean anchorStart, boolean anchorEnd) { + return new SubstringFilter<>(path, itemDefinition, matchingRule, null, expressionWrapper, anchorStart, anchorEnd); + } public boolean isAnchorStart() { return anchorStart; diff --git a/infra/schema/src/test/java/com/evolveum/midpoint/schema/TestQueryConvertor.java b/infra/schema/src/test/java/com/evolveum/midpoint/schema/TestQueryConvertor.java index 8ef7447445c..cb809a0326a 100644 --- a/infra/schema/src/test/java/com/evolveum/midpoint/schema/TestQueryConvertor.java +++ b/infra/schema/src/test/java/com/evolveum/midpoint/schema/TestQueryConvertor.java @@ -400,7 +400,9 @@ public void testUserQuery() throws Exception { File[] userQueriesToTest = new File[] { new File(TEST_DIR, "filter-user-by-fullName.xml"), new File(TEST_DIR, "filter-user-by-name.xml"), new File(TEST_DIR, "filter-user-substring-fullName.xml"), - new File(TEST_DIR, "filter-user-substring-employeeType.xml") + new File(TEST_DIR, "filter-user-substring-employeeType.xml"), + new File(TEST_DIR, "filter-user-substring-expression.xml"), + new File(TEST_DIR, "filter-user-substring-anchor-start-end-expression.xml") }; // prismContext.silentMarshalObject(queryTypeNew, LOGGER); for (File file : userQueriesToTest) { diff --git a/infra/schema/src/test/resources/queryconvertor/filter-user-substring-anchor-start-end-expression.xml b/infra/schema/src/test/resources/queryconvertor/filter-user-substring-anchor-start-end-expression.xml new file mode 100644 index 00000000000..b45fd142683 --- /dev/null +++ b/infra/schema/src/test/resources/queryconvertor/filter-user-substring-anchor-start-end-expression.xml @@ -0,0 +1,35 @@ + + + + + + + + c:employeeType + + + + return "12345" + + + + false + false + + + diff --git a/infra/schema/src/test/resources/queryconvertor/filter-user-substring-expression.xml b/infra/schema/src/test/resources/queryconvertor/filter-user-substring-expression.xml new file mode 100644 index 00000000000..10ab4445ae0 --- /dev/null +++ b/infra/schema/src/test/resources/queryconvertor/filter-user-substring-expression.xml @@ -0,0 +1,33 @@ + + + + + + + + c:employeeType + + + + return "12345" + + + + + +