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.deliver.applications.databeans.WebPage; 8 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController; 9 10 15 public class HardcodedPageComparator implements Comparator 16 { 17 private final static Logger logger = Logger.getLogger(HardcodedPageComparator.class.getName()); 18 19 private String sortProperty; 20 private String sortOrder; 21 private boolean numberOrder; 22 private String nameProperty; 23 private String namesInOrderString; 24 25 private TemplateController templateController; 26 27 public HardcodedPageComparator(String sortProperty, String sortOrder, boolean numberOrder, String nameProperty, String 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 o1, Object o2) 38 { 39 41 Comparable valueOne = getProperty(o1, sortProperty); 42 Comparable valueTwo = getProperty(o2, sortProperty); 43 44 Comparable valueOneName = getProperty(o1, nameProperty); 45 Comparable valueTwoName = getProperty(o2, nameProperty); 46 47 if(valueOne == null) 48 { 49 WebPage webPage1 = (WebPage)o1; 50 WebPage webPage2 = (WebPage)o2; 51 52 Integer meta1Id = webPage1.getMetaInfoContentId(); Integer meta2Id = webPage2.getMetaInfoContentId(); 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 )new Long (valueOne.toString()); 70 else 71 { 72 if(sortOrder.equalsIgnoreCase("desc")) 73 valueOne = (Comparable )new Long (Long.MIN_VALUE); 74 else 75 valueOne = (Comparable )new Long (Long.MAX_VALUE); 76 } 77 } 78 catch(Exception e) 79 { 80 logger.info("Not a number..." + e.getMessage()); 81 } 82 83 try 84 { 85 if(valueTwo != null && !valueTwo.equals("")) 86 valueTwo = (Comparable )new Long (valueTwo.toString()); 87 else 88 { 89 if(sortOrder.equalsIgnoreCase("desc")) 90 valueTwo = (Comparable )new Long (Long.MIN_VALUE); 91 else 92 valueTwo = (Comparable )new Long (Long.MAX_VALUE); 93 } 94 } 95 catch(Exception 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 valueOne, Comparable valueTwo, Comparable valueOneName, Comparable valueTwoName) 109 { 110 int index1 = namesInOrderString.indexOf(valueOneName.toString()); 111 int index2 = namesInOrderString.indexOf(valueTwoName.toString()); 112 113 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 171 } 172 } 173 } 174 175 private Comparable getProperty(Object o, String property) 176 { 177 try 178 { 179 Object propertyObject = PropertyUtils.getProperty(o, sortProperty); 180 if(propertyObject instanceof String ) 181 return (Comparable )propertyObject.toString().toLowerCase(); 182 else 183 return (Comparable )propertyObject; 184 } 185 catch (Exception e) 186 { 187 logger.info(getClass().getName() + " Error finding property " + property, e); 188 return null; 189 } 190 } 191 } 192 | Popular Tags |