@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;
  }
 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);
   }
 }
 /**
  * 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;
 }
Ejemplo n.º 4
0
 @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());
   }
 }
Ejemplo n.º 5
0
  @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"));
    }
  }
Ejemplo n.º 6
0
  /**
   * Returns the requested page of layer objects after applying any keyword filtering set on the
   * page
   */
  private Iterator<LayerInfo> filteredItems(Integer first, Integer count) {
    final Catalog catalog = getCatalog();

    // global sorting
    final SortParam sort = getSort();
    final Property<LayerInfo> property = getProperty(sort);

    SortBy sortOrder = null;
    if (sort != null) {
      if (property instanceof BeanProperty) {
        final String sortProperty = ((BeanProperty<LayerInfo>) property).getPropertyPath();
        sortOrder = sortBy(sortProperty, sort.isAscending());
      } else if (property == ENABLED) {
        sortOrder = sortBy("enabled", sort.isAscending());
      }
    }

    final Filter filter = getFilter();
    // our already filtered and closeable iterator
    Iterator<LayerInfo> items = catalog.list(LayerInfo.class, filter, first, count, sortOrder);

    return items;
  }
Ejemplo n.º 7
0
 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);
   }
 }
 @Override
 public Iterator<InvoiceConfiguration> iterator(int first, int count) {
   SortParam sp = getSort();
   return find(first, count, sp.getProperty(), sp.isAscending()).iterator();
 }
Ejemplo n.º 9
0
 @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);
 }