1 64 65 package com.jcorporate.expresso.services.controller.dbmaint; 66 67 import com.jcorporate.expresso.core.controller.Block; 68 import com.jcorporate.expresso.core.controller.Controller; 69 import com.jcorporate.expresso.core.controller.ControllerException; 70 import com.jcorporate.expresso.core.controller.ControllerRequest; 71 import com.jcorporate.expresso.core.controller.Output; 72 import com.jcorporate.expresso.core.controller.Transition; 73 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData; 74 import com.jcorporate.expresso.core.dataobjects.DataObject; 75 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 76 import com.jcorporate.expresso.core.db.DBException; 77 import com.jcorporate.expresso.core.dbobj.ValidValue; 78 import com.jcorporate.expresso.core.misc.Format; 79 import com.jcorporate.expresso.core.misc.RecordPaginator; 80 import com.jcorporate.expresso.core.security.User; 81 82 import java.text.DateFormat ; 83 import java.text.SimpleDateFormat ; 84 import java.util.ArrayList ; 85 import java.util.Date ; 86 import java.util.Iterator ; 87 import java.util.Locale ; 88 import java.util.StringTokenizer ; 89 90 91 97 public abstract class ListBase 98 extends DynamicCmd { 99 private long totalRecordCount = 0; 100 101 private RecordPaginator paginator; 102 103 public ListBase() { 104 super(); 105 } 106 107 113 public ListBase(String code, String descrip) { 114 super(code, descrip); 115 } 116 117 125 private synchronized void autoList() 126 throws DBException, ControllerException { 127 DataObject myDBObj = this.getDataObject(); 128 String oneFieldName = null; 129 long recordCount = 0; 130 131 if (myDBObj == null) { 132 throw new DBException("Database object not initialized - cannot list"); 133 } 134 135 DataObjectMetaData metadata = myDBObj.getMetaData(); 136 137 showUserName(metadata.getDescription(this.getControllerRequest().getLocale())); 138 139 Block myTable = new Block("recordList"); 140 myTable.setDescription(metadata.getDescription(this.getControllerRequest().getLocale()) + " (" + 141 User.getLoginFromId(this.getUid(), this.getDataContext()) + ")"); 142 myTable.setAttribute("table", "Y"); 143 addBlock(myTable); 144 145 146 StringBuffer headerString = new StringBuffer (); 147 boolean pipeNeeded = false; 148 149 ArrayList browseArrayList = (ArrayList ) myDBObj.getAttribute("browseArrayList"); 151 boolean hasBrowseArrayList = false; 152 153 if (browseArrayList != null) { 154 hasBrowseArrayList = true; 155 } 156 for (Iterator e = (hasBrowseArrayList) 157 ? browseArrayList.iterator() 158 : myDBObj.getMetaData().getFieldListArray().iterator(); e.hasNext();) { 159 oneFieldName = (String ) e.next(); 160 DataFieldMetaData fieldMetadata = myDBObj.getFieldMetaData(oneFieldName); 161 if (!fieldMetadata.isSecret()) { 162 if (pipeNeeded) { 163 headerString.append("|"); 164 } 165 166 headerString.append(metadata.getDescription(this.getControllerRequest().getLocale(), 167 oneFieldName)); 168 pipeNeeded = true; 169 } 170 } 171 172 173 myTable.setAttribute("header-row", headerString.toString()); 174 175 String sortKey = getSortKey(); 176 recordCount = listRecords(sortKey, myTable); 177 178 if (recordCount == 0) { 179 addOutput(new Output("recordCount", "No records found")); 180 } else { 181 addOutput(new Output("recordCount", 182 "" + totalRecordCount + " records found")); 183 184 addBlock(this.generatePageTransitions(getControllerRequest(), 185 this.getRecordPaginator(), 10)); 186 } 187 } 188 189 196 private DateFormat modifyDateFormat(DateFormat df) { 197 SimpleDateFormat sdf = null; 198 try { 199 sdf = (SimpleDateFormat ) df; 200 } catch (ClassCastException cce) { 201 return df; 202 } 203 String sTemp = sdf.toPattern(); 204 int iLen = sTemp.length(); 205 int i = sTemp.lastIndexOf('y') + 1; 206 sTemp = sTemp.substring(0, i) + 207 "yy" + 208 (i < iLen 209 ? sTemp.substring(i, iLen) 210 : ""); 211 sdf.applyPattern(sTemp); 212 213 return sdf; 214 } 216 225 private String displayDateValue(DataFieldMetaData metaData, Date dt, Locale l) { 226 if (dt == null) { 227 return ""; 228 } 229 if (metaData.isDateOnlyType()) { 230 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, l); 231 df = this.modifyDateFormat(df); 232 return df.format(dt); 233 } else if (metaData.isTimeType()) { 234 DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, l); 235 df = this.modifyDateFormat(df); 236 return df.format(dt); 237 } else { 238 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, 239 DateFormat.SHORT, 240 l); 241 df = this.modifyDateFormat(df); 242 return df.format(dt); 243 244 } 245 246 } 247 248 253 protected RecordPaginator getRecordPaginator() throws ControllerException { 254 if (paginator == null) { 255 paginator = new RecordPaginator(); 256 } 257 paginator.setCountRecords(true); 258 paginator.setPageNumber(getPageNumber()); 259 260 return paginator; 261 } 262 263 272 protected long listRecords(String sortKey, Block myTable) 273 throws DBException, ControllerException { 274 DataObject myDBObj = this.getDataObject(); 275 276 int startShowingRecord = 0; 277 int endShowingRecord = 0; 278 paginator = getRecordPaginator(); 279 setupListSearchCriteria(); 280 281 ArrayList allRecords = paginator.searchAndRetrieve(myDBObj, sortKey); 282 totalRecordCount = paginator.getTotalRecordCount(); 283 284 startShowingRecord = paginator.getStartRecordNumber(); 285 endShowingRecord = paginator.getEndRecordNumber(); 286 if (allRecords.size() > 0) { 287 addRecordsToBlock(allRecords, myTable); 288 } 289 290 291 292 293 showNext = paginator.isMoreRecords(); 295 addOutput(new Output("recordRange", 296 "Records " + startShowingRecord + " to " + 297 endShowingRecord)); 298 299 return totalRecordCount; 300 } 301 302 303 312 protected String getSortKey() 313 throws DBException, ControllerException { 314 315 DataObject dataobj = this.getDataObject(); 316 317 String sortKey = (""); 318 boolean needPipe = false; 319 320 for (Iterator k = dataobj.getMetaData().getKeyFieldListArray().iterator(); k.hasNext();) { 321 if (needPipe) { 322 sortKey = sortKey + "|"; 323 } 324 325 sortKey = sortKey + (String ) k.next(); 326 needPipe = true; 327 } 328 329 return sortKey; 330 } 331 332 341 protected void setupListSearchCriteria() 342 throws DBException, ControllerException { 343 String searchLimits = getSearchParam(); 344 345 if (!searchLimits.equals("")) { 346 DataObject myDBObj = getDataObject(); 347 StringTokenizer stk = new StringTokenizer (searchLimits, "|"); 348 String oneSearchFieldName = null; 349 String oneSearchFieldValue = null; 350 351 while (stk.hasMoreTokens()) { 352 oneSearchFieldName = stk.nextToken(); 353 354 if (!stk.hasMoreTokens()) { 355 throw new DBException("Parameter 'search' must " + 356 "contain name/value pairs seperated with " + 357 "the '|' symbol. It contained '" + 358 searchLimits + "'"); 359 } 360 361 oneSearchFieldValue = stk.nextToken(); 362 myDBObj.set(oneSearchFieldName, oneSearchFieldValue); 363 } 364 } 365 366 } 367 368 380 protected Block generatePageTransitions(ControllerRequest request, RecordPaginator rp, int pageLimit) 381 throws ControllerException { 382 if (rp.isCountRecords() == false) { 383 throw new java.lang.IllegalArgumentException ("You must have countRecords set " + 384 "to true for pagination to work"); 385 } 386 Block returnBlock = new Block(); 387 returnBlock.setName("PageJumpBlock"); 388 389 390 int curPage = rp.getPageNumber(); 391 int endPage = rp.getPageCount(); 392 393 int startPage; 394 int index = 1; 395 if (curPage >= 10) { 396 startPage = curPage - (curPage % 10); 397 } else { 398 startPage = curPage - (curPage % 10) + 1; 399 } 400 if (endPage >= (startPage + 10)) { 401 endPage = curPage + (10 - (curPage % 10)); 402 } 403 404 Class controllerClass = this.getController().getClass(); 405 java.util.Hashtable allParameters = request.getParameters(); 406 allParameters.remove(Controller.CONTROLLER_PARAM_KEY); 407 String state = (String ) allParameters.get(Controller.STATE_PARAM_KEY); 408 allParameters.remove(Controller.STATE_PARAM_KEY); 409 410 if (curPage != 1) { 411 Transition t = new Transition(); 412 t.setParams(allParameters); 413 t.setState(state); 414 t.setControllerObject(controllerClass); 415 t.addParam("page", Integer.toString(curPage - 1)); 416 t.setLabel("< Previous"); 417 t.setName(Integer.toString(index)); 418 index++; 419 returnBlock.add(t); 420 } 421 422 423 for (int i = startPage; i <= endPage; i++) { 424 Transition t = new Transition(); 425 t.setParams(allParameters); 426 t.setControllerObject(controllerClass); 427 t.addParam("page", Integer.toString(i)); 428 t.setLabel(Integer.toString(i)); 429 t.setState(state); 430 if (curPage == i) { 431 t.setAttribute("currentPage", "true"); 432 Output o = new Output(); 433 o.setName("CurrentPage"); 434 o.setContent(Integer.toString(curPage)); 435 returnBlock.add(o); 436 } 437 t.setName(Integer.toString(index)); 438 index++; 439 440 returnBlock.add(t); 441 } 442 443 if (curPage < endPage) { 444 Transition t = new Transition(); 445 t.setParams(allParameters); 446 t.setControllerObject(controllerClass); 447 t.addParam("page", Integer.toString(curPage + 1)); 448 t.setState(state); 449 t.setLabel("Next >"); 450 t.setName(Integer.toString(index)); 451 index++; 452 returnBlock.add(t); 453 } 454 455 addOutput(new Output("pageCount", 456 Integer.toString(this.getRecordPaginator().getPageCount()) + " Pages")); 457 return returnBlock; 458 } 459 460 461 471 protected void addRecordsToBlock(ArrayList i_AllRecords, Block myTable) 472 throws DBException, ControllerException { 473 DataObject myDBObj = this.getDataObject(); 474 475 int recordCount = 0; 476 String oneFieldName = null; 477 String oneFieldValue = null; 478 DataObject oneRecord = null; 479 String controller = getControllerName(); 480 Iterator i = i_AllRecords.iterator(); 481 482 ArrayList browseArrayList = (ArrayList ) myDBObj.getAttribute("browseArrayList"); 484 boolean hasBrowseArrayList = false; 485 486 if (browseArrayList != null) { 487 hasBrowseArrayList = true; 488 } 489 while (i.hasNext()) { 490 oneRecord = (DataObject) i.next(); 491 recordCount++; 492 493 Block oneRow = new Block("" + recordCount); 494 oneRow.setAttribute("row", "Y"); 495 myTable.add(oneRow); 496 497 498 for (Iterator e2 = (hasBrowseArrayList) 499 ? browseArrayList.iterator() 500 : myDBObj.getMetaData().getFieldListArray().iterator(); 501 e2.hasNext();) { 502 oneFieldName = (String ) e2.next(); 503 DataFieldMetaData metaData = myDBObj.getFieldMetaData(oneFieldName); 504 505 if (!metaData.isSecret()) { 506 oneFieldValue = oneRecord.getField(oneFieldName); 507 508 if (oneFieldValue == null) { 509 oneFieldValue = ""; 510 } 511 512 Output oneCell = new Output(); 513 oneCell.setName(oneFieldName); 514 oneRow.addNested(oneCell); 515 516 if (metaData.isKey()) { 517 showEditLink(oneFieldName, oneFieldValue, oneRecord, 518 oneCell, controller); 519 } else { 520 if (oneFieldValue.equalsIgnoreCase("")) { 521 oneCell.setContent(oneFieldValue); 522 } else if (myDBObj.getFieldMetaData(oneFieldName).getTypeString().equalsIgnoreCase("money")) { 523 try { 524 oneCell.setContent(new Format("%-10.2f").form(new Double (oneFieldValue).doubleValue())); 525 } catch (NumberFormatException ne) { 526 throw new DBException("Number for " + 527 "field '" + 528 oneFieldName + 529 "' not in a valid numeric format:" + 530 oneFieldValue + ":" + 531 ne.getMessage()); 532 } 533 } else { 534 if (metaData.isMultiValued()) { 535 java.util.List values = myDBObj.getValidValuesList(oneFieldName); 536 537 if (values == null) { 538 throw new DBException("Valid values for field '" + 539 oneFieldName + "' from object " + 540 ((Object ) myDBObj).getClass().getName() + 541 " were null"); 542 } 543 544 String fieldValue = null; 545 ValidValue oneVV = null; 546 547 for (Iterator ve = values.iterator(); 548 ve.hasNext();) { 549 oneVV = (ValidValue) ve.next(); 550 551 if (oneVV.getValue().equals(oneFieldValue)) { 552 fieldValue = oneVV.getDescription(); 553 } 554 } 555 if (fieldValue == null) { 556 oneCell.setContent(oneFieldValue); 557 } else { 558 oneCell.setContent(fieldValue); 559 } 560 } else { 561 if (metaData.isDateType()) { 562 java.util.Date dt = oneRecord.getDataField(oneFieldName).asDate(); 563 Locale l = this.getResponse().getLocale(); 564 oneCell.setContent(displayDateValue(metaData, dt, l)); 565 } else if (metaData.isCharacterLongObjectType()) { 566 oneCell.setContent(oneFieldValue + ""); 567 } else { 568 oneCell.setContent(oneFieldValue); 569 } 570 } 571 } 572 } 573 574 } else { 575 576 if (isKeyField(oneFieldName)) { 578 oneFieldName = (String ) e2.next(); 579 oneFieldValue = oneRecord.getField(oneFieldName); 580 581 if (oneFieldValue == null) { 582 oneFieldValue = ""; 583 } 584 585 Output oneCell = new Output(); 586 oneCell.setName(oneFieldName); 587 oneRow.addNested(oneCell); 588 showEditLink(oneFieldName, oneFieldValue, oneRecord, 589 oneCell, controller); 590 } 591 } 592 } 593 594 } 595 596 } 597 598 605 protected void showList() 606 throws DBException, ControllerException { 607 autoList(); 608 showOptions(); 609 } 610 611 612 } 613 614 | Popular Tags |