List of usage examples for javax.persistence.criteria CriteriaBuilder or
Predicate or(Predicate... restrictions);
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaClusterSpecs.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 av a 2 s . c om */ public static Specification<ClusterEntity> find(@Nullable final String name, @Nullable final Set<ClusterStatus> statuses, @Nullable final Set<TagEntity> tags, @Nullable final Instant minUpdateTime, @Nullable final Instant maxUpdateTime) { return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ClusterEntity_.name), name)); } if (minUpdateTime != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(ClusterEntity_.updated), minUpdateTime)); } if (maxUpdateTime != null) { predicates.add(cb.lessThan(root.get(ClusterEntity_.updated), maxUpdateTime)); } if (tags != null && !tags.isEmpty()) { final Join<ClusterEntity, TagEntity> tagEntityJoin = root.join(ClusterEntity_.tags); predicates.add(tagEntityJoin.in(tags)); cq.groupBy(root.get(ClusterEntity_.id)); cq.having(cb.equal(cb.count(root.get(ClusterEntity_.id)), tags.size())); } if (statuses != null && !statuses.isEmpty()) { //Could optimize this as we know size could use native array predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(ClusterEntity_.status), status)) .toArray(Predicate[]::new))); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.netflix.genie.core.jpa.specifications.JpaClusterSpecs.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 ww. j av a 2 s . co m*/ */ public static Specification<ClusterEntity> find(final String name, final Set<ClusterStatus> statuses, final Set<String> tags, final Date minUpdateTime, final Date maxUpdateTime) { return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ClusterEntity_.name), name)); } if (minUpdateTime != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(ClusterEntity_.updated), minUpdateTime)); } if (maxUpdateTime != null) { predicates.add(cb.lessThan(root.get(ClusterEntity_.updated), maxUpdateTime)); } if (tags != null && !tags.isEmpty()) { predicates .add(cb.like(root.get(ClusterEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } if (statuses != null && !statuses.isEmpty()) { //Could optimize this as we know size could use native array final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(ClusterEntity_.status), status)) .collect(Collectors.toList()); predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.netflix.genie.core.jpa.specifications.JpaJobSpecs.java
/** * Generate a criteria query predicate for a where clause based on the given parameters. * * @param root The root to use//www . j a v a 2s . c o m * @param cb The criteria builder to use * @param id The job id * @param name The job name * @param user The user who created the job * @param statuses The job statuses * @param tags The tags for the jobs to find * @param clusterName The cluster name * @param cluster The cluster the job should have been run on * @param commandName The command name * @param command The command the job should have been run with * @param minStarted The time which the job had to start after in order to be return (inclusive) * @param maxStarted The time which the job had to start before in order to be returned (exclusive) * @param minFinished The time which the job had to finish after in order to be return (inclusive) * @param maxFinished The time which the job had to finish before in order to be returned (exclusive) * @return The specification */ public static Predicate getFindPredicate(final Root<JobEntity> root, final CriteriaBuilder cb, final String id, final String name, final String user, final Set<JobStatus> statuses, final Set<String> tags, final String clusterName, final ClusterEntity cluster, final String commandName, final CommandEntity command, final Date minStarted, final Date maxStarted, final Date minFinished, final Date maxFinished) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(id)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.id), id)); } if (StringUtils.isNotBlank(name)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(JobEntity_.status), status)).collect(Collectors.toList()); predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } if (tags != null && !tags.isEmpty()) { predicates.add(cb.like(root.get(JobEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } if (cluster != null) { predicates.add(cb.equal(root.get(JobEntity_.cluster), cluster)); } if (StringUtils.isNotBlank(clusterName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.clusterName), clusterName)); } if (command != null) { predicates.add(cb.equal(root.get(JobEntity_.command), command)); } if (StringUtils.isNotBlank(commandName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.commandName), commandName)); } if (minStarted != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.started), minStarted)); } if (maxStarted != null) { predicates.add(cb.lessThan(root.get(JobEntity_.started), maxStarted)); } if (minFinished != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.finished), minFinished)); } if (maxFinished != null) { predicates.add(cb.lessThan(root.get(JobEntity_.finished), maxFinished)); } return cb.and(predicates.toArray(new Predicate[predicates.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 ww .ja va 2 s .co m*/ */ 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:com.dbs.sdwt.jpa.JpaUtil.java
public Predicate orPredicate(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 w w.j av a 2 s .com*/ return builder.or(toArray(predicates, Predicate.class)); } }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaJobSpecs.java
/** * Generate a criteria query predicate for a where clause based on the given parameters. * * @param root The root to use * @param cb The criteria builder to use * @param id The job id//from w ww . j a v a 2 s . co m * @param name The job name * @param user The user who created the job * @param statuses The job statuses * @param tags The tags for the jobs to find * @param clusterName The cluster name * @param cluster The cluster the job should have been run on * @param commandName The command name * @param command The command the job should have been run with * @param minStarted The time which the job had to start after in order to be return (inclusive) * @param maxStarted The time which the job had to start before in order to be returned (exclusive) * @param minFinished The time which the job had to finish after in order to be return (inclusive) * @param maxFinished The time which the job had to finish before in order to be returned (exclusive) * @param grouping The job grouping to search for * @param groupingInstance The job grouping instance to search for * @return The specification */ @SuppressWarnings("checkstyle:parameternumber") public static Predicate getFindPredicate(final Root<JobEntity> root, final CriteriaBuilder cb, @Nullable final String id, @Nullable final String name, @Nullable final String user, @Nullable final Set<JobStatus> statuses, @Nullable final Set<String> tags, @Nullable final String clusterName, @Nullable final ClusterEntity cluster, @Nullable final String commandName, @Nullable final CommandEntity command, @Nullable final Instant minStarted, @Nullable final Instant maxStarted, @Nullable final Instant minFinished, @Nullable final Instant maxFinished, @Nullable final String grouping, @Nullable final String groupingInstance) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(id)) { predicates.add( JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.uniqueId), id)); } if (StringUtils.isNotBlank(name)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(JobEntity_.status), status)).collect(Collectors.toList()); predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } if (tags != null && !tags.isEmpty()) { predicates.add( cb.like(root.get(JobEntity_.tagSearchString), JpaSpecificationUtils.getTagLikeString(tags))); } if (cluster != null) { predicates.add(cb.equal(root.get(JobEntity_.cluster), cluster)); } if (StringUtils.isNotBlank(clusterName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.clusterName), clusterName)); } if (command != null) { predicates.add(cb.equal(root.get(JobEntity_.command), command)); } if (StringUtils.isNotBlank(commandName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.commandName), commandName)); } if (minStarted != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.started), minStarted)); } if (maxStarted != null) { predicates.add(cb.lessThan(root.get(JobEntity_.started), maxStarted)); } if (minFinished != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.finished), minFinished)); } if (maxFinished != null) { predicates.add(cb.lessThan(root.get(JobEntity_.finished), maxFinished)); } if (grouping != null) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.grouping), grouping)); } if (groupingInstance != null) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.groupingInstance), groupingInstance)); } 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/*w w w .jav a 2 s . c om*/ * @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:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java
/** * @param length/*w ww. j a va 2 s . c o m*/ * maxResult * @param start * pagination starting point * @param search * search string like ' name: toto , dupont' to search for %toto% on name + %dupont% in all properties * @param searchProperties * properties to search or all if null * @param orders * sorted result by properties * @return */ public List<ENTITY> findFiltered(Integer length, Integer start, String search, List<String> searchProperties, List<Order> orders) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<ENTITY> query = builder.createQuery(entityClass); Root<ENTITY> root = query.from(entityClass); if (!Strings.isNullOrEmpty(search)) { Predicate[] buildFilterPredicates = BuildFilterPredicates(root, search, searchProperties); if (buildFilterPredicates.length > 0) { query.where(builder.or(buildFilterPredicates)); } } if (orders != null) { query.orderBy(OrderToOrder.toJpa(builder, root, orders)); } TypedQuery<ENTITY> q = entityManager.createQuery(query); if (start != null) { q.setFirstResult(start); } if (length != null) { q.setMaxResults(length); } return findList(q); }
From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java
public Long findFilteredCount(String search, List<String> searchProperties) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<ENTITY> from = query.from(entityClass); query.select(builder.count(from));//from ww w . jav a2 s.co m if (!Strings.isNullOrEmpty(search)) { Predicate[] buildFilterPredicates = BuildFilterPredicates(from, search, searchProperties); if (buildFilterPredicates.length > 0) { query.where(builder.or(buildFilterPredicates)); } } return count(entityManager.createQuery(query)); }
From source file:it.attocchi.jpa2.JPAEntityFilter.java
/** * /*from w ww . ja v a 2s. c o m*/ * @param criteriaBuilder * @param paths * @return */ protected Predicate buildMultiWordLikePredicate(CriteriaBuilder criteriaBuilder, Path<String>... paths) { String[] words = semeRicerca.split(" "); List<Predicate> likeAllFields = new ArrayList<Predicate>(); for (Path<String> path : paths) { List<Predicate> likeOnWord = new ArrayList<Predicate>(); for (String word : words) { likeOnWord.add(criteriaBuilder.like(path, getForLike(word))); } Predicate p1 = criteriaBuilder.and(likeOnWord.toArray(new Predicate[likeOnWord.size()])); likeAllFields.add(p1); likeOnWord.clear(); } Predicate res = criteriaBuilder.or(likeAllFields.toArray(new Predicate[likeAllFields.size()])); return res; }