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