Example usage for javax.persistence.criteria CriteriaBuilder lessThan

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

Introduction

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

Prototype

<Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y);

Source Link

Document

Create a predicate for testing whether the first argument is less than the second.

Usage

From source file:example.springdata.jpa.showcase.snippets.CustomerSpecifications.java

/**
 * All customers with an {@link Account} expiring before the given date.
 * //from  w  w  w.  j  av  a  2  s.  com
 * @param date
 * @return
 */
public static Specification<Customer> accountExpiresBefore(final LocalDate date) {

    return new Specification<Customer>() {

        @Override
        public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

            Root<Account> accounts = query.from(Account.class);
            Path<Date> expiryDate = accounts.<Date>get("expiryDate");
            Predicate customerIsAccountOwner = cb.equal(accounts.<Customer>get("customer"), root);
            Predicate accountExpiryDateBefore = cb.lessThan(expiryDate, date.toDateTimeAtStartOfDay().toDate());

            return cb.and(customerIsAccountOwner, accountExpiryDateBefore);
        }
    };
}

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

private static Predicate createdBefore(Root<Annotation> root, CriteriaBuilder cb, Date date) {
    return cb.lessThan(root.<Date>get(CREATED_DATE), date);
}

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

private static Predicate modifiedBefore(Root<Annotation> root, CriteriaBuilder cb, Date date) {
    return cb.lessThan(root.<Date>get(CREATED_DATE), date);
}

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

/**
 * Find jobs that are zombies.
 *
 * @param currentTime The current time/*from  www.ja va 2  s. c o m*/
 * @param zombieTime  The time that zombies should be marked by
 * @return The specification for this query
 */
public static Specification<Job> findZombies(final long currentTime, final long zombieTime) {
    return new Specification<Job>() {
        @Override
        public Predicate toPredicate(final Root<Job> root, final CriteriaQuery<?> cq,
                final CriteriaBuilder cb) {
            // the equivalent query is as follows:
            // update Job set status='FAILED', finishTime=$max, exitCode=$zombie_code,
            // statusMsg='Job has been marked as a zombie'
            // where updateTime < $min and (status='RUNNING' or status='INIT')"
            final List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.lessThan(root.get(Job_.updated), new Date(currentTime - zombieTime)));
            final Predicate orPredicate1 = cb.equal(root.get(Job_.status), JobStatus.RUNNING);
            final Predicate orPredicate2 = cb.equal(root.get(Job_.status), JobStatus.INIT);
            predicates.add(cb.or(orPredicate1, orPredicate2));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

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// www.  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.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 .  j  a  v  a 2  s.  c o  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.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  av a 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:net.sf.gazpachoquest.qbe.RangeSpecification.java

public static <E, D extends Comparable<? super D>> Specification<E> toSpecification(final Range<E, D> range) {
    Validate.isTrue(range.isSet(), "You must pass an exploitable range");
    return new Specification<E>() {
        @Override//from  w  ww.  j a v  a2s  .  c  o m
        public Predicate toPredicate(final Root<E> root, final CriteriaQuery<?> query,
                final CriteriaBuilder builder) {
            Predicate rangePredicate = null;

            if (range.isBetween()) {
                rangePredicate = builder.between(root.get(range.getField()), range.getFrom(), range.getTo());
            } else if (range.isFromSet()) {
                // rangePredicate =
                // builder.greaterThanOrEqualTo(root.get(range.getField()),
                // range.getFrom());
                rangePredicate = builder.greaterThan(root.get(range.getField()), range.getFrom());
            } else if (range.isToSet()) {
                // rangePredicate =
                // builder.lessThanOrEqualTo(root.get(range.getField()),
                // range.getTo());
                rangePredicate = builder.lessThan(root.get(range.getField()), range.getTo());
            }

            if (rangePredicate != null) {
                if (!range.isIncludeNullSet() || Boolean.FALSE.equals(range.getIncludeNull())) {
                    return rangePredicate;
                } else {
                    return builder.or(rangePredicate, builder.isNull(root.get(range.getField())));
                }
            }

            // no range at all
            // take the opportunity to keep only null...
            if (Boolean.TRUE.equals(range.getIncludeNull())) {
                return builder.isNull(root.get(range.getField()));
            }

            // ... or non-null only...
            if (Boolean.FALSE.equals(range.getIncludeNull())) {
                return builder.isNotNull(root.get(range.getField()));
            }

            throw new IllegalStateException("You must pass an exploitable range (should not happen here)");
        }
    };
}

From source file:com.goodhuddle.huddle.repository.BlogPostSpecification.java

public static Specification<BlogPost> search(final Huddle huddle, final SearchBlogPostRequest request) {
    return new Specification<BlogPost>() {
        @Override//from w  ww  .  ja  va  2s .com
        public Predicate toPredicate(Root<BlogPost> blogPost, CriteriaQuery<?> query, CriteriaBuilder builder) {

            Predicate conjunction = builder.conjunction();
            conjunction.getExpressions().add(builder.equal(blogPost.get("huddle"), huddle));

            if (StringUtils.isNotBlank(request.getPhrase())) {
                String phrase = "%" + request.getPhrase().toLowerCase() + "%";
                conjunction.getExpressions()
                        .add(builder.like(builder.lower(blogPost.<String>get("title")), phrase));
            }

            if (CollectionUtils.isNotEmpty(request.getBlogIds())) {
                Join<Object, Object> blog = blogPost.join("blog");
                conjunction.getExpressions().add(builder.in(blog.get("id")).value(request.getBlogIds()));
            }

            if (!request.isIncludeUnpublished()) {
                conjunction.getExpressions()
                        .add(builder.lessThan((Expression) blogPost.get("publishedOn"), new DateTime()));
            }

            return conjunction;
        }
    };
}

From source file:example.springdata.jpa.showcase.snippets.AccountRepositoryImpl.java

@Override
public void removedExpiredAccounts(LocalDate reference) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Account> query = cb.createQuery(Account.class);
    Root<Account> account = query.from(Account.class);

    query.where(/*from w  w w .  j a  va  2s.co  m*/
            cb.lessThan(account.get("expiryDate").as(Date.class), reference.toDateTimeAtStartOfDay().toDate()));

    for (Account each : em.createQuery(query).getResultList()) {
        em.remove(each);
    }
}