Example usage for javax.persistence.criteria CriteriaBuilder isNotEmpty

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

Introduction

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

Prototype

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

Source Link

Document

Create a predicate that tests whether a collection is not empty.

Usage

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  w  ww  .j  a  v  a 2s .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);

}