1 package org.infoglue.cms.util.sorters; 2 3 import java.util.Comparator ; 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 16 public class ContentComparator implements Comparator 17 { 18 private final static Logger logger = Logger.getLogger(ContentComparator.class.getName()); 19 20 private String sortProperty; 21 private String sortOrder; 22 private BasicTemplateController basicTemplateController; 23 private Database db; 24 public long extractTime = 0; 25 26 public ContentComparator(String sortProperty, String 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 o1, Object o2) 35 { 36 ContentVO contentVO1 = (ContentVO)o1; 37 ContentVO contentVO2 = (ContentVO)o2; 38 39 Comparable valueOne = (String )contentVO1.getExtraProperties().get(sortProperty); 40 Comparable valueTwo = (String )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 getProperty(Object o, String property) 71 { 72 try 73 { 74 Object propertyObject = PropertyUtils.getProperty(o, sortProperty); 75 if(propertyObject instanceof String ) 76 return (Comparable )propertyObject.toString().toLowerCase(); 77 else 78 return (Comparable )propertyObject; 79 } 80 catch (Exception e) 81 { 82 logger.info(getClass().getName() + " Error finding property " + property, e); 83 return null; 84 } 85 } 86 } 87 | Popular Tags |