1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.form;
17
18 import org.apache.commons.beanutils.BeanComparator;
19 import org.apache.commons.beanutils.PropertyUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.rice.kns.util.TableRenderUtil;
22
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.Date;
26 import java.util.List;
27
28
29
30
31 public class KualiTableRenderFormMetadata {
32 private int viewedPageNumber;
33 private int totalNumberOfPages;
34 private int firstRowIndex;
35 private int lastRowIndex;
36 private int switchToPageNumber;
37
38
39
40
41 private int resultsActualSize;
42
43
44
45
46
47 private int resultsLimitedSize;
48
49
50
51
52
53 private int previouslySortedColumnIndex;
54
55
56
57
58 private int columnToSortIndex;
59
60
61
62
63 private String columnToSortName;
64
65
66
67
68
69 private String previouslySortedColumnName;
70
71 private boolean sortDescending;
72
73 public KualiTableRenderFormMetadata() {
74 sortDescending = false;
75 }
76
77
78
79
80
81 public int getColumnToSortIndex() {
82 return columnToSortIndex;
83 }
84
85
86
87
88
89 public void setColumnToSortIndex(int columnToSortIndex) {
90 this.columnToSortIndex = columnToSortIndex;
91 }
92
93
94
95
96
97 public int getPreviouslySortedColumnIndex() {
98 return previouslySortedColumnIndex;
99 }
100
101
102
103
104
105 public void setPreviouslySortedColumnIndex(int previouslySortedColumnIndex) {
106 this.previouslySortedColumnIndex = previouslySortedColumnIndex;
107 }
108
109
110
111
112
113 public int getResultsActualSize() {
114 return resultsActualSize;
115 }
116
117
118
119
120
121 public void setResultsActualSize(int resultsActualSize) {
122 this.resultsActualSize = resultsActualSize;
123 }
124
125
126
127
128
129 public int getResultsLimitedSize() {
130 return resultsLimitedSize;
131 }
132
133
134
135
136
137 public void setResultsLimitedSize(int resultsLimitedSize) {
138 this.resultsLimitedSize = resultsLimitedSize;
139 }
140
141
142
143
144
145 public int getSwitchToPageNumber() {
146 return switchToPageNumber;
147 }
148
149
150
151
152
153 public void setSwitchToPageNumber(int switchToPageNumber) {
154 this.switchToPageNumber = switchToPageNumber;
155 }
156
157
158
159
160
161 public int getViewedPageNumber() {
162 return viewedPageNumber;
163 }
164
165
166
167
168
169 public void setViewedPageNumber(int viewedPageNumber) {
170 this.viewedPageNumber = viewedPageNumber;
171 }
172
173
174
175
176
177 public int getTotalNumberOfPages() {
178 return totalNumberOfPages;
179 }
180
181
182
183
184
185 public void setTotalNumberOfPages(int totalNumberOfPages) {
186 this.totalNumberOfPages = totalNumberOfPages;
187 }
188
189
190
191
192
193 public int getFirstRowIndex() {
194 return firstRowIndex;
195 }
196
197
198
199
200
201 public void setFirstRowIndex(int firstRowIndex) {
202 this.firstRowIndex = firstRowIndex;
203 }
204
205
206
207
208
209 public int getLastRowIndex() {
210 return lastRowIndex;
211 }
212
213
214
215
216
217 public void setLastRowIndex(int lastRowIndex) {
218 this.lastRowIndex = lastRowIndex;
219 }
220
221
222
223
224
225 public boolean isSortDescending() {
226 return sortDescending;
227 }
228
229
230
231
232
233 public void setSortDescending(boolean sortDescending) {
234 this.sortDescending = sortDescending;
235 }
236
237
238
239
240 public String getColumnToSortName() {
241 return this.columnToSortName;
242 }
243
244
245
246
247 public void setColumnToSortName(String columnToSortName) {
248 this.columnToSortName = columnToSortName;
249 }
250
251
252
253
254 public String getPreviouslySortedColumnName() {
255 return this.previouslySortedColumnName;
256 }
257
258
259
260
261 public void setPreviouslySortedColumnName(String previouslySortedColumnName) {
262 this.previouslySortedColumnName = previouslySortedColumnName;
263 }
264
265
266
267
268
269
270
271
272 public void jumpToFirstPage(int listSize, int maxRowsPerPage) {
273 jumpToPage(0, listSize, maxRowsPerPage);
274 }
275
276
277
278
279
280
281
282 public void jumpToLastPage(int listSize, int maxRowsPerPage) {
283 jumpToPage(TableRenderUtil.computeTotalNumberOfPages(listSize, maxRowsPerPage) - 1, listSize, maxRowsPerPage);
284 }
285
286
287
288
289
290
291
292
293
294
295
296 public void jumpToPage(int pageNumber, int listSize, int maxRowsPerPage) {
297 int totalPages = TableRenderUtil.computeTotalNumberOfPages(listSize, maxRowsPerPage);
298 setTotalNumberOfPages(totalPages);
299 if (pageNumber >= totalPages) {
300 pageNumber = totalPages - 1;
301 }
302 setViewedPageNumber(pageNumber);
303 setFirstRowIndex(TableRenderUtil.computeStartIndexForPage(pageNumber, listSize, maxRowsPerPage));
304 setLastRowIndex(TableRenderUtil.computeLastIndexForPage(pageNumber, listSize, maxRowsPerPage));
305 }
306
307
308
309
310
311
312
313
314
315 public void sort(List<?> items, int maxRowsPerPage) {
316
317
318 if (items == null || items.size() <= 1)
319 return;
320
321 String columnToSortOn = getColumnToSortName();
322
323
324 if (StringUtils.isEmpty(columnToSortOn))
325 return;
326
327 String previouslySortedColumnName = getPreviouslySortedColumnName();
328
329
330 Object firstItem = items.get(0);
331
332 Comparator comparator = null;
333 Comparator subComparator = new Comparator<Object>() {
334
335 public int compare(Object o1, Object o2) {
336 if (o1 == null)
337 return -1;
338 if (o2 == null)
339 return 1;
340
341 if (o1 instanceof java.util.Date && o2 instanceof java.util.Date) {
342 Date d1 = (Date)o1;
343 Date d2 = (Date)o2;
344 return d1.compareTo(d2);
345 }
346
347 String s1 = o1.toString();
348 String s2 = o2.toString();
349 int n1=s1.length(), n2=s2.length();
350 for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
351 char c1 = s1.charAt(i1);
352 char c2 = s2.charAt(i2);
353 if (c1 != c2) {
354 c1 = Character.toUpperCase(c1);
355 c2 = Character.toUpperCase(c2);
356 if (c1 != c2) {
357 c1 = Character.toLowerCase(c1);
358 c2 = Character.toLowerCase(c2);
359 if (c1 != c2) {
360 return c1 - c2;
361 }
362 }
363 }
364 }
365 return n1 - n2;
366 }
367 };
368
369
370 if (PropertyUtils.isReadable(firstItem, columnToSortOn))
371 comparator = new BeanComparator(columnToSortOn, subComparator);
372 else
373 comparator = new BeanComparator(new StringBuilder().append("qualifierAsMap<String, String>(").append(columnToSortOn).append(")").toString(), subComparator);
374
375
376
377 if (!StringUtils.isEmpty(columnToSortOn) && !StringUtils.isEmpty(previouslySortedColumnName) && columnToSortOn.equals(previouslySortedColumnName)) {
378
379 if (isSortDescending())
380 comparator = Collections.reverseOrder(comparator);
381
382 setSortDescending(!isSortDescending());
383 } else {
384
385 setPreviouslySortedColumnName(columnToSortOn);
386 setSortDescending(true);
387 }
388
389 Collections.sort(items, comparator);
390
391 jumpToFirstPage(items.size(), maxRowsPerPage);
392 }
393
394 }