1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.core.controller.session.PersistentSession; 68 import com.jcorporate.expresso.core.dbobj.DBObject; 69 import com.jcorporate.expresso.core.misc.StringUtil; 70 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 71 import org.apache.log4j.Logger; 72 import org.apache.oro.text.regex.MalformedPatternException; 73 import org.apache.oro.text.regex.Pattern; 74 import org.apache.oro.text.regex.PatternCompiler; 75 import org.apache.oro.text.regex.PatternMatcher; 76 import org.apache.oro.text.regex.Perl5Compiler; 77 import org.apache.oro.text.regex.Perl5Matcher; 78 79 import java.io.Serializable ; 80 import java.util.ArrayList ; 81 import java.util.HashMap ; 82 import java.util.Iterator ; 83 import java.util.List ; 84 import java.util.Map ; 85 import java.util.Vector ; 86 87 88 99 public class State 100 implements Cloneable , 101 Serializable { 102 103 106 private String name; 107 108 111 private String descrip; 112 private static final String className = State.class.getName(); 113 114 119 private List requiredParameters = new ArrayList (); 120 121 125 private List optionalParameters = new ArrayList (); 126 127 130 private Map paramMasks = null; 131 132 136 private Map paramMaskErrors = null; 137 138 141 private Controller myController = null; 142 143 146 private ControllerRequest myRequest = null; 147 148 152 private ControllerResponse myResponse = null; 153 154 157 private static Logger log = Logger.getLogger(State.class); 158 private String handlerName = null; 159 private String errorState = null; 160 private String stateFormClass = null; 161 private Transition returnToSender; 162 163 166 private Transition successTransition = null; 167 168 171 private Transition errorTransition = null; 172 173 177 transient private static ThreadLocal patternMatcher = new ThreadLocal () { 178 protected synchronized Object initialValue() { 179 return new Perl5Matcher(); 180 } 181 }; 182 183 189 protected PatternMatcher getPatternMatcher() { 190 return (PatternMatcher) patternMatcher.get(); 191 } 192 193 194 198 public static final String NOTEMPTY_MASK = "(?s).+"; 199 200 205 protected boolean secure; 206 207 213 public void setController(Controller newController) 214 throws ControllerException { 215 if (newController == null) { 216 throw new ControllerException("Cannot set a null Controller here"); 217 } 218 219 myController = newController; 220 } 221 222 227 public synchronized void setErrorState(String newErrorState) { 228 errorState = newErrorState; 229 } 230 231 236 public String getErrorState() { 237 return errorState; 238 } 239 240 250 public synchronized void setMask(String paramName, String mask) { 251 StringUtil.assertNotBlank(paramName, 252 "You may not specify a blank parameter name"); 253 StringUtil.assertNotBlank(mask, 254 "You may not specify a blank mask here"); 255 256 try { 257 PatternCompiler compiler = new Perl5Compiler(); 258 Pattern pattern = null; 259 pattern = compiler.compile(mask, Perl5Compiler.READ_ONLY_MASK); 260 261 if (paramMasks == null) { 262 paramMasks = new HashMap (); 263 } 264 265 paramMasks.put(paramName, pattern); 266 267 if (mask.equals(DBObject.INT_MASK)) { 268 String err = "Value supplied for parameter '" + paramName + 269 "' must be an integer number."; 270 this.setMaskError(paramName, err); 271 } 272 } catch (MalformedPatternException rre) { 273 log.error("Error Compiling Regular Expression for parameter: " + 274 paramName, rre); 275 throw new IllegalArgumentException (rre.getMessage()); 276 } 277 } 278 279 289 public synchronized void setMask(String paramName, String mask, 290 String errorMsg) { 291 this.setMask(paramName, mask); 292 this.setMaskError(paramName, errorMsg); 293 } 294 295 304 public void setMaskError(String paramName, String errorMsg) { 305 StringUtil.assertNotBlank(errorMsg, 306 "You may not specify a blank errorMessage"); 307 308 if (paramMaskErrors == null) { 309 paramMaskErrors = new HashMap (); 310 } 311 312 String i18nString = null; 313 314 try { 315 i18nString = StringUtil.notNull(this.getString(errorMsg)); 316 } catch (ControllerException e) { 317 System.err.println(e); 318 } 319 if (i18nString.length() != 0) { 320 paramMaskErrors.put(paramName, i18nString); 321 } else { 322 paramMaskErrors.put(paramName, errorMsg); 323 } 324 } 325 326 332 protected void setResponse(ControllerResponse res) 333 throws ControllerException { 334 if (res == null) { 335 throw new ControllerException("Null response object not allowed here"); 336 } 337 338 myResponse = res; 339 } 340 341 348 public void setReturnToSender(Transition newReturnToSender) { 349 returnToSender = newReturnToSender; 350 } 351 352 357 public void setStateFormClass(String newStateFormClass) { 358 stateFormClass = newStateFormClass; 359 } 360 361 367 public void setSuccessTransition(Transition newSuccessTransition) { 368 successTransition = newSuccessTransition; 369 } 370 371 public ControllerResponse getResponse() { 372 return myResponse; 373 } 374 375 381 public Controller getController() 382 throws ControllerException { 383 return myController; 384 } 385 386 395 protected DBController getDBController() 396 throws ControllerException { 397 if (myController instanceof DBController) { 398 return (DBController) myController; 399 } else { 400 throw new ControllerException("This state's controller was not " + 401 "an DBController"); 402 } 403 } 404 405 410 public State() { 411 412 } 413 414 426 public State(String newName, String newDescrip) { 427 setName(newName); 428 setDescription(newDescrip); 429 } 430 431 438 public void setName(String newName) { 439 name = newName; 440 this.calculateHandlerName(newName); 441 } 442 443 449 public void setSecure(boolean newSecure) { 450 this.secure = newSecure; 451 } 452 453 461 public void setDescription(String newDescrip) { 462 descrip = newDescrip; 463 } 464 465 470 public void addRequiredParameter(String newParam) { 471 requiredParameters.add(newParam); 472 setMask(newParam, NOTEMPTY_MASK); 473 } 474 475 480 public void addOptionalParameter(String newParam) { 481 optionalParameters.add(newParam); 482 } 483 484 490 public void addParameter(String newParam, String mask) { 491 addRequiredParameter(newParam); 492 setMask(newParam, mask); 493 } 494 495 502 public void addParameter(String newParam, boolean required, String mask) { 503 if (required) { 504 addRequiredParameter(newParam); 505 } else { 506 addOptionalParameter(newParam); 507 } 508 509 setMask(newParam, mask); 510 } 511 512 520 public void addParameter(String newParam, boolean required, String mask, 521 String maskError) { 522 if (required) { 523 addRequiredParameter(newParam); 524 } else { 525 addOptionalParameter(newParam); 526 } 527 setMask(newParam, mask); 528 setMaskError(newParam, maskError); 529 } 530 531 540 public String getDescription() { 541 ControllerResponse theResponse = getControllerResponse(); 542 543 if (theResponse == null) { 544 return descrip; 545 } 546 try { 547 return theResponse.getString(descrip); 548 } catch (Exception ee) { 549 return descrip; 550 } 551 } 552 553 560 public String getName() { 561 return name; 562 } 563 564 569 public Vector getParameters() { 570 return new Vector (requiredParameters); 571 } 572 573 578 public List getRequiredParametersList() { 579 return new ArrayList (requiredParameters); 580 } 581 582 588 public Vector getOptionalParameters() { 589 return new Vector (optionalParameters); 590 } 591 592 598 public List getOptionalParametersList() { 599 return new ArrayList (optionalParameters); 600 } 601 602 607 public String toString() { 608 return className + "[" + name + " " + descrip + "]"; 609 } 610 611 618 public boolean equals(Object o) { 619 if (o instanceof String ) { 620 String cmp = (String ) o; 621 622 if (cmp != null) { 623 return cmp.equals(name); 624 } else { 625 return false; 626 } 627 } else { 628 if (o instanceof State) { 629 State cmp = (State) o; 630 631 return name.equals(cmp.getName()); 632 } 633 } 634 635 return false; 636 } 637 638 644 public Transition getErrorTransition() { 645 return errorTransition; 646 } 647 648 659 public void run(ControllerRequest newRequest, 660 ControllerResponse newResponse) 661 throws ControllerException, NonHandleableException { 662 myRequest = newRequest; 663 myResponse = newResponse; 664 checkParamMasks(); 665 } 666 667 682 public void perform(StateForm stateForm, ControllerRequest newRequest, 683 ControllerResponse newResponse) 684 throws NonHandleableException, ControllerException { 685 run(newRequest, newResponse); 686 } 687 688 696 protected void add(ControllerElement t) 697 throws ControllerException { 698 myResponse.add(t); 699 } 700 701 702 708 public void addTransition(Transition newTransition) 709 throws ControllerException { 710 myResponse.add(newTransition); 711 } 712 713 714 720 protected void addBlock(Block newBlock) 721 throws ControllerException { 722 myResponse.addBlock(newBlock); 723 } 724 725 726 743 protected void addAutoInput(String fieldName, String friendlyName, 744 String defaultValue, int displayLength, 745 int maxLength, Vector validValues) 746 throws ControllerException { 747 myResponse.addAutoInput(fieldName, friendlyName, defaultValue, 748 displayLength, maxLength, validValues); 749 } 750 751 757 protected void addInput(Input newInput) 758 throws ControllerException { 759 myResponse.addInput(newInput); 760 } 761 762 768 protected void addOutput(Output newOutput) 769 throws ControllerException { 770 myResponse.addOutput(newOutput); 771 } 772 773 787 protected void addAutoInput(String fieldName, String dbClassName, 788 String defaultValue) 789 throws ControllerException { 790 myResponse.addAutoInput(fieldName, dbClassName, defaultValue); 791 } 792 793 807 protected void addAutoInput(String fieldName, String friendlyName, 808 String dbClassName, String defaultValue) 809 throws ControllerException { 810 myResponse.addAutoInput(fieldName, friendlyName, dbClassName, 811 defaultValue); 812 } 813 814 827 protected void autoValidate(String checkClassName, 828 ErrorCollection errorCollection) 829 throws ValidationException, ControllerException { 830 myResponse.autoValidate(checkClassName, errorCollection); 831 } 832 833 850 protected void autoValidate(String checkClassName, 851 ErrorCollection errorCollection, 852 Vector requiredFields) 853 throws ValidationException, ControllerException { 854 myResponse.autoValidate(checkClassName, errorCollection, 855 requiredFields); 856 } 857 858 867 protected String getFileName(String paramName) { 868 return myRequest.getFileName(paramName); 869 } 870 871 877 protected String getAttribute(String attrib) { 878 return (String ) myRequest.getAttrib(attrib); 879 } 880 881 888 public Object getObjectParameter(String paramName) 889 throws ControllerException { 890 return myRequest.getObjectParameter(paramName); 891 } 892 893 899 public String getHandlerName() { 900 return this.handlerName; 901 } 902 903 910 public void setHandlerName(String newName) { 911 this.handlerName = newName; 912 } 913 914 920 public String getInitParameter(String paramName) { 921 return myRequest.getInitParameter(paramName); 922 } 923 924 930 public Transition getSuccessTransition() { 931 return successTransition; 932 } 933 934 940 public String getUser() { 941 return myRequest.getUser(); 942 } 943 944 952 public String getParameter(String paramCode) 953 throws ControllerException { 954 if (myRequest == null) { 955 throw new ControllerException("No request object available " + 956 "- did this state's 'run' method called super.run?"); 957 } 958 959 return myRequest.getParameter(paramCode); 960 } 961 962 968 public boolean isFileParameter(String paramName) { 969 return myRequest.isFileParameter(paramName); 970 } 971 972 977 public boolean isSecure() { 978 return this.secure; 979 } 980 981 987 public String getDBName() { 988 return myRequest.getDataContext(); 989 } 990 991 997 public String getDataContext() { 998 return myRequest.getDataContext(); 999 } 1000 1001 1006 protected ControllerRequest getControllerRequest() { 1007 return myRequest; 1008 } 1009 1010 1015 protected ControllerResponse getControllerResponse() { 1016 return myResponse; 1017 } 1018 1019 1025 protected void calculateHandlerName(String stateName) { 1026 FastStringBuffer fsb = FastStringBuffer.getInstance(); 1027 try { 1028 fsb.append("run"); 1029 fsb.append(Character.toUpperCase(stateName.charAt(0))); 1030 fsb.append(stateName.substring(1)); 1031 fsb.append("State"); 1032 handlerName = fsb.toString(); 1033 } finally { 1034 fsb.release(); 1035 fsb = null; 1036 } 1037 } 1038 1039 1046 public Object clone() 1047 throws java.lang.CloneNotSupportedException { 1048 Object o = (State) super.clone(); 1049 1050 synchronized (this) { 1051 State s = (State) o; 1052 s.setName(getName()); 1053 s.setDescription(getDescription()); 1054 s.optionalParameters = new ArrayList (optionalParameters); 1055 s.requiredParameters = new ArrayList (requiredParameters); 1056 1057 if (paramMasks != null) { 1058 s.paramMasks = new HashMap (paramMasks); 1059 } 1060 if (paramMaskErrors != null) { 1061 s.paramMaskErrors = new HashMap (paramMaskErrors); 1062 } 1063 1064 s.errorState = errorState; 1065 s.stateFormClass = stateFormClass; 1066 1067 if (successTransition != null) { 1068 s.successTransition = (Transition) successTransition.clone(); 1069 } 1070 if (errorTransition != null) { 1071 s.errorTransition = (Transition) errorTransition.clone(); 1072 } 1073 1074 } 1077 1078 return o; 1079 } 1080 1081 1088 protected void saveErrors(ErrorCollection e) 1089 throws ControllerException { 1090 myResponse.saveErrors(e); 1091 } 1092 1093 1099 public void setErrorTransition(Transition newErrorTransition) { 1100 errorTransition = newErrorTransition; 1101 } 1102 1103 1108 protected void setFormCache() 1109 throws ControllerException { 1110 myResponse.setFormCache(); 1111 } 1112 1113 1118 protected void clearFormCache() 1119 throws ControllerException { 1120 myResponse.clearFormCache(); 1121 } 1122 1123 1130 protected String getFormCache(String fieldName) 1131 throws ControllerException { 1132 return myResponse.getFormCache(fieldName); 1133 } 1134 1135 protected boolean isParameter(String paramName) 1136 throws ControllerException { 1137 return myRequest.isParameter(paramName); 1138 } 1139 1140 protected void transition(String newState, ControllerRequest req, 1141 ControllerResponse res) 1142 throws ControllerException, NonHandleableException { 1143 getController().transition(newState, req, res); 1144 } 1145 1146 1153 public Transition getReturnToSender() { 1154 return returnToSender; 1155 } 1156 1157 public PersistentSession getSession() 1158 throws ControllerException { 1159 return myRequest.getSession(); 1160 } 1161 1162 1168 public String getStateFormClass() { 1169 return stateFormClass; 1170 } 1171 1172 1178 protected int getUid() 1179 throws ControllerException { 1180 return myRequest.getUid(); 1181 } 1182 1183 1190 public void checkParamMasks() 1191 throws ControllerException { 1192 if (paramMasks == null) { 1193 return; 1194 } 1195 1196 String oneParamName = null; 1197 String oneParamValue = null; 1198 1199 for (Iterator i = this.requiredParameters.iterator(); i.hasNext();) { 1214 oneParamName = (String ) i.next(); 1215 oneParamValue = getParameter(oneParamName); 1216 checkMask(oneParamName, oneParamValue, 1217 (Pattern) paramMasks.get(oneParamName)); 1218 } 1219 for (Iterator i = this.optionalParameters.iterator(); i.hasNext();) { 1221 oneParamName = (String ) i.next(); 1222 oneParamValue = StringUtil.notNull(getParameter(oneParamName)); 1223 1224 if (oneParamValue.length() > 0) { 1225 checkMask(oneParamName, oneParamValue, 1226 (Pattern) paramMasks.get(oneParamName)); 1227 } 1228 } 1229 } 1230 1231 1237 public ErrorCollection getErrors() 1238 throws ControllerException { 1239 return myResponse.getErrors(); 1240 } 1241 1242 1248 public void addError(String newError) 1249 throws ControllerException { 1250 myResponse.addError(newError); 1251 } 1252 1253 1264 protected void checkMask(String paramName, String paramValue, 1265 Pattern compiledRegExp) 1266 throws ControllerException { 1267 if (compiledRegExp == null) { 1268 return; 1269 } 1270 1271 1272 if (!getPatternMatcher().matches(paramValue, compiledRegExp)) { 1273 String maskError = null; 1274 1275 if (paramMaskErrors != null) { 1276 maskError = StringUtil.notNull((String ) paramMaskErrors.get(paramName)); 1277 } 1278 if (maskError == null || maskError.length() == 0) { 1279 String err = "Value '" + paramValue + 1280 "' supplied for parameter '" + paramName + 1281 "' does not" + 1282 " match the regular expression specified for this field."; 1283 addError(err); 1284 } else { 1285 if (maskError != null) { 1286 addError(maskError); 1287 } 1288 } 1289 } 1290 } 1291 1292 1293 1302 protected String getString(String key, Object [] args) 1303 throws ControllerException { 1304 if (myResponse != null) { 1305 return myResponse.getString(key, args); 1306 } 1307 1308 return key; 1309 } 1310 1311 1319 protected String getString(String key) 1320 throws ControllerException { 1321 return getString(key, new Object []{}); 1322 } 1323 1324 1330 public int hashCode() { 1331 return name.hashCode(); 1332 } 1333} 1334 | Popular Tags |