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);
   }
 }
 @Override
 public void buildOrdered(Criteria criteria) {
   buildUnordered(criteria);
   SortParam<String> sort = ((SingleSortState<String>) getSortState()).getSort();
   if (sort != null) {
     if (sort.isAscending()) criteria.addOrder(Order.asc(sort.getProperty()).ignoreCase());
     else criteria.addOrder(Order.desc(sort.getProperty()).ignoreCase());
   }
 }
  @Override
  protected List<Order> getOrder(SortParam sort) {
    if (sort.getProperty() == null || !sort.getProperty().equals("priority")) {
      return super.getOrder(sort);
    }

    if (sort.isAscending()) {
      return Arrays.asList(Order.asc("priority"), Order.asc("type"));
    } else {
      return Arrays.asList(Order.desc("priority"), Order.desc("type"));
    }
  }
 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);
   }
 }
Example #5
0
 public static List selectEntries(
     List list,
     int first,
     int count,
     final org.apache.wicket.extensions.markup.html.repeater.util.SortParam sortParam) {
   List sortedEntries = new ArrayList(list);
   if (sortParam != null) {
     if (sortParam.getProperty().equals("user_id")) {
       Collections.sort(
           sortedEntries,
           new Comparator() {
             public int compare(Object arg0, Object arg1) {
               User entry1 = (User) arg0;
               User entry2 = (User) arg1;
               int result =
                   ((Integer) entry1.getUser_id()).compareTo((Integer) entry2.getUser_id());
               return sortParam.isAscending() ? result : -result;
             }
           });
     }
     if (sortParam.getProperty().equals("name")) {
       Collections.sort(
           sortedEntries,
           new Comparator() {
             public int compare(Object arg0, Object arg1) {
               User entry1 = (User) arg0;
               User entry2 = (User) arg1;
               int result = entry1.getLname().compareTo(entry2.getLname());
               return sortParam.isAscending() ? result : -result;
             }
           });
     }
     if (sortParam.getProperty().equals("acct_status")) {
       Collections.sort(
           sortedEntries,
           new Comparator() {
             public int compare(Object arg0, Object arg1) {
               User entry1 = (User) arg0;
               User entry2 = (User) arg1;
               int result = entry1.getAcct_status().compareTo(entry2.getAcct_status());
               return sortParam.isAscending() ? result : -result;
             }
           });
     }
   }
   return sortedEntries.subList(first, first + count);
   // return list.subList(first, first + count);
 }
  @Override
  public Iterator<? extends T> iterator(long first, long count) {
    Collection<T> data = dataModel.getObject();
    if (data == null || data.size() == 0) return Iterators.emptyIterator();
    Iterator<T> it;
    final SortParam<S> sortParam = getSort();
    if (sortParam != null && sortParam.getProperty() != null) {
      Ordering<T> ordering =
          Ordering.natural()
              .nullsFirst()
              .onResultOf(
                  new Function<T, Comparable<?>>() {

                    @Override
                    public Comparable<?> apply(T input) {
                      return comparableValue(input, sortParam.getProperty());
                    }
                  });
      if (!sortParam.isAscending()) ordering = ordering.reverse();
      it = ordering.sortedCopy(data).iterator();
    } else {
      it = data.iterator();
    }
    if (first > 0) Iterators.advance(it, (int) first);
    return count >= 0 ? Iterators.limit(it, (int) count) : it;
  }
 /**
  * Sorts the given list by getting the {@link SortParam#getProperty()} and if not null the given
  * list will be sort.
  *
  * @param unsortedList the unsorted list
  * @return the same list but sorted.
  */
 protected List<T> sort(final List<T> unsortedList) {
   final SortParam<S> sortParam = getSort();
   if (sortParam != null) {
     final String property = (String) sortParam.getProperty();
     final boolean ascending = sortParam.isAscending();
     ListExtensions.sortByProperty(unsortedList, property, ascending);
   }
   return unsortedList;
 }
 @Override
 public Iterator<InvoiceConfiguration> iterator(int first, int count) {
   SortParam sp = getSort();
   return find(first, count, sp.getProperty(), sp.isAscending()).iterator();
 }
 @Override
 public synchronized Iterator<T> iterator(int first, int count) {
   SortParam sp = getSort();
   Collections.sort(filteredList, new ConfDataComparator<T>(sp.getProperty(), sp.isAscending()));
   return new ListIterator<T>(filteredList, first, count);
 }