KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > sorters > ContentSort


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 package org.infoglue.cms.util.sorters;
24
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Comparator JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.apache.commons.beanutils.PropertyUtils;
34 import org.infoglue.cms.entities.content.Content;
35 import org.infoglue.cms.entities.content.ContentVO;
36 import org.infoglue.cms.entities.content.ContentVersion;
37 import org.infoglue.cms.entities.content.ContentVersionVO;
38 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController;
39
40 /**
41  * A <code>CompoundComparable</code> is a compound comparable.
42  */

43 class CompoundComparable implements Comparable JavaDoc
44 {
45     /**
46      * The list of contained comparables.
47      */

48     private final List JavaDoc comparables = new ArrayList JavaDoc();
49
50     /**
51      * Each order in this list indicates how the comparable at the same position should be sorted (ascending or not).
52      */

53     private final List JavaDoc orders = new ArrayList JavaDoc(); // type: <Boolean>
54

55     /**
56      * Compares two <code>CompoundComparable</code> objects by comparing all
57      * contained comparable against each other.
58      *
59      * @param o the object to ne compared.
60      * @return a negative integer, zero, or a positive integer if this object is less than,
61      * equal to, or greater than the specified object.
62      */

63     public final int compareTo(final Object JavaDoc o)
64     {
65         if(!(o instanceof CompoundComparable))
66         {
67             throw new ClassCastException JavaDoc();
68         }
69         final CompoundComparable other = (CompoundComparable) o;
70         if(other.comparables.size() != comparables.size())
71         {
72             throw new IllegalStateException JavaDoc("Trying to compare SortComparable with different number of elements.");
73         }
74         for(int i=0; i<comparables.size(); i++)
75         {
76             final int result = compareTo(other, i);
77             if(result != 0)
78             {
79                 return result;
80             }
81         }
82         return 0;
83     }
84     
85     /**
86      * Compares the n:th comparable of two <code>SortComparable</code> objects.
87      *
88      * @param other the other <code>CompoundComparable</code> object.
89      * @param index indicates which contained comparable to compare.
90      * @return a negative integer, zero, or a positive integer if the n:th comparable of this object
91      * is less than, equal to, or greater than the n:th comparable of the specified object.
92      */

93     private final int compareTo(final CompoundComparable other, final int index)
94     {
95         final Comparable JavaDoc c1 = (Comparable JavaDoc) comparables.get(index);
96         final Comparable JavaDoc c2 = (Comparable JavaDoc) other.comparables.get(index);
97         final Boolean JavaDoc ascending = (Boolean JavaDoc) orders.get(index);
98         return ascending.booleanValue() ? c1.compareTo(c2) : c2.compareTo(c1);
99     }
100     
101     /**
102      * Adds the specified comparable. The comparable will be sorted in the specified order.
103      *
104      * @param c the comparable to add.
105      * @param ascending indicates the sort order of the comparable.
106      */

107     public final void add(final Comparable JavaDoc c, final boolean ascending)
108     {
109         comparables.add(c);
110         orders.add(new Boolean JavaDoc(ascending));
111     }
112 }
113
114
115 /**
116  * Utility class for sorting content/content version objects.
117  * Any number of properties and/or attributes of the content/content versions can be used in the sort.
118  */

