KickJava   Java API By Example, From Geeks To Geeks.

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


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 HardcodedPageComparator implements Comparator JavaDoc
16 {
17     private final static Logger logger = Logger.getLogger(HardcodedPageComparator.class.getName());
18
19     private String JavaDoc sortProperty;
20     private String JavaDoc sortOrder;
21     private boolean numberOrder;
22     private String JavaDoc nameProperty;
23     private String JavaDoc namesInOrderString;
24     
25     private TemplateController templateController;
26
27     public HardcodedPageComparator(String JavaDoc sortProperty, String JavaDoc sortOrder, boolean numberOrder, String JavaDoc nameProperty, String JavaDoc namesInOrderString, TemplateController templateController)
28     {
29         this.sortProperty = sortProperty;
30         this.sortOrder = sortOrder;
31         this.numberOrder = numberOrder;
32         this.nameProperty = nameProperty;
33         this.namesInOrderString = namesInOrderString;
34         this.templateController = templateController;
35     }
36
37     public int compare(Object JavaDoc o1, Object JavaDoc o2)
38     {
39         //System.out.println("sortOrder: " + sortOrder);
40

41         Comparable JavaDoc valueOne = getProperty(o1, sortProperty);
42         Comparable JavaDoc valueTwo = getProperty(o2, sortProperty);
43         
44         Comparable JavaDoc valueOneName = getProperty(o1, nameProperty);
45         Comparable JavaDoc valueTwoName = getProperty(o2, nameProperty);
46         
47         if(valueOne == null)
48         {
49             WebPage webPage1 = (WebPage)o1;
50             WebPage webPage2 = (WebPage)o2;
51             
52             Integer JavaDoc meta1Id = webPage1.getMetaInfoContentId(); //this.templateController.getMetaInformationContentId(webPage1.getSiteNodeId());
53
Integer JavaDoc meta2Id = webPage2.getMetaInfoContentId(); //this.templateController.getMetaInformationContentId(webPage2.getSiteNodeId());
54

55             valueOne = this.templateController.getContentAttribute(meta1Id, this.templateController.getLanguageId(), sortProperty);
56             valueTwo = this.templateController.getContentAttribute(meta2Id, this.templateController.getLanguageId(), sortProperty);
57
58             if(valueOneName == null)
59             {
60                 valueOneName = this.templateController.getContentAttribute(meta1Id, this.templateController.getLanguageId(), nameProperty);
61                 valueTwoName = this.templateController.getContentAttribute(meta2Id, this.templateController.getLanguageId(), nameProperty);
62             }
63             
64             if(this.numberOrder)
65             {
66                 try
67                 {
68                     if(valueOne != null && !valueOne.equals(""))
69                         valueOne = (Comparable JavaDoc)new Long JavaDoc(valueOne.toString());
70                     else
71                     {
72                         if(sortOrder.equalsIgnoreCase("desc"))
73                             valueOne = (Comparable JavaDoc)new Long JavaDoc(Long.MIN_VALUE);
74                         else
75                             valueOne = (Comparable JavaDoc)new Long JavaDoc(Long.MAX_VALUE);
76                     }
77                 }
78                 catch(Exception JavaDoc e)
79                 {
80                     logger.info("Not a number..." + e.getMessage());
81                 }
82                 
83                 try
84                 {
85                     if(valueTwo != null && !valueTwo.equals(""))
86                         valueTwo = (Comparable JavaDoc)new Long JavaDoc(valueTwo.toString());
87                     else
88                     {
89                         if(sortOrder.equalsIgnoreCase("desc"))
90                             valueTwo = (Comparable JavaDoc)new Long JavaDoc(Long.MIN_VALUE);
91                         else
92                             valueTwo = (Comparable JavaDoc)new Long JavaDoc(Long.MAX_VALUE);
93                     }
94                 }
95                 catch(Exception JavaDoc e)
96                 {
97                     logger.info("Not a number..." + e.getMessage());
98                 }
99             }
100         }
101
102         if(after(valueOne, valueTwo, valueOneName, valueTwoName))
103             return 1;
104         else
105             return -1;
106     }
107
108     private boolean after(Comparable JavaDoc valueOne, Comparable JavaDoc valueTwo, Comparable JavaDoc valueOneName, Comparable JavaDoc valueTwoName)
109     {
110         int index1 = namesInOrderString.indexOf(valueOneName.toString());
111         int index2 = namesInOrderString.indexOf(valueTwoName.toString());
112         
113         //System.out.println("" + valueOneName.toString() + ":" + index1);
114
//System.out.println("" + valueTwoName.toString() + ":" + index2);
115
if(index1 != -1 && index2 != -1)
116         {
117             if(index1 > index2)
118                 return true;
119             else
120                 return false;
121         }
122         else
123         {
124             if(index1 == -1 && index2 != -1)
125                 return true;
126             else if(index2 == -1 && index1 != -1)
127                 return false;
128             else
129             {
130                 int result;
131                 if(sortOrder.equalsIgnoreCase("desc"))
132                 {
133                     if((valueOne != null && !valueOne.toString().equalsIgnoreCase("")) && (valueTwo == null || valueTwo.toString().equalsIgnoreCase("")))
134                         result = -1;
135                     if((valueTwo != null && !valueTwo.toString().equalsIgnoreCase("")) && (valueOne == null || valueOne.toString().equalsIgnoreCase("")))
136                         result = 1;
137                     
138                     result = valueTwo.compareTo(valueOne);
139                 }
140                 else
141                 {
142                     if((valueOne != null && !valueOne.toString().equalsIgnoreCase("")) && (valueTwo == null || valueTwo.toString().equalsIgnoreCase("")))
143                         result = -1;
144                     if((valueTwo != null && !valueTwo.toString().equalsIgnoreCase("")) && (valueOne == null || valueOne.toString().equalsIgnoreCase("")))
145                         result = 1;
146                     
147                     result = valueOne.compareTo(valueTwo);
148                 }
149                 
150                 if(result > 0)
151                     return true;
152                 else
153                     return false;
154                 
155                 /*
156                 if(sortOrder.equalsIgnoreCase("desc"))
157                 {
158                     if(valueTwo.compareTo(valueOne) < 0)
159                         return false;
160                     else
161                         return true;
162                 }
163                 else
164                 {
165                     if(valueTwo.compareTo(valueOne) > 0)
166                         return false;
167                     else
168                         return true;
169                 }
170                 */

171             }
172         }
173     }
174     
175     private Comparable JavaDoc getProperty(Object JavaDoc o, String JavaDoc property)
176     {
177         try
178         {
179             Object JavaDoc propertyObject = PropertyUtils.getProperty(o, sortProperty);
180             if(propertyObject instanceof String JavaDoc)
181                 return (Comparable JavaDoc)propertyObject.toString().toLowerCase();
182             else
183                 return (Comparable JavaDoc)propertyObject;
184         }
185         catch (Exception JavaDoc e)
186         {
187             logger.info(getClass().getName() + " Error finding property " + property, e);
188             return null;
189         }
190     }
191 }
192
Popular Tags