Example usage for javax.persistence.criteria CriteriaBuilder desc

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

Introduction

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

Prototype

Order desc(Expression<?> x);

Source Link

Document

Create an ordering by the descending value of the expression.

Usage

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 .j  a  v  a2s  .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  w  ww . j  a  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:eu.uqasar.service.dataadapter.CubesDataService.java

/**
 * /* w  w w .j  a v a2 s . co m*/
 * @param metric
 * @param date
 * @return
 * @throws uQasarException
 */
public int countMeasurementsByMetricAndDate(String metric, Date date) throws uQasarException {
    logger.info("Count measurements for metric: " + metric + "and date: " + date);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList().size();
}

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

/**
 * /* w w  w  .  j  a v  a  2 s  .c  o  m*/
 * @param metric
 * @return
 * @throws uQasarException
 */
private List<CubesMetricMeasurement> getMeasurementsByMetricAndDate(String metric, Date date)
        throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

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

/**
 * @param project//  w  w  w . j  a va2  s.  c o  m
 * @param metric
 * @return
 */
public CubesMetricMeasurement getLatestMeasurementByProjectAndMetric(String project, String metric) {
    logger.info("Obtaining measurements for the project: " + project + " and metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<CubesMetricMeasurement> query = cb.createQuery(CubesMetricMeasurement.class);
    Root<CubesMetricMeasurement> root = query.from(CubesMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(CubesMetricMeasurement_.cubeName), project);
    Predicate condition2 = cb.equal(root.get(CubesMetricMeasurement_.cubesMetric), metric);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    List<CubesMetricMeasurement> measurements = em.createQuery(query).getResultList();
    CubesMetricMeasurement measurement = null;
    if (measurements != null && measurements.size() > 0 && measurements.get(0) != null) {
        measurement = measurements.get(0); // Get the most "fresh" result 
    }
    return measurement;
}

From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java

protected int applySlicing(CriteriaQuery<E> criteriaQuery, CriteriaBuilder criteriaBuilder,
        Path<? extends Number> taskIdPath, Integer first, Integer last) {
    // apply slicing
    int maxResults = -1;

    // first is set
    if (first != null) {
        if (first < 0) {
            throw new IllegalArgumentException("Argument 'first' must be equal or greater than 0");
        }//from www .  ja  v a2 s .  c  o m

        criteriaQuery.orderBy(criteriaBuilder.asc(taskIdPath));
        maxResults = first;
    }

    // last is set
    if (last != null) {
        if (last < 0) {
            throw new IllegalArgumentException("Argument 'last' must be equal or greater than 0");
        }

        criteriaQuery.orderBy(criteriaBuilder.desc(taskIdPath));
        maxResults = last;
    }

    if (first == null && last == null) {
        criteriaQuery.orderBy(criteriaBuilder.asc(taskIdPath));
    }
    return maxResults;
}

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   www. jav 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());
    }
    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:com.ims.service.OrderService.java

public List<PurchaseOrder> searchPurchaseOrderList(final OrderSearchCriteria orderSearchCriteria) {
    List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();
    Specification<PurchaseOrder> speci = new Specification<PurchaseOrder>() {
        @Override/*from   ww w  .j  a  va 2s.  c om*/
        public Predicate toPredicate(Root<PurchaseOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<Predicate>();
            if (StringUtils.isNotEmpty(orderSearchCriteria.getPurchaseOrderStatus())) {
                predicates.add(cb.equal(root.get("status"), orderSearchCriteria.getPurchaseOrderStatus()));
            }

            if (StringUtils.isNotEmpty(orderSearchCriteria.getPurchaseOrderNum())) {
                predicates.add(cb.like(root.<String>get("purchaseOrderNumber"),
                        "%" + orderSearchCriteria.getPurchaseOrderNum() + "%"));
            }

            if (StringUtils.isNotEmpty(orderSearchCriteria.getProformaInvoiceNum())) {
                predicates.add(cb.like(root.<String>get("piNumber"),
                        "%" + orderSearchCriteria.getProformaInvoiceNum() + "%"));
            }

            query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])))
                    .orderBy(cb.desc(root.get("changeLog").get("createdDate")));
            return null;
        }
    };

    return purchaseOrderRepository.findAll(speci);
}

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

/**
 * /*from ww w  . jav a 2  s.c  o  m*/
 * @param metric
 * @return
 * @throws uQasarException
 */
public int countMeasurementsPerProjectByMetricWithinPeriod(Project project, String metric, String period)
        throws uQasarException {
    logger.info("Count measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition1 = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.project), project);

    Date from = getDateForPeriod(period);
    Date to = DateTime.now().toDate();
    Predicate condition3 = cb.between(root.get(JiraMetricMeasurement_.timeStamp), from, to);
    Predicate condition4 = cb.and(condition1, condition2, condition3);
    query.where(condition4);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    Integer res = em.createQuery(query).getResultList().size();
    logger.info("Results' count: " + res);
    return res;
}

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  av a 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());
    }
    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);
}