1 64 65 package com.jcorporate.expresso.core.servlet.viewhandler; 66 67 import com.jcorporate.expresso.core.controller.Block; 68 import com.jcorporate.expresso.core.controller.ControllerElement; 69 import com.jcorporate.expresso.core.controller.ControllerException; 70 import com.jcorporate.expresso.core.controller.ControllerResponse; 71 import com.jcorporate.expresso.core.controller.ErrorCollection; 72 import com.jcorporate.expresso.core.controller.Input; 73 import com.jcorporate.expresso.core.controller.Output; 74 import com.jcorporate.expresso.core.controller.State; 75 import com.jcorporate.expresso.core.controller.Transition; 76 import com.jcorporate.expresso.core.db.DBException; 77 import com.jcorporate.expresso.core.dbobj.ValidValue; 78 import com.jcorporate.expresso.core.jsdkapi.GenericSession; 79 import com.jcorporate.expresso.core.misc.ConfigManager; 80 import com.jcorporate.expresso.core.misc.ConfigurationException; 81 import com.jcorporate.expresso.core.misc.StringUtil; 82 import com.jcorporate.expresso.services.dbobj.Setup; 83 import com.jcorporate.expresso.services.html.Break; 84 import com.jcorporate.expresso.services.html.Button; 85 import com.jcorporate.expresso.services.html.Cell; 86 import com.jcorporate.expresso.services.html.CheckBox; 87 import com.jcorporate.expresso.services.html.DropDown; 88 import com.jcorporate.expresso.services.html.FileInput; 89 import com.jcorporate.expresso.services.html.Form; 90 import com.jcorporate.expresso.services.html.Heading; 91 import com.jcorporate.expresso.services.html.HiddenField; 92 import com.jcorporate.expresso.services.html.HtmlElement; 93 import com.jcorporate.expresso.services.html.HtmlException; 94 import com.jcorporate.expresso.services.html.Page; 95 import com.jcorporate.expresso.services.html.Paragraph; 96 import com.jcorporate.expresso.services.html.Radio; 97 import com.jcorporate.expresso.services.html.Row; 98 import com.jcorporate.expresso.services.html.Table; 99 import com.jcorporate.expresso.services.html.Text; 100 import com.jcorporate.expresso.services.html.TextArea; 101 import com.jcorporate.expresso.services.html.TextField; 102 import org.apache.log4j.Logger; 103 import org.apache.struts.action.ActionMessage; 104 105 import javax.servlet.ServletException ; 106 import javax.servlet.http.HttpServletRequest ; 107 import javax.servlet.http.HttpServletResponse ; 108 import java.io.IOException ; 109 import java.util.Enumeration ; 110 import java.util.Hashtable ; 111 import java.util.Iterator ; 112 import java.util.Vector ; 113 114 115 120 public class DefaultViewHandler 121 extends ViewHandler { 122 private static Logger log = Logger.getLogger(DefaultViewHandler.class); 123 124 125 public DefaultViewHandler() { 126 } 127 128 138 public void handleView(ControllerResponse con, HttpServletRequest req, 139 HttpServletResponse res) 140 throws ServletException , IOException , ControllerException { 141 try { 142 if (log.isDebugEnabled()) { 143 log.debug("Begin processing default view"); 144 } 145 146 Page myPage = new Page(con.getTitle()); 147 Paragraph p1 = new Paragraph(new Text(con.getTitle())); 148 p1.setCSSClass("jc-pageheader"); 149 myPage.add(p1); 150 151 State currState = con.getCurrentState(); 152 myPage.add(new Heading(currState.getDescription(), 4)); 153 154 Vector blocks = con.getBlocks(); 155 Vector inputs = con.getInputs(); 156 Vector outputs = con.getOutputs(); 157 Vector transitions = con.getTransitions(); 158 ErrorCollection ee = con.getErrors(); 159 160 if (ee != null) { 161 if (!ee.isEmpty()) { 162 displayErrors(myPage, ee); 163 } 164 } 165 if ((blocks == null) && (inputs == null) && (outputs == null) && 166 (transitions == null)) { 167 if (log.isDebugEnabled()) { 168 log.debug("No results to display"); 169 } 170 throw new ServletException ("No input, output or transitions " + 171 "defined for this Controller"); 172 } 173 if (outputs != null) { 174 Table outputTable = new Table("Output Table"); 175 outputTable.setAlignment("center"); 176 177 outputTable.setBorder(0); 179 add(con, outputs, outputTable); 180 181 if (outputTable.getContentCount() > 0) { 182 myPage.add(outputTable); 183 } 184 } 185 186 187 Form f = new Form("Input and transitions"); 188 f.setCSSClass("jc-default"); 189 String encoding = null; 190 f.setAction(Setup.getValueRequired(con.getDBName(), "ContextPath") + 191 con.getRequestPath()); 192 193 194 195 196 encoding = StringUtil.notNull(con.getAttribute("formEncoding")); 197 198 if (!encoding.equals("")) { 199 f.setEncoding(encoding); 200 } 201 if (blocks != null) { 202 for (Enumeration eb = blocks.elements(); eb.hasMoreElements();) { 203 addBlock(con, f, (Block) eb.nextElement()); 204 } 205 } 206 207 if (inputs != null) { 208 209 210 211 212 213 214 Input oneInput = null; 215 216 for (Enumeration ei = inputs.elements(); ei.hasMoreElements();) { 217 oneInput = (Input) ei.nextElement(); 218 219 String s = oneInput.getType(); 220 221 if (s != null && s.equals("file")) { 222 if (encoding.equals("")) { 223 f.setEncoding("multipart/form-data"); 224 } 225 } 226 } 227 228 Table inputTable = new Table("Input Table"); 229 add(con, inputs, inputTable); 230 231 if (inputTable.getContentCount() > 0) { 232 f.add(inputTable); 233 } 234 } 235 236 if (transitions != null) { 237 Table transitionTable = new Table("Transitions Table"); 238 Row transitionRow = new Row(); 239 transitionTable.add(transitionRow); 240 addTransitions(transitions, transitionRow, f, con); 241 242 if (transitionRow.getContentCount() > 0) { 243 f.add(transitionTable); 244 } 245 } 246 247 if (f.getContentCount() > 0) { 248 myPage.add(f); 249 } 250 251 Object o = GenericSession.getAttribute(req, 252 com.jcorporate.expresso.ext.taglib.Back.BACK_KEY + 253 ".URL"); 254 Object ot = GenericSession.getAttribute(req, 255 com.jcorporate.expresso.ext.taglib.Back.BACK_KEY + 256 ".TITLE"); 257 258 if (o != null) { 259 String backUrl = (String ) o; 260 String title = (String ) ot; 261 String backImage = ConfigManager.getContext("default").getImages() + 262 "/FingerLeft.gif"; 263 264 if (!backUrl.equals("NONE")) { 265 myPage.add(new Text("<p align=\"center\"><a HREF=\"" + backUrl + 266 "\" title=\"" + title + 267 "\"><img SRC=\"" + backImage + 268 "\" border=\"0\"></a></p>")); 269 } 270 } 271 272 printDisplay(con, req, res, myPage); 273 if (log.isDebugEnabled()) { 274 log.debug("Finished displaying default view"); 275 } 276 } catch (NullPointerException npe) { 277 npe.printStackTrace(); 278 log.warn("NullPointerException:", npe); 279 throw npe; 280 } catch (HtmlException he) { 281 log.warn("HtmlException:", he); 282 throw new ControllerException(he); 283 } catch (DBException de) { 284 log.warn("DBException:", de); 285 throw new ControllerException(de); 286 } catch (ConfigurationException ce) { 287 throw new ControllerException(ce); 288 } 289 } 290 291 292 302 protected void printDisplay(ControllerResponse con, 303 HttpServletRequest request, 304 HttpServletResponse response, Page p) 305 throws HtmlException, ControllerException { 306 p.display(request, response, con.getString("charset")); 307 } 308 309 316 private void displayErrors(Page myPage, ErrorCollection errs) 317 throws HtmlException { 318 Heading errorHeading = new Heading("Errors", 3); 319 errorHeading.setFontColor("red"); 320 myPage.add(errorHeading); 321 322 Table errorTable = new Table("errors"); 323 errorTable.setBorder(1); 324 325 Row oneRow = null; 326 327 for (Iterator i = errs.get(); i.hasNext();) { 328 ActionMessage oneError = (ActionMessage) i.next(); 329 oneRow = new Row("one error"); 330 331 Text t = new Text(oneError.getKey()); 332 t.setFontColor("red"); 333 oneRow.add(new Cell(t)); 334 errorTable.add(oneRow); 335 } 336 337 myPage.add(errorTable); 338 myPage.add(new Break()); 339 } 340 341 352 private void addTransitions(Vector h, Row r, HtmlElement f, 353 ControllerResponse res) 354 throws HtmlException { 355 Transition a = null; 356 Cell oneCell = null; 357 Button oneButton = null; 358 String paramString = null; 359 Hashtable params = null; 360 HiddenField hidden = null; 361 362 363 for (Enumeration e = h.elements(); e.hasMoreElements();) { 364 a = (Transition) e.nextElement(); 365 366 if (a != null) { 367 params = a.getParams(); 368 369 if (params != null) { 370 paramString = a.getParamString(); 371 hidden = new HiddenField(a.getName() + "_params", 372 paramString); 373 } 374 if (f != null) { 375 f.add(hidden); 376 } else { 377 r.add(hidden); 378 } 379 } 380 } 381 382 for (Enumeration e = h.elements(); e.hasMoreElements();) { 383 a = (Transition) e.nextElement(); 384 385 if (a != null) { 386 oneCell = new Cell(); 387 oneCell.setAlignment("center"); 388 oneCell.setVerticalAlignment("center"); 389 390 String labelString = null; 391 392 try { 393 labelString = res.getString(a.getLabel()); 394 } catch (Exception ee) { 395 labelString = a.getLabel(); 396 } 397 398 oneButton = new Button("button_" + a.getName(), labelString); 399 oneCell.add(oneButton); 400 r.add(oneCell); 401 } 402 } 403 } 404 405 406 416 private void addInput(Input i, Row oneRow, ControllerResponse con) 417 throws HtmlException, ConfigurationException, 418 ControllerException { 419 String l = i.getLabel(); 420 Cell oneCell; 421 422 if (l != null && l.length() != 0) { 423 oneCell = new Cell(); 424 oneCell.add(new Text(l + ":")); 425 oneCell.setCSSClass("jc-label"); 426 oneRow.add(oneCell); 427 } 428 429 oneCell = new Cell(); 430 oneCell.setCSSClass("jc-formfield"); 431 oneRow.add(oneCell); 432 433 if (StringUtil.notNull(i.getAttribute("readOnly")).equalsIgnoreCase("Y")) { 434 oneCell.add(new Text(i.getDefaultValue())); 435 436 return; 437 } 438 439 String defaultValue = i.getDefaultValue(); 440 441 if (defaultValue == null) { 442 defaultValue = (""); 443 } 444 445 446 Vector validValues = i.getValidValues(); 447 ValidValue oneValue = null; 448 449 if (validValues != null && !validValues.isEmpty()) { 450 if (i.getAttributes().containsKey("radio") || 451 i.getAttributes().containsKey("radio-vertical")) { 452 Radio radio = new Radio(i.getName(), defaultValue); 453 454 for (Enumeration vv = validValues.elements(); 455 vv.hasMoreElements();) { 456 ValidValue v = (ValidValue) vv.nextElement(); 457 radio.addOption(v.getValue(), v.getDescription()); 458 } 459 if (i.getAttributes().containsKey("radio-vertical")) { 460 radio.setVertical(true); 461 } 462 463 oneCell.add(radio); 464 } else if (i.getAttributes().containsKey("checkbox") || 465 i.getAttributes().containsKey("checkbox-vertical")) { 466 CheckBox check = new CheckBox(i.getName(), defaultValue); 467 468 for (Enumeration vv = validValues.elements(); 469 vv.hasMoreElements();) { 470 ValidValue v = (ValidValue) vv.nextElement(); 471 check.addOption(v.getValue(), v.getDescription()); 472 } 473 474 oneCell.add(check); 475 476 if (i.getAttributes().containsKey("checkbox-vertical")) { 477 check.setVertical(true); 478 } 479 } else { 480 DropDown d = new DropDown(i.getName()); 481 482 for (Enumeration vv = validValues.elements(); 483 vv.hasMoreElements();) { 484 oneValue = (ValidValue) vv.nextElement(); 485 d.addOption(oneValue.getValue(), oneValue.getDescription()); 486 } 487 488 d.setCurrentValue(defaultValue); 489 oneCell.add(d); 490 } 491 } else if (i.getAttributes().containsKey("checkbox")) { 492 493 494 if (defaultValue.equalsIgnoreCase("Y")) { 495 oneCell.add(new CheckBox(i.getName(), "Y", true)); 496 } else { 497 oneCell.add(new CheckBox(i.getName(), "Y", false)); 498 } 499 500 oneCell.setAlignment("center"); 501 } else if (i.getAttributes().containsKey("textarea")) { 502 oneCell.add(new TextArea(i.getName(), 503 StringUtil.notNull(defaultValue).trim(), 504 i.getLines(), i.getDisplayLength())); 505 } else if (i.getAttributes().containsKey("password")) { 506 oneCell.add(new TextField(i.getName(), defaultValue, 507 i.getMaxLength(), i.getDisplayLength())); 508 } else if (i.getAttributes().containsKey("submit")) { 509 oneCell.add(new Button(i.getName(), i.getName())); 510 } else if (i.getAttributes().containsKey("reset")) { 511 } else if (i.getAttributes().containsKey("file")) { 512 oneCell.add(new FileInput(i.getName(), defaultValue)); 513 } else if (i.getAttributes().containsKey("hidden")) { 514 oneCell.add(new HiddenField(i.getName(), defaultValue)); 515 } else { 516 517 518 if (i.getLines() > 1) { 519 520 521 oneCell.add(new TextArea(i.getName(), defaultValue, 522 i.getLines(), i.getDisplayLength())); 523 } else { 524 oneCell.add(new TextField(i.getName(), defaultValue, 525 i.getMaxLength(), 526 i.getDisplayLength())); 527 } 528 } 529 if (i.getNested() != null) { 530 Vector v = i.getNested(); 531 ControllerElement oneElement = null; 532 533 for (Enumeration ve = v.elements(); ve.hasMoreElements();) { 534 oneElement = (ControllerElement) ve.nextElement(); 535 536 if (oneElement instanceof Input) { 537 addInput((Input) oneElement, oneRow, con); 538 } else if (oneElement instanceof Output) { 539 addOutput((Output) oneElement, oneRow, con); 540 } else if (oneElement instanceof Transition) { 541 Transition nestedTransition = (Transition) oneElement; 542 String newText = "<a HREF=\"" + 543 ConfigManager.getContextPath() + 544 con.getRequestPath() + "?" + 545 nestedTransition.getParamString() + 546 "\" target=\"_new\"><img SRC=\"" + 547 ConfigManager.getContext(con.getDBName()).getImages() + 548 "/search.png\" border=\"0\"></a>"; 549 Cell lookupCell = new Cell(new Text(newText)); 550 oneRow.add(lookupCell); 551 } 552 } 553 554 } 555 556 557 } 561 562 private void addInput(Input i, Table t, ControllerResponse con) 563 throws HtmlException, ConfigurationException, 564 ControllerException { 565 Row oneRow = new Row(); 566 t.add(oneRow); 567 addInput(i, oneRow, con); 568 } 569 570 571 private void addOutput(Output o, Row oneRow, ControllerResponse con) 572 throws HtmlException { 573 Cell oneCell = null; 574 String l = o.getLabel(); 575 576 if (l != null && l.length() > 0) { 577 oneCell = new Cell(); 578 oneCell.setCSSClass("jc-label"); 579 580 if (o.getStyle() != null) { 581 oneCell.add(new Text(l, o.getStyle())); 582 } else { 583 oneCell.add(new Text(l)); 584 } 585 if (oneRow.isCaptionRow()) { 586 oneCell.setCSSClass("jc-tabletitle"); 587 oneCell.setHeaderCell(true); 588 } else { 589 oneCell.setNoWrap(false); 590 } 591 592 oneRow.add(oneCell); 593 } 594 595 boolean anyNested = false; 596 Vector v = o.getNested(); 597 598 if (v != null) { 599 if (v.size() > 0) { 600 anyNested = true; 601 } 602 } 603 604 Transition nestedTransition = null; 605 ControllerElement ce = null; 606 607 if (anyNested) { 608 for (Enumeration tn = v.elements(); tn.hasMoreElements();) { 609 ce = (ControllerElement) tn.nextElement(); 610 611 if (ce instanceof Transition) { 612 nestedTransition = (Transition) ce; 613 } 614 } 615 } 616 617 String c = o.getContent(); 618 619 if (c != null) { 620 oneCell = new Cell(); 621 622 if (oneRow.isCaptionRow()) { 623 oneCell.setCSSClass("jc-tabletitle"); 624 oneCell.setHeaderCell(true); 625 } else { 626 oneCell.setCSSClass("jc-default"); 627 } 628 if (nestedTransition == null) { 629 oneCell.add(new Text(c)); 630 } else { 631 String newText = "<a HREF=\"" + 632 ConfigManager.getContextPath() + 633 con.getRequestPath() + "?" + 634 nestedTransition.getParamString() + "\">" + c + 635 "</a>"; 636 oneCell.add(new Text(newText)); 637 } 638 639 oneRow.add(oneCell); 640 } 641 if (anyNested) { 642 Cell c2 = new Cell(); 643 c2.setNoWrap(false); 644 oneRow.add(c2); 645 646 Table t2 = new Table("Nested Output"); 647 t2.setBorder(1); 648 c2.add(t2); 649 650 for (Enumeration ve = v.elements(); ve.hasMoreElements();) { 651 ce = (ControllerElement) ve.nextElement(); 652 653 if (ce instanceof Output) { 654 addOutput((Output) ce, t2, con); 655 } else if (ce instanceof Transition) { 656 nestedTransition = (Transition) ce; 657 } 658 } 659 } 660 661 } 662 663 671 private void addOutput(Output o, Table t, ControllerResponse con) 672 throws HtmlException { 673 Row oneRow = new Row(); 674 t.add(oneRow); 675 addOutput(o, oneRow, con); 676 } 677 678 679 686 private void add(ControllerResponse con, Vector v, Table t) 687 throws HtmlException, ConfigurationException, 688 ControllerException { 689 for (Enumeration e = v.elements(); e.hasMoreElements();) { 690 ControllerElement o = (ControllerElement) e.nextElement(); 691 692 if (o instanceof Output) { 693 addOutput((Output) o, t, con); 694 } else if (o instanceof Input) { 695 addInput((Input) o, t, con); 696 697 } 700 } 701 702 } 703 704 705 private void addBlockAsTable(Block b, HtmlElement myPage, 706 ControllerResponse con) 707 throws HtmlException, ConfigurationException, 708 ControllerException { 709 Table blockTable = new Table(); 710 blockTable.setBorder(1); 711 myPage.add(blockTable); 712 713 if (b.getAttributes().containsKey("header-row")) { 714 Row oneRow = new Row(); 715 oneRow.setTitle(b.getAttribute("header-row")); 716 blockTable.add(oneRow); 717 } 718 719 Block oneBlockRow = null; 720 ControllerElement ce = null; 721 722 for (Enumeration erows = b.getNested().elements(); 723 erows.hasMoreElements();) { 724 ce = (ControllerElement) erows.nextElement(); 725 726 if (ce instanceof Block) { 727 oneBlockRow = (Block) ce; 728 } else { 729 throw new HtmlException("Block '" + b.getName() + 730 "' is specified as a table, but tables must " + 731 "contain only blocks (one per row) - this block contained a '" + 732 ce.getClass().getName() + "'"); 733 } 734 if ("Y".equals(oneBlockRow.getAttribute("row"))) { 735 ControllerElement oneCell = null; 736 Row oneRow = new Row(); 737 blockTable.add(oneRow); 738 739 for (Enumeration rowContents = oneBlockRow.getNested().elements(); 740 rowContents.hasMoreElements();) { 741 oneCell = (ControllerElement) rowContents.nextElement(); 742 743 if (oneCell instanceof Output) { 744 addOutput((Output) oneCell, oneRow, con); 745 } else if (oneCell instanceof Input) { 746 addInput((Input) oneCell, oneRow, con); 747 } 748 } 749 } else { 750 throw new HtmlException("Block '" + b.getName() + 751 "' is specified as a table, but tables must " + 752 "contain only blocks designed as rows. This block contained a row '" + 753 oneBlockRow.getName() + 754 "' that was not specified as a row."); 755 } 756 } 757 } 758 759 760 768 private void addBlock(ControllerResponse con, HtmlElement container, 769 Block thisBlock) 770 throws HtmlException, ConfigurationException, 771 ControllerException { 772 if ("Y".equals(thisBlock.getAttribute("table"))) { 773 addBlockAsTable(thisBlock, container, con); 774 } else { 775 if (thisBlock.getNested() != null) { 776 Vector transitions = new Vector (); 777 Vector v = thisBlock.getNested(); 778 779 if (v.size() > 0) { 780 Table myBlockTable = new Table("Table for Block " + 781 thisBlock.getName()); 782 container.add(myBlockTable); 783 784 for (Enumeration ve = v.elements(); ve.hasMoreElements();) { 785 ControllerElement ce = (ControllerElement) ve.nextElement(); 786 787 if (ce instanceof Output) { 788 addOutput((Output) ce, myBlockTable, con); 789 } else if (ce instanceof Input) { 790 addInput((Input) ce, myBlockTable, con); 791 } else if (ce instanceof Transition) { 792 transitions.addElement(ce); 793 } else if (ce instanceof Block) { 794 addBlock(con, container, (Block) ce); 795 } 796 } 797 if (transitions.size() > 0) { 798 Row r = new Row("transitions for block"); 799 myBlockTable.add(r); 800 addTransitions(transitions, r, null, con); 801 } 802 } 803 804 } 805 806 } 807 808 } 809 810 811 } | Popular Tags |