KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package org.infoglue.cms.util.sorters;
25
26 import java.lang.reflect.Constructor JavaDoc;
27
28 import org.apache.commons.beanutils.PropertyUtils;
29 import org.infoglue.cms.entities.content.ContentVO;
30 import org.infoglue.cms.entities.content.ContentVersionVO;
31 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController;
32
33 /**
34  * A <code>SortElement</code> object is created for each content/content version
35  * to be sorted. If the <code>SortElement</code> is constructed using a content
36  * and the content version is needed, the content version to use is decided by
37  * fetching the language used by the <code>TemplateController</code>.
38  * <p>
39  * Strictly speaking we never sort on a content or a content version, we are
40  * sorting on a content <strong>and</strong> the content version indicated by the
41  * language used by the <code>TemplateController</code> at the same time.
42  * </p>
43  */

44
45 public class SortElement implements Comparable JavaDoc
46 {
47     /**
48      * The controller to use when interacting with the model.
49      */

50     private final TemplateController controller;
51     
52     /**
53      * The content version to sort.
54      */

55     private ContentVersionVO contentVersionVO;
56
57     /**
58      * The content to sort.
59      */

60     private ContentVO contentVO;
61     
62     /**
63      * The comparable of the element to sort.
64      */

65     private final CompoundComparable comparable = new CompoundComparable();
66
67     /**
68      * Constructs a sort element for the specified content.
69      *
70      * @param contentVO the content to sort.
71      * @param controller the controller to use when interacting with the model.
72      */

73     SortElement(final TemplateController controller, final ContentVO contentVO)
74     {
75         this.controller = controller;
76         this.contentVO = contentVO;
77     }
78
79     /**
80      * Constructs a sort element for the specified content version.
81      *
82      * @param contentVO the content to sort.
83      * @param controller the controller to use when interacting with the model.
84      */

85     SortElement(final TemplateController controller, final ContentVersionVO contentVersionVO)
86     {
87         this.controller = controller;
88         this.contentVersionVO = contentVersionVO;
89     }
90
91     /**
92      * Use the specified content property in the sort.
93      *
94      * @paran name the name of the content property.
95      * @param ascending indicates the sort order to use when sorting on the specified property.
96      */

97     public void addContentProperty(final String JavaDoc name, final boolean ascending)
98     {
99         comparable.add(getProperty(getContentVO(), name), ascending);
100     }
101
102     /**
103      * Use the specified content version property in the sort.
104      *
105      * @paran name the name of the content version property.
106      * @param ascending indicates the sort order to use when sorting on the specified property.
107      */

108     public void addContentVersionProperty(final String JavaDoc name, final boolean ascending)
109     {
110         comparable.add(getProperty(getContentVersionVO(), name), ascending);
111     }
112
113     /**
114      * Use the specified content version attribute in the sort. The type (Comparable) used when
115      * sorting on the attribute will be the specified class.
116      *
117      * @paran name the name of the content version attribute.
118      * @param clazz indicates the <code>Comparable</code> to use when sorting on the attribute.
119      * @param ascending indicates the sort order to use when sorting on the specified attribute.
120      */

121     public void addContentVersionAttribute(final String JavaDoc name, final Class JavaDoc clazz, final boolean ascending, final boolean caseSensitive)
122     {
123         final Integer JavaDoc contentId = getContentId();
124         String JavaDoc stringValue = controller.getContentAttribute(contentId, controller.getLanguageId(), name);
125         if(!caseSensitive)
126             stringValue = stringValue.toLowerCase();
127         
128         comparable.add(castAttribute(name, clazz, stringValue), ascending);
129     }
130
131     /**
132      * Cast the specified string value to the specified class (which must be a <code>Comparable</code>.
133      *
134      * @param name the name of the content version attribute (used for debug only).
135      * @param clazz the class to cast to. Must implement the <code>Comparable</code> interface.
136      * @param stringValue the value to cast.
137      * @throws IllegalArgumentException if the cast fails.
138      */

139     private Comparable JavaDoc castAttribute(final String JavaDoc name, final Class JavaDoc clazz, final String JavaDoc stringValue)
140     {
141         if(String JavaDoc.class.equals(clazz))
142         {
143             return stringValue;
144         }
145         try
146         {
147             final Constructor JavaDoc ctor = clazz.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
148             final String JavaDoc s = (Number JavaDoc.class.isAssignableFrom(clazz) && stringValue.equals("")) ? "0" : stringValue;
149             return (Comparable JavaDoc) ctor.newInstance(new Object JavaDoc[] { s });
150         }
151         catch(Exception JavaDoc e)
152         {
153             e.printStackTrace();
154             throw new IllegalArgumentException JavaDoc("Unable to cast [" + name + "] to [" + clazz.getName() + "].");
155         }
156     }
157     
158     /**
159      * Returns the identifier of the content element.
160      *
161      * @return the identifier.
162      */

163     private Integer JavaDoc getContentId()
164     {
165         return (contentVO != null) ? getContentVO().getContentId() : getContentVersionVO().getContentId();
166     }
167     
168     /**
169      * Returns the value of the specified property of the specified object.
170      *
171      * @param o object whose property is to be extracted.
172      * @param name the name of the property to be extracted.
173      * @returns the value of the specified property of the specified object.
174      */

175     private Comparable JavaDoc getProperty(final Object JavaDoc o, final String JavaDoc name)
176     {
177         try
178         {
179             return (Comparable JavaDoc) PropertyUtils.getProperty(o, name);
180         }
181         catch(Exception JavaDoc e)
182         {
183             e.printStackTrace();
184             throw new IllegalArgumentException JavaDoc("Illegal property [" + name + "] : " + e);
185         }
186     }
187     
188     /**
189      * Returns the content version value object. This is a convenience method as we don't
190      * if the element was constructed using a content or a content version.
191      *
192      * @return the content version value object.
193      */

194     public ContentVersionVO getContentVersionVO()
195     {
196         if(contentVersionVO == null)
197         {
198             contentVersionVO = controller.getContentVersion(contentVO.getContentId());
199         }
200         return contentVersionVO;
201     }
202     
203     /**
204      * Returns the content value object. This is a convenience method as we don't
205      * if the element was constructed using a content or a content version.
206      *
207      * @return the content value object.
208      */

209     public ContentVO getContentVO()
210     {
211         if(contentVO == null)
212         {
213             contentVO = controller.getContent(contentVersionVO.getContentId());
214         }
215         return contentVO;
216     }
217
218     /**
219      * Compares two <code>SortStruct</code> objects by comparing their <code>SortComparable</code>:s.
220      *
221      * @param o the object to ne compared.
222      * @return a negative integer, zero, or a positive integer if this object is less than,
223      * equal to, or greater than the specified object.
224      */

225     public final int compareTo(Object JavaDoc o)
226     {
227         if(!(o instanceof SortElement))
228         {
229             throw new ClassCastException JavaDoc();
230         }
231         final SortElement other = (SortElement) o;
232         return comparable.compareTo(other.comparable);
233     }
234 }
Popular Tags