1 17 package org.alfresco.web.data; 18 19 import java.lang.reflect.Method ; 20 import java.text.CollationKey ; 21 import java.text.Collator ; 22 import java.util.ArrayList ; 23 import java.util.Comparator ; 24 import java.util.Date ; 25 import java.util.List ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 29 import org.apache.log4j.Logger; 30 31 39 public abstract class Sort 40 { 41 44 52 public Sort(List data, String column, boolean bForward, String mode) 53 { 54 this.data = data; 55 this.column = column; 56 this.bForward = bForward; 57 this.sortMode = mode; 58 59 if (this.data.size() != 0) 60 { 61 Collator collator = Collator.getInstance(Locale.getDefault()); 63 64 if (mode.equals(IDataContainer.SORT_CASEINSENSITIVE)) 66 { 67 collator.setStrength(Collator.SECONDARY); 68 } 69 else 70 { 71 collator.setStrength(Collator.IDENTICAL); 72 } 73 74 this.keys = buildCollationKeys(collator); 75 } 76 } 77 78 79 82 85 public abstract void sort(); 86 87 88 91 97 protected List buildCollationKeys(Collator collator) 98 { 99 List data = this.data; 100 int iSize = data.size(); 101 List keys = new ArrayList (iSize); 102 103 try 104 { 105 String methodName = getGetterMethodName(this.column); 107 Class returnType = null;; 108 Method getter = null; 109 Object bean = this.data.get(0); 111 try 112 { 113 getter = bean.getClass().getMethod(methodName, (Class [])null); 114 returnType = getter.getReturnType(); 115 } 116 catch (NoSuchMethodException nsmerr) 117 { 118 if (bean instanceof Map ) 120 { 121 Object obj = ((Map )bean).get(this.column); 122 if (obj != null) 123 { 124 returnType = obj.getClass(); 125 } 126 else 127 { 128 if (s_logger.isInfoEnabled()) 129 { 130 s_logger.info("Unable to get return type class for RichList column: " + column + 131 ". Suggest set java type directly in sort component tag."); 132 } 133 returnType = Object .class; 134 } 135 } 136 else 137 { 138 throw new IllegalStateException ("Unable to find bean getter or Map impl for column name: " + this.column); 139 } 140 } 141 142 boolean bknownType = true; 146 if (returnType.equals(String .class)) 147 { 148 if (strongStringCompare == true) 149 { 150 this.comparator = new StringComparator(); 151 } 152 else 153 { 154 this.comparator = new SimpleStringComparator(); 155 } 156 } 157 else if (returnType.equals(Date .class)) 158 { 159 this.comparator = new DateComparator(); 160 } 161 else if (returnType.equals(boolean.class) || returnType.equals(Boolean .class)) 162 { 163 this.comparator = new BooleanComparator(); 164 } 165 else if (returnType.equals(int.class) || returnType.equals(Integer .class)) 166 { 167 this.comparator = new IntegerComparator(); 168 } 169 else if (returnType.equals(long.class) || returnType.equals(Long .class)) 170 { 171 this.comparator = new LongComparator(); 172 } 173 else if (returnType.equals(float.class) || returnType.equals(Float .class)) 174 { 175 this.comparator = new FloatComparator(); 176 } 177 else 178 { 179 s_logger.warn("Unsupported sort data type: " + returnType + " defaulting to .toString()"); 180 this.comparator = new SimpleComparator(); 181 bknownType = false; 182 } 183 184 for (int iIndex=0; iIndex<iSize; iIndex++) 186 { 187 Object obj; 188 if (getter != null) 189 { 190 obj = getter.invoke(data.get(iIndex), (Object [])null); 192 } 193 else 194 { 195 obj = ((Map )data.get(iIndex)).get(column); 197 } 198 199 if (obj instanceof String ) 200 { 201 String str = (String )obj; 202 if (strongStringCompare == true) 203 { 204 if (str.indexOf(' ') != -1) 205 { 206 int iLength = str.length(); 208 StringBuilder s = new StringBuilder (iLength + 4); 209 char c; 210 for (int i=0; i<iLength; i++) 211 { 212 c = str.charAt(i); 213 if (c != ' ') 214 { 215 s.append(c); 216 } 217 else 218 { 219 s.append('\'').append(c).append('\''); 220 } 221 } 222 str = s.toString(); 223 } 224 keys.add(collator.getCollationKey(str)); 225 } 226 else 227 { 228 keys.add(str); 229 } 230 } 231 else if (bknownType == true) 232 { 233 keys.add(obj); 237 } 238 else 239 { 240 if (obj != null) 241 { 242 keys.add(obj.toString()); 243 } 244 else 245 { 246 keys.add(null); 247 } 248 } 249 } 250 } 251 catch (Exception err) 252 { 253 throw new RuntimeException (err); 254 } 255 256 return keys; 257 } 258 259 263 protected void swap(final List v, final int a, final int b) 264 { 265 Object temp = v.get(a); 266 v.set(a, v.get(b)); 267 v.set(b, temp); 268 } 269 270 275 protected Comparator getComparator() 276 { 277 return this.comparator; 278 } 279 280 287 protected static String getGetterMethodName(String name) 288 { 289 return "get" + name.substring(0, 1).toUpperCase() + 290 name.substring(1, name.length()); 291 } 292 293 294 297 private class SimpleComparator implements Comparator 298 { 299 302 public int compare(final Object obj1, final Object obj2) 303 { 304 if (obj1 == null && obj2 == null) return 0; 305 if (obj1 == null) return -1; 306 if (obj2 == null) return 1; 307 return (obj1.toString()).compareTo(obj2.toString()); 308 } 309 } 310 311 private class SimpleStringComparator implements Comparator 312 { 313 316 public int compare(final Object obj1, final Object obj2) 317 { 318 if (obj1 == null && obj2 == null) return 0; 319 if (obj1 == null) return -1; 320 if (obj2 == null) return 1; 321 return ((String )obj1).compareToIgnoreCase((String )obj2); 322 } 323 } 324 325 private class StringComparator implements Comparator 326 { 327 330 public int compare(final Object obj1, final Object obj2) 331 { 332 if (obj1 == null && obj2 == null) return 0; 333 if (obj1 == null) return -1; 334 if (obj2 == null) return 1; 335 return ((CollationKey )obj1).compareTo((CollationKey )obj2); 336 } 337 } 338 339 private class IntegerComparator implements Comparator 340 { 341 344 public int compare(final Object obj1, final Object obj2) 345 { 346 if (obj1 == null && obj2 == null) return 0; 347 if (obj1 == null) return -1; 348 if (obj2 == null) return 1; 349 return ((Integer )obj1).compareTo((Integer )obj2); 350 } 351 } 352 353 private class FloatComparator implements Comparator 354 { 355 358 public int compare(final Object obj1, final Object obj2) 359 { 360 if (obj1 == null && obj2 == null) return 0; 361 if (obj1 == null) return -1; 362 if (obj2 == null) return 1; 363 return ((Float )obj1).compareTo((Float )obj2); 364 } 365 } 366 367 private class LongComparator implements Comparator 368 { 369 372 public int compare(final Object obj1, final Object obj2) 373 { 374 if (obj1 == null && obj2 == null) return 0; 375 if (obj1 == null) return -1; 376 if (obj2 == null) return 1; 377 return ((Long )obj1).compareTo((Long )obj2); 378 } 379 } 380 381 private class BooleanComparator implements Comparator 382 { 383 386 public int compare(final Object obj1, final Object obj2) 387 { 388 if (obj1 == null && obj2 == null) return 0; 389 if (obj1 == null) return -1; 390 if (obj2 == null) return 1; 391 return ((Boolean )obj1).equals((Boolean )obj2) ? -1 : 1; 392 } 393 } 394 395 private class DateComparator implements Comparator 396 { 397 400 public int compare(final Object obj1, final Object obj2) 401 { 402 if (obj1 == null && obj2 == null) return 0; 403 if (obj1 == null) return -1; 404 if (obj2 == null) return 1; 405 return ((Date )obj1).compareTo((Date )obj2); 406 } 407 } 408 409 410 413 414 protected List data; 415 416 417 protected String column; 418 419 420 protected boolean bForward; 421 422 423 protected String sortMode; 424 425 426 protected Collator collator; 427 428 429 protected List keys = null; 430 431 432 private Comparator comparator = null; 433 434 436 private boolean strongStringCompare = false; 437 438 private static Logger s_logger = Logger.getLogger(IDataContainer.class); 439 440 } | Popular Tags |