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:gov.gtas.repository.CaseDispositionRepositoryImpl.java

@Override
public Pair<Long, List<Case>> findByCriteria(CaseRequestDto dto) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Case> q = cb.createQuery(Case.class);
    Root<Case> root = q.from(Case.class);
    List<Predicate> predicates = new ArrayList<>();

    TypedQuery<Case> typedQuery = em.createQuery(q);

    // sorting//from   www  .j a  va2  s .c  o m
    if (dto.getSort() != null) {
        List<Order> orders = new ArrayList<>();
        for (SortOptionsDto sort : dto.getSort()) {
            Expression<?> e = root.get(sort.getColumn());
            Order order;
            if ("desc".equalsIgnoreCase(sort.getDir())) {
                order = cb.desc(e);
            } else {
                order = cb.asc(e);
            }
            orders.add(order);
        }
        q.orderBy(orders);
    }

    if (dto.getFlightId() != null) {
        predicates.add(cb.equal(root.<Long>get("flightId"), dto.getFlightId()));
    }

    if (dto.getPaxId() != null) {
        predicates.add(cb.equal(root.<Long>get("paxId"), dto.getPaxId()));
    }

    if (dto.getPaxName() != null) {
        String likeString = String.format("%%%s%%", dto.getPaxName().toUpperCase());
        predicates.add(cb.like(root.<String>get("paxName"), likeString));
    }

    if (dto.getLastName() != null) { // map this to full pax name
        String likeString = String.format("%%%s%%", dto.getLastName().toUpperCase());
        predicates.add(cb.like(root.<String>get("paxName"), likeString));
    }

    if (dto.getStatus() != null) {
        String likeString = String.format("%%%s%%", dto.getStatus().toUpperCase());
        predicates.add(cb.like(root.<String>get("status"), likeString));
    }

    if (dto.getFlightNumber() != null) {
        predicates.add(cb.equal(root.<Long>get("flightNumber"), dto.getFlightNumber()));
    }

    if (dto.getRuleCatId() != null) {
        predicates.add(cb.equal(root.<Long>get("highPriorityRuleCatId"), dto.getRuleCatId()));
    }

    Predicate etaCondition;
    if (dto.getEtaStart() != null && dto.getEtaEnd() != null) {

        Path<Date> eta = root.<Date>get("flightETADate");
        Predicate startPredicate = cb.or(cb.isNull(eta), cb.greaterThanOrEqualTo(eta, dto.getEtaStart()));
        Predicate endPredicate = cb.or(cb.isNull(eta), cb.lessThanOrEqualTo(eta, dto.getEtaEnd()));
        etaCondition = cb.and(startPredicate, endPredicate);
        predicates.add(etaCondition);
    }

    q.select(root).where(predicates.toArray(new Predicate[] {}));
    typedQuery = em.createQuery(q);

    // total count
    CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
    countQuery.select(cb.count(countQuery.from(Case.class))).where(predicates.toArray(new Predicate[] {}));
    Long count = em.createQuery(countQuery).getSingleResult();

    // pagination
    int pageNumber = dto.getPageNumber();
    int pageSize = dto.getPageSize();
    int firstResultIndex = (pageNumber - 1) * pageSize;
    typedQuery.setFirstResult(firstResultIndex);
    typedQuery.setMaxResults(dto.getPageSize());

    logger.debug(typedQuery.unwrap(org.hibernate.Query.class).getQueryString());
    List<Case> results = typedQuery.getResultList();

    return new ImmutablePair<>(count, results);

}

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

/**
 * /* w  ww  . ja v  a  2  s  .c om*/
 * @param metric
 * @return
 * @throws uQasarException
 */
public List<JiraMetricMeasurement> getMeasurementsForMetric(String metric) throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    query.where(condition);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

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

/**
 * @param project/*from  ww  w . ja v a 2  s  .  com*/
 * @return List with all the measurements of a project
 */
public List<CubesMetricMeasurement> getMeasurementByProject(Project project) {
    logger.info("Obtaining measurements for the project: " + project);

    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_.project), project);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();

}

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

/**
 * /*from   ww w  . ja  v a  2s .  c  om*/
 * @param metric
 * @return
 * @throws uQasarException
 */
