List of usage examples for javax.persistence.criteria CriteriaBuilder and
Predicate and(Predicate... restrictions);
From source file:com.netflix.genie.server.repository.jpa.CommandSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the command * @param userName The name of the user who created the command * @param statuses The status of the command * @param tags The set of tags to search the command for * @return A specification object used for querying *///from w ww . ja v a2 s . c o m public static Specification<Command> find(final String name, final String userName, final Set<CommandStatus> statuses, final Set<String> tags) { return new Specification<Command>() { @Override public Predicate toPredicate(final Root<Command> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(cb.equal(root.get(Command_.name), name)); } if (StringUtils.isNotBlank(userName)) { predicates.add(cb.equal(root.get(Command_.user), userName)); } if (statuses != null && !statuses.isEmpty()) { //Could optimize this as we know size could use native array final List<Predicate> orPredicates = new ArrayList<>(); for (final CommandStatus status : statuses) { orPredicates.add(cb.equal(root.get(Command_.status), status)); } predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } if (tags != null) { for (final String tag : tags) { if (StringUtils.isNotBlank(tag)) { predicates.add(cb.isMember(tag, root.get(Command_.tags))); } } } return cb.and(predicates.toArray(new Predicate[predicates.size()])); } }; }
From source file:com.github.dactiv.orm.core.spring.data.jpa.specification.support.PropertyFilterSpecification.java
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { List<Predicate> list = new ArrayList<Predicate>(); for (PropertyFilter filter : filters) { list.add(JpaRestrictionBuilder.getRestriction(filter, new SpecificationEntity(root, query, builder))); }/*from ww w .ja v a 2 s . co m*/ return list.size() > 0 ? builder.and(list.toArray(new Predicate[list.size()])) : null; }
From source file:edu.pitt.dbmi.ccd.db.specification.AnnotationSpecification.java
private static Predicate buildSearchSpec(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb, UserAccount requester, String username, String group, Long target, String vocab, String attributeLevel, String attributeName, String attributeReqLevel, Boolean showRedacted, Boolean parentless, Date createdBefore, Date createdAfter, Date modifiedBefore, Date modifiedAfter, Set<String> matches, Set<String> nots) { final List<Predicate> predicates = new ArrayList<>(0); final Predicate authPredicate = authFilter(root, cb, requester); final List<Predicate> filterPredicates = buildFilterPredicates(root, query, cb, username, group, target, vocab, attributeName, attributeLevel, attributeReqLevel, showRedacted, parentless, createdBefore, createdAfter, modifiedBefore, modifiedAfter); final List<Predicate> searchPredicates = buildSearchPredicates(root, query, cb, matches, nots); predicates.add(authPredicate);/*from w w w . j av a 2 s. c o m*/ predicates.addAll(filterPredicates); predicates.addAll(searchPredicates); return cb.and(predicates.toArray(new Predicate[predicates.size()])); }
From source file:com.netflix.genie.core.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 the command for * @param type The type of applications to fine * @return A specification object used for querying *//* w w w.j av a 2s .c o m*/ public static Specification<ApplicationEntity> find(final String name, final String user, final Set<ApplicationStatus> statuses, final Set<String> tags, 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()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(ApplicationEntity_.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(ApplicationEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } 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: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 w w . j a va 2 s . c o 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:eu.uqasar.service.ProductService.java
public List<Product> getAllByDescendingNameFiltered(ProductFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = cb.createQuery(Product.class); Root<Product> root = criteria.from(Product.class); List<Predicate> predicates = getFilterPredicates(filter, cb, root); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }/*from w w w. j a v a 2s . c o m*/ criteria.orderBy(cb.desc(root.get(Product_.releaseDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:eu.uqasar.service.ProductService.java
public List<Product> getAllByAscendingNameFiltered( eu.uqasar.web.pages.products.panels.ProductFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = cb.createQuery(Product.class); Root<Product> from = criteria.from(Product.class); List<Predicate> predicates = getFilterPredicates(filter, cb, from); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }/*from w w w .ja va 2 s . c o m*/ criteria.orderBy(cb.asc(from.get(Product_.releaseDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
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// w w w.ja va 2 s . com */ 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:eu.uqasar.service.ProcessService.java
public long countAllFiltered(final ProcessesFilterStructure filter) { logger.infof("counting all Processes matching the filter %s ...", filter); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Process> from = criteria.from(Process.class); List<Predicate> predicates = getFilterPredicates(filter, cb, from); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }//from www . ja v a 2 s . c om criteria.select(cb.countDistinct(from)); return em.createQuery(criteria).getSingleResult(); }
From source file:eu.uqasar.service.ProcessService.java
public List<Process> getAllByAscendingEndDateNameFiltered(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()]))); }/* w w w . j ava 2 s. co m*/ criteria.orderBy(cb.asc(root.get(Process_.endDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }