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(Expression<Boolean> x, Expression<Boolean> y);

Source Link

Document

Create a conjunction of the given boolean expressions.

Usage

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

private static Predicate existsInData(Root<Annotation> root, CriteriaQuery query, CriteriaBuilder cb,
        String term) {// ww w .  j av  a  2  s  . com
    final Subquery<AnnotationData> subquery = query.subquery(AnnotationData.class);
    final Root<AnnotationData> subroot = subquery.from(AnnotationData.class);
    return cb.exists(subquery.select(subroot)
            .where(cb.and(cb.equal(subroot.get(ANNO), root), cb.like(cb.lower(subroot.get(VALUE)), term))));
}

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

/**
 * Requester access predicate//from   w w  w  . j  ava2  s  .c  o m
 * <p>
 * annotation has public access OR annotation has private access AND
 * annotation belongs to requester OR annotation has group access AND
 * requester is in group
 */
private static Predicate authFilter(Root<Annotation> root, CriteriaBuilder cb, UserAccount requester) {
    final List<Predicate> predicates = new ArrayList<>(0);

    // public access
    predicates.add(cb.like(root.get(ACCESS).get(NAME), PUBLIC_ACCESS));

    // private access AND belongs to requester
    predicates.add(
            cb.and(cb.like(root.get(ACCESS).get(NAME), PRIVATE_ACCESS), cb.equal(root.get(USER), requester)));

    // group access AND requester in group
    // criteriabuilder's in clause throws
    // sql error if collection has no elements
    if (requester.getGroups().size() > 0) {
        predicates.add(cb.and(cb.like(root.get(ACCESS).get(NAME), GROUP_ACCESS),
                root.get(GROUP).in(requester.getGroups())));
    }
    return cb.or(predicates.toArray(new Predicate[predicates.size()]));
}

From source file:com.hengyi.japp.execution.Util.java

public static void queryCommand(CriteriaBuilder cb, CriteriaQuery<?> cq, Root<Task> root,
        TaskQueryCommand command) {/*from   w w w  . j  ava  2 s  .c o  m*/
    Predicate p1 = cb.equal(root.get(Task_.charger), command.getOperator());
    ListJoin<Task, Operator> joinFollowers = root.join(Task_.followers, JoinType.LEFT);
    Predicate p2 = cb.equal(joinFollowers.get(Operator_.id), command.getOperator().getId());
    ListJoin<Task, Operator> joinExecutors = root.join(Task_.executors, JoinType.LEFT);
    Predicate p3 = cb.equal(joinExecutors.get(Operator_.id), command.getOperator().getId());
    Predicate p = cb.or(p1, p2, p3);
    if (command.getExecutor() != null) {
        p = cb.and(p, cb.equal(p, cb.isMember(command.getExecutor(), root.get(Task_.executors))));
    }
    if (command.getCustomer() != null) {
        p = cb.and(p, cb.equal(root.get(Task_.customer), command.getCustomer()));
    }
    if (!isBlank(command.getContent())) {
        p = cb.and(p, cb.like(root.get(Task_.content), command.getContentQuery()));
    }
    Collection<TaskType> TaskTypes = command.getTypes();
    if (TaskTypes != null && !TaskTypes.isEmpty()) {
        p = cb.and(p, root.get(Task_.type).in(TaskTypes));
    }
    Collection<TaskStatus> statuses = command.getStatuses();
    if (statuses != null && !statuses.isEmpty()) {
        p = cb.and(p, root.get(Task_.status).in(statuses));
    }
    if (command.getCreateDate() != null) {
        Date createDateStart = LocalDate.fromDateFields(command.getCreateDate()).toDate();
        Date createDateEnd = LocalDate.fromDateFields(command.getCreateDate()).plusDays(1).toDate();
        p = cb.and(p, cb.between(root.get(Task_.logInfo).get(LogInfo_.createDateTime), createDateStart,
                createDateEnd));
        // TODO timestamp date convert
        //p = cb.and(p, cb.equal(root.get(Task_.logInfo).get(LogInfo_.createDateTime).as(java.sql.Date.class), command.getCreateDate()));
    }
    cq.where(p);
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a predicate based on an "And" filter
 * /*  w w  w.j  a  v a2  s.c  om*/
 * @param builder
 *            the criteria builder
 * @param root
 *            the root object
 * @param filter
 *            the "And" filter
 * @return
 */
private static Predicate createAndPredicate(CriteriaBuilder builder, Root<?> root, Filter filter) {
    And and = (And) filter;
    List<Filter> filters = new ArrayList<>(and.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.and(predicate, next);
            }
        }
    }
    return predicate;
}

From source file:com.sixsq.slipstream.persistence.Run.java

private static Predicate andPredicate(CriteriaBuilder builder, Predicate currentPredicate,
        Predicate newPredicate) {
    return (currentPredicate != null) ? builder.and(currentPredicate, newPredicate) : newPredicate;
}

From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

private Predicate isEqual(String field, Object value, Root<Order> root, Predicate predicate,
        CriteriaBuilder builder) {
    return value != null ? builder.and(predicate, builder.equal(root.get(field), value)) : predicate;
}

From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java

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

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findRandomStalledTask() {

    Calendar TOO_LONG_AGO = Calendar.getInstance();
    TOO_LONG_AGO.add(Calendar.SECOND, -7200);

    // select qth from QueuedTask where 
    //      qth.startedStamp != null AND
    //      qth.startedStamp < TOO_LONG_AGO
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth).where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.completedStamp)),
            cb.lessThan(qth.get(QueuedTaskHolder_.startedStamp), TOO_LONG_AGO)));

    List<QueuedTaskHolder> stalledTasks = this.entityManager.createQuery(cq).getResultList();

    if (stalledTasks.isEmpty()) {
        return null;
    } else {/*from  w  w w.ja  v  a 2s  .co m*/
        Random rand = new Random(System.currentTimeMillis());
        return stalledTasks.get(rand.nextInt(stalledTasks.size()));
    }

}

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findNextTaskForExecution() {

    Calendar NOW = Calendar.getInstance();

    // select qt from QueuedTask where
    //      qt.startedStamp == null AND
    //      (qth.triggerStamp == null || qth.triggerStamp < NOW)
    // order by qth.version ASC, qt.creationStamp ASC
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth)//from www  .  jav a2s .c o  m
            .where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.startedStamp)),
                    cb.or(cb.isNull(qth.get(QueuedTaskHolder_.triggerStamp)),
                            cb.lessThan(qth.get(QueuedTaskHolder_.triggerStamp), NOW))))
            .orderBy(cb.asc(qth.get(QueuedTaskHolder_.version)),
                    cb.asc(qth.get(QueuedTaskHolder_.creationStamp)));

    List<QueuedTaskHolder> results = this.entityManager.createQuery(cq).setMaxResults(1).getResultList();
    if (results.isEmpty()) {
        return null;
    } else {
        return results.get(0);
    }

}

From source file:net.groupbuy.dao.impl.ParameterDaoImpl.java

public List<Parameter> findList(ParameterGroup parameterGroup, Set<Parameter> excludes) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Parameter> criteriaQuery = criteriaBuilder.createQuery(Parameter.class);
    Root<Parameter> root = criteriaQuery.from(Parameter.class);
    criteriaQuery.select(root);// w ww .  j  av a2s.  com
    Predicate restrictions = criteriaBuilder.conjunction();
    if (parameterGroup != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("parameterGroup"), parameterGroup));
    }
    if (excludes != null && !excludes.isEmpty()) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes)));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}