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.db.DBException; 69 import com.jcorporate.expresso.core.dbobj.DBObject; 70 import com.jcorporate.expresso.core.misc.ConfigManager; 71 import com.jcorporate.expresso.core.misc.ConfigurationException; 72 import com.jcorporate.expresso.core.misc.StringUtil; 73 import com.jcorporate.expresso.core.security.User; 74 import com.jcorporate.expresso.kernel.util.ClassLocator; 75 import org.apache.log4j.Logger; 76 import org.apache.struts.Globals; 77 78 import java.io.Serializable ; 79 import java.util.HashMap ; 80 import java.util.Hashtable ; 81 import java.util.Iterator ; 82 import java.util.Locale ; 83 import java.util.Map ; 84 85 86 92 public class ControllerRequest 93 implements Serializable , 94 com.jcorporate.expresso.core.dbobj.RequestContext, 95 Cloneable { 96 97 private Hashtable params = null; 98 private Map initParams = null; 99 private Map objectParams = null; 100 private String userName = null; 101 private int uid = 0; 102 private String mySchema = null; 103 private Locale currentLocale = null; 104 105 106 private String formAttribute = null; 107 108 109 110 111 private String initialState = null; 112 113 116 120 private Map attributes = null; 121 private String dbName = null; 122 123 private static transient final Logger log = Logger.getLogger(ControllerRequest.class); 124 125 126 133 private PersistentSession mySession = null; 134 135 139 private Hashtable fileParams = null; 140 141 145 private Hashtable formResponseCache = null; 146 147 150 public ControllerRequest() { 151 152 } 153 154 155 public void setFileParameter(String paramName, String paramValue, 156 String fileName) 157 throws ControllerException { 158 setParameter(paramName, paramValue); 159 StringUtil.assertNotBlank(fileName, "File name must not be blank here"); 160 161 if (fileParams == null) { 162 fileParams = new Hashtable (); 163 } 164 165 fileParams.put(paramName, fileName); 166 } 167 168 176 public String getFileName(String paramName) { 177 if (isFileParameter(paramName)) { 178 return (String ) fileParams.get(paramName); 179 } else { 180 return null; 181 } 182 } 183 184 191 public void setAttribute(String attrib, String val) { 192 if (attributes == null) { 193 attributes = new HashMap (3); 194 } 195 196 attributes.put(attrib, val); 197 } 198 199 205 public void setAttrib(String attrib, Object val) { 206 if (attributes == null) { 207 attributes = new HashMap (3); 208 } 209 210 attributes.put(attrib, val); 211 } 212 213 220 public String getAttribute(String attrib) { 221 if (attributes == null) { 222 return null; 223 } else { 224 return (String ) attributes.get(attrib); 225 } 226 } 227 228 234 public Object getAttrib(String attrib) { 235 if (attributes == null) { 236 return null; 237 } else { 238 return attributes.get(attrib); 239 } 240 } 241 242 248 public Map getAttributes() { 249 return attributes; 250 } 251 252 257 public void setAttributes(Map attributes) { 258 this.attributes = attributes; 259 } 260 261 266 public void removeAttrib(String key) { 267 attributes.remove(key); 268 } 269 270 278 public void setInitParameter(String paramName, String paramValue) { 279 if (initParams == null) { 280 initParams = new HashMap (); 281 } 282 283 initParams.put(paramName, paramValue); 284 } 285 286 291 public void setParameter(String paramName, String paramValue) 292 throws ControllerException { 293 if (params == null) { 294 params = new Hashtable (10); 295 } 296 if (paramName == null) { 297 throw new ControllerException("Parameter name may not be null"); 298 } 299 if (paramValue == null) { 300 throw new ControllerException("Parameter value may not be" + 301 " null for parameter '" + paramName + 302 "'"); 303 } 304 305 params.put(paramName, paramValue); 306 } 307 308 309 316 public void setObjectParameter(String paramName, Object paramValue) 317 throws ControllerException { 318 StringUtil.assertNotBlank(paramName, 319 "Parameter name may not be blank or null here"); 320 321 if (paramValue == null) { 322 throw new IllegalArgumentException ("Parameter value may not be null here"); 323 } 324 if (objectParams == null) { 325 objectParams = new HashMap (); 326 } 327 328 objectParams.put(paramName, paramValue); 329 } 330 331 332 337 public Object getObjectParameter(String paramName) 338 throws ControllerException { 339 StringUtil.assertNotBlank(paramName, 340 "Parameter name may not be blank or null here"); 341 342 if (objectParams == null) { 343 return null; 344 } else { 345 return objectParams.get(paramName); 346 } 347 } 348 349 350 355 public void setParameters(Hashtable h) { 356 357 358 359 360 if (h != null) { 361 params = h; 362 } else { 363 params = new Hashtable (1); 364 } 365 } 366 367 373 public void setUser(String newUser) { 374 userName = newUser; 375 } 376 377 382 public void setUid(int newUid) { 383 uid = newUid; 384 } 385 386 394 public void setSession(PersistentSession newSession) 395 throws ControllerException { 396 if (newSession == null) { 397 throw new ControllerException("Cannot set a null PersistentSession object"); 398 } 399 400 mySession = newSession; 401 } 402 403 404 411 public PersistentSession getSession() 412 throws ControllerException { 413 if (mySession == null) { 414 throw new ControllerException("No PersistentSession object available"); 415 } 416 417 return mySession; 418 } 419 420 426 public String getInitParameter(String paramName) { 427 if (initParams == null) { 428 return null; 429 } else { 430 return (String ) initParams.get(paramName); 431 } 432 } 433 434 439 public String getUser() { 440 if (userName == null) { 441 return User.UNKNOWN_USER; 442 } 443 444 return userName; 445 } 446 447 452 public int getUid() { 453 return uid; 454 } 455 456 461 public Hashtable getParameters() { 462 if (params != null) { 463 return (Hashtable ) params.clone(); 464 } else { 465 return new Hashtable (); 466 } 467 } 468 469 476 public String getParameter(String paramCode) { 477 if (params == null) { 478 return null; 479 } 480 481 return (String ) params.get(paramCode); 482 } 483 484 485 491 public boolean isFileParameter(String paramName) { 492 if (fileParams == null) { 493 return false; 494 } 495 if (fileParams.containsKey(paramName)) { 496 return true; 497 } 498 499 return false; 500 } 501 502 509 public String getDBName() { 510 if (StringUtil.notNull(dbName).equals("")) { 511 return "default"; 512 } 513 514 return dbName; 515 } 516 517 523 public String getDataContext() { 524 if (StringUtil.notNull(dbName).equals("")) { 525 return "default"; 526 } 527 return dbName; 528 } 529 530 535 public synchronized void setDBName(String newDBName) { 536 if (StringUtil.notNull(newDBName).equals("")) { 537 dbName = "default"; 538 } else { 539 dbName = newDBName; 540 } 541 } 542 543 548 public synchronized void setDataContext(String newDBName) { 549 this.setDBName(newDBName); 550 } 551 552 559 public void populate(DBObject myDBObj) 560 throws ControllerException { 561 try { 562 String oneFieldName = null; 563 564 for (Iterator e = myDBObj.getJDBCMetaData().getFieldListArray().iterator() 565 ; e.hasNext();) { 566 oneFieldName = (String ) e.next(); 567 568 if (getParameter(oneFieldName) != null) { 569 myDBObj.setField(oneFieldName, getParameter(oneFieldName)); 570 } 571 } 572 573 } catch (DBException de) { 574 throw new ControllerException(de); 575 } 576 } 577 578 579 586 public ErrorCollection getErrorCollection() 587 throws ControllerException { 588 ErrorCollection ec = (ErrorCollection) getSession().getAttribute(Globals.ERROR_KEY); 589 590 if (ec == null) { 596 ec = (ErrorCollection) getSession().getPersistentAttribute(Globals.ERROR_KEY); 598 599 if (ec != null) { 601 getSession().removePersistentAttribute(Globals.ERROR_KEY); 602 getSession().setAttribute(Globals.ERROR_KEY, ec); 603 } 604 } 605 606 return ec; 607 } 608 609 public synchronized void setFormAttribute(String newAttribute) { 610 formAttribute = newAttribute; 611 } 612 613 public String getFormAttribute() { 614 return formAttribute; 615 } 616 617 624 public boolean isParameter(String paramName) { 625 Hashtable allParams = getParameters(); 626 627 if (allParams == null) { 628 return false; 629 } 630 631 return allParams.containsKey(paramName); 632 } 633 634 644 public void validateDBObject(DBObject oneObject, ErrorCollection ec) 645 throws ControllerException, ValidationException { 646 try { 647 String oneFieldName = null; 648 for (Iterator af = oneObject.getMetaData().getFieldListArray().iterator(); af.hasNext();) { 649 oneFieldName = (String ) af.next(); 650 validateDBField(oneFieldName, oneObject, ec); 651 } 652 } catch (DBException de) { 653 throw new ControllerException(de); 654 } 655 } 656 657 658 670 public void validateDBField(String dbFieldName, 671 DBObject oneObject, 672 ErrorCollection ec) 673 throws ControllerException, DBException { 674 675 validateField(dbFieldName, dbFieldName, oneObject, ec); 676 } 677 678 679 690 public void validateField(String dbFieldName, 691 String reqFieldName, 692 DBObject oneObject, 693 ErrorCollection ec) 694 throws ControllerException, DBException { 695 696 Hashtable allParams = getParameters(); 697 698 if ((!oneObject.getMetaData().getFieldMetadata(dbFieldName).isReadOnly()) && 699 (!oneObject.getMetaData().getFieldMetadata(dbFieldName).isVirtual())) { 700 if (allParams.containsKey(reqFieldName)) { 701 String oneFieldParam = getParameter(reqFieldName); 702 703 try { 704 oneObject.checkField(dbFieldName, oneFieldParam); 705 } catch (DBException de) { 706 if (log.isInfoEnabled()) { 707 log.info("Validation error for field:", de); 708 } 709 710 ec.addError(de.getMessage()); 711 } 712 713 oneObject.setField(dbFieldName, oneFieldParam); 714 } 715 } 716 } 717 718 719 724 public synchronized void setLocale(Locale newLocale) { 725 currentLocale = newLocale; 726 } 727 728 733 public Locale getLocale() { 734 if (currentLocale != null) { 735 return currentLocale; 736 } 737 738 Locale l = null; 739 740 try { 741 l = new Locale (ConfigManager.getContext(getDataContext()).getLanguage(), 742 ConfigManager.getContext(getDataContext()).getCountry()); 743 } catch (ConfigurationException ce) { 744 l = null; 745 } 746 if (l == null) { 747 l = Locale.getDefault(); 748 } 749 750 return l; 751 } 752 753 758 public Object clone() { 759 Object o = null; 760 String className = this.getClass().getName(); 761 762 try { 763 Class c = ClassLocator.loadClass(className); 764 o = c.newInstance(); 765 } catch (ClassNotFoundException cn) { 766 throw new IllegalArgumentException ("State object '" + className + 767 "' not found"); 768 } catch (InstantiationException ie) { 769 throw new IllegalArgumentException ("State object '" + className + 770 "' cannot be instantiated"); 771 } catch (IllegalArgumentException e) { 772 throw new IllegalArgumentException ("State object '" + className + 773 "' cannot be instantiated (IllegalArgumentException)"); 774 } catch (IllegalAccessException iae) { 775 throw new IllegalArgumentException ("llegal access loading " + 776 "State object '" + className + 777 "'"); 778 } 779 synchronized (this) { 780 ControllerRequest cr = (ControllerRequest) o; 781 782 if (this.attributes != null) { 783 cr.attributes = new HashMap (this.attributes); 784 } 785 786 cr.currentLocale = this.currentLocale; 787 cr.dbName = this.dbName; 788 789 if (this.fileParams != null) { 790 cr.fileParams = (Hashtable ) this.fileParams.clone(); 791 } 792 793 cr.formAttribute = this.formAttribute; 794 795 if (this.formResponseCache != null) { 796 cr.formResponseCache = (Hashtable ) this.formResponseCache.clone(); 797 } 798 799 cr.initialState = this.initialState; 800 801 if (this.initParams != null) { 802 cr.initParams = new HashMap (this.initParams); 803 } 804 805 cr.mySchema = this.mySchema; 806 cr.mySession = this.mySession; 807 808 if (this.objectParams != null) { 809 cr.objectParams = new HashMap (this.objectParams); 810 } 811 if (this.params != null) { 812 cr.params = (Hashtable ) this.params.clone(); 813 } 814 815 cr.uid = this.uid; 816 cr.userName = this.userName; 817 } 818 819 return o; 820 } 821 822 827 public void removeParameter(String paramName) { 828 params.remove(paramName); 829 } 830 831 832 837 public User getUserInfo() throws DBException { 838 return User.getUser(this); 839 } 840 } 841 | Popular Tags |