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:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java

protected void attachOrderBy(ProductSearchCriteria searchCriteria, From<?, ? extends Product> product,
        Path<? extends Sku> sku, CriteriaQuery<?> criteria) {
    if (StringUtils.isNotBlank(searchCriteria.getSortQuery())) {
        CriteriaBuilder builder = em.getCriteriaBuilder();

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

        String sortQueries = searchCriteria.getSortQuery();
        for (String sortQuery : sortQueries.split(",")) {
            String[] sort = sortQuery.split(" ");
            if (sort.length == 2) {
                String key = sort[0];
                boolean asc = sort[1].toLowerCase().contains("asc");

                // Determine whether we should use the product path or the sku path
                Path<?> pathToUse;
                if (key.contains("defaultSku.")) {
                    pathToUse = sku;//from www.j  a  va  2  s. co m
                    key = key.substring("defaultSku.".length());
                } else if (key.contains("product.")) {
                    pathToUse = product;
                    key = key.substring("product.".length());
                } else {
                    // We don't know which path this facet is built on - resolves previous bug that attempted
                    // to attach search facet to any query parameter
                    continue;
                }

                if (asc) {
                    sorts.add(builder.asc(pathToUse.get(key)));
                } else {
                    sorts.add(builder.desc(pathToUse.get(key)));
                }
            }
        }

        criteria.orderBy(sorts.toArray(new Order[sorts.size()]));
    }
}

From source file:ru.codeinside.adm.AdminServiceImpl.java

@Override
public List<InfoSystem> queryInfoSystems(boolean source, String[] sort, boolean[] asc, int start, int count) {
    final CriteriaBuilder c = em.getCriteriaBuilder();
    final CriteriaQuery<InfoSystem> query = c.createQuery(InfoSystem.class);
    final Root<InfoSystem> system = query.from(InfoSystem.class);
    if (source) {
        query.where(c.equal(system.get(InfoSystem_.source), true));
    }/*from  w w  w.  ja v  a  2  s.  c o  m*/
    query.select(system);
    if (sort != null) {
        final Order[] orders = new Order[sort.length];
        for (int i = 0; i < sort.length; i++) {
            final Path<String> path = system.get(sort[i]);
            orders[i] = asc[i] ? c.asc(path) : c.desc(path);
        }
        query.orderBy(orders);
    }
    return chunk(start, count, query);
}