List of usage examples for javax.persistence.criteria CriteriaBuilder asc
Order asc(Expression<?> x);
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 ww w . j a v a 2 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:net.groupbuy.dao.impl.BaseDaoImpl.java
protected Page<T> findPage(CriteriaQuery<T> criteriaQuery, Pageable pageable) { Assert.notNull(criteriaQuery);/*ww w . j av a 2s . 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: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 www.j av a 2 s .co m 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:com.qpark.eip.core.model.analysis.AnalysisDao.java
/** * Get the list of serviceIds available. * * @param modelVersion//w w w. j a v a 2s . c o m * the model version. * @return the list of serviceIds available. * @since 3.5.1 */ @Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED) public List<String> getServiceIds(final String modelVersion) { final List<String> value = new ArrayList<String>(); final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<ServiceType> q = cb.createQuery(ServiceType.class); final Root<ServiceType> f = q.from(ServiceType.class); q.where(cb.equal(f.<String>get(ServiceType_.modelVersion), modelVersion)); q.orderBy(cb.asc(f.<String>get(ServiceType_.serviceId)), cb.asc(f.<String>get(ServiceType_.namespace))); final TypedQuery<ServiceType> typedQuery = this.em.createQuery(q); final List<ServiceType> services = typedQuery.getResultList(); services.stream().filter(s -> Objects.nonNull(s.getServiceId())).forEach(s -> value.add(s.getServiceId())); return value; }
From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java
/** * Get the list of target name spaces available. * * @param modelVersion/* w w w. java 2 s . c o m*/ * the model version. * @return the list of target name spaces available. * @since 3.5.1 */ @Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED) public List<String> getTargetNamespaces(final String modelVersion) { final List<String> value = new ArrayList<String>(); final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<ClusterType> q = cb.createQuery(ClusterType.class); final Root<ClusterType> f = q.from(ClusterType.class); q.where(cb.equal(f.<String>get(ClusterType_.modelVersion), modelVersion)); q.orderBy(cb.asc(f.<String>get(ClusterType_.name)), cb.asc(f.<String>get(ClusterType_.id))); final TypedQuery<ClusterType> typedQuery = this.em.createQuery(q); final List<ClusterType> cluster = typedQuery.getResultList(); cluster.stream().filter(c -> Objects.nonNull(c.getName())).forEach(c -> value.add(c.getName())); return value; }
From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java
private Query applicationServersForSuggestBox(String input) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<String> q = cb.createQuery(String.class); Root<ResourceEntity> r = q.from(ResourceEntity.class); Join<ResourceEntity, ResourceTypeEntity> resType = r.join("resourceType"); Predicate appServerNamePred = cb.like(resType.<String>get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()); input = input + "%"; q.where(cb.and(appServerNamePred, cb.like(r.<String>get("name"), input))); q.select(r.<String>get("name")); q.distinct(true);//from ww w .j a v a2s.c o m q.orderBy(cb.asc(r.get("name"))); return entityManager.createQuery(q); }
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;/* ww w. j a va 2s . com*/ } 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:br.nom.abdon.gastoso.dal.FatosDao.java
private void trataOrdenacao(final FiltroFatos filtro, final Root<Fato> root, final CriteriaBuilder cb, final CriteriaQuery<Fato> q) { final List<FiltroFatos.ORDEM> ordem = filtro.getOrdem(); if (ordem != null && !ordem.isEmpty()) { List<Order> orders = new LinkedList<>(); for (FiltroFatos.ORDEM itemOrdenacao : ordem) { final Path path; switch (itemOrdenacao) { case POR_CRIACAO: path = root.get("id"); break; case POR_DESCRICAO: path = root.get("descricao"); break; case POR_DIA: default: path = root.get("dia"); break; }/* w w w. j a va2 s . co m*/ orders.add(cb.asc(path)); } q.orderBy(orders); } }
From source file:org.jdal.dao.jpa.JpaDao.java
/** * Get JPA Order from a page order for a CriteriaQuery * @param page request page/* w ww. ja va 2s. c o m*/ * @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:edu.umm.radonc.ca_dash.model.TxInstanceFacade.java
public List<TxInstance> itemsDateRange(Date startDate, Date endDate, int[] range) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(TxInstance.class); Root<TxInstance> rt = cq.from(TxInstance.class); cq.where(cb.and(rt.get(TxInstance_.completed).isNotNull(), cb.between(rt.get(TxInstance_.completed), startDate, endDate))); cq.orderBy(cb.asc(rt.get(TxInstance_.completed))); Query q = em.createQuery(cq); q.setMaxResults(range[1] - range[0] + 1); q.setFirstResult(range[0]);/*from ww w .j av a 2 s.c o m*/ return q.getResultList(); }