Example usage for javax.persistence.criteria CriteriaBuilder and

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

Introduction

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

Prototype

Predicate and(Predicate... restrictions);

Source Link

Document

Create a conjunction of the given restriction predicates.

Usage

From source file:com.mxep.web.common.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*from w  ww .ja v a 2s  .  co  m*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {
                List<Predicate> predicates = getPredicate(filters, root, query, builder);
                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }
            return builder.conjunction();
        }
    };
}

From source file:edu.pitt.dbmi.ccd.db.specification.GroupSpecification.java

private static Predicate buildSearchSpec(Root<Group> root, CriteriaBuilder cb, Set<String> matches,
        Set<String> nots) {
    List<Predicate> predicates = new ArrayList<>(0);
    if (!isEmpty(matches)) {
        predicates.addAll(inNameOrDescription(root, cb, matches));
    }//from ww  w .  j  ava2 s.  com
    if (!isEmpty(nots)) {
        predicates.addAll(notInNameOrDescription(root, cb, nots));
    }
    return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:edu.pitt.dbmi.ccd.db.specification.VocabularySpecification.java

private static Predicate buildSearchSpec(Root<Vocabulary> root, CriteriaBuilder cb, Set<String> matches,
        Set<String> nots) {
    List<Predicate> predicates = new ArrayList<>(0);
    if (!isEmpty(matches)) {
        predicates.addAll(inNameOrDescription(root, cb, matches));
    }/*from  w w  w.jav  a2 s  .  c o  m*/
    if (!isEmpty(nots)) {
        predicates.addAll(notInNameOrDescription(root, cb, nots));
    }
    return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

public static Predicate andPredicate(final CriteriaBuilder builder,
        final Iterable<Predicate> predicatesNullAllowed) {
    List<Predicate> predicates = withoutNullEntries(predicatesNullAllowed);
    if (predicates.isEmpty()) {
        return null;
    } else if (predicates.size() == 1) {
        return predicates.get(0);
    } else {// www . ja  v  a2s . c o m
        return builder.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}

From source file:com.mxep.web.common.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz, final SpecificationCallback<T> callback) {
    return new Specification<T>() {
        @Override//from  ww  w.  j a  v  a  2  s . c  om
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = getPredicate(filters, root, query, builder);

                if (null != callback) {
                    predicates.addAll(callback.addPredicate(root, query, builder));
                }

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

            return builder.conjunction();
        }
    };
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationTargetSpecification.java

private static Predicate buildSearchSpec(Root<AnnotationTarget> root, CriteriaBuilder cb, String username,
        String type, Set<String> matches, Set<String> nots) {
    List<Predicate> predicates = new ArrayList<>(0);
    predicates.add(buildFilterSpec(root, cb, username, type));
    if (!isEmpty(matches)) {
        predicates.addAll(inTitleOrAddressOrName(root, cb, matches));
    }/*w ww  .  j  av a 2s .c o m*/
    if (!isEmpty(nots)) {
        predicates.addAll(notInTitleOrAddressOrName(root, cb, nots));
    }
    return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationTargetSpecification.java

private static Predicate buildFilterSpec(Root<AnnotationTarget> root, CriteriaBuilder cb, String username,
        String type) {//from w  w  w  .j  a  va2 s  . c  o m
    List<Predicate> predicates = new ArrayList<>(0);
    if (!isEmpty(username)) {
        predicates.add(belongsToUser(root, cb, username));
    }
    if (!isEmpty(type)) {
        if (type.equalsIgnoreCase(FILE)) {
            predicates.add(isFile(root, cb));
        } else if (type.equalsIgnoreCase(URL)) {
            predicates.add(isURL(root, cb));
        }
    }
    return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:org.querybyexample.jpa.JpaUtil.java

public static Predicate andPredicate(CriteriaBuilder builder, Iterable<Predicate> predicatesNullAllowed) {
    List<Predicate> predicates = newArrayList(filter(predicatesNullAllowed, notNull()));
    if (predicates == null || predicates.isEmpty()) {
        return null;
    } else if (predicates.size() == 1) {
        return predicates.get(0);
    } else {//  www  .  ja v a 2 s. c o  m
        return builder.and(toArray(predicates, Predicate.class));
    }
}

From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java

private static Predicate buildIdSpec(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester,
        Long id) {/* www .java  2 s  .  c  om*/
    final Predicate authPredicate = authFilter(root, cb, requester);
    final Predicate idPredicate = cb.equal(root.get(ID), id);
    final Predicate[] predicates = new Predicate[] { authPredicate, idPredicate };
    return cb.and(predicates);
}

From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaCommandSpecs.java

/**
 * Get all the clusters given the specified parameters.
 *
 * @param applicationId The id of the application that is registered with these commands
 * @param statuses      The status of the commands
 * @return The specification//from w  w w. j  a  v  a2 s .  co m
 */
public static Specification<CommandEntity> findCommandsForApplication(final String applicationId,
        @Nullable final Set<CommandStatus> statuses) {
    return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        final Join<CommandEntity, ApplicationEntity> application = root.join(CommandEntity_.applications);

        predicates.add(cb.equal(application.get(ApplicationEntity_.uniqueId), applicationId));

        if (statuses != null && !statuses.isEmpty()) {
            predicates.add(
                    cb.or(statuses.stream().map(status -> cb.equal(root.get(CommandEntity_.status), status))
                            .toArray(Predicate[]::new)));
        }

        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
}