Example usage for javax.persistence.criteria CriteriaBuilder isFalse

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

Introduction

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

Prototype

Predicate isFalse(Expression<Boolean> x);

Source Link

Document

Create a predicate testing for a false value.

Usage

From source file:de.hopmann.repositories.cran.service.CRANPackageListingService.java

public void removeCurrentListing() {
    // TODO Bulk removal, JPQL not cascading
    // entityManager.createQuery("DELETE FROM DependencyEntity").executeUpdate();
    // or//from   w ww.java  2s  .c  o  m
    // entityManager.createQuery("DELETE FROM PackageEntity").executeUpdate();

    // Alternative

    // CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    //
    // CriteriaDelete<PackageEntity> delete = cb
    // .createCriteriaDelete(PackageEntity.class);
    // Root<PackageEntity> p = delete.from(PackageEntity.class);
    // delete.where(cb.isFalse(p.get(PackageEntity_.archived)));
    //
    // entityManager.createQuery(delete).executeUpdate();

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<CRANPackageEntity> query = cb.createQuery(CRANPackageEntity.class);
    Root<CRANPackageEntity> p = query.from(CRANPackageEntity.class);
    query.where(cb.isFalse(p.get(CRANPackageEntity_.archived)));

    List<CRANPackageEntity> resultList = entityManager.createQuery(query).getResultList();

    for (CRANPackageEntity cRANPackageEntity : resultList) {
        entityManager.remove(cRANPackageEntity);
    }
}

From source file:com.dbs.sdwt.jpa.ByPropertySelectorUtil.java

private <E> void byBooleanSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates,
        SearchParameters sp, PropertySelector<? super E, Boolean> selector) {
    if (selector.isNotEmpty()) {
        List<Predicate> selectorPredicates = newArrayList();

        for (Boolean selection : selector.getSelected()) {
            Path<Boolean> path = jpaUtil.getPath(root, selector.getAttributes());
            if (selection == null) {
                selectorPredicates.add(builder.isNull(path));
            } else {
                selectorPredicates.add(selection ? builder.isTrue(path) : builder.isFalse(path));
            }/*  w  w w  .  j a va 2s  .  c  o m*/
        }
        if (selector.isOrMode()) {
            predicates.add(jpaUtil.orPredicate(builder, selectorPredicates));
        } else {
            predicates.add(jpaUtil.andPredicate(builder, selectorPredicates));
        }
    }
}

From source file:de.whs.poodle.repositories.StatisticsRepository.java

public List<Statistic> getStatistics(FeedbackSearchCriteria s, int instructorId, int limit) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Statistic> cq = cb.createQuery(Statistic.class);
    Root<Statistic> statistic = cq.from(Statistic.class);

    /* fetch the exercises immediately so they don't have to
     * be lazy-fetched one by one while rendering the table rows. */
    statistic.fetch("exercise");

    // join we need to match the course
    Join<Statistic, Course> course = statistic.join("courseTerm").join("course");

    // create empty "and" predicate
    Predicate where = cb.conjunction();

    // only non-empty
    Predicate notEmpty = cb.isFalse(statistic.get("empty"));
    where = cb.and(where, notEmpty);// w  w w.  ja v a2  s .  co m

    // only statistics for courses that the instructor has access to
    Predicate hasInstructorAccessToCourse = cb.isTrue(cb.function("has_instructor_access_to_course",
            Boolean.class, course.get("id"), cb.literal(instructorId)));

    where = cb.and(where, hasInstructorAccessToCourse);

    // filter by courseId
    if (s.getCourseId() != 0) {
        Predicate courseMatches = cb.equal(course.get("id"), s.getCourseId());
        where = cb.and(where, courseMatches);
    }

    // filter by student
    if (s.getStudent() != null) {
        Predicate studentMatches = cb.equal(statistic.get("student").get("id"), s.getStudent().getId());
        where = cb.and(where, studentMatches);
    }

    cq.where(where);

    // order by date
    cq.orderBy(cb.desc(statistic.get("completedAt")));

    return em.createQuery(cq).setMaxResults(limit).getResultList();
}

