1 64 package com.jcorporate.expresso.core.controller; 65 66 import com.jcorporate.expresso.core.controller.session.SimplePersistentSession; 67 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 68 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCObjectMetaData; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.dbobj.DBField; 71 import com.jcorporate.expresso.core.dbobj.DBObject; 72 import com.jcorporate.expresso.core.i18n.Messages; 73 import com.jcorporate.expresso.core.misc.StringUtil; 74 import com.jcorporate.expresso.kernel.util.ClassLocator; 75 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 76 import org.apache.log4j.Logger; 77 import org.apache.struts.Globals; 78 import org.apache.struts.action.ActionError; 79 import org.w3c.dom.NamedNodeMap ; 80 import org.w3c.dom.Node ; 81 import org.w3c.dom.NodeList ; 82 83 import javax.servlet.http.HttpServletRequest ; 84 import java.io.Serializable ; 85 import java.util.Enumeration ; 86 import java.util.HashMap ; 87 import java.util.Hashtable ; 88 import java.util.Iterator ; 89 import java.util.Locale ; 90 import java.util.Map ; 91 import java.util.Stack ; 92 import java.util.Vector ; 93 94 95 104 public class ControllerResponse implements Serializable { 105 108 private static final transient Logger log = Logger.getLogger(ControllerResponse.class); 109 110 113 protected ControllerRequest myRequest = null; 114 115 116 119 protected Locale responseLocale = null; 120 121 124 protected HashMap blockCache = null; 125 126 129 protected HashMap inputCache = null; 130 131 134 protected HashMap outputCache = null; 135 136 139 protected HashMap transitionCache = null; 140 141 144 protected State currentState = null; 145 146 149 protected String myControllerClass = null; 150 151 154 protected String dataContext = "default"; 155 156 159 protected String myRequestPath = null; 160 161 164 protected String requestedState = null; 165 166 169 protected String style = null; 170 171 174 protected String title = null; 175 176 179 protected Vector blockCacheOrdered = null; 180 181 184 protected Vector inputCacheOrdered = null; 185 186 189 protected Vector outputCacheOrdered = null; 190 191 194 protected Vector transitionCacheOrdered = null; 195 196 197 205 protected boolean customResponse = false; 206 207 212 private java.util.Stack schemaStack; 213 214 215 218 public ControllerResponse() { 219 super(); 220 } 221 222 229 public void setAttribute(String attrib, String val) 230 throws ControllerException { 231 getRequest().setAttrib(attrib, val); 232 } 233 234 235 236 243 public String getAttribute(String attrib) throws ControllerException { 244 return (String ) getRequest().getAttrib(attrib); 245 } 246 247 248 249 255 public void setAttributes(Map attributes) throws ControllerException { 256 getRequest().setAttributes(attributes); 257 } 258 259 260 261 267 public Map getAttributes() throws ControllerException { 268 return getRequest().getAttributes(); 269 } 270 271 272 273 280 public Block getBlock(String blockName) throws ControllerException { 281 validState(); 282 283 if (blockCache != null) { 284 return (Block) blockCache.get(blockName); 285 } 286 287 return null; 288 } 289 290 291 292 297 public final Vector getBlocks() { 298 validState(); 299 300 if (blockCacheOrdered != null) { 301 return blockCacheOrdered; 302 } 303 304 return null; 305 } 306 307 308 309 314 public void setControllerClass(String newClass) { 315 myControllerClass = newClass; 316 } 317 318 323 public String getControllerClass() { 324 return myControllerClass; 325 } 326 327 333 public State getCurrentState() { 334 return currentState; 335 } 336 337 343 public final Input getInput(String inputName) { 344 validState(); 345 346 if (inputCache != null) { 347 return (Input) inputCache.get(inputName); 348 } 349 350 return null; 351 } 352 353 354 355 361 public final Vector getInputs() { 362 validState(); 363 364 if (inputCacheOrdered != null) { 365 return inputCacheOrdered; 366 } 367 368 return new Vector (); 369 } 370 371 372 373 386 public void setCustomResponse(boolean newCustom) { 387 customResponse = newCustom; 388 } 389 390 396 public boolean isCustomResponse() { 397 return customResponse; 398 } 399 400 406 public synchronized void setDBName(String newDBName) 407 throws ControllerException { 408 this.dataContext = newDBName; 409 } 410 411 417 public String getDBName() throws ControllerException { 418 return getDataContext(); 419 } 420 421 427 public String getDataContext() throws ControllerException { 428 if (dataContext != null) { 429 return dataContext; 430 } 431 432 return "default"; 433 } 434 435 442 public void setFormCache() throws ControllerException { 443 getDefaultForm().saveForm(getRequest()); 444 } 445 446 447 448 456 public void setFormCache(String fieldName, String fieldValue) 457 throws ControllerException { 458 getDefaultForm().setField(fieldName, fieldValue); 459 } 460 461 462 463 470 public Hashtable getFormCache() throws ControllerException { 471 return getDefaultForm().getFields(); 472 } 473 474 475 476 485 public String getFormCache(String fieldName) throws ControllerException { 486 return StringUtil.notNull(getDefaultForm().getField(fieldName)); 487 } 488 489 490 491 498 public void setFormCacheAttribute(String fieldName, Object fieldAttribute) 499 throws ControllerException { 500 getDefaultForm().setAttribute(fieldName, fieldAttribute); 501 } 502 503 510 public Object getFormCacheAttribute(String fieldName) 511 throws ControllerException { 512 return getDefaultForm().getAttribute(fieldName); 513 } 514 515 521 public Locale getLocale() throws ControllerException { 522 if (responseLocale == null) { 523 return Locale.getDefault(); 524 } 525 526 return responseLocale; 527 } 528 529 536 public Map getNamedInputs() throws ControllerException { 537 validState(); 538 539 return inputCache; 540 } 541 542 549 public Output getOutput(String outputName) throws ControllerException { 550 validState(); 551 552 if (outputCache != null) { 553 return (Output) outputCache.get(outputName); 554 } 555 556 return null; 557 } 558 559 560 561 568 public String getOutputContent(String outputName) 569 throws ControllerException { 570 validState(); 571 572 Output o = getOutput(outputName); 573 574 if (o == null) { 575 return ("No Such Output named '" + outputName + "'"); 576 } 577 578 return o.getContent(); 579 } 580 581 582 583 590 public Output getOutputRequired(String outputName) 591 throws ControllerException { 592 Output o = getOutput(outputName); 593 594 if (o == null) { 595 throw new ControllerException("There is no output named '" + 596 outputName + "'"); 597 } 598 599 return o; 600 } 601 602 603 604 610 public final Vector getOutputs() { 611 validState(); 612 613 if (outputCacheOrdered != null) { 614 return outputCacheOrdered; 615 } 616 617 return new Vector (); 618 } 619 620 621 622 628 public ErrorCollection getErrors() throws ControllerException { 629 ErrorCollection errs = (ErrorCollection) getRequest().getSession() 630 .getAttribute(Globals.ERROR_KEY); 631 632 return errs; 633 } 634 635 642 public String getParameter(String paramName) throws ControllerException { 643 return getRequest().getParameter(paramName); 644 } 645 646 654 public void setRequest(ControllerRequest newRequest) 655 throws ControllerException { 656 if (newRequest == null) { 657 throw new ControllerException("You cannot set a null Controller Request here"); 658 } 659 660 myRequest = newRequest; 661 requestedState = StringUtil.notNull(myRequest.getParameter(Controller.STATE_PARAM_KEY)); 662 this.dataContext = newRequest.getDataContext(); 663 this.responseLocale = newRequest.getLocale(); 664 } 665 666 673 public ControllerRequest getRequest() { 674 if (myRequest == null) { 675 throw new IllegalStateException ("ControllerResponse.setRequest() must be set before calling getRequest()"); 676 } 677 return myRequest; 678 } 679 680 681 686 public synchronized void setRequestPath(String newPath) { 687 myRequestPath = newPath + ".do"; 688 } 689 690 697 public String getRequestPath() { 698 return myRequestPath; 699 } 700 701 706 public String getRequestedState() { 707 return requestedState; 708 } 709 710 716 public String getSchema() { 717 if ((schemaStack == null) || schemaStack.isEmpty()) { 718 return "com.jcorporate.expresso.core.ExpressoSchema"; 719 } else { 720 return (String ) schemaStack.peek(); 721 } 722 } 723 724 725 726 731 public void setSchemaStack(java.util.Stack schemaStack) { 732 this.schemaStack = schemaStack; 733 } 734 735 741 public java.util.Stack getSchemaStack() { 742 if (schemaStack == null) { 743 schemaStack = new Stack (); 744 } 745 746 if (schemaStack.isEmpty()) { 747 schemaStack.push(com.jcorporate.expresso.core.ExpressoSchema.class.getName()); 748 } 749 750 return schemaStack; 751 } 752 753 763 public String getString(String stringCode, Object [] args) 764 throws ControllerException { 765 StringUtil.assertNotBlank(stringCode, "You must specify a string key"); 766 767 if (myRequest == null) { 768 log.error("No request object was established for this controller response, " + 769 " for controller class '" + 770 StringUtil.notNull(myControllerClass) + "'"); 771 772 return null; 773 } 774 775 Stack s = this.getSchemaStack(); 776 try { 777 return Messages.getString(s, this.responseLocale, stringCode, args); 778 } catch (IllegalArgumentException ex) { 779 throw new IllegalArgumentException ("Unable to locate string " + stringCode + " for any schema"); 780 } 781 } 782 783 784 791 public String getString(String stringCode) throws ControllerException { 792 Object [] args = {}; 793 794 return getString(stringCode, args); 795 } 796 797 798 799 807 public String getString(String stringCode, String string1) 808 throws ControllerException { 809 Object [] args = {string1}; 810 811 return getString(stringCode, args); 812 } 813 814 823 public String getString(String stringCode, String string1, String string2) 824 throws ControllerException { 825 Object [] args = {string1, string2}; 826 827 return getString(stringCode, args); 828 } 829 830 840 public String getString(String stringCode, String string1, String string2, 841 String string3) throws ControllerException { 842 Object [] args = {string1, string2, string3}; 843 844 return getString(stringCode, args); 845 } 846 847 858 public String getString(String stringCode, String string1, String string2, 859 String string3, String string4) throws ControllerException { 860 Object [] args = {string1, string2, string3, string4}; 861 862 return getString(stringCode, args); 863 } 864 865 872 public void setStyle(String newStyle) { 873 style = newStyle; 874 } 875 876 881 public String getStyle() { 882 return style; 883 } 884 885 890 public void setTitle(String newTitle) { 891 title = newTitle; 892 } 893 894 902 public static ControllerResponse fromXML(Node n) throws ControllerException { 903 if (n.getNodeName().equals("#document")) { 905 return fromXML(n.getChildNodes().item(0)); 906 } 907 908 if (!n.getNodeName().equals("controller-response")) { 909 throw new ControllerException("Failed To Get DOM Node of " + 910 " type 'controller-response' Got " + n.getNodeName() + 911 " instead."); 912 } 913 914 ControllerResponse cr = new ControllerResponse(); 915 916 SimplePersistentSession session = new SimplePersistentSession(); 922 ControllerRequest request = new ControllerRequest(); 923 request.setUid(3); 924 request.setSession(session); 925 cr.setRequest(request); 926 927 NamedNodeMap attributes = n.getAttributes(); 931 Node oneAttribute = attributes.getNamedItem("name"); 932 933 if (oneAttribute != null) { 934 cr.setControllerClass(oneAttribute.getNodeValue()); 935 } 936 937 oneAttribute = attributes.getNamedItem("title"); 938 939 if (oneAttribute != null) { 940 cr.setTitle(oneAttribute.getNodeValue()); 941 } 942 943 oneAttribute = attributes.getNamedItem("requestedState"); 944 945 if (oneAttribute != null) { 946 cr.requestedState = oneAttribute.getNodeValue(); 947 } 948 949 oneAttribute = attributes.getNamedItem("style"); 950 951 if (oneAttribute != null) { 952 cr.setStyle(oneAttribute.getNodeValue()); 953 } 954 955 try { 956 NodeList children = n.getChildNodes(); 957 958 for (int index = 0; index < children.getLength(); index++) { 959 Node oneChild = children.item(index); 960 String nodeName = oneChild.getNodeName(); 961 962 if (nodeName != null) { 963 if (nodeName.equals("block")) { 964 Block b = (Block) Block.fromXML(oneChild); 965 966 if (b != null) { 967 cr.addBlock(b); 968 } 969 } else if (nodeName.equals("input")) { 970 Input i = (Input) Input.fromXML(oneChild); 971 972 if (i != null) { 973 cr.addInput(i); 974 } 975 } else if (nodeName.equals("output")) { 976 Output o = (Output) Output.fromXML(oneChild); 977 978 if (o != null) { 979 cr.addOutput(o); 980 } 981 } else if (nodeName.equals("transition")) { 982 Transition t = (Transition) Transition.fromXML(oneChild); 983 984 if (t != null) { 985 cr.addTransition(t); 986 } 987 } 988 } 989 990 991 } 992 993 994 } catch (ClassCastException cce) { 995 cce.printStackTrace(); 996 throw new ControllerException("Error Loading Subobjects " + 997 "for ControllerResponse: " + cce.getMessage()); 998 } 999 1000 return cr; 1001 } 1002 1003 1009 public String getName() { 1010 return getTitle(); 1011 } 1012 1013 1019 public String getTitle() { 1020 try { 1021 String temp = getString(title); 1022 1023 if (temp == null) { 1024 temp = title; 1025 } 1026 1027 return temp; 1028 } catch (Exception e) { 1029 return title; 1030 } 1031 } 1032 1033 1040 public Transition getTransition(String transitionName) 1041 throws ControllerException { 1042 validState(); 1043 1044 if (transitionCache != null) { 1045 return (Transition) transitionCache.get(transitionName); 1046 } 1047 1048 return null; 1049 } 1050 1051 1052 1053 1062 public final Vector getTransitions() { 1063 validState(); 1064 1065 if (transitionCacheOrdered != null) { 1066 return transitionCacheOrdered; 1067 } 1068 1069 return null; 1070 } 1071 1072 1073 1074 1081 public Map getNamedBlocks() throws ControllerException { 1082 validState(); 1083 1084 return blockCache; 1085 } 1086 1087 1094 public Map getNamedOutputs() throws ControllerException { 1095 validState(); 1096 1097 return outputCache; 1098 } 1099 1100 1107 public Map getNamedTransitions() throws ControllerException { 1108 validState(); 1109 1110 return transitionCache; 1111 } 1112 1113 1119 public synchronized void setUser(String newUser) throws ControllerException { 1120 getRequest().setUser(newUser); 1121 } 1122 1123 1129 public String getUser() throws ControllerException { 1130 return getRequest().getUser(); 1131 } 1132 1133 1134 1135 1143 public void add(ControllerElement t) throws ControllerException { 1144 if (t instanceof Input) { 1145 addInput((Input) t); 1146 } else if (t instanceof Output) { 1147 addOutput((Output) t); 1148 } else if (t instanceof Transition) { 1149 addTransition((Transition) t); 1150 } else if (t instanceof Block) { 1151 addBlock((Block) t); 1152 } else { 1153 throw new ControllerException("Element " + t.getName() + 1154 " is not a Block, Input, Output or Transition"); 1155 } 1156 } 1157 1158 1159 1160 1173 public void addAutoInput(String fieldName, String friendlyName, 1174 String defaultValue, int displayLength, int maxLength, 1175 Vector validValues) throws ControllerException { 1176 addAutoInput(fieldName, friendlyName, defaultValue, displayLength, 1177 maxLength, validValues, "text"); 1178 } 1179 1180 1181 1182 1193 public void addAutoInput(String fieldName, DBObject dbobj, 1194 String defaultValue) throws ControllerException { 1195 int displayLength = 0; 1196 int maxLength = 0; 1197 String friendlyName = null; 1198 Vector validValues = null; 1199 JDBCObjectMetaData metadata = dbobj.getJDBCMetaData(); 1200 1201 try { 1202 if (!metadata.isField(fieldName)) { 1203 throw new ControllerException("The field name specified is not valid for this DBObject."); 1204 } 1205 1206 friendlyName = metadata.getDescription(fieldName); 1207 maxLength = Integer.parseInt(metadata.getLength(fieldName)); 1208 displayLength = maxLength; 1209 1210 if (displayLength > 40) { 1211 displayLength = 40; 1212 } 1213 1214 if (metadata.isMultiValued(fieldName)) { 1215 validValues = dbobj.getValidValues(fieldName); 1216 } 1217 1218 if (defaultValue == null) { 1219 defaultValue = dbobj.getField(fieldName); 1220 } 1221 1222 addAutoInput(fieldName, friendlyName, defaultValue, displayLength, 1223 maxLength, validValues, metadata.getType(fieldName)); 1224 } catch (DBException dbe) { 1225 throw new ControllerException("There was a problem accessing the " + 1226 "DBObject for autoInput generation", dbe); 1227 } 1228 } 1229 1230 1231 1232 1243 public void addAutoInput(String fieldName, String dbClassName, 1244 String defaultValue) throws ControllerException { 1245 DBObject dbobj = null; 1246 int displayLength = 0; 1247 int maxLength = 0; 1248 String friendlyName = null; 1249 Vector validValues = null; 1250 1251 try { 1252 try { 1253 Class clazz = ClassLocator.loadClass(dbClassName); 1254 dbobj = (DBObject) clazz.newInstance(); 1255 } catch (Exception e) { 1256 throw new ControllerException("The dbClass specified could not be loaded."); 1257 } 1258 1259 DataObjectMetaData metadata = dbobj.getMetaData(); 1260 1261 if (!metadata.isField(fieldName)) { 1262 throw new ControllerException("The field name specified is not valid for this DBObject."); 1263 } 1264 1265 friendlyName = metadata.getDescription(fieldName); 1266 maxLength = Integer.parseInt(metadata.getLength(fieldName)); 1267 displayLength = maxLength; 1268 1269 if (displayLength > 40) { 1270 displayLength = 40; 1271 } 1272 1273 if (metadata.isMultiValued(fieldName)) { 1274 validValues = dbobj.getValidValues(fieldName); 1275 } 1276 1277 if (defaultValue == null) { 1278 defaultValue = dbobj.getField(fieldName); 1279 } 1280 1281 addAutoInput(fieldName, friendlyName, defaultValue, displayLength, 1282 maxLength, validValues, metadata.getType(fieldName)); 1283 } catch (DBException dbe) { 1284 throw new ControllerException("There was a problem accessing the " + 1285 "DBObject for autoInput generation", dbe); 1286 } 1287 } 1288 1289 1290 1291 1299 public void addAutoInput(DBObject myDBObj) throws ControllerException { 1300 try { 1301 String oneFieldName = null; 1302 DataObjectMetaData metadata = myDBObj.getMetaData(); 1303 String className = myDBObj.getClass().getName(); 1304 1305 for (Iterator e = metadata.getFieldListArray().iterator(); 1306 e.hasNext();) { 1307 oneFieldName = (String ) e.next(); 1308 1309 if (!metadata.isSecret(oneFieldName)) { 1310 addAutoInput(oneFieldName, 1311 metadata.getDescription(oneFieldName), className, 1312 myDBObj.getField(oneFieldName)); 1313 } 1314 } 1315 1316 1317 } catch (DBException de) { 1318 throw new ControllerException(de); 1319 } 1320 } 1321 1322 1323 1324 1336 public void addAutoInput(String fieldName, String friendlyName, 1337 String dbClassName, String defaultValue) throws ControllerException { 1338 DBObject dbobj = null; 1339 int displayLength = 0; 1340 int maxLength = 0; 1341 Vector validValues = null; 1342 1343 try { 1344 try { 1345 Class clazz = ClassLocator.loadClass(dbClassName); 1346 dbobj = (DBObject) clazz.newInstance(); 1347 } catch (Exception e) { 1348 throw new ControllerException("The dbClass specified could not be loaded."); 1349 } 1350 1351 DataObjectMetaData metadata = dbobj.getMetaData(); 1352 1353 if (!metadata.isField(fieldName)) { 1354 throw new ControllerException("The field name specified is not valid for this DBObject."); 1355 } 1356 1357 maxLength = Integer.parseInt(metadata.getLength(fieldName)); 1358 displayLength = maxLength; 1359 1360 if (displayLength > 40) { 1361 displayLength = 40; 1362 } 1363 1364 if (metadata.isMultiValued(fieldName)) { 1365 validValues = dbobj.getValidValues(fieldName); 1366 } 1367 1368 if (defaultValue == null) { 1369 defaultValue = dbobj.getField(fieldName); 1370 } 1371 1372 this.addAutoInput(fieldName, friendlyName, defaultValue, 1373 displayLength, maxLength, validValues, 1374 metadata.getType(fieldName)); 1375 } catch (DBException dbe) { 1376 throw new ControllerException("There was a problem accessing the " + 1377 "DBObject for autoInput generation: " + dbe.getMessage()); 1378 } 1379 } 1380 1381 1382 1383 1398 public void addAutoInput(String fieldName, String friendlyName, 1399 String defaultValue, int displayLength, int maxLength, 1400 Vector validValues, String fieldType) throws ControllerException { 1401 Input oneInput = new Input(); 1402 1403 if ((displayLength == 0) || (maxLength == 0)) { 1404 if (fieldType.equalsIgnoreCase(DBField.DATETIME_TYPE)) { 1405 displayLength = maxLength = 19; 1406 } 1407 } 1408 1409 oneInput.setName(fieldName); 1410 oneInput.setLabel(friendlyName); 1411 oneInput.setDisplayLength(displayLength); 1412 oneInput.setDefaultValue(StringUtil.notNull(defaultValue)); 1413 oneInput.setMaxLength(maxLength); 1414 1415 boolean expandToTextArea = false; 1420 1421 if (fieldType.equalsIgnoreCase(DBField.VARCHAR_TYPE)) { 1422 if (displayLength > 150) { 1423 expandToTextArea = true; 1424 } 1425 } 1426 1427 if (validValues != null) { 1428 oneInput.setValidValues(validValues); 1429 oneInput.setType("LISTBOX"); 1430 } else { 1431 if (fieldType.equalsIgnoreCase("text") || expandToTextArea) { 1432 oneInput.setType("TEXTAREA"); 1433 } else if (fieldType.equalsIgnoreCase(DBField.VARCHAR_TYPE)) { 1434 oneInput.setType("TEXTFIELD"); 1435 } else if (fieldType.equalsIgnoreCase("date")) { 1436 oneInput.setType("TEXTFIELD"); 1437 oneInput.setDisplayLength(10); 1438 oneInput.setMaxLength(10); 1439 } else if (fieldType.equalsIgnoreCase("time")) { 1440 oneInput.setType("TEXTFIELD"); 1441 oneInput.setDisplayLength(10); 1442 oneInput.setMaxLength(10); 1443 } else if (fieldType.equalsIgnoreCase(DBField.DATETIME_TYPE)) { 1444 oneInput.setType("TEXTFIELD"); 1445 oneInput.setDisplayLength(21); 1446 oneInput.setMaxLength(21); 1447 } else { 1448 oneInput.setType("TEXTFIELD"); 1449 } 1450 } 1451 1452 addInput(oneInput); 1453 } 1454 1455 1456 1457 1472 public void addAutoInputRenameField(String fieldName, String renameFieldTo, 1473 DBObject dbobj, String defaultValue) throws ControllerException { 1474 int displayLength = 0; 1478 int maxLength = 0; 1479 String friendlyName = null; 1480 Vector validValues = null; 1481 JDBCObjectMetaData metadata = dbobj.getJDBCMetaData(); 1482 1483 try { 1484 if (!metadata.isField(fieldName)) { 1485 throw new ControllerException("The field name specified is not valid for this DBObject."); 1486 } 1487 1488 friendlyName = metadata.getDescription(fieldName); 1489 maxLength = Integer.parseInt(metadata.getLength(fieldName)); 1490 displayLength = maxLength; 1491 1492 if (displayLength > 40) { 1493 displayLength = 40; 1494 } 1495 1496 if (metadata.isMultiValued(fieldName)) { 1497 validValues = dbobj.getValidValues(fieldName); 1498 } 1499 1500 if (defaultValue == null) { 1501 defaultValue = dbobj.getField(fieldName); 1502 } 1503 1504 addAutoInput(renameFieldTo, friendlyName, defaultValue, 1505 displayLength, maxLength, validValues, 1506 metadata.getType(fieldName)); 1507 } catch (DBException dbe) { 1508 throw new ControllerException("There was a problem accessing the " + 1509 "DBObject for autoInput generation", dbe); 1510 } 1511 } 1512 1513 1514 1515 1522 public void addBlock(Block newBlock) throws ControllerException { 1523 if (blockCache == null) { 1524 blockCache = new HashMap (); 1525 } 1526 1527 if (StringUtil.notNull(newBlock.getName()).equals("")) { 1528 int nextNum = blockCache.size() + 1; 1529 newBlock.setName("" + nextNum); 1530 } 1531 1532 newBlock.setControllerResponse(this); 1533 1534 synchronized (blockCache) { 1535 blockCache.put(newBlock.getName(), newBlock); 1536 } 1537 1538 if (blockCacheOrdered == null) { 1539 blockCacheOrdered = new Vector (); 1540 } 1541 1542 blockCacheOrdered.addElement(newBlock); 1543 } 1544 1545 1546 1547 1550 public void addError(String errorMessage) throws ControllerException { 1551 ErrorCollection errs = getErrors(); 1552 1553 if (errs == null) { 1554 errs = new ErrorCollection(); 1555 } 1556 1557 errs.addError(errorMessage); 1558 saveErrors(errs); 1559 } 1560 1561 1568 public void addError(String errorMessage, Object arg) throws ControllerException { 1569 ErrorCollection errs = getErrors(); 1570 1571 if (errs == null) { 1572 errs = new ErrorCollection(); 1573 } 1574 1575 errs.addError(errorMessage, arg); 1576 saveErrors(errs); 1577 } 1578 1579 1580 1587 public void addError(String errorMessage, Object [] args) throws ControllerException { 1588 ErrorCollection errs = getErrors(); 1589 1590 if (errs == null) { 1591 errs = new ErrorCollection(); 1592 } 1593 1594 errs.addError(errorMessage, args); 1595 saveErrors(errs); 1596 } 1597 1598 1603 public void addError(String propName, String errorMessage) 1604 throws ControllerException { 1605 ErrorCollection errs = getErrors(); 1606 1607 if (errs == null) { 1608 errs = new ErrorCollection(); 1609 } 1610 1611 errs.addError(propName, errorMessage); 1612 saveErrors(errs); 1613 } 1614 1615 1621 public void addInput(Input newInput) throws ControllerException { 1622 if (inputCache == null) { 1623 inputCache = new HashMap (); 1624 } 1625 1626 newInput.setControllerResponse(this); 1627 1628 if (StringUtil.notNull(newInput.getName()).equals("")) { 1629 int nextNum = inputCache.size() + 1; 1630 newInput.setName("i" + nextNum); 1631 } 1632 1633 synchronized (inputCache) { 1634 inputCache.put(newInput.getName(), newInput); 1635 } 1636 1637 if (inputCacheOrdered == null) { 1638 inputCacheOrdered = new Vector (); 1639 } 1640 1641 inputCacheOrdered.addElement(newInput); 1642 } 1643 1644 1645 1646 1652 public void addOutput(Output newOutput) throws ControllerException { 1653 if (outputCache == null) { 1654 outputCache = new HashMap (); 1655 } 1656 1657 newOutput.setControllerResponse(this); 1658 1659 if (StringUtil.notNull(newOutput.getName()).equals("")) { 1660 int nextNum = outputCache.size() + 1; 1661 newOutput.setName("o" + nextNum); 1662 } 1663 1664 synchronized (outputCache) { 1665 outputCache.put(newOutput.getName(), newOutput); 1666 } 1667 1668 if (outputCacheOrdered == null) { 1669 outputCacheOrdered = new Vector (); 1670 } 1671 1672 outputCacheOrdered.addElement(newOutput); 1673 } 1674 1675 1676 1677 1683 public void addTransition(Transition newTransition) 1684 throws ControllerException { 1685 if (transitionCache == null) { 1686 transitionCache = new HashMap (); 1687 } 1688 1689 if (StringUtil.notNull(newTransition.getName()).equals("")) { 1690 1691 int nextNum = transitionCache.size() + 1; 1692 newTransition.setName("t" + nextNum); 1693 } 1694 1695 synchronized (transitionCache) { 1696 transitionCache.put(newTransition.getName(), newTransition); 1697 } 1698 1699 newTransition.setControllerResponse(this); 1700 1701 if (transitionCacheOrdered == null) { 1702 transitionCacheOrdered = new Vector (); 1703 } 1704 1705 transitionCacheOrdered.addElement(newTransition); 1706 } 1707 1708 1709 1710 1719 public void autoValidate(String checkClassName, 1720 ErrorCollection errorCollection) 1721 throws ValidationException, ControllerException { 1722 String paramName = null; 1723 ValidationItem vi = null; 1724 StringUtil.assertNotBlank(checkClassName, 1725 "The checkClassName cannot be null"); 1726 1727 ValidationSet validSet = new ValidationSet(); 1728 1729 for (Enumeration e = getRequest().getParameters().keys(); 1730 e.hasMoreElements();) { 1731 paramName = (String ) e.nextElement(); 1732 vi = new ValidationItem(paramName, false, checkClassName); 1733 validSet.setValidationItem(vi); 1734 } 1735 1736 validate(validSet, errorCollection); 1737 } 1738 1739 1740 1741 1754 public void autoValidate(String checkClassName, 1755 ErrorCollection errorCollection, Vector requiredFields) 1756 throws ValidationException, ControllerException { 1757 if (requiredFields == null) { 1758 requiredFields = new Vector (); 1759 } 1760 1761 String fieldName = null; 1762 ValidationItem vi = null; 1763 1764 if ((checkClassName == null) || "".equals(checkClassName)) { 1765 throw new ValidationException("The checkClassName cannot be null"); 1766 } 1767 1768 ValidationSet validSet = new ValidationSet(); 1769 DBObject dbobj = null; 1770 1771 try { 1772 Class clazz = ClassLocator.loadClass(checkClassName); 1773 dbobj = (DBObject) clazz.newInstance(); 1774 } catch (Exception e) { 1775 throw new ControllerException("The dbClass specified could not be loaded."); 1776 } 1777 1778 DataObjectMetaData metadata = dbobj.getMetaData(); 1779 Iterator it = null; 1780 1781 it = metadata.getFieldListArray().iterator(); 1782 1783 boolean requireField = false; 1784 1785 while (it.hasNext()) { 1786 fieldName = (String ) it.next(); 1787 1788 if (requiredFields.contains(fieldName)) { 1789 requireField = true; 1790 } else { 1791 requireField = false; 1792 } 1793 1794 vi = new ValidationItem(fieldName, requireField, checkClassName); 1795 validSet.setValidationItem(vi); 1796 } 1797 1798 validate(validSet, errorCollection); 1799 } 1800 1801 1802 1803 1806 public void clearAttributes() { 1807 myRequest.setAttributes(null); 1808 } 1809 1810 1811 1814 public void clearBlockCache() { 1815 blockCache = null; 1816 } 1817 1818 1819 1820 1826 public void clearFormCache() throws ControllerException { 1827 getDefaultForm().clear(); 1828 } 1829 1830 1831 1832 1835 public void clearInputCache() { 1836 inputCache = null; 1837 } 1838 1839 1840 1841 1844 public void clearOutputCache() { 1845 outputCache = null; 1846 } 1847 1848 1849 1852 public void clearTransitionCache() { 1853 transitionCache = null; 1854 } 1855 1856 1857 1858 1864 public boolean hasErrors() throws ControllerException { 1865 ErrorCollection errs = getErrors(); 1866 1867 if (errs == null) { 1868 return false; 1869 } 1870 1871 if (errs.getErrorCount() > 0) { 1872 return true; 1873 } 1874 1875 return false; 1876 } 1877 1878 1884 public boolean hasErrors(String label) throws ControllerException { 1885 ErrorCollection errs = getErrors(); 1886 1887 if (errs == null) { 1888 return false; 1889 } 1890 1891 if (errs.getErrors(label).hasNext()) { 1892 return true; 1893 } 1894 1895 return false; 1896 } 1897 1898 1901 public void restoreForm() throws ControllerException { 1902 getDefaultForm().restoreForm(getRequest()); 1903 } 1904 1905 1919 public void saveErrors(ErrorCollection errorCollection) 1920 throws ControllerException { 1921 ControllerRequest req = getRequest(); 1922 HttpServletRequest hreq = null; 1923 1924 if (req instanceof ServletControllerRequest) { 1925 ServletControllerRequest sreq = (ServletControllerRequest) req; 1926 hreq = (HttpServletRequest ) sreq.getServletRequest(); 1927 } 1928 1929 if ((errorCollection == null) || 1931 (errorCollection.getErrorCount() == 0)) { 1932 req.getSession().setAttribute(Globals.ERROR_KEY, null); 1933 1934 if (hreq != null) { 1935 hreq.setAttribute(Globals.ERROR_KEY, null); 1936 } 1937 1938 return; 1939 } 1940 1941 req.getSession().setAttribute(Globals.ERROR_KEY, errorCollection); 1943 1944 if (hreq != null) { 1945 hreq.setAttribute(Globals.ERROR_KEY, errorCollection); 1946 } 1947 } 1948 1949 1950 1951 1957 public synchronized String toXML() throws ControllerException { 1958 FastStringBuffer returnBuffer = FastStringBuffer.getInstance(); 1959 String returnValue = null; 1960 1961 try { 1962 returnBuffer.append("<controller-response name=\"" + 1963 myControllerClass + "\""); 1964 1965 if (title != null) { 1966 returnBuffer.append(" title=\"" + title + "\""); 1967 } 1968 1969 if (requestedState != null) { 1970 returnBuffer.append(" requestedState=\"" + requestedState + 1971 "\""); 1972 } 1973 1974 if (style != null) { 1975 returnBuffer.append(" style=\"" + style + "\""); 1976 } 1977 1978 returnBuffer.append(">\n"); 1979 1980 ErrorCollection ec = getErrors(); 1981 1982 if ((ec != null) && (ec.getErrorCount() > 0)) { 1983 returnBuffer.append("<errors>"); 1984 1985 for (Iterator i = ec.getErrors(); i.hasNext();) { 1986 ActionError oneError = (ActionError) i.next(); 1987 returnBuffer.append("<error-message>"); 1988 returnBuffer.append(oneError.getKey()); 1989 returnBuffer.append("</error-message>"); 1990 } 1991 1992 returnBuffer.append("</errors>"); 1993 } 1994 1995 Vector blockList = getBlocks(); 1996 Block oneBlock = null; 1997 1998 if (blockList != null) { 1999 2000 for (Enumeration bl = blockList.elements(); 2001 bl.hasMoreElements();) { 2002 oneBlock = (Block) bl.nextElement(); 2003 returnBuffer = oneBlock.toXML(returnBuffer); 2004 } 2005 2006 2007 } 2008 2009 Vector outputs = getOutputs(); 2010 2011 if (outputs != null) { 2012 for (Enumeration eo = outputs.elements(); eo.hasMoreElements();) { 2013 returnBuffer = ((ControllerElement) eo.nextElement()).toXML(returnBuffer); 2014 } 2015 } 2016 2017 Vector inputs = getInputs(); 2018 2019 if (inputs != null) { 2020 for (Enumeration ei = inputs.elements(); ei.hasMoreElements();) { 2021 returnBuffer = ((ControllerElement) ei.nextElement()).toXML(returnBuffer); 2022 } 2023 } 2024 2025 Vector transitions = getTransitions(); 2026 2027 if (transitions != null) { 2028 for (Enumeration et = transitions.elements(); 2029 et.hasMoreElements();) { 2030 returnBuffer = ((ControllerElement) et.nextElement()).toXML(returnBuffer); 2031 } 2032 } 2033 2034 returnBuffer.append("</controller-response>"); 2035 returnValue = returnBuffer.toString(); 2036 } finally { 2037 returnBuffer.release(); 2038 returnBuffer = null; 2039 } 2040 2041 return returnValue; 2042 } 2043 2044 2055 public void validate(ValidationSet validSet, ErrorCollection errorCollection) 2056 throws ValidationException { 2057 Vector validationVector = validSet.getValidationItems(); 2058 ValidationItem vi = null; 2059 Hashtable checkClassCache = new Hashtable (); 2060 String fieldValue = null; 2061 String fieldName = null; 2062 String friendlyFieldName = null; 2063 String checkClassName = null; 2064 String dbFieldName = null; 2065 DBObject dbobject = null; 2066 boolean oneErrorFlag = false; 2067 boolean isDBField = false; 2068 2069 2074 try { 2075 for (int i = 0; i < validationVector.size(); i++) { 2076 vi = (ValidationItem) validationVector.elementAt(i); 2077 vi.checkThisItem(); 2078 fieldName = StringUtil.notNull(vi.getFieldName()); 2079 fieldValue = this.getFormCache(fieldName); 2080 friendlyFieldName = StringUtil.notNull(vi.getFriendlyFieldName()); 2081 checkClassName = StringUtil.notNull(vi.getCheckClassName()); 2082 dbFieldName = StringUtil.notNull(vi.getDbFieldName()); 2083 2084 if (dbFieldName.equals("")) { 2087 dbFieldName = fieldName; 2088 } 2089 2090 oneErrorFlag = false; 2091 2092 if (!checkClassName.equals("")) { 2095 if (!checkClassCache.contains(checkClassName)) { 2096 Class clazz = ClassLocator.loadClass(checkClassName); 2098 dbobject = (DBObject) clazz.newInstance(); 2099 checkClassCache.put(checkClassName, dbobject); 2100 2101 DataObjectMetaData metadata = dbobject.getMetaData(); 2102 isDBField = metadata.isField(dbFieldName); 2103 2104 if (isDBField) { 2105 if ("".equals(friendlyFieldName)) { 2108 friendlyFieldName = metadata.getDescription(dbFieldName); 2109 } 2110 } 2111 } 2112 } 2113 2114 if (vi.isRequired() && "".equals(fieldValue)) { 2117 oneErrorFlag = true; 2118 2119 if (!"".equals(friendlyFieldName)) { 2120 errorCollection.addError("The " + friendlyFieldName + 2121 " field is required. Please complete this field."); 2122 } 2123 } 2124 2125 if ((!oneErrorFlag) && (isDBField)) { 2128 if (checkClassCache.containsKey(checkClassName)) { 2130 dbobject = (DBObject) checkClassCache.get(checkClassName); 2131 2132 try { 2133 dbobject.checkField(dbFieldName, fieldValue); 2134 } catch (DBException dbe) { 2135 oneErrorFlag = true; 2136 2137 if (!"".equals(friendlyFieldName)) { 2138 errorCollection.addError("There was a problem " + 2139 "with your input for the " + 2140 friendlyFieldName + " field: " + 2141 dbe.getMessage()); 2142 } 2143 } 2144 } 2145 } 2146 } 2147 } catch (Exception e) { 2148 e.printStackTrace(); 2149 throw new ValidationException("Unable to validate '" + 2150 checkClassName + "'", e); 2151 } 2152 } 2153 2154 2155 2156 2162 protected DefaultForm getDefaultForm() throws ControllerException { 2163 String formKey = getRequest().getFormAttribute(); 2164 2165 if (StringUtil.notNull(formKey).equals("")) { 2166 throw new ControllerException("No ActionForm has been specified for this Controller mapping," + 2167 " unable to utilize form cache. Please set an action form in struts-config.xml or similar files" + 2168 " by setting name=\"default\" in the Action mapping. (i.e., use the default expresso formbean)"); 2169 } 2170 2171 DefaultForm myForm = (DefaultForm) getRequest().getSession() 2172 .getAttribute(formKey); 2173 2174 if (myForm == null) { 2175 myForm = (DefaultForm) getRequest().getSession() 2176 .getPersistentAttribute(formKey); 2177 } 2178 2179 if (myForm == null) { 2180 throw new ControllerException("No ActionForm found for key '" + 2181 formKey + "' in request or session scope."); 2182 } 2183 2184 return myForm; 2185 } 2186 2187 2196 protected void setSchema(String schemaClass) { 2197 StringUtil.assertNotBlank(schemaClass, "Must specify a non-blank schema"); 2198 2199 if (schemaStack == null) { 2200 schemaStack = new Stack (); 2201 } 2202 2203 schemaStack.push(schemaClass); 2204 } 2205 2206 2207 2208 2213 protected boolean validState() { 2214 if (getCurrentState() == null) { 2215 return false; 2216 } 2217 2218 return true; 2219 } 2220 2221 2226 void setCurrentState(State newState) { 2227 currentState = newState; 2228 } 2229 2230 2235 public Map getNestedMap() throws ControllerException { 2236 HashMap mappedNested = new HashMap (); 2237 2238 Map map = this.getNamedBlocks(); 2239 if (map != null) { 2240 mappedNested.putAll(map); 2241 } 2242 2243 map = this.getNamedInputs(); 2244 if (map != null) { 2245 mappedNested.putAll(map); 2246 } 2247 2248 map = this.getNamedOutputs(); 2249 if (map != null) { 2250 mappedNested.putAll(map); 2251 } 2252 2253 map = this.getNamedTransitions(); 2254 if (map != null) { 2255 mappedNested.putAll(map); 2256 } 2257 2258 return mappedNested; 2259 } 2260 2261 2264 public String getTitleKey() { 2265 return title; 2266 } 2267 2268} 2269 | Popular Tags |