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:eu.uqasar.service.ProcessService.java

public List<Process> getAllByDescendingEndDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from www  . j a v a  2  s .c o  m

    criteria.orderBy(cb.desc(root.get(Process_.endDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:eu.uqasar.service.ProcessService.java

public List<Process> getAllByDescendingStartDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from ww  w . ja v a2s . c o  m

    criteria.orderBy(cb.desc(root.get(Process_.startDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:eu.uqasar.service.ProcessService.java

public List<Process> getAllAscendingStartDateNameFiltered(ProcessesFilterStructure filter, int first,
        int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Process> criteria = cb.createQuery(Process.class);
    Root<Process> root = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from  ww  w. ja  va2  s .c o m*/

    criteria.orderBy(cb.asc(root.get(Process_.startDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

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

/**
 * Get a specification using the specified parameters.
 *
 * @param name     The name of the application
 * @param user     The name of the user who created the application
 * @param statuses The status of the application
 * @param tags     The set of tags to search with
 * @param type     The type of applications to fine
 * @return A specification object used for querying
 *///from w  w  w  . j av  a  2s  .c  o  m
public static Specification<ApplicationEntity> find(@Nullable final String name, @Nullable final String user,
        @Nullable final Set<ApplicationStatus> statuses, @Nullable final Set<TagEntity> tags,
        @Nullable final String type) {
    return (final Root<ApplicationEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> {
        final List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(name)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(ApplicationEntity_.name), name));
        }
        if (StringUtils.isNotBlank(user)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(ApplicationEntity_.user), user));
        }
        if (statuses != null && !statuses.isEmpty()) {
            predicates.add(
                    cb.or(statuses.stream().map(status -> cb.equal(root.get(ApplicationEntity_.status), status))
                            .toArray(Predicate[]::new)));
        }
        if (tags != null && !tags.isEmpty()) {
            final Join<ApplicationEntity, TagEntity> tagEntityJoin = root.join(ApplicationEntity_.tags);
            predicates.add(tagEntityJoin.in(tags));
            cq.groupBy(root.get(ApplicationEntity_.id));
            cq.having(cb.equal(cb.count(root.get(ApplicationEntity_.id)), tags.size()));
        }
        if (StringUtils.isNotBlank(type)) {
            predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb,
                    root.get(ApplicationEntity_.type), type));
        }
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

/**
 * build final sql select where predicate
 *
 * @param predicates/*from ww  w .  ja  va  2  s.  c  o m*/
 * @param cursorPredicate
 * @param criteriaBuilder
 * @return where predicate array of the sql
 */
private Predicate[] buildWherePredicate(List<Predicate[]> predicates, Predicate cursorPredicate,
        CriteriaBuilder criteriaBuilder) {

    List<Predicate> concatenatePredicate = new ArrayList<>();

    // custom filter predicates
    if (!predicates.isEmpty()) {
        List<Predicate> andPredicates = predicates.stream().map(array -> criteriaBuilder.and(array))
                .collect(Collectors.toList());

        concatenatePredicate
                .add(criteriaBuilder.or(andPredicates.toArray(new Predicate[andPredicates.size()])));
    }

    if (cursorPredicate != null) {
        concatenatePredicate.add(cursorPredicate);
    }

    // final where clause predicate list
    List<Predicate> wherePredicate = new ArrayList<>();

    if (concatenatePredicate.size() > 1) {
        wherePredicate.add(
                criteriaBuilder.and(concatenatePredicate.toArray(new Predicate[concatenatePredicate.size()])));
    } else if (concatenatePredicate.size() == 1) {
        wherePredicate.addAll(concatenatePredicate);
    } else {
        return new Predicate[] {};
    }

    return wherePredicate.toArray(new Predicate[wherePredicate.size()]);
}

From source file:com.netflix.genie.server.repository.jpa.ClusterSpecs.java

/**
 * Generate a specification given the parameters.
 *
 * @param name          The name of the cluster to find
 * @param statuses      The statuses of the clusters to find
 * @param tags          The tags of the clusters to find
 * @param minUpdateTime The minimum updated time of the clusters to find
 * @param maxUpdateTime The maximum updated time of the clusters to find
 * @return The specification//from   w w  w. j  ava  2  s.com
 */
public static Specification<Cluster> find(final String name, final Set<ClusterStatus> statuses,
        final Set<String> tags, final Long minUpdateTime, final Long maxUpdateTime) {
    return new Specification<Cluster>() {
        @Override
        public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            final List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(name)) {
                predicates.add(cb.like(root.get(Cluster_.name), name));
            }
            if (minUpdateTime != null) {
                predicates.add(cb.greaterThanOrEqualTo(root.get(Cluster_.updated), new Date(minUpdateTime)));
            }
            if (maxUpdateTime != null) {
                predicates.add(cb.lessThan(root.get(Cluster_.updated), new Date(maxUpdateTime)));
            }
            if (tags != null) {
                for (final String tag : tags) {
                    if (StringUtils.isNotBlank(tag)) {
                        predicates.add(cb.isMember(tag, root.get(Cluster_.tags)));
                    }
                }
            }
            if (statuses != null && !statuses.isEmpty()) {
                //Could optimize this as we know size could use native array
                final List<Predicate> orPredicates = new ArrayList<>();
                for (final ClusterStatus status : statuses) {
                    orPredicates.add(cb.equal(root.get(Cluster_.status), status));
                }
                predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
            }

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

From source file:eu.uqasar.service.user.UserService.java

public long countAllFiltered(final UserFilterStructure filter) {
    logger.infof("counting all Users matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<User> from = criteria.from(User.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from ww  w  .  j a  v  a2 s.  co  m
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllByAscendingNameFiltered(UserFilterStructure filter, int first, int count) {
    logger.infof("loading all Users ordered by ascending name matching the given filter %s...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> criteria = cb.createQuery(User.class);
    Root<User> root = criteria.from(User.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }// w w w.j  av  a 2s .  c  om
    criteria.orderBy(cb.asc(root.get(User_.lastName)), cb.asc(root.get(User_.firstName)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:eu.uqasar.service.user.UserService.java

public User getLdapBasedUser(final LdapUser user) {
    logger.infof("looking for LDAP users with username %s or mail %s", user.getUserName(), user.getMail());
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Predicate userName = cb.equal(root.get(User_.userName), user.getUserName());
    Predicate mail = cb.equal(root.get(User_.mail), user.getMail());
    query.where(cb.and(cb.or(userName, mail)));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    return resultList.isEmpty() ? null : resultList.get(0);
}

From source file:com.dbs.sdwt.jpa.JpaUtil.java

public 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 {/*from w  ww.ja va2 s . c o  m*/
        return builder.and(toArray(predicates, Predicate.class));
    }
}