Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed May 5, 2014
2 parents 72fcc59 + 237f824 commit e048a27
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 85 deletions.
Expand Up @@ -86,9 +86,12 @@ public class QueryConvertor {
public static final QName KEY_FILTER_SUBSTRING = new QName(NS_QUERY, "substring");
public static final QName KEY_FILTER_ORG = new QName(NS_QUERY, "org");

private static final QName KEY_FILTER_EQUALS_PATH = new QName(NS_QUERY, "path");
private static final QName KEY_FILTER_EQUALS_MATCHING = new QName(NS_QUERY, "matching");
private static final QName KEY_FILTER_EQUALS_VALUE = new QName(NS_QUERY, "value");
private static final QName KEY_FILTER_EQUAL_PATH = new QName(NS_QUERY, "path");
private static final QName KEY_FILTER_EQUAL_MATCHING = new QName(NS_QUERY, "matching");
private static final QName KEY_FILTER_EQUAL_VALUE = new QName(NS_QUERY, "value");

public static final QName KEY_FILTER_SUBSTRING_ANCHOR_START = new QName(NS_QUERY, "anchorStart");
public static final QName KEY_FILTER_SUBSTRING_ANCHOR_END = new QName(NS_QUERY, "anchorEnd");

public static final QName KEY_FILTER_ORG_REF = new QName(NS_QUERY, "orgRef");
public static final QName KEY_FILTER_ORG_REF_OID = new QName(NS_QUERY, "oid");
Expand Down Expand Up @@ -280,7 +283,7 @@ private static <T,C extends Containerable> EqualsFilter<PrismPropertyDefinition<
// parentPath = null;
// }

XNode valueXnode = xmap.get(KEY_FILTER_EQUALS_VALUE);
XNode valueXnode = xmap.get(KEY_FILTER_EQUAL_VALUE);



Expand All @@ -296,7 +299,7 @@ private static <T,C extends Containerable> EqualsFilter<PrismPropertyDefinition<

} else {
Entry<QName,XNode> expressionEntry = xmap.getSingleEntryThatDoesNotMatch(
KEY_FILTER_EQUALS_VALUE, KEY_FILTER_EQUALS_MATCHING, KEY_FILTER_EQUALS_PATH);
KEY_FILTER_EQUAL_VALUE, KEY_FILTER_EQUAL_MATCHING, KEY_FILTER_EQUAL_PATH);
if (expressionEntry != null) {
ExpressionWrapper expressionWrapper = new ExpressionWrapper();
PrismPropertyValue expressionPropertyValue = prismContext.getXnodeProcessor().parsePrismPropertyFromGlobalXNodeValue(expressionEntry);
Expand Down Expand Up @@ -338,7 +341,7 @@ private static <C extends Containerable> RefFilter parseRefFilter(XNode xnode, P
}
}

XNode valueXnode = xmap.get(KEY_FILTER_EQUALS_VALUE);
XNode valueXnode = xmap.get(KEY_FILTER_EQUAL_VALUE);

Item<?> item = pcd.getPrismContext().getXnodeProcessor().parseItem(valueXnode, itemName, itemDefinition);
PrismReference ref = (PrismReference)item;
Expand Down Expand Up @@ -368,30 +371,24 @@ private static <C extends Containerable> SubstringFilter parseSubstringFilter(XN
throw new SchemaException("Cannot convert query, becasue query does not contian property path.");
}
QName itemName = ItemPath.getName(itemPath.last());
// ItemPath parentPath = itemPath.allExceptLast();
// if (parentPath.isEmpty()){
// parentPath = null;
// }

XNode valueXnode = xmap.get(KEY_FILTER_EQUALS_VALUE);
XNode valueXnode = xmap.get(KEY_FILTER_EQUAL_VALUE);

ItemDefinition itemDefinition = locateItemDefinition(valueXnode, itemPath, pcd, prismContext);

Item item = parseItem(valueXnode, itemName, itemDefinition, prismContext);
// if (pcd != null) {
// itemDefinition = pcd.findItemDefinition(itemPath);
// if (itemDefinition == null) {
// throw new SchemaException("No definition for item "+itemPath+" in "+pcd);
// }
// }

Boolean anchorStart = xmap.getParsedPrimitiveValue(KEY_FILTER_SUBSTRING_ANCHOR_START, DOMUtil.XSD_BOOLEAN);
if (anchorStart == null) {
anchorStart = false;
}

// String substring = xmap.getParsedPrimitiveValue(KEY_FILTER_EQUALS_VALUE, DOMUtil.XSD_STRING);

// if (StringUtils.isBlank(substring)) {
// throw new IllegalStateException("No substring values to search specified for item " + itemName);
// }
Boolean anchorEnd = xmap.getParsedPrimitiveValue(KEY_FILTER_SUBSTRING_ANCHOR_END, DOMUtil.XSD_BOOLEAN);
if (anchorEnd == null) {
anchorEnd = false;
}

