/** * Returns Operator from query parameter operator notation. * * @param string operator notation from query parameter * @return Operator */ public static Operator fromString(final String string) { for (final Operator operator : values()) { if (operator.getNotation().equals(string)) { return operator; } } throw new InvalidPredicateException("Unknown operator in filter: " + string); }
private static <T> Predicate<T> getComparator(String field, List<Object> values, RequestScope requestScope, Predicate<Integer> condition) { return (T entity) -> { if (values.size() == 0) { throw new InvalidPredicateException("No value to compare"); } Object fieldVal = getFieldValue(entity, field, requestScope); return fieldVal != null && values.stream() .anyMatch(testVal -> condition.test(compare(fieldVal, testVal))); }; }
private static <T> Predicate<T> ge(String field, List<Object> values, RequestScope requestScope) { return getComparator(field, values, requestScope, compareResult -> compareResult >= 0); }
: Operator.fromString(matcher.group(3)); if (operator.isParameterized()) { for (String valueParams : paramValues) { for (String valueParam : valueParams.split(",")) {
private static <T> Predicate<T> isNull(String field, RequestScope requestScope) { return (T entity) -> getFieldValue(entity, field, requestScope) == null; }
params.add(new QueryParameter() .type("string") .name("filter[" + typeName + "." + name + "][" + op.getNotation() + "]") .description("Filters the collection of " + typeName + " by the attribute " + name + " " + "using the operator " + op.getNotation()));
/** * Given a collection of filter predicates and a Hibernate query, populates the named parameters in the * Hibernate query. * * @param query The HQL query * @param predicates The predicates to extract named parameter values from */ protected void supplyFilterQueryParameters(Query query, Collection<FilterPredicate> predicates) { for (FilterPredicate filterPredicate : predicates) { if (filterPredicate.getOperator().isParameterized()) { boolean shouldEscape = filterPredicate.isMatchingOperator(); filterPredicate.getParameters().forEach(param -> { query.setParameter(param.getName(), shouldEscape ? param.escapeMatching() : param.getValue()); }); } } }
@Override public Predicate apply(RequestScope dictionary) { return operator.contextualize(getFieldPath(), values, dictionary); }
public FilterPredicate negate() { Operator newOp = operator.negate(); return new FilterPredicate(this.path, newOp, this.values); }
: Operator.fromString(matcher.group(3)); if (operator.isParameterized()) { for (String valueParams : paramValues) { for (String valueParam : valueParams.split(",")) {
private static <T> Predicate<T> isNull(String field, RequestScope requestScope) { return (T entity) -> getFieldValue(entity, field, requestScope) == null; }
@Override public Predicate apply(RequestScope dictionary) { return operator.contextualize(getFieldPath(), values, dictionary); }
public FilterPredicate negate() { Operator newOp = operator.negate(); return new FilterPredicate(this.path, newOp, this.values); }
/** * Returns Operator from query parameter operator notation. * * @param string operator notation from query parameter * @return Operator */ public static Operator fromString(final String string) { for (final Operator operator : values()) { if (operator.getNotation().equals(string)) { return operator; } } throw new InvalidPredicateException("Unknown operator in filter: " + string); }
private static <T> Predicate<T> in(String field, List<Object> values, RequestScope requestScope) { return (T entity) -> { Object val = getFieldValue(entity, field, requestScope); return val != null && values.stream() .map(v -> CoerceUtil.coerce(v, val.getClass())) .anyMatch(val::equals); }; }
private static <T> Predicate<T> getComparator(String field, List<Object> values, RequestScope requestScope, Predicate<Integer> condition) { return (T entity) -> { if (values.size() == 0) { throw new InvalidPredicateException("No value to compare"); } Object fieldVal = getFieldValue(entity, field, requestScope); return fieldVal != null && values.stream() .anyMatch(testVal -> condition.test(compare(fieldVal, testVal))); }; }
private static <T> Predicate<T> le(String field, List<Object> values, RequestScope requestScope) { return getComparator(field, values, requestScope, compareResult -> compareResult <= 0); }
/** * Applies a filter predicate to the object in question. * * @param object object returned from datastore * @param filterPredicate A predicate from filterExpressionCheck * @param requestScope Request scope object * @return true if the object pass evaluation against Predicate. */ public boolean applyPredicateToObject(T object, FilterPredicate filterPredicate, RequestScope requestScope) { try { String fieldPath = filterPredicate.getFieldPath(); com.yahoo.elide.core.RequestScope scope = coreScope(requestScope); Predicate fn = filterPredicate.getOperator().contextualize(fieldPath, filterPredicate.getValues(), scope); return fn.test(object); } catch (Exception e) { log.error("Failed to apply predicate {}", filterPredicate, e); return false; } }
private static <T> Predicate<T> in(String field, List<Object> values, RequestScope requestScope) { return (T entity) -> { Object val = getFieldValue(entity, field, requestScope); return val != null && values.stream() .map(v -> CoerceUtil.coerce(v, val.getClass())) .anyMatch(val::equals); }; }
private static <T> Predicate<T> le(String field, List<Object> values, RequestScope requestScope) { return getComparator(field, values, requestScope, compareResult -> compareResult <= 0); }