Example usage for javax.persistence.criteria CriteriaBuilder isEmpty

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

Introduction

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

Prototype

<C extends Collection<?>> Predicate isEmpty(Expression<C> collection);

Source Link

Document

Create a predicate that tests whether a collection is empty.

Usage

From source file:com.faceye.feature.repository.jpa.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//from w  w w.  ja va  2  s.  co  m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (CollectionUtils.isNotEmpty(filters)) {

                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:
                        if (filter.value instanceof Number) {
                            predicates.add(builder.equal(expression, (Number) filter.value));
                        } else if (filter.value instanceof String) {
                            if (StringUtils.isNotEmpty(filter.value.toString())) {
                                predicates.add(builder.equal(expression, filter.value));
                            }
                        } else {
                            predicates.add(builder.equal(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;
                    case ISTRUE:
                        predicates.add(builder.isTrue(expression));
                        break;
                    case ISFALSE:
                        predicates.add(builder.isFalse(expression));
                        break;
                    case ISEMPTY:
                        predicates.add(builder.isEmpty(expression));
                        break;
                    case ISNULL:
                        predicates.add(builder.isNull(expression));
                        break;
                    case NE:
                        predicates.add(builder.notEqual(expression, filter.value));
                    }
                }

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

            return builder.conjunction();
        }
    };
}

From source file:utils.jpa.EntityResource.java

private Query createCommonQuery(CriteriaBuilder cb, CriteriaQuery cq, Root root, TableSearchQuery tb,
        List<String> fieldsSearchBy) {

    List<Predicate> orPredicates = new ArrayList<>();
    List<Predicate> andPredicates = new ArrayList<>();

    //setting where conditions      
    if (fieldsSearchBy != null && !fieldsSearchBy.isEmpty()) {
        for (String field : fieldsSearchBy) {
            if (tb.getFilter() != null && !tb.getFilter().isEmpty()) {
                orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field), "::text")),
                        "%" + tb.getFilter().toLowerCase() + "%"));
            }/*from  ww w.  j a  va2s .  c om*/
        }
    } else {
        List<Field> filterColumns = SearchFields.getAll(entityClass);
        for (Field field : filterColumns) {
            EntityFilter entityFilter = tb.getFilterParameters().get(field.getName());

            if ((Collection.class.isAssignableFrom(field.getType())
                    || List.class.isAssignableFrom(field.getType()))
                    && (entityFilter == null
                            || !entityFilter.getEntityFilterType().equals(EntityFilterType.EMPTY))) {
                continue;
            }

            if (entityFilter == null) {
                if (tb.getFilter() != null && !tb.getFilter().isEmpty()) {
                    orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")),
                            "%" + tb.getFilter().toLowerCase() + "%"));
                }
                continue;
            }

            switch (entityFilter.getEntityFilterType()) {
            case WILDCARD:
                andPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")),
                        "%" + entityFilter.getValue().toLowerCase() + "%"));
                break;
            case EMPTY:
                if (Collection.class.isAssignableFrom(field.getType())
                        || List.class.isAssignableFrom(field.getType())) {
                    andPredicates.add(entityFilter.getValue().equals("hide")
                            ? cb.isEmpty(root.<Collection>get(field.getName()))
                            : cb.isNotEmpty(root.<Collection>get(field.getName())));
                } else {
                    andPredicates.add(
                            entityFilter.getValue().equals("hide") ? root.<Object>get(field.getName()).isNull()
                                    : root.<Object>get(field.getName()).isNotNull());
                }

                break;
            case MIN_MAX:
                andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValues().get(0)))));
                andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValues().get(1)))));
                break;
            case MIN:
                andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValue()))));
                break;
            case MAX:
                andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValue()))));
                break;
            }

        }
    }
    if (orPredicates.size() > 0 && andPredicates.size() > 0) {
        cq.where(cb.or(cb.or(orPredicates.toArray(new Predicate[0])),
                cb.and(andPredicates.toArray(new Predicate[0]))));
    } else if (orPredicates.size() > 0) {
        cq.where(cb.or(orPredicates.toArray(new Predicate[0])));
    } else if (andPredicates.size() > 0) {
        cq.where(cb.and(andPredicates.toArray(new Predicate[0])));
    }

    return em.createQuery(cq);

}