1 23 package org.infoglue.cms.util.sorters; 24 25 import java.lang.reflect.Constructor ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Comparator ; 30 import java.util.Iterator ; 31 import java.util.List ; 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 43 class CompoundComparable implements Comparable 44 { 45 48 private final List comparables = new ArrayList (); 49 50 53 private final List orders = new ArrayList (); 55 63 public final int compareTo(final Object o) 64 { 65 if(!(o instanceof CompoundComparable)) 66 { 67 throw new ClassCastException (); 68 } 69 final CompoundComparable other = (CompoundComparable) o; 70 if(other.comparables.size() != comparables.size()) 71 { 72 throw new IllegalStateException ("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 93 private final int compareTo(final CompoundComparable other, final int index) 94 { 95 final Comparable c1 = (Comparable ) comparables.get(index); 96 final Comparable c2 = (Comparable ) other.comparables.get(index); 97 final Boolean ascending = (Boolean ) orders.get(index); 98 return ascending.booleanValue() ? c1.compareTo(c2) : c2.compareTo(c1); 99 } 100 101 107 public final void add(final Comparable c, final boolean ascending) 108 { 109 comparables.add(c); 110 orders.add(new Boolean (ascending)); 111 } 112 } 113 114 115 119 public class ContentSort 120 { 121 124 TemplateController controller; 125 126 129 private final List elements = new ArrayList (); 131 137 public ContentSort(final TemplateController controller, final Collection elements) 138 { 139 this.controller = controller; 140 addElements(elements); 141 } 142 143 148 private final void addElements(final Collection elements) 149 { 150 if(elements == null || elements.isEmpty()) 151 { 152 return; 153 } 154 final Object 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 178 private final void initializeWithContent(final Collection elements) 179 { 180 for(final Iterator i=elements.iterator(); i.hasNext(); ) 181 { 182 addElement(((Content) i.next()).getValueObject()); 183 } 184 } 185 186 191 private final void initializeWithContentVO(final Collection elements) 192 { 193 for(final Iterator i=elements.iterator(); i.hasNext(); ) 194 { 195 addElement((ContentVO) i.next()); 196 } 197 } 198 199 204 private final void initializeWithContentVersion(final Collection elements) 205 { 206 for(final Iterator i=elements.iterator(); i.hasNext(); ) 207 { 208 addElement(((ContentVersion) i.next()).getValueObject()); 209 } 210 } 211 212 217 private final void initializeWithContentVersionVO(final Collection elements) 218 { 219 for(final Iterator i=elements.iterator(); i.hasNext(); ) 220 { 221 addElement((ContentVersionVO) i.next()); 222 } 223 } 224 225 231 public void addContentProperty(final String name, final boolean ascending) 232 { 233 for(final Iterator i=elements.iterator(); i.hasNext(); ) 234 { 235 ((SortElement) i.next()).addContentProperty(name, ascending); 236 } 237 } 238 239 245 public void addContentVersionProperty(final String name, final boolean ascending) 246 { 247 for(final Iterator i=elements.iterator(); i.hasNext(); ) 248 { 249 ((SortElement) i.next()).addContentVersionProperty(name, ascending); 250 } 251 } 252 253 260 public void addContentVersionAttribute(final String name, final boolean ascending, final boolean caseSensitive) 261 { 262 addContentVersionAttribute(name, String .class, ascending, caseSensitive); 263 } 264 265 273 public void addContentVersionAttribute(final String name, final String className, final boolean ascending, final boolean caseSensitive) 274 { 275 try 276 { 277 addContentVersionAttribute(name, Class.forName(className), ascending, caseSensitive); 278 } 279 catch(ClassNotFoundException e) 280 { 281 e.printStackTrace(); 282 throw new IllegalArgumentException (e.getMessage()); 283 } 284 } 285 286 294 public void addContentVersionAttribute(final String name, final Class clazz, final boolean ascending, final boolean caseSensitive) 295 { 296 for(final Iterator i=elements.iterator(); i.hasNext(); ) 297 { 298 ((SortElement) i.next()).addContentVersionAttribute(name, clazz, ascending, caseSensitive); 299 } 300 } 301 302 307 private void addElement(final ContentVO contentVO) 308 { 309 elements.add(new SortElement(controller, contentVO)); 310 } 311 312 317 private void addElement(final ContentVersionVO contentVersionVO) 318 { 319 elements.add(new SortElement(controller, contentVersionVO)); 320 } 321 322 327 public List getContentResult() 328 { 329 Collections.sort(elements); 330 final List result = new ArrayList (); 331 for(final Iterator 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 345 public List getContentResult(String comparatorClass) 346 { 347 TemplateControllerAwareComparator comp = null; 348 if(comparatorClass!=null && !comparatorClass.equals("")) 349 { 350 try 351 { 352 Class 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 e) 361 { 362 throw new IllegalArgumentException ("Couldnt instantiate comparator class " + comparatorClass + " " + e.getMessage()); 363 } catch (IllegalAccessException e) 364 { 365 throw new IllegalArgumentException ("Couldnt access comparator class " + comparatorClass + " " + e.getMessage()); 366 } 367 } 368 } 369 catch (ClassNotFoundException e) 370 { 371 throw new IllegalArgumentException ("Couldnt find comparator class " + comparatorClass + " " + e.getMessage()); 372 } 373 } 374 else 375 { 376 throw new IllegalArgumentException ("Must specify a comparator classname"); 377 } 378 379 Collections.sort(elements,comp); 380 381 final List result = new ArrayList (); 382 for(final Iterator 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 396 public List getContentVersionResult() 397 { 398 Collections.sort(elements); 399 final List result = new ArrayList (); 400 for(final Iterator 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 |