1 21 package net.mlw.vlh; 22 23 import java.io.Serializable ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 40 public class ValueListInfo implements Serializable , PagingInfo 41 { 42 45 private static final long serialVersionUID = 3257572797588191543L; 46 47 48 public static final String VALUE_LIST_NAME = "valueListName"; 49 50 51 public static final Integer DESCENDING = new Integer (-1); 52 53 54 public static final Integer ASCENDING = new Integer (1); 55 56 57 private static final String DESCENDING_STRING = "desc"; 58 59 60 private static final String ASCENDING_STRING = "asc"; 61 62 65 public static final String PAGING_PARAM_PREFIX = "paging"; 66 67 70 public static final String PAGING_PAGE = PAGING_PARAM_PREFIX + "Page"; 71 72 75 public static final String PAGING_NUMBER_PER = PAGING_PARAM_PREFIX + "NumberPer"; 76 77 80 public static final String SORT_PARAM_PREFIX = "sort"; 81 82 85 public static final String SORT_COLUMN = SORT_PARAM_PREFIX + "Column"; 86 87 90 public static final String SORT_DIRECTION = SORT_PARAM_PREFIX + "Direction"; 91 92 95 public static final String DO_FOCUS = "doFocus"; 96 97 100 public static final String FOCUS_PARAM_PREFIX = "focus"; 101 102 105 public static final String FOCUS_VALUE = FOCUS_PARAM_PREFIX + "Value"; 106 107 110 public static final String FOCUS_PROPERTY = FOCUS_PARAM_PREFIX + "Property"; 111 112 116 private int focusedRowNumberInTable = -1; 117 118 119 private byte focusStatus = FOCUS_STATUS_NOT_DEFINED; 120 121 122 public static final byte FOCUS_STATUS_NOT_DEFINED = 0; 123 124 125 public static final byte FOCUS_FOUND = 1; 126 127 128 public static final byte FOCUS_NOT_FOUND = 2; 129 130 131 public static final byte FOCUS_TOO_MANY_ITEMS = 4; 132 133 134 private Map filters = null; 135 136 137 private int totalNumberOfEntries = 0; 138 139 140 142 143 private static final Log LOGGER = LogFactory.getLog(ValueListInfo.class); 144 145 148 public ValueListInfo() 149 { 150 filters = new HashMap (); 151 } 152 153 159 public ValueListInfo(Map parameters) 160 { 161 filters = new HashMap (parameters); 162 String sortColumn = getFilterParameterAsString(SORT_COLUMN); 163 if (sortColumn != null) 164 { 165 setPrimarySortColumn(sortColumn); 166 } 167 168 String direction = getFilterParameterAsString(SORT_DIRECTION); 169 if (direction != null) 170 { 171 setPrimarySortDirection("asc".equals(direction) || "1".equals(direction) ? ASCENDING : DESCENDING); 172 } 173 } 174 175 183 public ValueListInfo(String primaryColumn, Integer primaryDirection) 184 { 185 this(primaryColumn, primaryDirection, new HashMap ()); 186 } 187 188 198 public ValueListInfo(String primaryColumn, Integer primaryDirection, Map filters) 199 { 200 this.filters = (filters == null) ? new HashMap () : filters; 202 setPrimarySortColumn(primaryColumn); 203 setPrimarySortDirection(primaryDirection); 204 } 205 206 211 public Map getFilters() 212 { 213 return filters; 214 } 215 216 221 public String getSortingColumn() 222 { 223 return getFilterParameterAsString(SORT_COLUMN); 224 } 225 226 public void setPrimarySortColumn(String column) 227 { 228 if (column == null) 229 { 230 filters.remove(SORT_COLUMN); 231 } 232 else 233 { 234 filters.put(SORT_COLUMN, column); 235 } 236 237 } 238 239 244 public Integer getSortingDirection() 245 { 246 String direction = getFilterParameterAsString(SORT_DIRECTION); 247 return (("asc".equals(direction) || "1".equals(direction)) ? ValueListInfo.ASCENDING : ValueListInfo.DESCENDING); 248 } 249 250 public void setPrimarySortDirection(Integer direction) 251 { 252 if (direction == null) 253 { 254 filters.remove(SORT_DIRECTION); 255 } 256 else 257 { 258 filters.put(SORT_DIRECTION, ((direction.intValue() > 0) ? "asc" : "desc")); 259 } 260 } 261 262 267 public int getPagingPage() 268 { 269 String page = getFilterParameterAsString(PAGING_PAGE); 270 271 try 272 { 273 return (page == null || page.length() == 0) ? 1 : Integer.parseInt(page); 274 } 275 catch (NumberFormatException e) 276 { 277 return 1; 278 } 279 } 280 281 286 public int getTotalNumberOfPages() 287 { 288 return (((totalNumberOfEntries - 1) / getPagingNumberPer()) + 1); 289 } 290 291 296 public int getTotalNumberOfEntries() 297 { 298 return totalNumberOfEntries; 299 } 300 301 306 public int getPagingNumberPer() 307 { 308 String number = getFilterParameterAsString(PAGING_NUMBER_PER); 309 310 try 311 { 312 int count = (number == null || number.length() == 0) ? Integer.MAX_VALUE : Integer.parseInt(number); 313 if (count > 0) 314 { 315 return count; 316 } 317 else 318 { 319 return Integer.MAX_VALUE; 320 } 321 322 } 323 catch (NumberFormatException e) 324 { 325 return Integer.MAX_VALUE; 326 } 327 } 328 329 335 public void setTotalNumberOfEntries(int totalNumberOfEntries) 336 { 337 this.totalNumberOfEntries = totalNumberOfEntries; 338 } 339 340 346 public void setFilters(Map filters) 347 { 348 this.filters = filters; 349 } 350 351 357 public void setPagingNumberPer(int numberPerPage) 358 { 359 filters.put(PAGING_NUMBER_PER, String.valueOf(numberPerPage)); 360 } 361 362 368 public void setPagingPage(int pageNumber) 369 { 370 filters.put(PAGING_PAGE, String.valueOf(pageNumber)); 371 } 372 373 376 public String getFocusValue() 377 { 378 return getFilterParameterAsString(FOCUS_VALUE); 379 } 380 381 385 public void setFocusValue(String focusValue) 386 { 387 if (focusValue == null) 388 { 389 filters.remove(FOCUS_VALUE); 390 } 391 else 392 { 393 filters.put(FOCUS_VALUE, focusValue); 394 } 395 } 396 397 400 public String getFocusProperty() 401 { 402 return getFilterParameterAsString(FOCUS_PROPERTY); 403 } 404 405 411 public void setFocusProperty(String focusProperty) 412 { 413 if ((focusProperty == null || focusProperty.length() == 0)) 414 { 415 filters.remove(FOCUS_PROPERTY); 416 filters.remove(FOCUS_VALUE); 417 } 418 else 419 { 420 filters.put(FOCUS_PROPERTY, focusProperty); 421 } 422 } 423 424 427 public boolean isFocusEnabled() 428 { 429 return (getFocusProperty() != null); 430 } 431 432 435 public boolean isDoFocus() 436 { 437 String doFocus = getFilterParameterAsString(DO_FOCUS); 438 try 439 { 440 return (doFocus != null) && (doFocus.length() > 0) && Boolean.valueOf(doFocus).booleanValue(); 441 } 442 catch (Exception e) 443 { 444 return false; 445 } 446 } 447 448 453 public boolean isDoFocusAgain() 454 { 455 return isDoFocus() && (getFocusStatus() == FOCUS_FOUND); 456 } 457 458 461 public void setDoFocus(boolean enabled) 462 { 463 filters.put(DO_FOCUS, String.valueOf(enabled)); 464 focusedRowNumberInTable = -1; 465 } 466 467 472 public void setFocusedRowNumberInTable(int absolutPositionInResultSet) 473 { 474 focusedRowNumberInTable = absolutPositionInResultSet % getPagingNumberPer(); 475 } 476 477 480 public int getFocusedRowNumberInTable() 481 { 482 if (getFocusStatus() == FOCUS_FOUND) 483 { 484 return focusedRowNumberInTable; 485 } 486 else 487 { 488 return -1; 489 } 490 } 491 492 493 public void setPagingPageFromRowNumber(int resultSetRowNumber) 494 { 495 setPagingPage(1 + Math.round(resultSetRowNumber / getPagingNumberPer())); 496 } 497 498 501 public void setFocusStatus(byte status) 502 { 503 focusStatus = status; 504 } 505 506 509 public byte getFocusStatus() 510 { 511 return focusStatus; 512 } 513 514 517 public String toString() 518 { 519 return "[filters='" + getFilters() + ", totalNumberOfEntries='" + totalNumberOfEntries + "', focusedRowNumberInTable='" 520 + focusedRowNumberInTable + "',focusStatus=" + focusStatus + "']"; 521 } 522 523 public void resetSorting() 524 { 525 List keys = new ArrayList (filters.keySet()); 526 for (Iterator iter = keys.iterator(); iter.hasNext();) 527 { 528 String key = (String ) iter.next(); 529 if (key.startsWith(SORT_COLUMN) || key.startsWith(SORT_DIRECTION)) 530 { 531 filters.remove(key); 532 } 533 } 534 } 535 536 543 protected String getFilterParameterAsString(String key) 544 { 545 Object parameter = filters.get(key); 546 547 if (parameter != null) 548 { 549 if (parameter instanceof String ) 550 { 551 return (String ) parameter; 552 } 553 else 554 { 555 if (LOGGER.isWarnEnabled()) 556 { 557 LOGGER.warn("Parameters contain the parameter '" + key + "' but it isn't String, as expected. It is a " 558 + parameter.getClass() + " with the value " + parameter, new ClassCastException ()); 559 } 560 } 561 } 562 return null; 563 } 564 } | Popular Tags |