View Javadoc

1   // Copyright 2008 Thiago H. de Paula Figueiredo
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
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   * {@link GridDataSource} implementation using a {@link Controller} instance, specifically its
29   * {@link Controller#findAll(int, int, SortCriterion[])} method.
30   * 
31   * @param <T> the type of the elements returned.
32   * @author Thiago H. de Paula Figueiredo
33   */
34  public class PagedSearchGridDataSource<T> implements GridDataSource {
35  
36  	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  	 * Single construtctor of this class.
48  	 * 
49  	 * @param clasz the type of the returned objects. It cannot be null.
50  	 * @param pagedSearch a {@link Controller}. It cannot be null.
51  	 */
52  	@SuppressWarnings("unchecked")
53  	public PagedSearchGridDataSource(Class<T> clasz, PagedSearch<T> pagedSearch) {
54  
55  		if (pagedSearch == null) {
56  			throw new IllegalArgumentException("Parameter pagedSearch cannot be null");
57  		}
58  
59  		this.pagedSearch = pagedSearch;
60  		entityClass = clasz;
61  
62  		assert pagedSearch != null;
63  		assert entityClass != null;
64  
65  	}
66  
67  	public int getAvailableRows() {
68  		return pagedSearch.count();
69  	}
70  
71  	@SuppressWarnings("unchecked")
72  	public Class getRowType() {
73  		return entityClass;
74  	}
75  
76  	public Object getRowValue(int index) {
77  		final int position = index - firstIndex;
78  		return list.get(position);
79  	}
80  
81  	public void prepare(int firstIndex, int endIndex, List<SortConstraint> sortConstraints) {
82  
83  		this.firstIndex = firstIndex;
84  		SortCriterion[] sortCriteria; 
85  		
86  		sortCriteria = convertSortConstraintToSortCriterion(sortConstraints);
87  		
88  		final int maximumResults = (endIndex - firstIndex) + 1;
89  		list = pagedSearch.search(firstIndex, maximumResults, sortCriteria);
90  
91  	}
92  
93  	/**
94  	 * Converts a {@link List} of {@link SortConstraint} to a {@link SortCriterion} array.
95  	 * @param sortConstraints a {@link List} of {@link SortConstraint}.
96  	 * @return a {@link SortCriterion} array.
97  	 */
98  	private SortCriterion[] convertSortConstraintToSortCriterion(
99  			List<SortConstraint> sortConstraints) {
100 		
101 		SortCriterion[] sortCriteria;
102 		if (sortConstraints.size() > 0) {
103 			
104 			List<SortCriterion> list = new ArrayList<SortCriterion>();
105 	
106 			for (SortConstraint sortConstraint : sortConstraints) {
107 	
108 				final ColumnSort columnSort = sortConstraint.getColumnSort();
109 	
110 				if (columnSort != ColumnSort.UNSORTED) {
111 	
112 					final String propertyName = sortConstraint.getPropertyModel().getPropertyName();
113 	
114 					final boolean ascending = columnSort == ColumnSort.ASCENDING;
115 					list.add(new SortCriterion(propertyName, ascending));
116 	
117 				}
118 	
119 			}
120 			
121 			sortCriteria = list.toArray(new SortCriterion[sortConstraints.size()]);
122 			
123 		}
124 		else {
125 			sortCriteria = EMPTY_SORT_CRITERION_ARRAY;
126 		}
127 		
128 		return sortCriteria;
129 		
130 	}
131 
132 }