Example usage for javax.persistence.criteria CriteriaBuilder asc

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

Introduction

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

Prototype

Order asc(Expression<?> x);

Source Link

Document

Create an ordering by the ascending value of the expression.

Usage

From source file:com.sfs.ucm.controller.HelpContentAction.java

/**
 * load resources//from   w ww . j  av a 2  s.c o  m
 * 
 * @throws UCMException
 */
private void loadList() throws UCMException {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Help> c = cb.createQuery(Help.class);
        Root<Help> obj = c.from(Help.class);
        c.select(obj).orderBy(cb.asc(obj.get("keyword")));
        this.helpItems = em.createQuery(c).getResultList();

        this.itemCount = this.helpItems.size();
    } catch (Exception e) {
        throw new UCMException(e);
    }
}

From source file:org.openmeetings.app.data.basic.dao.LdapConfigDaoImpl.java

public List<LdapConfig> getLdapConfigs(int start, int max, String orderby, boolean asc) {
    try {//from   w  w w  .ja  va  2  s . c  o m
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<LdapConfig> cq = cb.createQuery(LdapConfig.class);
        Root<LdapConfig> c = cq.from(LdapConfig.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<LdapConfig> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<LdapConfig> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getLdapConfigs]", ex2);
    }
    return null;
}

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

private <T> CriteriaQuery<T> addSortProperties(CriteriaQuery<T> query, Root root, Pageable pageable) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    List<javax.persistence.criteria.Order> orders = new ArrayList<>();
    Iterator<Sort.Order> iterator = pageable.getSort().iterator();
    Sort.Order order;//from   w  w  w  . j  a va 2  s . c om

    while (iterator.hasNext()) {
        order = iterator.next();
        String property = order.getProperty();

        Path path = root.get(property);
        if (order.isAscending()) {
            orders.add(builder.asc(path));
        } else {
            orders.add(builder.desc(path));
        }
    }
    return query.orderBy(orders);
}

From source file:eu.uqasar.service.dataadapter.JiraDataService.java

/**
 * /*w w  w .ja  va  2s.co  m*/
 * @param first
 * @param count
 * @return
 */
