1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.controlx.list;
35
36
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.Comparator;
41 import java.util.Iterator;
42 import java.util.List;
43
44 import org.apache.commons.lang.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52 public abstract class AbstractListModel implements ListModel {
53
54 private static Logger log = LoggerFactory.getLogger(AbstractListModel.class);
55
56
57
58
59 public static final String DESCENDING = "DESC";
60
61
62
63
64 public static final String ASCENDING = "ASC";
65
66
67
68
69 protected String sortBy;
70
71
72
73
74 protected String sortByOrder;
75
76
77
78
79 protected String groupBy;
80
81
82
83
84 protected String groupByOrder;
85
86
87
88
89 private ValueProvider valueProvider;
90
91
92
93
94
95
96 public ListModelIterator getListModelIterator() {
97 try {
98 Collection items = getResult();
99 items = doSort(items);
100 return createIterator(items);
101 }
102 catch (Exception re) {
103 log.error("can't create the list model iterator, will return an empty list", re);
104 return new ListModelIteratorImpl(this, new ArrayList());
105 }
106 }
107
108 public Iterator iterator() {
109 return getListModelIterator();
110 }
111
112
113
114
115 protected abstract Collection getResult() throws Exception;
116
117
118
119
120
121
122 protected ListModelIterator createIterator(Collection items) {
123 if(!(items instanceof List)){
124 throw new RuntimeException("items must be a List");
125 }
126 return new ListModelIteratorImpl(this, (List) items);
127 }
128
129
130
131
132
133 public void setSortBy(String name) {
134 this.sortBy = name;
135 }
136
137
138
139
140
141
142 public void setSortBy(String name, String order) {
143 this.sortBy = name;
144 this.sortByOrder = order;
145 }
146
147
148
149
150
151 public void setGroupBy(String name) {
152 this.groupBy = name;
153 }
154
155
156
157
158
159
160 public void setGroupBy(String name, String order) {
161 this.groupBy = name;
162 this.groupByOrder = order;
163 }
164
165
166
167
168
169 public String getSortBy() {
170 return this.sortBy;
171 }
172
173
174
175
176
177 public String getSortByOrder() {
178 return this.sortByOrder;
179 }
180
181
182
183
184
185 public String getGroupBy() {
186 return this.groupBy;
187 }
188
189
190
191
192
193 public String getGroupByOrder() {
194 return this.groupByOrder;
195 }
196
197
198
199
200
201
202 protected Collection doSort(Collection collection) {
203 if(!(collection instanceof List)){
204 log.warn("can sort only collections of type {} but got a {}", List.class, collection.getClass());
205 return collection;
206 }
207 List list = (List) collection;
208 if (StringUtils.isNotEmpty(this.getGroupBy())) {
209 ListComparator comparator = newComparator();
210 comparator.setSortBy(this.getGroupBy());
211 comparator.setOrder(this.getGroupByOrder());
212 Collections.sort(list, comparator);
213 }
214 if (StringUtils.isNotEmpty(this.getGroupBy()) && StringUtils.isNotEmpty(this.getSortBy())) {
215 ListComparator comparator = newComparator();
216 comparator.setPreSort(this.getGroupBy());
217 comparator.setSortBy(this.getSortBy());
218 comparator.setOrder(this.getSortByOrder());
219 Collections.sort(list, comparator);
220 }
221 if (StringUtils.isEmpty(this.getGroupBy()) && StringUtils.isNotEmpty(this.getSortBy())) {
222 ListComparator comparator = newComparator();
223 comparator.setSortBy(this.getSortBy());
224 comparator.setOrder(this.getSortByOrder());
225 Collections.sort(list, comparator);
226 }
227 return list;
228 }
229
230 protected ListComparator newComparator() {
231 return new ListComparator();
232 }
233
234
235
236
237 public void setValueProvider(ValueProvider valueProvider) {
238 this.valueProvider = valueProvider;
239 }
240
241
242
243
244 public ValueProvider getValueProvider() {
245 if (valueProvider == null) {
246 valueProvider = DefaultValueProvider.getInstance();
247 }
248 return valueProvider;
249 }
250
251
252
253
254 protected String resolveId(int index, Object value){
255 return Integer.toString(index);
256 }
257
258
259
260
261 protected class ListComparator implements Comparator {
262
263 private String preSort;
264
265 private String sortBy;
266
267 private String order;
268
269 public int compare(Object object, Object object1) {
270 if (StringUtils.isNotEmpty(this.sortBy) && StringUtils.isEmpty(this.preSort)) {
271 return this.sort(object, object1);
272 }
273 else if (StringUtils.isNotEmpty(this.sortBy) && StringUtils.isNotEmpty(this.preSort)) {
274 return this.subSort(object, object1);
275 }
276 return 0;
277 }
278
279
280
281
282
283
284 protected int sort(Object object, Object object1) {
285 Comparable firstKey = (Comparable) getValueProvider().getValue(this.sortBy, object);
286 Comparable secondKey = (Comparable) getValueProvider().getValue(this.sortBy, object1);
287 if (this.getOrder().equalsIgnoreCase(ASCENDING)) {
288 return firstKey.compareTo(secondKey);
289 }
290
291 return secondKey.compareTo(firstKey);
292 }
293
294
295
296
297
298
299 protected int subSort(Object object, Object object1) {
300 String firstKey = (String) getValueProvider().getValue(this.preSort, object);
301 String secondKey = (String) getValueProvider().getValue(this.preSort, object1);
302 Comparable subSortFirstKey = (Comparable) getValueProvider().getValue(this.sortBy, object);
303 Comparable subSortSecondKey = (Comparable) getValueProvider().getValue(this.sortBy, object1);
304 if (firstKey.equalsIgnoreCase(secondKey)) {
305 if (this.getOrder().equalsIgnoreCase(ASCENDING)) {
306 return subSortFirstKey.compareTo(subSortSecondKey);
307 }
308 return subSortSecondKey.compareTo(subSortFirstKey);
309 }
310 return -1;
311 }
312
313 public String getPreSort() {
314 return preSort;
315 }
316
317 public void setPreSort(String preSort) {
318 this.preSort = preSort;
319 }
320
321 public String getSortBy() {
322 return sortBy;
323 }
324
325 public void setSortBy(String sortBy) {
326 this.sortBy = sortBy;
327 }
328
329 public String getOrder() {
330 if (order == null) {
331 return ASCENDING;
332 }
333 return order;
334 }
335
336 public void setOrder(String order) {
337 this.order = order;
338 }
339
340 }
341 }