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);
}
}
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;
}
};
}
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);
}
}
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();
}
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();
}
Aggregations