public List<JiraMetricMeasurement> getAllByAscendingName(int first, int count) {
    logger.infof("loading all JiraMetricMeasurement ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> criteria = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = criteria.from(JiraMetricMeasurement.class);
    criteria.orderBy(cb.asc(root.get(JiraMetricMeasurement_.self)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

private List<Order> toOrders(Sort sort, Root<T> root, CriteriaBuilder cb) {

    List<Order> orders = new ArrayList<Order>();

    if (sort == null) {
        return orders;
    }//from   ww  w .  jav a  2  s  .c  o  m

    for (Property property : sort) {
        Expression<?> expression = root.get(property.getName());
        orders.add(property.isAscending() ? cb.asc(expression) : cb.desc(expression));

    }

    return orders;
}

From source file:org.pushio.webapp.support.persistence.DynamicSpecifications.java

/**
 * @see ??JPA/*from w  w  w.  j ava2s  . c o m*/
 * @param filters
 * @param entityClazz
 * @param isDistinct  trueSQLdistinctfalse?
 * @return
 */
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz, final boolean isDistinct, final List<OrderParam> orderParams) {
    return new Specification<T>() {
        @Override
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (orderParams != null && orderParams.size() > 0) {
                /*
                CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
                Root<Foo> from = criteriaQuery.from(Foo.class);
                CriteriaQuery<Foo> select = criteriaQuery.select(from);
                criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
                */
                List<Order> orders = new ArrayList<Order>(orderParams.size());
                for (OrderParam orderParam : orderParams) {
                    if (orderParam != null && orderParam.getField() != null) {
                        String fields[] = StringUtil.split(orderParam.getField(), '.');
                        Path expression = (fields.length > 1) ? root.join(fields[0]) : root.get(fields[0]);
                        for (int i = 1, len = fields.length; i < len; ++i) {
                            expression = expression.get(fields[i]);
                        }
                        if (expression != null) {
                            Order order = (orderParam.getType() == null
                                    || orderParam.getType().equalsIgnoreCase("asc")) ? builder.asc(expression)
                                            : builder.desc(expression);
                            orders.add(order);
                            //                        query.orderBy(order);
                        }
                    }
                }
                query.orderBy(orders);
            }
            query.distinct(isDistinct);
            if (Collections3.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 = null;

                    //////  ?
                    boolean hasJoin = names[0].startsWith("[join]");
                    String fn = hasJoin ? names[0].substring(6) : names[0];
                    boolean isNotDateType = !(filter.value instanceof Date);
                    try {
                        expression = hasJoin ? root.join(fn) : root.get(fn);
                        if (isNotDateType && isDateType(expression.getJavaType())) {
                            // filter.value??
                            filter.value = parseDate(filter.value.toString(), filter.operator);
                        }
                    } catch (Exception e) {
                        //                     logger.error(e.getMessage(), e);
                        continue; // ??
                    }
                    boolean isPropertyNotValid = false;
                    for (int i = 1; i < names.length; i++) {
                        try {
                            expression = expression.get(names[i]);
                            if (isNotDateType && isDateType(expression.getJavaType())) {
                                filter.value = parseDate(filter.value.toString(), filter.operator);
                            }
                        } catch (Exception e) {
                            //                        logger.error(e.getMessage(), e);
                            isPropertyNotValid = true; // 
                            break; // ??
                        }
                    }
                    if (expression == null || isPropertyNotValid) {
                        continue;
                    }
                    ///////

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        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 ORLIKE:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.like(expression, "%" + values[i] + "%");
                            }
                            predicates.add(builder.or(like));
                        }

                        break;
                    case ANDLIKE:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.like(expression, "%" + values[i] + "%");
                            }
                            predicates.add(builder.and(like));
                        }
                        break;
                    case ANDNOTLIKE:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.notLike(expression, "%" + values[i] + "%");
                            }
                            predicates.add(builder.and(like));
                        }
                        break;
                    case OREQ:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.equal(expression, values[i]);
                            }
                            predicates.add(builder.or(like));
                        }
                        break;

                    case ANDNOTEQ:
                        if (filter.value instanceof String) {
                            String value = (String) filter.value;
                            String[] values = value.split(",");
                            Predicate[] like = new Predicate[values.length];
                            for (int i = 0; i < values.length; i++) {
                                like[i] = builder.notEqual(expression, values[i]);
                            }
                            predicates.add(builder.and(like));
                        }
                        break;
                    case NOTEQ:
                        predicates.add(builder.notEqual(expression, (Comparable) filter.value));
                        break;
                    case NOTLIKE:
                        predicates.add(builder.notLike(expression, "%" + filter.value + "%"));
                        break;
                    case NULL:
                        predicates.add(builder.isNull(expression));
                        break;
                    case NOTNULL:
                        predicates.add(builder.isNotNull(expression));
                        break;
                    //                  case IN:
                    //                     predicates.add(builder.in(expression).in(values));
                    //                     break;
                    }
                }

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

            return builder.conjunction();
        }
    };
}

From source file:org.openmeetings.app.data.basic.Configurationmanagement.java

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {/*from   w  w  w  .j ava 2  s .com*/
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
        Root<Configuration> c = cq.from(Configuration.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Configuration> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Configuration> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getConfigurations]", ex2);
    }
    return null;
}

From source file:com.moderndrummer.data.MemberDaoImpl.java

@Override
public List<Member> findAllCreatedMembersByFromAndToDate(Date fromDate, Date toDate) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
    Root<Member> member = criteria.from(Member.class);
    Path<Date> dateCreatedPath = member.get("createdDate");
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(cb.lessThanOrEqualTo(dateCreatedPath, toDate));
    predicates.add(cb.greaterThanOrEqualTo(dateCreatedPath, fromDate));
    criteria.where(predicates.toArray(new Predicate[predicates.size()]));
    criteria.select(member).orderBy(cb.asc(member.get("name")));
    return em.createQuery(criteria).getResultList();
}

From source file:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * //w  w w .ja v  a  2 s.  c  o m
 * @param first
 * @param count
 * @return
 */
public List<CubesMetricMeasurement> getAllByAscendingName(int first, int count) {
    logger.infof("loading all CubesMetricMeasurement ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> criteria = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = criteria.from(CubesMetricMeasurement.class);
    criteria.orderBy(cb.asc(root.get(CubesMetricMeasurement_.self)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();
}