Example usage for javax.persistence.criteria CriteriaQuery getOrderList

List of usage examples for javax.persistence.criteria CriteriaQuery getOrderList

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery getOrderList.

Prototype

List<Order> getOrderList();

Source Link

Document

Return the ordering expressions in order of precedence.

Usage

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Copy Criteria without Selection.//  w ww.j a  va  2 s. c om
 * @param from source Criteria.
 * @param to destination Criteria.
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {
    copyCriteriaWithoutSelectionAndOrder(from, to);
    to.orderBy(from.getOrderList());
}

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

private void addOrders(CriteriaQuery<T> criteriaQuery, Pageable pageable) {
    if (criteriaQuery == null || pageable == null) {
        return;//  w w w  . j a v a  2  s .c  om
    }
    Root<T> root = getRoot(criteriaQuery);
    if (root == null) {
        return;
    }
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    List<javax.persistence.criteria.Order> orderList = new ArrayList<javax.persistence.criteria.Order>();
    if (!criteriaQuery.getOrderList().isEmpty()) {
        orderList.addAll(criteriaQuery.getOrderList());
    }
    if (StringUtils.isNotEmpty(pageable.getOrderProperty()) && pageable.getOrderDirection() != null) {
        if (pageable.getOrderDirection() == Direction.asc) {
            orderList.add(criteriaBuilder.asc(root.get(pageable.getOrderProperty())));
        } else if (pageable.getOrderDirection() == Direction.desc) {
            orderList.add(criteriaBuilder.desc(root.get(pageable.getOrderProperty())));
        }
    }
    if (pageable.getOrders() != null) {
        for (Order order : pageable.getOrders()) {
            if (order.getDirection() == Direction.asc) {
                orderList.add(criteriaBuilder.asc(root.get(order.getProperty())));
            } else if (order.getDirection() == Direction.desc) {
                orderList.add(criteriaBuilder.desc(root.get(order.getProperty())));
            }
        }
    }
    criteriaQuery.orderBy(orderList);
}

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

private void addOrders(CriteriaQuery<T> criteriaQuery, List<Order> orders) {
    if (criteriaQuery == null || orders == null || orders.isEmpty()) {
        return;/*from  w  w  w.j a  va 2  s. c o  m*/
    }
    Root<T> root = getRoot(criteriaQuery);
    if (root == null) {
        return;
    }
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    List<javax.persistence.criteria.Order> orderList = new ArrayList<javax.persistence.criteria.Order>();
    if (!criteriaQuery.getOrderList().isEmpty()) {
        orderList.addAll(criteriaQuery.getOrderList());
    }
    for (Order order : orders) {
        if (order.getDirection() == Direction.asc) {
            orderList.add(criteriaBuilder.asc(root.get(order.getProperty())));
        } else if (order.getDirection() == Direction.desc) {
            orderList.add(criteriaBuilder.desc(root.get(order.getProperty())));
        }
    }
    criteriaQuery.orderBy(orderList);
}

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

protected List<T> findList(CriteriaQuery<T> criteriaQuery, Integer first, Integer count, List<Filter> filters,
        List<Order> orders) {
    Assert.notNull(criteriaQuery);/*from   w  w  w . ja va  2  s.  c o  m*/
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Root<T> root = getRoot(criteriaQuery);
    addRestrictions(criteriaQuery, filters);
    addOrders(criteriaQuery, orders);
    if (criteriaQuery.getOrderList().isEmpty()) {
        if (OrderEntity.class.isAssignableFrom(entityClass)) {
            criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME)));
        } else {
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME)));
        }
    }
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (first != null) {
        query.setFirstResult(first);
    }
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

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

protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) {
    Assert.notNull(criteriaQuery);//from   ww  w  .  ja  v a 2  s. c  o  m
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    if (pageable == null) {
        pageable = new Pageable();
    }
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Root<T> root = getRoot(criteriaQuery);
    addRestrictions(criteriaQuery, pageable);
    addOrders(criteriaQuery, pageable);
    if (criteriaQuery.getOrderList().isEmpty()) {
        if (OrderEntity.class.isAssignableFrom(entityClass)) {
            criteriaQuery.orderBy(criteriaBuilder.asc(root.get(OrderEntity.ORDER_PROPERTY_NAME)));
        } else {
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get(OrderEntity.CREATE_DATE_PROPERTY_NAME)));
        }
    }
    long total = count(criteriaQuery, null);
    int totalPages = (int) Math.ceil((double) total / (double) pageable.getPageSize());
    if (totalPages < pageable.getPageNumber()) {
        pageable.setPageNumber(totalPages);
    }
    TypedQuery<T> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
    query.setMaxResults(pageable.getPageSize());
    return new Page<T>(query.getResultList(), total, pageable);
}

From source file:com.impetus.kundera.persistence.CriteriaQueryTranslator.java

/**
 * Method to translate criteriaQuery into JPQL.
 * /*from   w w  w .j  ava  2 s.  c om*/
 * @param criteriaQuery
 *            criteria query.
 * 
 * @return JPQL string.
 */
