Search in sources :

Example 1 with SortConstraint

use of org.apache.tapestry5.grid.SortConstraint in project tapestry-5 by apache.

the class CollectionGridDataSource method prepare.

public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {
    for (SortConstraint constraint : sortConstraints) {
        final ColumnSort sort = constraint.getColumnSort();
        if (sort == ColumnSort.UNSORTED)
            continue;
        final PropertyConduit conduit = constraint.getPropertyModel().getConduit();
        final Comparator valueComparator = new Comparator<Comparable>() {

            public int compare(Comparable o1, Comparable o2) {
                if (o1 == o2)
                    return 0;
                if (o2 == null)
                    return 1;
                if (o1 == null)
                    return -1;
                return o1.compareTo(o2);
            }
        };
        final Comparator rowComparator = new Comparator() {

            public int compare(Object row1, Object row2) {
                Comparable value1 = (Comparable) conduit.get(row1);
                Comparable value2 = (Comparable) conduit.get(row2);
                return valueComparator.compare(value1, value2);
            }
        };
        final Comparator reverseComparator = new Comparator() {

            public int compare(Object o1, Object o2) {
                int modifier = sort == ColumnSort.ASCENDING ? 1 : -1;
                return modifier * rowComparator.compare(o1, o2);
            }
        };
        // We can freely sort this list because its just a copy.
        Collections.sort(list, reverseComparator);
    }
}
Also used : SortConstraint(org.apache.tapestry5.grid.SortConstraint) ColumnSort(org.apache.tapestry5.grid.ColumnSort) PropertyConduit(org.apache.tapestry5.beanmodel.PropertyConduit) SortConstraint(org.apache.tapestry5.grid.SortConstraint) Comparator(java.util.Comparator)

Example 2 with SortConstraint

use of org.apache.tapestry5.grid.SortConstraint in project tapestry-5 by apache.

the class ShortGrid method getData.

public GridDataSource getData() {
    return new GridDataSource() {

        public int getAvailableRows() {
            return 50;
        }

        public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {
        }

        public Object getRowValue(int index) {
            if (index > 5)
                return null;
            ToDoItem item = new ToDoItem();
            item.setTitle("Index #" + index);
            return item;
        }

        public Class getRowType() {
            return ToDoItem.class;
        }
    };
}
Also used : GridDataSource(org.apache.tapestry5.grid.GridDataSource) ToDoItem(org.apache.tapestry5.integration.app1.data.ToDoItem) List(java.util.List)

Example 3 with SortConstraint

use of org.apache.tapestry5.grid.SortConstraint in project tapestry-5 by apache.

the class CollectionGridDataSourceTest method sort.

private void sort(String propertyName, boolean ascending, int... ids) {
    PropertyModel propertyModel = model.get(propertyName);
    int availableRows = source.getAvailableRows();
    SortConstraint constraint = new SortConstraint(propertyModel, ascending ? ColumnSort.ASCENDING : ColumnSort.DESCENDING);
    List<SortConstraint> constraints = Collections.singletonList(constraint);
    source.prepare(0, availableRows - 1, constraints);
    for (int i = 0; i < ids.length; i++) {
        Datum row = (Datum) source.getRowValue(i);
        assertEquals(row.getId(), ids[i], "Id for Datum #" + i);
    }
}
Also used : SortConstraint(org.apache.tapestry5.grid.SortConstraint) PropertyModel(org.apache.tapestry5.beanmodel.PropertyModel) SortConstraint(org.apache.tapestry5.grid.SortConstraint)

Example 4 with SortConstraint

use of org.apache.tapestry5.grid.SortConstraint in project tapestry-5 by apache.

the class HibernateGridDataSource method prepare.

/**
 * Prepares the results, performing a query (applying the sort results, and the provided start and end index). The
 * results can later be obtained from {@link #getRowValue(int)} }.
 *
 * @param startIndex      index, from zero, of the first item to be retrieved
 * @param endIndex        index, from zero, of the last item to be retrieved
 * @param sortConstraints zero or more constraints used to set the order of the returned values
 */
@Override
public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {
    assert sortConstraints != null;
    Criteria crit = session.createCriteria(entityType);
    crit.setFirstResult(startIndex).setMaxResults(endIndex - startIndex + 1);
    for (SortConstraint constraint : sortConstraints) {
        String propertyName = constraint.getPropertyModel().getPropertyName();
        switch(constraint.getColumnSort()) {
            case ASCENDING:
                crit.addOrder(Order.asc(propertyName));
                break;
            case DESCENDING:
                crit.addOrder(Order.desc(propertyName));
                break;
            default:
        }
    }
    applyAdditionalConstraints(crit);
    this.startIndex = startIndex;
    preparedResults = crit.list();
}
Also used : SortConstraint(org.apache.tapestry5.grid.SortConstraint) Criteria(org.hibernate.Criteria)

Example 5 with SortConstraint

use of org.apache.tapestry5.grid.SortConstraint in project tapestry-5 by apache.

the class JpaGridDataSource method prepare.

/**
 * {@inheritDoc}
 */
@Override
public void prepare(final int startIndex, final int endIndex, final List<SortConstraint> sortConstraints) {
    final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<E> criteria = builder.createQuery(entityType);
    final Root<E> root = criteria.from(entityType);
    applyAdditionalConstraints(criteria.select(root), root, builder);
    for (final SortConstraint constraint : sortConstraints) {
        final String propertyName = constraint.getPropertyModel().getPropertyName();
        final Path<Object> propertyPath = root.get(propertyName);
        switch(constraint.getColumnSort()) {
            case ASCENDING:
                criteria.orderBy(builder.asc(propertyPath));
                break;
            case DESCENDING:
                criteria.orderBy(builder.desc(propertyPath));
                break;
            default:
        }
    }
    final TypedQuery<E> query = entityManager.createQuery(criteria);
    query.setFirstResult(startIndex);
    query.setMaxResults(endIndex - startIndex + 1);
    this.startIndex = startIndex;
    preparedResults = query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SortConstraint(org.apache.tapestry5.grid.SortConstraint)

Aggregations

SortConstraint (org.apache.tapestry5.grid.SortConstraint)4 Comparator (java.util.Comparator)1 List (java.util.List)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 PropertyConduit (org.apache.tapestry5.beanmodel.PropertyConduit)1 PropertyModel (org.apache.tapestry5.beanmodel.PropertyModel)1 ColumnSort (org.apache.tapestry5.grid.ColumnSort)1 GridDataSource (org.apache.tapestry5.grid.GridDataSource)1 ToDoItem (org.apache.tapestry5.integration.app1.data.ToDoItem)1 Criteria (org.hibernate.Criteria)1