KickJava   Java API By Example, From Geeks To Geeks.

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


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.deliver.applications.databeans.WebPage;
8 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController;
9
10 /**
11  * Sort on a particular property, using reflection to find the value
12  *
13  * @author Frank Febbraro (frank@phase2technology.com)
14  */

15 public class PageComparator implements Comparator JavaDoc
16 {
17     private final static Logger logger = Logger.getLogger(PageComparator.class.getName());
18
19     private String JavaDoc sortProperty;
20     private String JavaDoc sortOrder;
21     private boolean numberOrder;
22     private TemplateController templateController;
23
24     public PageComparator(String JavaDoc sortProperty, String JavaDoc sortOrder, boolean numberOrder, TemplateController templateController)
25     {
26         this.sortProperty = sortProperty;
27         this.sortOrder = sortOrder;
28         this.numberOrder = numberOrder;
29         this.templateController = templateController;
30     }
31
32     public int compare(Object JavaDoc o1, Object JavaDoc o2)
33     {
34         Comparable JavaDoc valueOne = getProperty(o1, sortProperty);
35         Comparable JavaDoc valueTwo = getProperty(o2, sortProperty);
36         
37         if(valueOne == null)
38         {
39             WebPage webPage1 = (WebPage)o1;
40             WebPage webPage2 = (WebPage)o2;
41             
42             Integer JavaDoc meta1Id = this.templateController.getMetaInformationContentId(webPage1.getSiteNodeId());
43             Integer JavaDoc meta2Id = this.templateController.getMetaInformationContentId(webPage2.getSiteNodeId());
44             
45             valueOne = this.templateController.getContentAttribute(meta1Id, this.templateController.getLanguageId(), sortProperty);
46             valueTwo = this.templateController.getContentAttribute(meta2Id, this.templateController.getLanguageId(), sortProperty);
47         
48             if(this.numberOrder)
49             {
50                 try
51                 {
52                     if(valueOne != null && !valueOne.equals(""))
53                         valueOne = (Comparable JavaDoc)new Long JavaDoc(valueOne.toString());
54                     else
55                     {
56                         if(sortOrder.equalsIgnoreCase("desc"))
57                             valueOne = (Comparable JavaDoc)new Long JavaDoc(Long.MIN_VALUE);
58                         else
59                             valueOne = (Comparable JavaDoc)new Long JavaDoc(Long.MAX_VALUE);
60                     }
61                 }
62                 catch(Exception JavaDoc e)
63                 {
64                     logger.info("Not a number..." + e.getMessage());
65                 }
66                 
67                 try
68                 {
69                     if(valueTwo != null && !valueTwo.equals(""))
70                         valueTwo = (Comparable JavaDoc)new Long JavaDoc(valueTwo.toString());
71                     else
72                     {
73                         if(sortOrder.equalsIgnoreCase("desc"))
74                             valueTwo = (Comparable JavaDoc)new Long JavaDoc(Long.MIN_VALUE);
75                         else
76                             valueTwo = (Comparable JavaDoc)new Long JavaDoc(Long.MAX_VALUE);
77                     }
78                 }
79                 catch(Exception JavaDoc e)
80                 {
81                     logger.info("Not a number..." + e.getMessage());
82                 }
83             }
84         }
85
86         if(sortOrder.equalsIgnoreCase("desc"))
87         {
88             if((valueOne != null && !valueOne.toString().equalsIgnoreCase("")) && (valueTwo == null || valueTwo.toString().equalsIgnoreCase("")))
89                 return -1;
90             if((valueTwo != null && !valueTwo.toString().equalsIgnoreCase("")) && (valueOne == null || valueOne.toString().equalsIgnoreCase("")))
91                 return 1;
92             
93             return valueTwo.compareTo(valueOne);
94         }
95         else
96         {
97             if((valueOne != null && !valueOne.toString().equalsIgnoreCase("")) && (valueTwo == null || valueTwo.toString().equalsIgnoreCase("")))
98                 return -1;
99             if((valueTwo != null && !valueTwo.toString().equalsIgnoreCase("")) && (valueOne == null || valueOne.toString().equalsIgnoreCase("")))
100                 return 1;
101             
102             return valueOne.compareTo(valueTwo);
103         }
104     }
105
106     private Comparable JavaDoc getProperty(Object JavaDoc o, String JavaDoc property)
107     {
108         try
109         {
110             Object JavaDoc propertyObject = PropertyUtils.getProperty(o, sortProperty);
111             if(propertyObject instanceof String JavaDoc)
112             {
113                 if(this.numberOrder)
114                 {
115                     try
116                     {
117                         return (Comparable JavaDoc)new Long JavaDoc(propertyObject.toString());
118                     }
119                     catch(Exception JavaDoc e)
120                     {
121                         logger.info("Not a number..." + e.getMessage());
122                     }
123                 }
124                 
125                 return (Comparable JavaDoc)propertyObject.toString().toLowerCase();
126             }
127             else
128             {
129                 return (Comparable JavaDoc)propertyObject;
130             }
131         }
132         catch (Exception JavaDoc e)
133         {
134             logger.info(getClass().getName() + " Error finding property " + property, e);
135             return null;
136         }
137     }
138 }
139
Popular Tags