KickJava   Java API By Example, From Geeks To Geeks.

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


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.exolab.castor.jdo.Database;
8 import org.infoglue.cms.entities.content.ContentVO;
9 import org.infoglue.deliver.controllers.kernel.impl.simple.BasicTemplateController;
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 ContentComparator implements Comparator JavaDoc
17 {
18     private final static Logger logger = Logger.getLogger(ContentComparator.class.getName());
19
20     private String JavaDoc sortProperty;
21     private String JavaDoc sortOrder;
22     private BasicTemplateController basicTemplateController;
23     private Database db;
24     public long extractTime = 0;
25
26     public ContentComparator(String JavaDoc sortProperty, String JavaDoc sortOrder, BasicTemplateController basicTemplateController)
27     {
28         this.sortProperty = sortProperty;
29         this.sortOrder = sortOrder;
30         this.basicTemplateController = basicTemplateController;
31         extractTime = 0;
32     }
33
34     public int compare(Object JavaDoc o1, Object JavaDoc o2)
35     {
36         ContentVO contentVO1 = (ContentVO)o1;
37         ContentVO contentVO2 = (ContentVO)o2;
38     
39         Comparable JavaDoc valueOne = (String JavaDoc)contentVO1.getExtraProperties().get(sortProperty);
40         Comparable JavaDoc valueTwo = (String JavaDoc)contentVO2.getExtraProperties().get(sortProperty);
41         
42         long previousTime = System.currentTimeMillis();
43
44         if(valueOne == null)
45         {
46             valueOne = getProperty(o1, sortProperty);
47             valueTwo = getProperty(o2, sortProperty);
48         }
49
50         if(valueOne == null && this.basicTemplateController != null)
51         {
52             valueOne = this.basicTemplateController.getContentAttribute(contentVO1.getId(), this.basicTemplateController.getLanguageId(), sortProperty);
53             valueTwo = this.basicTemplateController.getContentAttribute(contentVO2.getId(), this.basicTemplateController.getLanguageId(), sortProperty);
54         }
55         
56         int result;
57         
58         if(sortOrder.equalsIgnoreCase("desc"))
59             result = valueTwo.compareTo(valueOne);
60         else
61             result = valueOne.compareTo(valueTwo);
62
63         long elapsedTime = System.currentTimeMillis() - previousTime;
64
65         extractTime = extractTime + elapsedTime;
66
67         return result;
68     }
69     
70     private Comparable JavaDoc getProperty(Object JavaDoc o, String JavaDoc property)
71     {
72         try
73         {
74             Object JavaDoc propertyObject = PropertyUtils.getProperty(o, sortProperty);
75             if(propertyObject instanceof String JavaDoc)
76                 return (Comparable JavaDoc)propertyObject.toString().toLowerCase();
77             else
78                 return (Comparable JavaDoc)propertyObject;
79         }
80         catch (Exception JavaDoc e)
81         {
82             logger.info(getClass().getName() + " Error finding property " + property, e);
83             return null;
84         }
85     }
86 }
87
Popular Tags