[New] Added ?:<paramId>:<value> search predicate to SearchBuilder
This commit is contained in:
parent
24f8296006
commit
3908d30400
|
@ -5,8 +5,10 @@ import static li.strolch.search.PredicatesSupport.containsIgnoreCase;
|
|||
import static li.strolch.search.PredicatesSupport.isEqualTo;
|
||||
import static li.strolch.utils.helper.StringHelper.trimOrEmpty;
|
||||
|
||||
import li.strolch.model.ParameterBag;
|
||||
import li.strolch.model.StrolchRootElement;
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.parameter.Parameter;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -85,6 +87,7 @@ public class SearchBuilder {
|
|||
query = query.substring(1);
|
||||
} else if (query.startsWith("and ")) {
|
||||
query = query.substring("and ".length());
|
||||
|
||||
} else if (query.startsWith("|")) {
|
||||
and = false;
|
||||
query = query.substring(1);
|
||||
|
@ -107,7 +110,7 @@ public class SearchBuilder {
|
|||
part = part.substring(1);
|
||||
}
|
||||
|
||||
String[] paramParts = part.split(":", -1);
|
||||
String[] paramParts = parseParts(part);
|
||||
if (paramParts.length != 3) {
|
||||
se = add(and, negate, se, id(containsIgnoreCase(part)).or(name(containsIgnoreCase(part))));
|
||||
} else {
|
||||
|
@ -121,7 +124,20 @@ public class SearchBuilder {
|
|||
else
|
||||
predicate = containsIgnoreCase(value);
|
||||
|
||||
se = add(and, negate, se, param(bagId, paramId, predicate));
|
||||
if (!bagId.equals("?")) {
|
||||
se = add(and, negate, se, param(bagId, paramId, predicate));
|
||||
} else {
|
||||
se = add(and, negate, se, element -> {
|
||||
for (String b : element.getParameterBagKeySet()) {
|
||||
ParameterBag bag = element.getParameterBag(b);
|
||||
Parameter<Object> param = bag.getParameter(paramId);
|
||||
if (param != null && predicate.matches(param.getValue()))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,6 +148,19 @@ public class SearchBuilder {
|
|||
return search;
|
||||
}
|
||||
|
||||
private static String[] parseParts(String part) {
|
||||
|
||||
int pos1 = part.indexOf(':');
|
||||
if (pos1 == -1)
|
||||
return new String[0];
|
||||
|
||||
int pos2 = part.indexOf(':', pos1 + 1);
|
||||
if (pos2 == -1)
|
||||
return new String[0];
|
||||
|
||||
return new String[] { part.substring(0, pos1), part.substring(pos1 + 1, pos2), part.substring(pos2 + 1) };
|
||||
}
|
||||
|
||||
private static <T extends StrolchRootElement> SearchExpression<T> add(boolean and, boolean negate,
|
||||
SearchExpression<T> se, SearchExpression<T> expression) {
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
|
||||
import li.strolch.utils.iso8601.ISO8601;
|
||||
|
||||
public class ObjectHelper {
|
||||
|
||||
public static boolean equals(Object left, Object right, boolean ignoreCase) {
|
||||
|
@ -183,6 +186,26 @@ public class ObjectHelper {
|
|||
if (left.getClass() == right.getClass())
|
||||
return left.equals(right);
|
||||
|
||||
// try to coerce the right side to the left side
|
||||
if (right instanceof String) {
|
||||
String rightString = (String) right;
|
||||
Object rightO;
|
||||
if (left instanceof Integer) {
|
||||
rightO = Integer.valueOf(rightString);
|
||||
} else if (left instanceof Float) {
|
||||
rightO = Float.valueOf(rightString);
|
||||
} else if (left instanceof Double) {
|
||||
rightO = Double.valueOf(rightString);
|
||||
} else if (left instanceof Date) {
|
||||
rightO = ISO8601.parseToDate(rightString);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Unhandled type combination " + left.getClass() + " / " + right.getClass());
|
||||
}
|
||||
|
||||
return rightO.equals(left);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unhandled type combination " + left.getClass() + " / " + right.getClass());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue