Skip to content

Commit

Permalink
New "Exists" filter. Further refactoring/cleanup of new query interpr…
Browse files Browse the repository at this point in the history
…eter.
  • Loading branch information
mederly committed Nov 24, 2015
1 parent 9dfb7ac commit 7165cc6
Show file tree
Hide file tree
Showing 49 changed files with 1,384 additions and 782 deletions.
Expand Up @@ -333,6 +333,10 @@ public static boolean isNullOrEmpty(ItemPath itemPath) {
return itemPath == null || itemPath.isEmpty();
}

public static boolean containsSingleNameSegment(ItemPath path) {
return path != null && path.size() == 1 && path.first() instanceof NameItemPathSegment;
}

public enum CompareResult {
EQUIVALENT,
SUPERPATH,
Expand Down
Expand Up @@ -120,13 +120,13 @@ public static <T> EqualFilter<T> createEqual(ItemPath path, PrismPropertyDefinit

public static <C extends Containerable, T> EqualFilter<T> createEqual(ItemPath parentPath, PrismContainerDefinition<C> containerDef,
PrismPropertyValue<T> values) throws SchemaException {
PrismPropertyDefinition<T> propertyDef = (PrismPropertyDefinition) findItemDefinition(parentPath, containerDef);
PrismPropertyDefinition<T> propertyDef = (PrismPropertyDefinition) FilterUtils.findItemDefinition(parentPath, containerDef);
return createEqual(parentPath, propertyDef, values);
}

public static <C extends Containerable, T> EqualFilter<T> createEqual(ItemPath itemPath, PrismContainerDefinition<C> containerDef,
T realValues) throws SchemaException {
PrismPropertyDefinition<T> propertyDef = (PrismPropertyDefinition) findItemDefinition(itemPath, containerDef);
PrismPropertyDefinition<T> propertyDef = (PrismPropertyDefinition) FilterUtils.findItemDefinition(itemPath, containerDef);
return createEqual(itemPath, propertyDef, realValues);
}

Expand All @@ -148,7 +148,7 @@ public static <C extends Containerable, T> EqualFilter<T> createEqual(ItemPath p

public static <C extends Containerable, T> EqualFilter<T> createEqual(ItemPath propertyPath, Class<C> type, PrismContext prismContext, QName matchingRule, T realValue)
{
PrismPropertyDefinition propertyDefinition = (PrismPropertyDefinition) findItemDefinition(propertyPath, type, prismContext);
PrismPropertyDefinition propertyDefinition = (PrismPropertyDefinition) FilterUtils.findItemDefinition(propertyPath, type, prismContext);
return createEqual(propertyPath, propertyDefinition, matchingRule, realValue);
}

Expand Down
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.prism.query;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;

import javax.xml.namespace.QName;

/**
* TODO think about creating abstract ItemFilter (ItemRelatedFilter) for this filter and ValueFilter.
*
* @author lazyman
* @author mederly
*/
public class ExistsFilter extends ObjectFilter {

private ItemPath fullPath;
private ItemDefinition definition;
private ObjectFilter filter;

public ExistsFilter(ItemPath fullPath, ItemDefinition definition, ObjectFilter filter) {
this.fullPath = fullPath;
this.definition = definition;
this.filter = filter;
checkConsistence();
}

public ItemPath getFullPath() {
return fullPath;
}

public ItemDefinition getDefinition() {
return definition;
}

public ObjectFilter getFilter() {
return filter;
}

public static <C extends Containerable> ExistsFilter createEqual(ItemPath itemPath, PrismContainerDefinition<C> containerDef,
ObjectFilter filter) throws SchemaException {
ItemDefinition itemDefinition = FilterUtils.findItemDefinition(itemPath, containerDef);
return new ExistsFilter(itemPath, itemDefinition, filter);
}

@Override
public ObjectFilter clone() {
ObjectFilter f = filter != null ? filter.clone() : null;
return new ExistsFilter(fullPath, definition, f);
}

@Override
public boolean match(PrismContainerValue value, MatchingRuleRegistry matchingRuleRegistry) throws SchemaException {
throw new UnsupportedOperationException();
}

@Override
public void checkConsistence() {
if (fullPath == null || fullPath.isEmpty()) {
throw new IllegalArgumentException("Null or empty path in "+this);
}
if (definition == null) {
throw new IllegalArgumentException("Null definition in "+this);
}
// null subfilter is legal. It means "ALL".
if (filter != null) {
filter.checkConsistence();
}
}

@Override
public String debugDump() {
return debugDump(0);
}

@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append("EXISTS: ");
sb.append(fullPath);
sb.append('\n');
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("DEF: ");
if (getDefinition() != null) {
sb.append(getDefinition().toString());
} else {
sb.append("null");
}
if (filter != null) {
sb.append(filter.debugDump(indent + 1));
}

return sb.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ExistsFilter)) return false;

ExistsFilter that = (ExistsFilter) o;

if (fullPath != null ? !fullPath.equals(that.fullPath) : that.fullPath != null) return false;
if (definition != null ? !definition.equals(that.definition) : that.definition != null) return false;
return !(filter != null ? !filter.equals(that.filter) : that.filter != null);

}

@Override
public int hashCode() {
int result = fullPath != null ? fullPath.hashCode() : 0;
result = 31 * result + (definition != null ? definition.hashCode() : 0);
result = 31 * result + (filter != null ? filter.hashCode() : 0);
return result;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("EXISTS(");
sb.append(PrettyPrinter.prettyPrint(fullPath));
sb.append(",");
sb.append(filter);
sb.append(")");
return sb.toString();
}
}
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.prism.query;

