View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * @author Sameer Charles $Id: AbstractListModel.java 41137 2011-01-06 18:19:25Z gjoseph $
51   */
52  public abstract class AbstractListModel implements ListModel {
53  
54      private static Logger log = LoggerFactory.getLogger(AbstractListModel.class);
55  
56      /**
57       * sort or group by order
58       */
59      public static final String DESCENDING = "DESC";
60  
61      /**
62       * sort or group by order
63       */
64      public static final String ASCENDING = "ASC";
65  
66      /**
67       * sort by field name
68       */
69      protected String sortBy;
70  
71      /**
72       * sort by order
73       */
74      protected String sortByOrder;
75  
76      /**
77       * group by field name
78       */
79      protected String groupBy;
80  
81      /**
82       * group by order
83       */
84      protected String groupByOrder;
85  
86      /**
87       * Used to get values out of the nodes/objects handled by the list
88       */
89      private ValueProvider valueProvider;
90  
91      /**
92       * this must be implemented by implementing classes
93       * @return Iterator over found records
94       * @see ListModelIterator
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      * @return the collection of the items passed to the iterator
114      */
115     protected abstract Collection getResult() throws Exception;
116 
117     /**
118      * Create the iterator
119      * @param items
120      * @return
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      * set sort by field
131      * @param name
132      */
133     public void setSortBy(String name) {
134         this.sortBy = name;
135     }
136 
137     /**
138      * set sort by field and order ('ASCENDING' | 'DESCENDING')
139      * @param name
140      * @param order
141      */
142     public void setSortBy(String name, String order) {
143         this.sortBy = name;
144         this.sortByOrder = order;
145     }
146 
147     /**
148      * set group by field
149      * @param name
150      */
151     public void setGroupBy(String name) {
152         this.groupBy = name;
153     }
154 
155     /**
156      * set group by field and order ('ASCENDING' | 'DESCENDING')
157      * @param name
158      * @param order
159      */
160     public void setGroupBy(String name, String order) {
161         this.groupBy = name;
162         this.groupByOrder = order;
163     }
164 
165     /**
166      * get sort on field name
167      * @return String field name
168      */
169     public String getSortBy() {
170         return this.sortBy;
171     }
172 
173     /**
174      * get sort by ordering
175      * @return order ('ASCENDING' | 'DESCENDING')
176      */
177     public String getSortByOrder() {
178         return this.sortByOrder;
179     }
180 
181     /**
182      * get group on field name
183      * @return String field name
184      */
185     public String getGroupBy() {
186         return this.groupBy;
187     }
188 
189     /**
190      * get group by ordering
191      * @return order ('ASCENDING' | 'DESCENDING')
192      */
193     public String getGroupByOrder() {
194         return this.groupByOrder;
195     }
196 
197     /**
198      * sort
199      * @param collection
200      * @return sorted collection
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())) { // sub sort
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      * @param valueProvider the valueProvider to set
236      */
237     public void setValueProvider(ValueProvider valueProvider) {
238         this.valueProvider = valueProvider;
239     }
240 
241     /**
242      * @return the valueProvider
243      */
244     public ValueProvider getValueProvider() {
245         if (valueProvider == null) {
246             valueProvider = DefaultValueProvider.getInstance();
247         }
248         return valueProvider;
249     }
250 
251     /**
252      * Use by the list iterator to resolve the id
253      */
254     protected String resolveId(int index, Object value){
255         return Integer.toString(index);
256     }
257 
258     /**
259      * Does simple or sub ordering
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          * group by
281          * @param object to be compared
282          * @param object1 to be compared
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          * sub sort
296          * @param object to be compared
297          * @param object1 to be compared
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 }