Example usage for javax.persistence.criteria CriteriaBuilder lessThan

List of usage examples for javax.persistence.criteria CriteriaBuilder lessThan

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder lessThan.

Prototype

<Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y);

Source Link

Document

Create a predicate for testing whether the first argument is less than the second.

Usage

From source file:org.jboss.pnc.datastore.predicates.rsql.RSQLNodeTravellerPredicate.java

public RSQLNodeTravellerPredicate(Class<Entity> entityClass, String rsql) throws RSQLParserException {
    operations.put(RSQLOperators.EQUAL, new AbstractTransformer<Entity>() {
        @Override/*  www . j a v a2s  .  co m*/
        Predicate transform(Root<Entity> r, Path<?> selectedPath, CriteriaBuilder cb, String operand,
                List<Object> convertedArguments) {
            return cb.equal(selectedPath, convertedArguments.get(0));
        }
    });

    operations.put(RSQLOperators.NOT_EQUAL, new AbstractTransformer<Entity>() {
        @Override
        Predicate transform(Root<Entity> r, Path<?> selectedPath, CriteriaBuilder cb, String operand,
                List<Object> convertedArguments) {
            return cb.notEqual(selectedPath, convertedArguments.get(0));
        }
    });

    operations.put(RSQLOperators.GREATER_THAN, (r, cb, clazz, operand, arguments) -> cb
            .greaterThan((Path) selectWithOperand(r, operand, clazz), arguments.get(0)));
    operations.put(RSQLOperators.GREATER_THAN_OR_EQUAL, (r, cb, clazz, operand, arguments) -> cb
            .greaterThanOrEqualTo((Path) selectWithOperand(r, operand, clazz), arguments.get(0)));
    operations.put(RSQLOperators.LESS_THAN, (r, cb, clazz, operand, arguments) -> cb
            .lessThan((Path) selectWithOperand(r, operand, clazz), arguments.get(0)));
    operations.put(RSQLOperators.LESS_THAN_OR_EQUAL, (r, cb, clazz, operand, arguments) -> cb
            .lessThanOrEqualTo((Path) selectWithOperand(r, operand, clazz), arguments.get(0)));
    operations.put(RSQLOperators.IN,
            (r, cb, clazz, operand, arguments) -> ((Path) selectWithOperand(r, operand, clazz)).in(arguments));
    operations.put(RSQLOperators.NOT_IN, (r, cb, clazz, operand, arguments) -> cb
            .not(((Path) selectWithOperand(r, operand, clazz)).in(arguments)));
    operations.put(LIKE,
            (r, cb, clazz, operand, arguments) -> cb.like(cb.lower((Path) selectWithOperand(r, operand, clazz)),
                    preprocessLikeOperatorArgument(arguments.get(0).toLowerCase())));
    operations.put(IS_NULL, (r, cb, clazz, operand, arguments) -> {
        if (Boolean.parseBoolean(arguments.get(0))) {
            return cb.isNull((Path) selectWithOperand(r, operand, clazz));
        } else {
            return cb.isNotNull((Path) selectWithOperand(r, operand, clazz));
        }
    });

    Set<ComparisonOperator> operators = RSQLOperators.defaultOperators();
    operators.add(LIKE);
    operators.add(IS_NULL);

    rootNode = new RSQLParser(operators).parse(preprocessRSQL(rsql));
    selectingClass = entityClass;
}

From source file:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java

protected void attachProductSearchCriteria(ProductSearchCriteria searchCriteria,
        From<?, ? extends Product> product, From<?, ? extends Sku> sku, List<Predicate> restrictions) {
    CriteriaBuilder builder = em.getCriteriaBuilder();

    // Build out the filter criteria from the users request
    for (Entry<String, String[]> entry : searchCriteria.getFilterCriteria().entrySet()) {
        String key = entry.getKey();
        List<String> eqValues = new ArrayList<String>();
        List<String[]> rangeValues = new ArrayList<String[]>();

        // Determine which path is the appropriate one to use
        Path<?> pathToUse;/*from w ww.j  a  va 2 s  .co  m*/
        if (key.contains("defaultSku.")) {
            pathToUse = sku;
            key = key.substring("defaultSku.".length());
        } else if (key.contains("productAttributes.")) {
            pathToUse = product.join("productAttributes");

            key = key.substring("productAttributes.".length());
            restrictions.add(builder.equal(pathToUse.get("name").as(String.class), key));

            key = "value";
        } else if (key.contains("product.")) {
            pathToUse = product;
            key = key.substring("product.".length());
        } else {
            // We don't know which path this facet is built on - resolves previous bug that attempted
            // to attach search facet to any query parameter
            continue;
        }

        // Values can be equality checks (ie manufacturer=Dave's) or range checks, which take the form
        // key=range[minRange:maxRange]. Figure out what type of check this is
        for (String value : entry.getValue()) {
            if (value.contains("range[")) {
                String[] rangeValue = new String[] {
                        value.substring(value.indexOf("[") + 1, value.indexOf(":")),
                        value.substring(value.indexOf(":") + 1, value.indexOf("]")) };
                rangeValues.add(rangeValue);
            } else {
                eqValues.add(value);
            }
        }

        // Add the equality range restriction with the "in" builder. That means that the query string
        // ?manufacturer=Dave&manufacturer=Bob would match either Dave or Bob
        if (eqValues.size() > 0) {
            restrictions.add(pathToUse.get(key).in(eqValues));
        }

        // If we have any range restrictions, we need to build those too. Ranges are also "or"ed together,
        // such that specifying range[0:5] and range[10:null] for the same field would match items
        // that were valued between 0 and 5 OR over 10 for that field
        List<Predicate> rangeRestrictions = new ArrayList<Predicate>();
        for (String[] range : rangeValues) {
            BigDecimal min = new BigDecimal(range[0]);
            BigDecimal max = null;
            if (range[1] != null && !range[1].equals("null")) {
                max = new BigDecimal(range[1]);
            }

            Predicate minRange = builder.greaterThan(pathToUse.get(key).as(BigDecimal.class), min);
            Predicate maxRange = null;
            if (max != null) {
                maxRange = builder.lessThan(pathToUse.get(key).as(BigDecimal.class), max);
                rangeRestrictions.add(builder.and(minRange, maxRange));
            } else {
                rangeRestrictions.add(minRange);
            }
        }

        if (rangeRestrictions.size() > 0) {
            restrictions.add(builder.or(rangeRestrictions.toArray(new Predicate[rangeRestrictions.size()])));
        }
    }
}

From source file:org.xiaoqiaotq.util.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*w ww .j a v  a  2s.co  m*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if ((filters != null) && !(filters.isEmpty())) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }
                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case NEQ:
                        predicates.add(builder.notEqual(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}