import com.evolveum.midpoint.prism.ComplexTypeDefinition;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.path.ItemPath;

/**
* @author mederly
*/
public class FilterUtils {

static ItemDefinition findItemDefinition(ItemPath itemPath, PrismContainerDefinition<? extends Containerable> containerDef) {
ItemDefinition itemDef = containerDef.findItemDefinition(itemPath);
if (itemDef == null) {
throw new IllegalStateException("No definition for item " + itemPath + " in container definition "
+ containerDef);
}

return itemDef;
}

static ItemDefinition findItemDefinition(ItemPath parentPath, ComplexTypeDefinition complexTypeDefinition) {
ItemDefinition itemDef = complexTypeDefinition.findItemDefinition(parentPath);
if (itemDef == null) {
throw new IllegalStateException("No definition for item " + parentPath + " in complex type definition "
+ complexTypeDefinition);
}
return itemDef;
}

static ItemDefinition findItemDefinition(ItemPath parentPath, Class type, PrismContext prismContext) {
ComplexTypeDefinition complexTypeDefinition = prismContext.getSchemaRegistry().findComplexTypeDefinitionByCompileTimeClass(type);
if (complexTypeDefinition == null) {
// TODO SchemaException instead?
throw new IllegalStateException("Definition of complex type " + type + " couldn't be not found");
}
return findItemDefinition(parentPath, complexTypeDefinition);
}
}
Expand Up @@ -16,25 +16,15 @@