From source file:com.faceye.feature.repository.jpa.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*from w  ww .  j a v  a  2  s  .c  om*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (CollectionUtils.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        if (filter.value instanceof Number) {
                            predicates.add(builder.equal(expression, (Number) filter.value));
                        } else if (filter.value instanceof String) {
                            if (StringUtils.isNotEmpty(filter.value.toString())) {
                                predicates.add(builder.equal(expression, filter.value));
                            }
                        } else {
                            predicates.add(builder.equal(expression, filter.value));
                        }
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case ISTRUE:
                        predicates.add(builder.isTrue(expression));
                        break;
                    case ISFALSE:
                        predicates.add(builder.isFalse(expression));
                        break;
                    case ISEMPTY:
                        predicates.add(builder.isEmpty(expression));
                        break;
                    case ISNULL:
                        predicates.add(builder.isNull(expression));
                        break;
                    case NE:
                        predicates.add(builder.notEqual(expression, filter.value));
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TBusApplyinfoEntity> getApplicationForSync(DateTime startTime, DateTime endTime, String status,
        Boolean isSend, PrincipalExt principalExt) {
    if (startTime == null) {
        startTime = DateTimeUtil.appStartTime;
    }//from www  . j  a va2 s.c  om
    if (endTime == null) {
        endTime = DateTimeUtil.appEndTime;
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class);
    applyRoot.fetch(TBusApplyinfoEntity_.passenger);
    applyRoot.fetch(TBusApplyinfoEntity_.coordinator);
    applyRoot.fetch(TBusApplyinfoEntity_.department);
    applyRoot.fetch(TBusApplyinfoEntity_.senduser, JoinType.LEFT);

    Predicate predicate = cb.conjunction();

    predicate = cb.and(predicate, cb.or(cb.isNull(applyRoot.get(TBusApplyinfoEntity_.updateBySync)),
            cb.isFalse(applyRoot.get(TBusApplyinfoEntity_.updateBySync))));
    if (status != null) {
        predicate = cb.and(predicate,
                // "2" ->  "3" -> ? "4" -> ?
                cb.equal(applyRoot.get(TBusApplyinfoEntity_.status), status));
    }
    if (isSend != null && isSend) {
        predicate = cb.and(predicate, cb.equal(applyRoot.get(TBusApplyinfoEntity_.issend), "1") // 
        );
    }
    if ("4".equals(status)) {
        Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot
                .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT);
        Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch;
        scheduleJoin.fetch(TBusScheduleRelaEntity_.reciveuser, JoinType.LEFT);
        Fetch<TBusScheduleRelaEntity, TBusScheduleCarEntity> fetchScheduleCar = scheduleFetch
                .fetch(TBusScheduleRelaEntity_.scheduleCars, JoinType.LEFT);
        fetchScheduleCar.fetch(TBusScheduleCarEntity_.car, JoinType.LEFT).fetch(TAzCarinfoEntity_.depot,
                JoinType.LEFT);
        fetchScheduleCar.fetch(TBusScheduleCarEntity_.driver, JoinType.LEFT)
                .fetch(TRsDriverinfoEntity_.department, JoinType.LEFT);
        predicate = cb.and(predicate,
                cb.or(cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime),
                        cb.between(scheduleJoin.get(TBusScheduleRelaEntity_.updatedate), startTime, endTime)));
    } else {
        predicate = cb.and(predicate,
                cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime));
    }

    cq.where(predicate);

    TypedQuery<TBusApplyinfoEntity> query = em.createQuery(cq);

    //        applySecurityFilter("applications", principalExt);
    return query.getResultList();
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TBusApproveSugEntity> listApplyApproveInfo(DateTime startTime, DateTime endTime,
        PrincipalExt principalExtOrNull) {
    if (startTime == null) {
        startTime = DateTimeUtil.appStartTime;
    }/*from   w  w w.ja  v a2 s.c  o  m*/
    if (endTime == null) {
        endTime = DateTimeUtil.appEndTime;
    }

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class);
    Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class);
    approveRoot.fetch(TBusApproveSugEntity_.application);
    approveRoot.fetch(TBusApproveSugEntity_.user, JoinType.LEFT);
    cq.select(approveRoot);

    Predicate predicate = cb.and(
            cb.between(approveRoot.get(TBusApproveSugEntity_.operatedate), new Timestamp(startTime.getMillis()),
                    new Timestamp(endTime.getMillis())),
            cb.or(cb.isNull(approveRoot.get(TBusApproveSugEntity_.updateBySync)),
                    cb.isFalse(approveRoot.get(TBusApproveSugEntity_.updateBySync))));

    cq.where(predicate);

    TypedQuery<TBusApproveSugEntity> query = em.createQuery(cq);

    applySecurityFilter("applications", principalExtOrNull);
    return query.getResultList();
}

From source file:org.opencastproject.messages.MailService.java

public List<MessageTemplate> findMessageTemplates(TemplateMessageQuery query) {
    EntityManager em = null;/*from  w  w  w.  ja  v  a 2s. c om*/
    List<MessageTemplate> messageTemplates = new ArrayList<MessageTemplate>();

    try {
        em = emf.createEntityManager();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<MessageTemplateDto> q = cb.createQuery(MessageTemplateDto.class);
        Root<MessageTemplateDto> messageTemplateRoot = q.from(MessageTemplateDto.class);

        List<Predicate> predicates = new ArrayList<Predicate>();

        q.select(messageTemplateRoot);

        String orgId = securityService.getOrganization().getId();
        predicates.add(cb.equal(messageTemplateRoot.get("organization"), orgId));

        if (!query.isIncludeHidden())
            predicates.add(cb.isFalse(messageTemplateRoot.get("hidden").as(Boolean.class)));

        if (StringUtils.isNotEmpty(query.getName()))
            predicates.add(cb.equal(messageTemplateRoot.get("name"), query.getName()));

        if (StringUtils.isNotEmpty(query.getCreator()))
            predicates.add(cb.equal(messageTemplateRoot.get("creator"), query.getCreator()));

        if (query.getType() != null)
            predicates.add(
                    cb.equal(messageTemplateRoot.get("type").as(TemplateType.Type.class), query.getType()));

        if (StringUtils.isNotEmpty(query.getFullText())) {
            List<Predicate> fullTextPredicates = new ArrayList<Predicate>();
            fullTextPredicates
                    .add(cb.like(messageTemplateRoot.<String>get("name"), "%" + query.getFullText() + "%"));
            fullTextPredicates
                    .add(cb.like(messageTemplateRoot.<String>get("subject"), "%" + query.getFullText() + "%"));
            fullTextPredicates
                    .add(cb.like(messageTemplateRoot.<String>get("body"), "%" + query.getFullText() + "%"));
            predicates.add(cb.or(fullTextPredicates.toArray(new Predicate[fullTextPredicates.size()])));
        }

        q.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));

        TypedQuery<MessageTemplateDto> typedQuery = em.createQuery(q);
        List<MessageTemplateDto> messageTemplatesDto = typedQuery.getResultList();

        for (MessageTemplateDto mt : messageTemplatesDto) {
            messageTemplates.add(mt.toMessageTemplate(userDirectoryService));
        }

        return messageTemplates;
    } finally {
        if (em != null)
            em.close();
    }
}