119 public class ContentSort
120 {
121     /**
122      * The controller to use when interacting with the model.
123      */

124     TemplateController controller;
125     
126     /**
127      * The elements to sort.
128      */

129     private final List JavaDoc elements = new ArrayList JavaDoc(); // type: <SortElement>
130

131     /**
132      * Constructs a sorter for the specified elements.
133      *
134      * @param controller the controller to use when interacting with the model.
135      * @param elements the list of objects to sort.
136      */

137     public ContentSort(final TemplateController controller, final Collection JavaDoc elements)
138     {
139         this.controller = controller;
140         addElements(elements);
141     }
142     
143     /**
144      * Sets the elements to sort.
145      *
146      * @param elements the list of objects to sort.
147      */

148     private final void addElements(final Collection JavaDoc elements)
149     {
150         if(elements == null || elements.isEmpty())
151         {
152             return;
153         }
154         final Object JavaDoc element = elements.toArray()[0];
155         if(element instanceof Content)
156         {
157             initializeWithContent(elements);
158         }
159         if(element instanceof ContentVO)
160         {
161             initializeWithContentVO(elements);
162         }
163         if(element instanceof ContentVersion)
164         {
165             initializeWithContentVersion(elements);
166         }
167         if(element instanceof ContentVersionVO)
168         {
169             initializeWithContentVersionVO(elements);
170         }
171     }
172     
173     /**
174      * Sets the elements to sort.
175      *
176      * @param the list of <code>Content</code> objects to sort.
177      */

178     private final void initializeWithContent(final Collection JavaDoc elements)
179     {
180         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
181         {
182             addElement(((Content) i.next()).getValueObject());
183         }
184     }
185     
186     /**
187      * Sets the elements to sort.
188      *
189      * @param the list of <code>ContentVO</code> objects to sort.
190      */

191     private final void initializeWithContentVO(final Collection JavaDoc elements)
192     {
193         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
194         {
195             addElement((ContentVO) i.next());
196         }
197     }
198     
199     /**
200      * Sets the elements to sort.
201      *
202      * @param the list of <code>ContentVersion</code> objects to sort.
203      */

204     private final void initializeWithContentVersion(final Collection JavaDoc elements)
205     {
206         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
207         {
208             addElement(((ContentVersion) i.next()).getValueObject());
209         }
210     }
211     
212     /**
213      * Sets the elements to sort.
214      *
215      * @param the list of <code>ContentVersionVO</code> objects to sort.
216      */

217     private final void initializeWithContentVersionVO(final Collection JavaDoc elements)
218     {
219         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
220         {
221             addElement((ContentVersionVO) i.next());
222         }
223     }
224     
225     /**
226      * Use the specified content property in the sort.
227      *
228      * @paran name the name of the content property.
229      * @param ascending indicates the sort order to use when sorting on the specified property.
230      */

231     public void addContentProperty(final String JavaDoc name, final boolean ascending)
232     {
233         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
234         {
235             ((SortElement) i.next()).addContentProperty(name, ascending);
236         }
237     }
238     
239     /**
240      * Use the specified content version property in the sort.
241      *
242      * @paran name the name of the content version property.
243      * @param ascending indicates the sort order to use when sorting on the specified property.
244      */

245     public void addContentVersionProperty(final String JavaDoc name, final boolean ascending)
246     {
247         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
248         {
249             ((SortElement) i.next()).addContentVersionProperty(name, ascending);
250         }
251     }
252     
253     /**
254      * Use the specified content version attribute in the sort. The type (Comparable) used when
255      * sorting on the attribute will be <code>String</code>.
256      *
257      * @paran name the name of the content version attribute.
258      * @param ascending indicates the sort order to use when sorting on the specified attribute.
259      */

260     public void addContentVersionAttribute(final String JavaDoc name, final boolean ascending, final boolean caseSensitive)
261     {
262         addContentVersionAttribute(name, String JavaDoc.class, ascending, caseSensitive);
263     }
264     
265     /**
266      * Use the specified content version attribute in the sort. The type (Comparable) used when
267      * sorting on the attribute will be the specified class.
268      *
269      * @paran name the name of the content version attribute.
270      * @param className indicates the name of the <code>Comparable</code> to use when sorting on the attribute.
271      * @param ascending indicates the sort order to use when sorting on the specified attribute.
272      */

273     public void addContentVersionAttribute(final String JavaDoc name, final String JavaDoc className, final boolean ascending, final boolean caseSensitive)
274     {
275         try
276         {
277             addContentVersionAttribute(name, Class.forName(className), ascending, caseSensitive);
278         }
279         catch(ClassNotFoundException JavaDoc e)
280         {
281             e.printStackTrace();
282             throw new IllegalArgumentException JavaDoc(e.getMessage());
283         }
284     }
285     
286     /**
287      * Use the specified content version attribute in the sort. The type (Comparable) used when
288      * sorting on the attribute will be the specified class.
289      *
290      * @paran name the name of the content version attribute.
291      * @param clazz indicates the <code>Comparable</code> to use when sorting on the attribute.
292      * @param ascending indicates the sort order to use when sorting on the specified attribute.
293      */

294     public void addContentVersionAttribute(final String JavaDoc name, final Class JavaDoc clazz, final boolean ascending, final boolean caseSensitive)
295     {
296         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
297         {
298             ((SortElement) i.next()).addContentVersionAttribute(name, clazz, ascending, caseSensitive);
299         }
300     }
301
302     /**
303      * Adds the specified <code>ContentVO</code> object to the elements to sort.
304      *
305      * @param contentVO the element to sort.
306      */

307     private void addElement(final ContentVO contentVO)
308     {
309         elements.add(new SortElement(controller, contentVO));
310     }
311     
312     /**
313      * Adds the specified <code>ContentVersionVO</code> object to the elements to sort.
314      *
315      * @param contentVO the element to sort.
316      */

317     private void addElement(final ContentVersionVO contentVersionVO)
318     {
319         elements.add(new SortElement(controller, contentVersionVO));
320     }
321     
322     /**
323      * Returns a list of sorted <code>ContentVO</code> objects.
324      *
325      * @return the sorted list.
326      */

327     public List JavaDoc getContentResult()
328     {
329         Collections.sort(elements);
330         final List JavaDoc result = new ArrayList JavaDoc();
331         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
332         {
333             final SortElement struct = (SortElement) i.next();
334             result.add(struct.getContentVO());
335         }
336         return result;
337     }
338
339     
340     /**
341      * Returns a list of sorted <code>ContentVO</code> objects.
342      * @param sorts the list using the comparatorClass specified. The comparator must implement the interface org.infoglue.cms.util.sorters.TemplateControllerAwareComparator
343      * @return the sorted list.
344      */

345     public List JavaDoc getContentResult(String JavaDoc comparatorClass)
346     {
347         TemplateControllerAwareComparator comp = null;
348         if(comparatorClass!=null && !comparatorClass.equals(""))
349         {
350             try
351             {
352                 Class JavaDoc clazz = Class.forName(comparatorClass);
353                 if(clazz!=null)
354                 {
355                     try
356                     {
357                         comp = (TemplateControllerAwareComparator) clazz.newInstance();
358                         comp.setController(this.controller);
359                     }
360                     catch (InstantiationException JavaDoc e)
361                     {
362                         throw new IllegalArgumentException JavaDoc("Couldnt instantiate comparator class " + comparatorClass + " " + e.getMessage());
363                     } catch (IllegalAccessException JavaDoc e)
364                     {
365                         throw new IllegalArgumentException JavaDoc("Couldnt access comparator class " + comparatorClass + " " + e.getMessage());
366                     }
367                 }
368             }
369             catch (ClassNotFoundException JavaDoc e)
370             {
371                 throw new IllegalArgumentException JavaDoc("Couldnt find comparator class " + comparatorClass + " " + e.getMessage());
372             }
373         }
374         else
375         {
376             throw new IllegalArgumentException JavaDoc("Must specify a comparator classname");
377         }
378
379         Collections.sort(elements,comp);
380         
381         final List JavaDoc result = new ArrayList JavaDoc();
382         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
383         {
384             final SortElement struct = (SortElement) i.next();
385             result.add(struct.getContentVO());
386         }
387         
388         return result;
389     }
390
391     /**
392      * Returns a list of sorted <code>ContentVersionVO</code> objects.
393      *
394      * @return the sorted list.
395      */

396     public List JavaDoc getContentVersionResult()
397     {
398         Collections.sort(elements);
399         final List JavaDoc result = new ArrayList JavaDoc();
400         for(final Iterator JavaDoc i=elements.iterator(); i.hasNext(); )
401         {
402             final SortElement struct = (SortElement) i.next();
403             result.add(struct.getContentVersionVO());
404         }
405         return result;
406     }
407 }
408
Popular Tags