return SubstringFilter.createSubstring(itemPath, (PrismProperty) item, matchingRule);
return SubstringFilter.createSubstring(itemPath, (PrismProperty) item, matchingRule, anchorStart, anchorEnd);
}

private static <C extends Containerable> OrgFilter parseOrgFilter(XNode xnode, PrismContainerDefinition<C> pcd) throws SchemaException {
Expand Down Expand Up @@ -448,22 +445,22 @@ private static MapXNode toMap(XNode xnode) throws SchemaException {
}

private static ItemPath getPath(MapXNode xmap, PrismContext prismContext) throws SchemaException {
XNode xnode = xmap.get(KEY_FILTER_EQUALS_PATH);
XNode xnode = xmap.get(KEY_FILTER_EQUAL_PATH);
if (xnode == null) {
return null;
}
if (!(xnode instanceof PrimitiveXNode<?>)) {
throw new SchemaException("Expected that field "+KEY_FILTER_EQUALS_PATH+" will be primitive, but it is "+xnode.getDesc());
throw new SchemaException("Expected that field "+KEY_FILTER_EQUAL_PATH+" will be primitive, but it is "+xnode.getDesc());
}
// XNodeProcessor processor = PrismUtil.getXnodeProcessor(prismContext);
// return processor.parseAtomicValue(xnode, new PrismPropertyDefinition<ItemPath>(ItemPathType.COMPLEX_TYPE, ItemPathType.COMPLEX_TYPE, prismContext));
// return ipt.getItemPath();
return xmap.getParsedPrimitiveValue(KEY_FILTER_EQUALS_PATH, ItemPath.XSD_TYPE);
return xmap.getParsedPrimitiveValue(KEY_FILTER_EQUAL_PATH, ItemPath.XSD_TYPE);
// return itemPathType.getItemPath();
}

private static QName determineMatchingRule(MapXNode xmap) throws SchemaException{
String matchingRuleLocalPart = xmap.getParsedPrimitiveValue(KEY_FILTER_EQUALS_MATCHING, DOMUtil.XSD_STRING);
String matchingRuleLocalPart = xmap.getParsedPrimitiveValue(KEY_FILTER_EQUAL_MATCHING, DOMUtil.XSD_STRING);
if (StringUtils.isNotBlank(matchingRuleLocalPart)){
return new QName(PrismConstants.NS_MATCHING_RULE, matchingRuleLocalPart);
} else {
Expand Down Expand Up @@ -607,7 +604,7 @@ private static <T extends PrismValue> MapXNode serializeValueFilter(PropertyValu
valuesNode.add(valNode);
}

map.put(KEY_FILTER_EQUALS_VALUE, valuesNode);
map.put(KEY_FILTER_EQUAL_VALUE, valuesNode);
}

ExpressionWrapper xexpression = filter.getExpression();
Expand Down Expand Up @@ -767,7 +764,7 @@ private static void serializeMatchingRule(ValueFilter filter, MapXNode map){
// matchingNode.setValue(filter.getMatchingRule().getLocalPart());
// matchingNode.setTypeQName(DOMUtil.XSD_STRING);
PrimitiveXNode<String> matchingNode = createPrimitiveXNode(filter.getMatchingRule().getLocalPart(), DOMUtil.XSD_STRING);
map.put(KEY_FILTER_EQUALS_MATCHING, matchingNode);
map.put(KEY_FILTER_EQUAL_MATCHING, matchingNode);
}

}
Expand All @@ -784,7 +781,7 @@ private static void serializePath(ValueFilter filter, MapXNode map) {

PrimitiveXNode<ItemPath> pathNode = createPrimitiveXNode(filter.getFullPath(), ItemPath.XSD_TYPE);

map.put(KEY_FILTER_EQUALS_PATH, pathNode);
map.put(KEY_FILTER_EQUAL_PATH, pathNode);
}

private static <T> XNode serializePropertyValue(PrismPropertyValue<T> value, PrismPropertyDefinition<T> definition, PrismBeanConverter beanConverter) throws SchemaException {
Expand Down
Expand Up @@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

Expand All @@ -39,6 +40,9 @@
import com.evolveum.midpoint.util.DebugUtil;

public class SubstringFilter<T> extends PropertyValueFilter<PrismPropertyValue<T>> {

private boolean anchorStart = false;
private boolean anchorEnd = false;

SubstringFilter(ItemPath parentPath, ItemDefinition definition, PrismPropertyValue<T> value) {
super(parentPath, definition, value);
Expand All @@ -52,11 +56,33 @@ public class SubstringFilter<T> extends PropertyValueFilter<PrismPropertyValue<T
super(parentPath, definition, matchingRule);
}

SubstringFilter(ItemPath parentPath, ItemDefinition definition, PrismPropertyValue<T> value, boolean anchorStart, boolean anchorEnd) {
super(parentPath, definition, value);
this.anchorStart = anchorStart;
this.anchorEnd = anchorEnd;
}

SubstringFilter(ItemPath parentPath, ItemDefinition definition, QName matchingRule, List<PrismPropertyValue<T>> value, boolean anchorStart, boolean anchorEnd) {
super(parentPath, definition, matchingRule, value);
this.anchorStart = anchorStart;
this.anchorEnd = anchorEnd;
}

SubstringFilter(ItemPath parentPath, ItemDefinition definition, QName matchingRule, boolean anchorStart, boolean anchorEnd) {
super(parentPath, definition, matchingRule);
this.anchorStart = anchorStart;
this.anchorEnd = anchorEnd;
}

public static <T> SubstringFilter createSubstring(ItemPath path, PrismProperty<T> item, QName matchingRule) {
return createSubstring(path, item.getDefinition(), matchingRule, item.getValue());
}

public static <T> SubstringFilter createSubstring(ItemPath path, PrismProperty<T> item, QName matchingRule,
boolean anchorStart, boolean anchorEnd) {
return createSubstring(path, item.getDefinition(), matchingRule, item.getValue(), anchorStart, anchorEnd);
}

public static <T> SubstringFilter createSubstring(QName path, PrismPropertyDefinition itemDefinition, PrismPropertyValue<T> values) {
return createSubstring(new ItemPath(path), itemDefinition, null, values);
}
Expand All @@ -68,7 +94,7 @@ public static <T> SubstringFilter createSubstring(ItemPath path, PrismPropertyDe
public static <T> SubstringFilter createSubstring(QName path, PrismPropertyDefinition itemDefinition, QName matchingRule, PrismPropertyValue<T> values) {
return createSubstring(new ItemPath(path), itemDefinition, matchingRule, values);
}

public static <T> SubstringFilter createSubstring(QName path, PrismPropertyDefinition itemDefinition, T realValues) {
return createSubstring(new ItemPath(path), itemDefinition, null, realValues);
}
Expand Down Expand Up @@ -102,6 +128,20 @@ public static <T> SubstringFilter createSubstring(ItemPath path, PrismPropertyDe
return new SubstringFilter(path, itemDefinition, matchingRule, pValues);
}

public static <T> SubstringFilter createSubstring(ItemPath path, PrismPropertyDefinition itemDefinition, QName matchingRule,
PrismPropertyValue<T> values, boolean anchorStart, boolean anchorEnd) {
Validate.notNull(path, "Item path in substring filter must not be null.");
Validate.notNull(itemDefinition, "Item definition in substring filter must not be null.");

if (values == null){
return createNullSubstring(path, itemDefinition, matchingRule);
}

List<PrismPropertyValue<T>> pValues = createPropertyList(itemDefinition, values);

return new SubstringFilter(path, itemDefinition, matchingRule, pValues, anchorStart, anchorEnd);
}

public static <O extends Objectable, T> SubstringFilter createSubstring(ItemPath path, Class<O> clazz, PrismContext prismContext, T value) {
return createSubstring(path, clazz, prismContext, null, value);
}
Expand Down Expand Up @@ -135,7 +175,14 @@ private static SubstringFilter createNullSubstring(ItemPath itemPath, PrismPrope
return new SubstringFilter(itemPath, propertyDef, matchingRule);

}


public boolean isAnchorStart() {
return anchorStart;
}

public boolean isAnchorEnd() {
return anchorEnd;
}

@Override
public SubstringFilter clone() {
Expand All @@ -151,7 +198,14 @@ public String debugDump() {
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("SUBSTRING: \n");
sb.append("SUBSTRING:");
if (anchorStart) {
sb.append(" anchorStart");
}
if (anchorEnd) {
sb.append(" anchorEnd");
}
sb.append("\n");
return debugDump(indent, sb);
}

Expand All @@ -171,9 +225,17 @@ public <T extends Objectable> boolean match(PrismObject<T> object, MatchingRuleR
for (Object val : item.getValues()){
if (val instanceof PrismPropertyValue){
Object value = ((PrismPropertyValue) val).getValue();
Iterator iterator = toRealValues().iterator();
Iterator<String> iterator = (Iterator<String>) toRealValues().iterator();
while(iterator.hasNext()){
if (matching.matches(value, ".*"+iterator.next()+".*")){
StringBuilder sb = new StringBuilder();
if (!anchorStart) {
sb.append(".*");
}
sb.append(Pattern.quote(iterator.next()));
if (!anchorEnd) {
sb.append(".*");
}
if (matching.matches(value, sb.toString())){
return true;
}
}
Expand Down

0 comments on commit e048a27

Please sign in to comment.