Search in sources :

Example 1 with Operator

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);
}
Also used : Path(com.yahoo.elide.core.Path) Operator(com.yahoo.elide.core.filter.Operator) HashMap(java.util.HashMap) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 2 with Operator

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);
}
Also used : Path(com.yahoo.elide.core.Path) Operator(com.yahoo.elide.core.filter.Operator) ArrayList(java.util.ArrayList) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate)

Example 3 with Operator

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);
}
Also used : Path(com.yahoo.elide.core.Path) Operator(com.yahoo.elide.core.filter.Operator) ArrayList(java.util.ArrayList) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate)

Example 4 with Operator

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;
}
Also used : Operator(com.yahoo.elide.core.filter.Operator) Path(com.yahoo.elide.core.Path) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) ParseException(com.yahoo.elide.core.filter.dialect.ParseException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 5 with Operator

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);
}
Also used : Path(com.yahoo.elide.core.Path) Operator(com.yahoo.elide.core.filter.Operator) ArrayList(java.util.ArrayList) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate)

Aggregations

Operator (com.yahoo.elide.core.filter.Operator)7 Path (com.yahoo.elide.core.Path)6 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)6 ArrayList (java.util.ArrayList)4 List (java.util.List)2 Preconditions (com.google.common.base.Preconditions)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)1 FilterOperation (com.yahoo.elide.core.filter.FilterOperation)1 BETWEEN (com.yahoo.elide.core.filter.Operator.BETWEEN)1 FALSE (com.yahoo.elide.core.filter.Operator.FALSE)1 GE (com.yahoo.elide.core.filter.Operator.GE)1 GT (com.yahoo.elide.core.filter.Operator.GT)1 HASMEMBER (com.yahoo.elide.core.filter.Operator.HASMEMBER)1 HASNOMEMBER (com.yahoo.elide.core.filter.Operator.HASNOMEMBER)1 IN (com.yahoo.elide.core.filter.Operator.IN)1 INFIX (com.yahoo.elide.core.filter.Operator.INFIX)1 INFIX_CASE_INSENSITIVE (com.yahoo.elide.core.filter.Operator.INFIX_CASE_INSENSITIVE)1 IN_INSENSITIVE (com.yahoo.elide.core.filter.Operator.IN_INSENSITIVE)1 ISEMPTY (com.yahoo.elide.core.filter.Operator.ISEMPTY)1