Example usage for javax.persistence.criteria CriteriaBuilder or

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

Introduction

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

Prototype

Predicate or(Expression<Boolean> x, Expression<Boolean> y);

Source Link

Document

Create a disjunction of the given boolean expressions.

Usage

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

private static List<Predicate> notInNameOrDescription(Root<Vocabulary> root, CriteriaBuilder cb,
        Set<String> terms) {
    return terms.stream().map(t -> containsLike(t))
            .map(t -> cb.not(cb.or(nameContains(root, cb, t), descriptionContains(root, cb, t))))
            .collect(Collectors.toList());
}

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

private static List<Predicate> inTitleOrAddressOrName(Root<AnnotationTarget> root, CriteriaBuilder cb,
        Set<String> terms) {
    return terms.stream().map(t -> containsLike(t))
            .map(t -> cb.or(titleContains(root, cb, t),
                    (root.get(ADDRESS) != null) ? addressContains(root, cb, t) : fileNameContains(root, cb, t)))
            .collect(Collectors.toList());
}

From source file:org.ngrinder.perftest.repository.PerfTestSpecification.java

/**
 * Get the search {@link Specification} for test name and description fields.
 *
 * @param queryString query String//from w  w  w  .  j av  a  2 s  .  c om
 * @return {@link Specification}
 */
public static Specification<PerfTest> likeTestNameOrDescription(final String queryString) {
    return new Specification<PerfTest>() {
        @Override
        public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            String queryStr = ("%" + queryString + "%").toLowerCase();
            return cb.or(cb.like(cb.lower(root.get("testName").as(String.class)), queryStr),
                    cb.like(root.get("description").as(String.class), queryStr));
        }
    };
}

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

private static List<Predicate> notInTitleOrAddressOrName(Root<AnnotationTarget> root, CriteriaBuilder cb,
        Set<String> terms) {
    return terms/*from  w  w  w .  j  av a2  s  . co  m*/
            .stream().map(
                    t -> containsLike(t))
            .map(t -> cb.not(
                    cb.or(titleContains(root, cb, t), (root.get(ADDRESS) != null) ? addressContains(root, cb, t)
                            : fileNameContains(root, cb, t))))
            .collect(Collectors.toList());
}

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

/**
 * Find jobs that are zombies.
 *
 * @param currentTime The current time/*w  w  w. j  ava2  s .  co 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.ocs.dynamo.dao.query.JpaQueryBuilder.java

private static Predicate createOrPredicate(CriteriaBuilder builder, Root<?> root, Filter filter) {
    Or or = (Or) filter;/*from www .  ja  v a2  s.  c  o m*/
    List<Filter> filters = new ArrayList<>(or.getFilters());

    Predicate predicate = null;
    if (!filters.isEmpty()) {
        predicate = createPredicate(filters.remove(0), builder, root);
        while (!filters.isEmpty()) {
            Predicate next = createPredicate(filters.remove(0), builder, root);
            if (next != null) {
                predicate = builder.or(predicate, next);
            }
        }
    }

    return predicate;
}

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/*ww w  . j a  v a 2s .c om*/
        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:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

private Predicate isOneOfOr(String field, Collection collection, Root<Order> root, Predicate predicate,
        CriteriaBuilder builder) {
    return !isEmpty(collection) ? builder.or(predicate, root.get(field).in(collection)) : predicate;
}

From source file:csns.model.core.dao.jpa.UserDaoImpl.java

@Override
public List<User> getUsers(Long ids[]) {
    if (ids == null || ids.length < 1)
        return new ArrayList<User>();

    CriteriaBuilder cbuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cquery = cbuilder.createQuery(User.class);
    Root<User> user = cquery.from(User.class);

    Predicate criteria = cbuilder.equal(user.get("id"), ids[0]);
    for (int i = 1; i < ids.length; ++i)
        criteria = cbuilder.or(criteria, cbuilder.equal(user.get("id"), ids[i]));
    cquery.where(criteria);// w  w  w  . ja  v a 2 s.  c  o m

    cquery.orderBy(cbuilder.asc(user.get("lastName")), cbuilder.asc(user.get("firstName")));

    return entityManager.createQuery(cquery).getResultList();
}

From source file:net.kaczmarzyk.spring.data.jpa.domain.EqualEnum.java

@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    List<Enum<?>> matchingEnumConstants = findMatchingEnumConstants(getEnumClass(root));
    Iterator<Enum<?>> iterator = matchingEnumConstants.iterator();
    Predicate combinedPredicates = builder.equal(this.<Enum<?>>path(root), iterator.next());
    while (iterator.hasNext()) {
        combinedPredicates = builder.or(builder.equal(this.<Enum<?>>path(root), iterator.next()),
                combinedPredicates);/*w  ww .  j  av a  2  s .  co m*/
    }
    return combinedPredicates;
}