package com.evolveum.midpoint.prism.query;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.PrismContainerValue;
import org.apache.commons.lang.Validate;
import org.w3c.dom.Element;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugUtil;
Expand All @@ -56,7 +46,7 @@ public static <T, O extends Objectable> GreaterFilter createGreater(ItemPath par

public static <T, O extends Objectable> GreaterFilter createGreater(ItemPath parentPath, PrismObjectDefinition<O> containerDef,
PrismPropertyValue<T> value, boolean equals) throws SchemaException {
PrismPropertyDefinition def = (PrismPropertyDefinition) findItemDefinition(parentPath, containerDef);
PrismPropertyDefinition def = (PrismPropertyDefinition) FilterUtils.findItemDefinition(parentPath, containerDef);
return createGreater(parentPath, def, value, equals);
}

Expand All @@ -72,7 +62,7 @@ public static <T> GreaterFilter createGreater(ItemPath parentPath, PrismProperty

public static <T, O extends Objectable> GreaterFilter createGreater(ItemPath parentPath, PrismObjectDefinition<O> containerDef,
T realValue, boolean equals) throws SchemaException {
PrismPropertyDefinition def = (PrismPropertyDefinition) findItemDefinition(parentPath, containerDef);
PrismPropertyDefinition def = (PrismPropertyDefinition) FilterUtils.findItemDefinition(parentPath, containerDef);
return createGreater(parentPath, def, realValue, equals);
}

Expand All @@ -84,7 +74,7 @@ public static <T, O extends Objectable> GreaterFilter createGreater(QName proper
public static <T, O extends Objectable> GreaterFilter createGreater(ItemPath path, Class<O> type, PrismContext prismContext, T realValue, boolean equals)
throws SchemaException {

PrismPropertyDefinition def = (PrismPropertyDefinition) findItemDefinition(path, type, prismContext);
PrismPropertyDefinition def = (PrismPropertyDefinition) FilterUtils.findItemDefinition(path, type, prismContext);

return createGreater(path, def, realValue, equals);
}
Expand Down
Expand Up @@ -53,7 +53,7 @@ public static <V> InFilter createIn(ItemPath path, PrismPropertyDefinition defin

public static <V> InFilter createIn(ItemPath path, Class type, PrismContext prismContext, QName matchingRule, V values) {

PrismPropertyDefinition definition = (PrismPropertyDefinition) findItemDefinition(path, type, prismContext);
PrismPropertyDefinition definition = (PrismPropertyDefinition) FilterUtils.findItemDefinition(path, type, prismContext);
return createIn(path, definition, null, values);
}

Expand Down
Expand Up @@ -18,11 +18,9 @@

import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismPropertyValue;
Expand Down Expand Up @@ -51,7 +49,7 @@ public static <T, O extends Objectable> LessFilter createLess(ItemPath parentPat

public static <T, O extends Objectable> LessFilter createLess(ItemPath parentPath, PrismObjectDefinition<O> containerDef,
PrismPropertyValue<T> value, boolean equals) throws SchemaException {
PrismPropertyDefinition def = (PrismPropertyDefinition) findItemDefinition(parentPath, containerDef);
PrismPropertyDefinition def = (PrismPropertyDefinition) FilterUtils.findItemDefinition(parentPath, containerDef);
return createLess(parentPath, def, value, equals);
}

Expand All @@ -71,7 +69,7 @@ public static <T> LessFilter createLess(ItemPath parentPath, PrismPropertyDefini

public static <T, O extends Objectable> LessFilter createLess(ItemPath parentPath, PrismObjectDefinition<O> containerDef,
T realValue, boolean equals) throws SchemaException {
PrismPropertyDefinition def = (PrismPropertyDefinition) findItemDefinition(parentPath, containerDef);
PrismPropertyDefinition def = (PrismPropertyDefinition) FilterUtils.findItemDefinition(parentPath, containerDef);
return createLess(parentPath, def, realValue, equals);
}

Expand All @@ -83,7 +81,7 @@ public static <T, O extends Objectable> LessFilter createLess(QName propertyName
public static <T, O extends Objectable> LessFilter createLess(ItemPath path, Class<O> type, PrismContext prismContext, T realValue, boolean equals)
throws SchemaException {

PrismPropertyDefinition def = (PrismPropertyDefinition) findItemDefinition(path, type, prismContext);
PrismPropertyDefinition def = (PrismPropertyDefinition) FilterUtils.findItemDefinition(path, type, prismContext);

return createLess(path, def, realValue, equals);
}
Expand Down
Expand Up @@ -24,8 +24,6 @@

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.match.MatchingRule;
import org.apache.commons.lang.Validate;

import com.evolveum.midpoint.prism.Item;
Expand Down Expand Up @@ -93,26 +91,26 @@ public static RefFilter createReferenceEqual(ItemPath path, PrismReferenceDefini
public static <O extends Containerable> RefFilter createReferenceEqual(QName propertyName, Class<O> type, PrismContext prismContext,
String... oids) {
ItemPath path = new ItemPath(propertyName);
PrismReferenceDefinition refDefinition = (PrismReferenceDefinition) findItemDefinition(path, type, prismContext);
PrismReferenceDefinition refDefinition = (PrismReferenceDefinition) FilterUtils.findItemDefinition(path, type, prismContext);
return createReferenceEqual(path, refDefinition, oids);
}

// beware, creating reference with (oid, ObjectType) may result in not matching a concrete reference of e.g. (oid, RoleType)
public static <O extends Containerable> RefFilter createReferenceEqual(ItemPath path, Class<O> type, PrismContext prismContext,
String... oids) throws SchemaException {
PrismReferenceDefinition refDefinition = (PrismReferenceDefinition) findItemDefinition(path, type, prismContext);
PrismReferenceDefinition refDefinition = (PrismReferenceDefinition) FilterUtils.findItemDefinition(path, type, prismContext);
return createReferenceEqual(path, refDefinition, oids);
}

public static <O extends Containerable> RefFilter createReferenceEqual(ItemPath path, Class<O> type, PrismContext prismContext,
PrismReferenceValue... values) throws SchemaException {
PrismReferenceDefinition refDefinition = (PrismReferenceDefinition) findItemDefinition(path, type, prismContext);
PrismReferenceDefinition refDefinition = (PrismReferenceDefinition) FilterUtils.findItemDefinition(path, type, prismContext);
return createReferenceEqual(path, refDefinition, values);
}


public static RefFilter createReferenceEqual(ItemPath path, PrismContainerDefinition containerDef, String... oids) {
ItemDefinition itemDef = findItemDefinition(path, containerDef);
ItemDefinition itemDef = FilterUtils.findItemDefinition(path, containerDef);

if (!(itemDef instanceof PrismReferenceDefinition)){
throw new IllegalStateException("Bad item definition. Expected that the definition will be instance of prism refenrence definition, but found " + itemDef);
Expand All @@ -130,7 +128,7 @@ public static <O extends Objectable> RefFilter createReferenceEqual(QName proper

ItemPath path = new ItemPath(propertyName);

ItemDefinition itemDef = findItemDefinition(path, type, targetObject.getPrismContext());
ItemDefinition itemDef = FilterUtils.findItemDefinition(path, type, targetObject.getPrismContext());

if (!(itemDef instanceof PrismReferenceDefinition)){
throw new IllegalStateException("Bad item definition. Expected that the definition will be instance of prism refenrence definition, but found " + itemDef);
Expand Down

0 comments on commit 7165cc6

Please sign in to comment.