use of com.yahoo.elide.core.filter.Operator in project elide by yahoo.
the class FilterTranslatorTest method testCustomLocalOperator.
@Test
public void testCustomLocalOperator() throws Exception {
JPQLPredicateGenerator generator = new CaseAwareJPQLGenerator("%s LIKE CONCAT('%%', %s, '%%')", CaseAwareJPQLGenerator.Case.NONE, CaseAwareJPQLGenerator.ArgumentCount.ONE);
FilterPredicate pred = new FilterPredicate(new Path.PathElement(Author.class, String.class, "name"), Operator.INFIX, Arrays.asList("value"));
Map<Operator, JPQLPredicateGenerator> overrides = new HashMap<>();
overrides.put(Operator.INFIX, generator);
String actual = new FilterTranslator(dictionary, overrides).apply(pred).replaceAll(":\\w+", ":XXX");
assertEquals("name LIKE CONCAT('%', :XXX, '%')", actual);
}
use of com.yahoo.elide.core.filter.Operator in project elide by yahoo.
the class Filtered method getPredicateOfId.
private static FilterPredicate getPredicateOfId(long id) {
Path.PathElement path1 = new Path.PathElement(Filtered.class, long.class, "id");
Operator op = Operator.IN;
List<Object> value = new ArrayList<>();
value.add(id);
return new FilterPredicate(path1, op, value);
}
use of com.yahoo.elide.core.filter.Operator in project elide by yahoo.
the class FilterExpressionCheckObj method createUserPredicate.
// Predicate that restrict resource's id to be the same as cuurent User's id.
public static FilterPredicate createUserPredicate(RequestScope requestScope, boolean setUserId, long setId) {
Path.PathElement path1 = new Path.PathElement(FilterExpressionCheckObj.class, long.class, "id");
Operator op = Operator.IN;
List<Object> value = new ArrayList<>();
int userId = Integer.valueOf(requestScope.getUser().getPrincipal().getName());
if (setUserId) {
value.add(setId);
} else {
value.add((long) userId);
}
return new FilterPredicate(path1, op, value);
}
use of com.yahoo.elide.core.filter.Operator in project elide by yahoo.
the class DefaultFilterDialect method extractPredicates.
/**
* Converts the query parameters to a list of predicates that are then conjoined or organized by type.
*
* @param queryParams the query params
* @return a list of the predicates from the query params
* @throws ParseException when a filter parameter cannot be parsed
*/
private List<FilterPredicate> extractPredicates(MultivaluedMap<String, String> queryParams, String apiVersion) throws ParseException {
List<FilterPredicate> filterPredicates = new ArrayList<>();
Pattern pattern = Pattern.compile("filter\\[([^\\]]+)\\](\\[([^\\]]+)\\])?");
for (MultivaluedMap.Entry<String, List<String>> entry : queryParams.entrySet()) {
// Match "filter[<type>.<field>]" OR "filter[<type>.<field>][<operator>]"
String paramName = entry.getKey();
List<String> paramValues = entry.getValue();
Matcher matcher = pattern.matcher(paramName);
if (!matcher.find()) {
throw new ParseException("Invalid filter format: " + paramName);
}
final String[] keyParts = matcher.group(1).split("\\.");
if (keyParts.length < 2) {
throw new ParseException("Invalid filter format: " + paramName);
}
final Operator operator = (matcher.group(3) == null) ? Operator.IN : Operator.fromString(matcher.group(3));
Path path = getPath(keyParts, apiVersion);
List<Path.PathElement> elements = path.getPathElements();
Path.PathElement last = elements.get(elements.size() - 1);
final List<Object> values = new ArrayList<>();
if (operator.isParameterized()) {
for (String valueParams : paramValues) {
for (String valueParam : valueParams.split(",")) {
values.add(CoerceUtil.coerce(valueParam, last.getFieldType()));
}
}
}
FilterPredicate filterPredicate = new FilterPredicate(path, operator, values);
filterPredicates.add(filterPredicate);
}
return filterPredicates;
}
use of com.yahoo.elide.core.filter.Operator in project elide by yahoo.
the class AnotherFilterExpressionCheckObj method createFilterPredicate.
public static FilterPredicate createFilterPredicate() {
Path.PathElement path1 = new Path.PathElement(AnotherFilterExpressionCheckObj.class, long.class, "createDate");
Operator op = Operator.IN;
List<Object> value = new ArrayList<>();
value.add(1999L);
return new FilterPredicate(path1, op, value);
}
Aggregations