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.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 16 public class SiteNodeComparator implements Comparator 17 { 18 private final static Logger logger = Logger.getLogger(SiteNodeComparator.class.getName()); 19 20 private String sortProperty; 21 private String sortOrder; 22 private TemplateController templateController; 23 24 public SiteNodeComparator(String sortProperty, String sortOrder, TemplateController templateController) 25 { 26 this.sortProperty = sortProperty; 27 this.sortOrder = sortOrder; 28 this.templateController = templateController; 29 } 30 31 public int compare(Object o1, Object o2) 32 { 33 SiteNodeVO siteNodeVO1 = (SiteNodeVO)o1; 34 SiteNodeVO siteNodeVO2 = (SiteNodeVO)o2; 35 36 Comparable valueOne = (String )siteNodeVO1.getExtraProperties().get(sortProperty); 37 Comparable valueTwo = (String )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 meta1Id = this.templateController.getMetaInformationContentId(siteNodeVO1.getId()); 50 Integer 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 getProperty(Object o, String property) 68 { 69 try 70 { 71 Object propertyObject = PropertyUtils.getProperty(o, sortProperty); 72 if(propertyObject instanceof String ) 73 return (Comparable )propertyObject.toString().toLowerCase(); 74 else 75 return (Comparable )propertyObject; 76 } 77 catch (Exception e) 78 { 79 logger.info(getClass().getName() + " Error finding property " + property, e); 80 return null; 81 } 82 } 83 } 84 | Popular Tags |