static <S> String translate(CriteriaQuery criteriaQuery) {
    QueryBuilder builder = new CriteriaQueryTranslator.QueryBuilder();

    // validate if criteria query is valid

    /**
     * select, from clause is mandatory
     * 
     * multiple from clause not support where clause is optional
     * 
     */

    Selection<S> select = criteriaQuery.getSelection();

    if (select != null) {
        builder.appendSelectClause();

    }

    if (select.getClass().isAssignableFrom(DefaultCompoundSelection.class)
            && ((CompoundSelection) select).isCompoundSelection()) {
        List<Selection<?>> selections = ((CompoundSelection) select).getCompoundSelectionItems();
        builder.appendMultiSelect(selections);
    } else if (select instanceof AggregateExpression) {
        builder.appendAggregate(((AggregateExpression) select).getAggregation());
    } else {
        String alias = select.getAlias();

        if (!StringUtils.isEmpty(alias)) {
            builder.appendAlias(alias);
        }

        Attribute attribute = ((DefaultPath) select).getAttribute();

        if (attribute != null) {
            builder.appendAttribute(attribute);
        }
    }
    Class<? extends S> clazzType = select.getJavaType();

    Set<Root<?>> roots = criteriaQuery.getRoots();

    Root<?> from = roots.iterator().next();

    Class entityClazz = from.getJavaType();

    builder.appendFromClause();

    // select.alias(paramString)
    builder.appendFrom(entityClazz);
    builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias());
    Predicate where = criteriaQuery.getRestriction(); // this could be null.
    if (where != null) {
        builder.appendWhereClause();
        List<Expression<Boolean>> expressions = where.getExpressions();
        for (Expression expr : expressions) {
            builder.appendWhere(expr, from.getAlias());
        }

    }

    List<Order> orderings = criteriaQuery.getOrderList();

    if (orderings != null) {
        if (!orderings.isEmpty()) {
            builder.appendOrderClause(where == null);
        }

        for (Order order : orderings) {
            builder.appendAlias(from.getAlias() != null ? from.getAlias() : select.getAlias());
            builder.appendOrdering(order);
        }
    }
    return builder.getQuery();

    // check that roots has to be one. multiple clause not yet supported

}

From source file:org.agric.oxm.utils.JpaUtils.java

/**
 * Copy Criteria without Selection// w  ww.  j a v  a2 s .c  o  m
 * 
 * @param from
 *            source Criteria
 * @param to
 *            destination Criteria
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {

    // Copy Roots
    for (Root<?> root : from.getRoots()) {
        Root<?> dest = to.from(root.getJavaType());
        dest.alias(getOrCreateAlias(root));
        copyJoins(root, dest);
    }

    to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());
    to.having(from.getGroupRestriction());
    to.where(from.getRestriction());
    to.orderBy(from.getOrderList());
}

From source file:org.jboss.pressgang.ccms.filter.utils.JPAUtils.java

/**
 * Copy Criteria without Selection/*w w  w.ja va 2  s .c  om*/
 *
 * @param from source Criteria
 * @param to   destination Criteria
 */
public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) {

    // Copy Roots
    for (Root<?> root : from.getRoots()) {
        Root<?> dest = to.from(root.getJavaType());
        dest.alias(getOrCreateAlias(root));
        copyJoins(root, dest);
    }

    if (from.getGroupList() != null)
        to.groupBy(from.getGroupList());
    to.distinct(from.isDistinct());
    if (from.getGroupRestriction() != null)
        to.having(from.getGroupRestriction());
    if (from.getRestriction() != null)
        to.where(from.getRestriction());
    if (from.getOrderList() != null)
        to.orderBy(from.getOrderList());
}

From source file:ru.savvy.jpafilterbuilder.FilterCriteriaBuilder.java

/**
 * Resulting query with filters and orders, if orders are empty, than makes
 * default ascending ordering by root id to prevent paging confuses
 *
 * @return/*from  w  w w.  jav  a 2 s. c o  m*/
 */
public CriteriaQuery<T> getQuery() {
    CriteriaQuery<T> query = cb.createQuery(clazz);
    Root<T> root = query.from(clazz);
    applyFilters(root, query);
    applyOrders(root, query);

    // add default ordering
    if (query.getOrderList() == null || query.getOrderList().isEmpty()) {
        EntityType<T> entityType = root.getModel();
        try {
            Field sortField = getSortAnnotation(entityType.getBindableJavaType());
            if (sortField == null)
                query.orderBy(
                        cb.asc(root.get(entityType.getId(entityType.getIdType().getJavaType()).getName())));
            else {
                DefaultOrder order = sortField.getAnnotation(DefaultOrder.class);
                if (order.asc()) {
                    query.orderBy(cb.asc(root.get(sortField.getName())));
                } else {
                    query.orderBy(cb.desc(root.get(sortField.getName())));
                }

            }
        } catch (Exception ex) {
            logger.warn("In" + this.getClass().getName(), ex);
        }
    }
    return query;
}