| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package br.com.arsmachina.tapestrycrud.grid; |
| 16 | |
|
| 17 | |
import java.util.ArrayList; |
| 18 | |
import java.util.List; |
| 19 | |
|
| 20 | |
import org.apache.tapestry5.grid.ColumnSort; |
| 21 | |
import org.apache.tapestry5.grid.GridDataSource; |
| 22 | |
import org.apache.tapestry5.grid.SortConstraint; |
| 23 | |
|
| 24 | |
import br.com.arsmachina.controller.Controller; |
| 25 | |
import br.com.arsmachina.dao.SortCriterion; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class PagedSearchGridDataSource<T> implements GridDataSource { |
| 35 | |
|
| 36 | 0 | private static final SortCriterion[] EMPTY_SORT_CRITERION_ARRAY = new SortCriterion[0]; |
| 37 | |
|
| 38 | |
final private Class<T> entityClass; |
| 39 | |
|
| 40 | |
final private PagedSearch<T> pagedSearch; |
| 41 | |
|
| 42 | |
private List<T> list; |
| 43 | |
|
| 44 | |
private int firstIndex; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
@SuppressWarnings("unchecked") |
| 53 | 0 | public PagedSearchGridDataSource(Class<T> clasz, PagedSearch<T> pagedSearch) { |
| 54 | |
|
| 55 | 0 | if (pagedSearch == null) { |
| 56 | 0 | throw new IllegalArgumentException("Parameter pagedSearch cannot be null"); |
| 57 | |
} |
| 58 | |
|
| 59 | 0 | this.pagedSearch = pagedSearch; |
| 60 | 0 | entityClass = clasz; |
| 61 | |
|
| 62 | 0 | assert pagedSearch != null; |
| 63 | 0 | assert entityClass != null; |
| 64 | |
|
| 65 | 0 | } |
| 66 | |
|
| 67 | |
public int getAvailableRows() { |
| 68 | 0 | return pagedSearch.count(); |
| 69 | |
} |
| 70 | |
|
| 71 | |
@SuppressWarnings("unchecked") |
| 72 | |
public Class getRowType() { |
| 73 | 0 | return entityClass; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public Object getRowValue(int index) { |
| 77 | 0 | final int position = index - firstIndex; |
| 78 | 0 | return list.get(position); |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void prepare(int firstIndex, int endIndex, List<SortConstraint> sortConstraints) { |
| 82 | |
|
| 83 | 0 | this.firstIndex = firstIndex; |
| 84 | |
SortCriterion[] sortCriteria; |
| 85 | |
|
| 86 | 0 | sortCriteria = convertSortConstraintToSortCriterion(sortConstraints); |
| 87 | |
|
| 88 | 0 | final int maximumResults = (endIndex - firstIndex) + 1; |
| 89 | 0 | list = pagedSearch.search(firstIndex, maximumResults, sortCriteria); |
| 90 | |
|
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
private SortCriterion[] convertSortConstraintToSortCriterion( |
| 99 | |
List<SortConstraint> sortConstraints) { |
| 100 | |
|
| 101 | |
SortCriterion[] sortCriteria; |
| 102 | 0 | if (sortConstraints.size() > 0) { |
| 103 | |
|
| 104 | 0 | List<SortCriterion> list = new ArrayList<SortCriterion>(); |
| 105 | |
|
| 106 | 0 | for (SortConstraint sortConstraint : sortConstraints) { |
| 107 | |
|
| 108 | 0 | final ColumnSort columnSort = sortConstraint.getColumnSort(); |
| 109 | |
|
| 110 | 0 | if (columnSort != ColumnSort.UNSORTED) { |
| 111 | |
|
| 112 | 0 | final String propertyName = sortConstraint.getPropertyModel().getPropertyName(); |
| 113 | |
|
| 114 | 0 | final boolean ascending = columnSort == ColumnSort.ASCENDING; |
| 115 | 0 | list.add(new SortCriterion(propertyName, ascending)); |
| 116 | |
|
| 117 | |
} |
| 118 | |
|
| 119 | |
} |
| 120 | |
|
| 121 | 0 | sortCriteria = list.toArray(new SortCriterion[sortConstraints.size()]); |
| 122 | |
|
| 123 | |
} |
| 124 | |
else { |
| 125 | 0 | sortCriteria = EMPTY_SORT_CRITERION_ARRAY; |
| 126 | |
} |
| 127 | |
|
| 128 | 0 | return sortCriteria; |
| 129 | |
|
| 130 | |
} |
| 131 | |
|
| 132 | |
} |