KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.infoglue.cms.util.sorters;
2
3 import java.util.Comparator JavaDoc;
4
5 import org.apache.commons.beanutils.PropertyUtils;
6 import org.apache.log4j.Logger;
7 import org.infoglue.cms.entities.content.ContentVO;
8 import org.infoglue.cms.entities.structure.SiteNodeVO;
9 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController;
10
11 /**
12  * Sort on a particular property, using reflection to find the value
13  *
14  * @author Frank Febbraro (frank@phase2technology.com)
15  */

16 public class SiteNodeComparator implements Comparator JavaDoc
17 {
18     private final static Logger logger = Logger.getLogger(SiteNodeComparator.class.getName());
19
20     private String JavaDoc sortProperty;
21     private String JavaDoc sortOrder;
22     private TemplateController templateController;
23
24     public SiteNodeComparator(String JavaDoc sortProperty, String JavaDoc sortOrder, TemplateController templateController)
25     {
26         this.sortProperty = sortProperty;
27         this.sortOrder = sortOrder;
28         this.templateController = templateController;
29     }
30
31     public int compare(Object JavaDoc o1, Object JavaDoc o2)
32     {
33         SiteNodeVO siteNodeVO1 = (SiteNodeVO)o1;
34         SiteNodeVO siteNodeVO2 = (SiteNodeVO)o2;
35
36         Comparable JavaDoc valueOne = (String JavaDoc)siteNodeVO1.getExtraProperties().get(sortProperty);
37         Comparable JavaDoc valueTwo = (String JavaDoc)siteNodeVO2.getExtraProperties().get(sortProperty);
38         
39         long previousTime = System.currentTimeMillis();
40
41         if(valueOne == null)
42         {
43             valueOne = getProperty(o1, sortProperty);
44             valueTwo = getProperty(o2, sortProperty);
45         }
46
47         if(valueOne == null && this.templateController != null)
48         {
49             Integer JavaDoc meta1Id = this.templateController.getMetaInformationContentId(siteNodeVO1.getId());
50             Integer JavaDoc meta2Id = this.templateController.getMetaInformationContentId(siteNodeVO2.getId());
51         
52             valueOne = this.templateController.getContentAttribute(meta1Id, this.templateController.getLanguageId(), sortProperty);
53             valueTwo = this.templateController.getContentAttribute(meta2Id, this.templateController.getLanguageId(), sortProperty);
54         }
55
56         if(valueOne != null && valueTwo != null)
57         {
58             if(sortOrder.equalsIgnoreCase("desc"))
59                 return valueTwo.compareTo(valueOne);
60             else
61                 return valueOne.compareTo(valueTwo);
62         }
63         else
64             return 0;
65     }
66
67     private Comparable JavaDoc getProperty(Object JavaDoc o, String JavaDoc property)
68     {
69         try
70         {
71             Object JavaDoc propertyObject = PropertyUtils.getProperty(o, sortProperty);
72             if(propertyObject instanceof String JavaDoc)
73                 return (Comparable JavaDoc)propertyObject.toString().toLowerCase();
74             else
75                 return (Comparable JavaDoc)propertyObject;
76         }
77         catch (Exception JavaDoc e)
78         {
79             logger.info(getClass().getName() + " Error finding property " + property, e);
80             return null;
81         }
82     }
83 }
84
Popular Tags