public void build(Criteria criteria) { SortParam sort = sortState.getSort(); String property; if (sort != null && sort.getProperty() != null) { property = sort.getProperty(); asc = sort.isAscending(); } else { property = defaultProperty; } if (property != null) { if (property.contains(".")) { // for 'dot' properties we need to add aliases // e.g. for the property 'orderbook.order.item.name' we need to add an aliases for 'order' // and 'order.item' String path[] = property.split("\\."); for (int ii = 0; ii < path.length - 1; ii++) { StringBuffer sb = new StringBuffer(); for (int jj = 0; jj <= ii; jj++) { if (sb.length() > 0) sb.append("."); sb.append(path[jj]); } criteria.createAlias(sb.toString(), path[ii], CriteriaSpecification.LEFT_JOIN); } // when we have a 'dot' property we want to sort by the sub tables field // e.g. for the property 'orderbook.order.item.name' we need to sort by 'item.name' if (path.length > 1) property = String.format("%s.%s", path[path.length - 2], path[path.length - 1]); else property = path[path.length - 1]; } Order order = asc ? Order.asc(property) : Order.desc(property); order = cased ? order : order.ignoreCase(); criteria.addOrder(order); } }
public void build(final List<Predicate> criteria) { final SortParam sort = sortState.getSort(); String property; if (sort != null && sort.getProperty() != null) { property = sort.getProperty(); asc = sort.isAscending(); } else { property = defaultProperty; } if (property != null) { if (property.contains(".")) { // for 'dot' properties we need to add aliases // e.g. for the property 'orderbook.order.item.name' we need to add an // aliases for 'order' and 'order.item' final String path[] = property.split("\\."); for (int ii = 0; ii < path.length - 1; ii++) { final StringBuffer sb = new StringBuffer(); for (int jj = 0; jj <= ii; jj++) { if (sb.length() > 0) { sb.append("."); } sb.append(path[jj]); } // TODO // criteria.createAlias(sb.toString(), path[ii], // CriteriaSpecification.LEFT_JOIN); } // when we have a 'dot' property we want to sort by the sub tables field // e.g. for the property 'orderbook.order.item.name' we need to sort by // 'item.name' if (path.length > 1) { property = String.format("%s.%s", path[path.length - 2], path[path.length - 1]); } else { property = path[path.length - 1]; } } final EntityManager em = Databinder.getEntityManager(); final CriteriaBuilder cb = em.getCriteriaBuilder(); final CriteriaQuery<T> cq = cb.createQuery(entityClass); final Root<T> root = cq.from(entityClass); cq.orderBy(cb.asc(root.get(property))); final javax.persistence.criteria.Order order = asc ? cb.asc(root.get(property)) : cb.desc(root.get(property)); // TODO order = cased ? order : order.ignoreCase(); // criteria.addOrder(order); cq.orderBy(order); } }
/** * Returns current sort state. * * @return current sort state */ public SortParam<S> getSort() { return sortState.getSort(); }