public List<CubesMetricMeasurement> getMeasurementsForMetric(String metric) 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);
    query.where(condition);
    query.orderBy(cb.desc(root.get(CubesMetricMeasurement_.timeStamp)));
    return em.createQuery(query).getResultList();
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the last model version (lexical compare).
 *
 * @return the last model version./*from   w  ww  .  j  ava2 s  .c om*/
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public String getLastModelVersion() {
    String value = null;
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<String> q = cb.createQuery(String.class);
    final Root<EnterpriseType> f = q.from(EnterpriseType.class);
    q.select(f.<String>get(EnterpriseType_.modelVersion));
    q.orderBy(cb.desc(f.<String>get(EnterpriseType_.modelVersion)));
    final TypedQuery<String> typedQuery = this.em.createQuery(q);
    final List<String> list = typedQuery.getResultList();
    if (Objects.nonNull(list) && list.size() > 0) {
        value = list.get(0);
    }
    return value;
}

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

/**
 * Get JPA Order from a page order for a CriteriaQuery
 * @param page request page//from   ww w.ja  v  a2s. com
 * @param criteria CriteriaQuery to apply Order on.
 * @return new Order
 */
private Order getOrder(Page<?> page, CriteriaQuery<?> criteria) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    Root<T> root = JpaUtils.findRoot(criteria, getEntityClass());
    String propertyPath = page.getSortName();

    if (log.isDebugEnabled())
        log.debug("Setting order as: " + propertyPath);

    Path<?> path = JpaUtils.getPath(root, propertyPath);

    return page.getOrder() == Page.Order.ASC ? cb.asc(path) : cb.desc(path);
}

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

/**
 * /*from  www.jav a2  s.co  m*/
 * @param metric
 * @return
 * @throws uQasarException
 */
private List<JiraMetricMeasurement> getMeasurementsByMetricAndDate(String metric, Date date)
        throws uQasarException {
    logger.info("Get measurements for metric: " + metric);

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

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

/**
 * /*from  w w  w  .  jav  a  2 s  .c  om*/
 * @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<JiraMetricMeasurement> query = cb.createQuery(JiraMetricMeasurement.class);
    Root<JiraMetricMeasurement> root = query.from(JiraMetricMeasurement.class);
    Predicate condition = cb.equal(root.get(JiraMetricMeasurement_.jiraMetric), metric);
    Predicate condition2 = cb.equal(root.get(JiraMetricMeasurement_.timeStamp), date);
    Predicate condition3 = cb.and(condition, condition2);
    query.where(condition3);
    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:org.businessmanager.dao.GenericDaoImpl.java

@Override
public List<T> findAll(SingularAttribute<T, ?> orderAttribute, boolean orderAsc, int firstResult,
        int maxResults, Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);/*from   ww  w .  jav a2s .c  om*/
    select.where(predicateList.toArray(new Predicate[0]));

    if (orderAsc) {
        criteriaQuery.orderBy(queryBuilder.asc(rootQuery.get(orderAttribute)));
    } else {
        criteriaQuery.orderBy(queryBuilder.desc(rootQuery.get(orderAttribute)));
    }

    TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);

    if (firstResult != -1) {
        typedQuery.setFirstResult(firstResult);
    }
    if (maxResults != -1) {
        typedQuery.setMaxResults(maxResults);
    }

    return typedQuery.getResultList();
}

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

/** 
 * @param metric//w  w  w .  j  ava2s  .c om
 * @param project
 * @return
 * @throws uQasarException
 */
public List<JiraMetricMeasurement> getMeasurementsPerProjectByMetricWithLatestDate(Project project,
        String metric) throws uQasarException {
    logger.info("Count measurements for project + " + project.getAbbreviatedName() + " and metric: " + metric
            + "with latest date");

    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);

    Predicate condition4 = cb.and(condition1, condition2);
    query.where(condition4);
    query.orderBy(cb.desc(root.get(JiraMetricMeasurement_.timeStamp)));
    List<JiraMetricMeasurement> returnResults = em.createQuery(query).getResultList();

    Date newDate = null;
    for (JiraMetricMeasurement jMM : returnResults) {
        if (newDate == null || jMM.getTimeStamp().compareTo(newDate) > 0) {
            newDate = jMM.getTimeStamp();
        }

    }

    return getMeasurementsByMetricAndDate(metric, newDate);
}