1 19 20 package za.org.coefficient.core; 21 22 import java.beans.IndexedPropertyDescriptor ; 23 import java.beans.PropertyDescriptor ; 24 import java.io.IOException ; 25 import java.lang.reflect.Method ; 26 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import java.beans.Introspector ; 34 import java.beans.PropertyDescriptor ; 35 36 import org.apache.commons.fileupload.FileUploadException; 37 import org.apache.commons.httpclient.Cookie; 38 39 import za.org.coefficient.authentication.CoefficientUser; 40 import za.org.coefficient.exception.ConfigurationException; 41 import za.org.coefficient.html.Page; 42 import za.org.coefficient.interfaces.CoefficientContext; 43 import za.org.coefficient.util.common.BaseMultipartRequest; 44 import za.org.coefficient.util.common.MultipartRequest; 45 import za.org.coefficient.util.common.UploadedFile; 46 47 54 public abstract class BaseCoefficientContext implements CoefficientContext { 55 56 public static final String CURRENT_PROJECT = "__current_project__"; 57 58 public static final String LAST_NON_HELP_OP = "__last_non_help_op"; 59 protected static final String URL_PREFIX = "index.html?module="; 60 61 62 protected Page page_; 63 64 protected String redirectionURL = null; 65 protected String domain = null; 66 protected String requestURL = null; 67 protected boolean inWorkflowAction = false; 68 protected boolean invalidateSession = false; 69 private CoefficientUser tempUser = null; 70 private CoefficientUser workflowUser = null; 71 72 protected MultipartRequest multipartRequest; 73 protected Map session = new HashMap (); 74 protected Map requestData = new HashMap (); 75 protected Map requestAttributes = new HashMap (); 76 protected Map fileUploadData = new HashMap (); 77 protected Cookie[] serializableExistingCookies; 78 protected ArrayList serializableNewCookies = new ArrayList (); 79 protected ArrayList removedSessionKeys = new ArrayList (); 80 81 87 public void setError(String html) { 88 getPage().setError(html); 89 } 90 91 public boolean isError() { 92 return getPage().getErrors() 93 .trim() 94 .length() > 0; 95 } 96 97 public Page getPage() { 98 if (this.page_ == null){ 99 try { 100 createPageObject(); 101 } catch (Exception e) { 102 e.printStackTrace(); 103 throw new RuntimeException ("Could not create Page object.", e); 104 } 105 } 106 return page_; 107 } 108 109 public String getTheme() { 110 String theme = (String )getSessionAttribute(Constants.CURRENT_THEME_STRING); 111 return theme; 112 } 113 114 public void setTheme(String theme) { 115 setSessionAttribute(Constants.CURRENT_THEME_STRING, theme); 116 } 117 118 121 public void setForward(String moduleName) throws ConfigurationException { 122 this.setForward(moduleName, null, null); 123 } 124 125 129 public void setForward(String moduleName, String opName) 130 throws ConfigurationException { 131 this.setForward(moduleName, opName, null); 132 } 133 134 150 public void setForward(String moduleName, String opName, HashMap params) 151 throws ConfigurationException { 152 if ((moduleName == null) || (moduleName.trim() 153 .length() == 0)) { 154 throw new ConfigurationException( 155 "You can not forward without providing a module name"); 156 } 157 StringBuffer sb = new StringBuffer (URL_PREFIX); 158 sb.append(moduleName); 159 if ((opName != null) && (opName.trim() 160 .length() != 0)) { 161 sb.append("&op=") 162 .append(opName); 163 } 164 if ((params != null) && (params.size() != 0)) { 165 for (Iterator it = params.keySet() 166 .iterator(); it.hasNext();) { 167 Object key = it.next(); 168 Object val = params.get(key); 169 sb.append("&") 170 .append(key) 171 .append("=") 172 .append(val); 173 } 174 } 175 redirectionURL = sb.toString(); 176 } 177 178 186 public void setModuleContent(String html, String name) throws RuntimeException { 187 try { 188 getPage().setModuleContent(html, name); 189 } catch (Exception e) { 190 throw new RuntimeException (); 191 } 192 } 193 194 197 public void setModuleContent(String html, String name, String theme) throws RuntimeException { 198 try { 199 getPage().setModuleContent(html, name, theme); 200 } catch (Exception e) { 201 throw new RuntimeException (); 202 } 203 } 204 205 public String clearModuleContent(String name) { 206 return getPage().clearModuleContent(name); 207 } 208 209 public void setCookie(Cookie cookie) { 210 serializableNewCookies.add(cookie); 211 } 212 213 public Cookie[] getCookies() { 214 return serializableExistingCookies; 215 } 216 217 public BaseMultipartRequest getMultipartRequest() throws FileUploadException { 218 if(this.multipartRequest == null) { 219 throw new RuntimeException ("The current request doesn't contain a multipart/form-data or multipart/mixed stream"); 220 } 221 return this.multipartRequest; 222 } 223 224 234 public String getParameter(String name) { 235 236 String value_str = null; 237 Object value = requestData.get(name); 238 if (value != null) { 239 if (value instanceof String []) { 240 value_str = ((String [])value)[0]; 241 } else { 242 value_str = value.toString(); 243 } 244 } 245 if ((value_str == null) || value_str.trim() .equals("")) { 246 value = getRequestAttribute(name); 247 if (value != null) { 248 if (value instanceof String []) { 249 value_str = ((String [])value)[0]; 250 } else { 251 value_str = value.toString(); 252 } 253 } 254 } 255 return value_str; 256 } 257 258 266 public String [] getParameterValues(String name) { 267 return getParametersValues(name); 268 } 269 270 public String [] getParametersValues(String name) { 271 Object value = requestData.get(name); 272 if (value != null) { 273 if (value instanceof String []) { 274 return (String [])value; 275 } else { 276 return new String []{value.toString()}; 277 } 278 } else { 279 return new String [0]; 280 } 281 } 282 283 292 public boolean setProperties(Object bean) { 293 boolean complete = true; 294 try { 295 Class beanClass = bean.getClass(); 296 Map params = new HashMap (); 297 for (Iterator e = requestData.keySet().iterator(); e.hasNext();) { 298 String name = (String ) e.next(); 299 String value = getParameter(name); 300 params.put(name, value); 301 } 302 PropertyDescriptor [] props = 303 Introspector.getBeanInfo(beanClass).getPropertyDescriptors(); 304 305 for (int i = 0; i < props.length; i++) { 306 String propname = props[i].getName(); 307 Method setter = props[i].getWriteMethod(); 308 if ((setter != null) && (params.containsKey(propname))) { 309 try { 310 setProperty(bean, props[i]); 311 } catch (Exception e) { 312 e.printStackTrace(); 313 complete = false; 314 } 315 } 316 } 317 } catch (Exception e) { 318 complete = false; 320 } 321 return complete; 322 } 323 324 public void setRequestAttribute(String name, Object value) { 325 requestAttributes.put(name, value); 326 } 327 328 public Object getRequestAttribute(String name) { 329 return requestAttributes.get(name); 330 } 331 332 public void setSessionAttribute(String name, Object value) { 333 session.put(name, value); 334 } 335 336 public Object getSessionAttribute(String name) { 337 return session.get(name); 338 } 339 340 public void removeRequestAttribute(String name) { 341 requestAttributes.remove(name); 342 } 343 344 public void removeSessionAttribute(String name) { 345 session.remove(name); 346 removedSessionKeys.add(name); 347 } 348 349 358 public String getParameter(String name, String defaultValue) { 359 String value = getParameter(name); 360 361 return (value != null) ? value : defaultValue; 362 } 363 364 public boolean getParameterAsBoolean(String name) { 365 String val = getParameter(name); 366 if("on".equals(val)) { 367 val = "true"; 368 } 369 return Boolean.valueOf(val).booleanValue(); 370 } 371 372 public Date getParameterAsDate(String name) { 373 Date retVal = null; 374 try { 375 java.text.SimpleDateFormat df = 376 new java.text.SimpleDateFormat (Constants.SYSTEM_DATE_FORMAT); 377 df.setLenient(false); 378 retVal = df.parse(getParameter(name, "")); 379 } catch (java.text.ParseException e) { 380 } 382 383 return retVal; 384 } 385 386 public Double getParameterAsDouble(String name, double defaultValue) { 387 Double value = getParameterAsDouble(name); 388 389 return (value != null) ? value : new Double (defaultValue); 390 } 391 392 public Double getParameterAsDouble(String name) { 393 String value = getParameter(name); 394 try { 395 return (value != null) ? new Double (value) : null; 396 } catch (NumberFormatException e) { 397 return null; 398 } 399 } 400 401 public double getParameterAsDoublePrimitive(String name, double defaultValue) { 402 return getParameterAsDouble(name, defaultValue) 403 .doubleValue(); 404 } 405 406 public int getParameterAsInt(String name, int defaultValue) { 407 return getParameterAsInteger(name, defaultValue) 408 .intValue(); 409 } 410 411 public Integer getParameterAsInteger(String name, int defaultValue) { 412 Integer value = getParameterAsInteger(name); 413 414 return (value != null) ? value : new Integer (defaultValue); 415 } 416 417 public Integer getParameterAsInteger(String name) { 418 String value = getParameter(name); 419 try { 420 return (value != null) ? new Integer (value) : null; 421 } catch (NumberFormatException e) { 422 return null; 423 } 424 } 425 426 public Long getParameterAsLong(String name, long defaultValue) { 427 Long value = getParameterAsLong(name); 428 429 return (value != null) ? value : new Long (defaultValue); 430 } 431 432 public Long getParameterAsLong(String name) { 433 String value = getParameter(name); 434 try { 435 return (value != null) ? new Long (value) : null; 436 } catch (NumberFormatException e) { 437 return null; 438 } 439 } 440 441 public long getParameterAsLongPrimitive(String name, long defaultValue) { 442 return getParameterAsLong(name, defaultValue) 443 .longValue(); 444 } 445 446 452 public Long [] getParameterValuesLong(String name) { 453 String [] vals = getParameterValues(name); 454 if (vals != null) { 455 Long [] retVals = new Long [vals.length]; 456 for (int i = 0; i < vals.length; i++) { 457 try { 458 retVals[i] = (vals[i] != null) ? new Long (vals[i]) : null; 459 } catch (NumberFormatException e) { 460 retVals[i] = null; 461 } 462 } 463 464 return retVals; 465 } else { 466 return new Long [0]; 467 } 468 } 469 470 471 public UploadedFile getUploadedFile(String name) throws FileUploadException { 472 return (getMultipartRequest()).getFileParameter(name); 473 } 474 475 public void setWorkflowUser(CoefficientUser workflowUser) { 476 this.workflowUser = workflowUser; 477 } 478 479 483 public void beginWorkflowAction() { 484 if (!inWorkflowAction) { 485 tempUser = getCurrentUser(); 486 if (workflowUser != null) { 487 setSessionAttribute(Constants.USER_SESSION_STRING, workflowUser); 488 } 489 inWorkflowAction = true; 490 } 491 } 492 493 496 public void endWorkflowAction() { 497 if (inWorkflowAction) { 498 setSessionAttribute(Constants.USER_SESSION_STRING, tempUser); 499 try { 500 clearPage(); 501 } catch (IOException ioex) { 502 } 504 inWorkflowAction = false; 505 } 506 } 507 508 public String getLastNonHelpOp() { 509 return (String )getSessionAttribute(LAST_NON_HELP_OP); 510 } 511 512 public void setProject(Project project) { 513 this.session.put(CURRENT_PROJECT, project); 514 } 515 516 public Project getProject() { 517 return (Project)this.session.get(CURRENT_PROJECT); 518 } 519 520 public void invalidateSession() { 521 session.clear(); 522 invalidateSession = true; 523 } 524 525 531 public CoefficientUser getCurrentUser() { 532 return (CoefficientUser) getSessionAttribute(Constants.USER_SESSION_STRING); 533 } 534 535 public String getRequestURL() { 536 return requestURL; 537 } 538 539 550 protected void setProperty(Object bean, PropertyDescriptor prop) 551 throws Exception { 552 if (prop instanceof IndexedPropertyDescriptor ) { 553 throw new Exception (prop.getName() 554 + " is an indexed property (not supported)"); 555 } 556 557 Method setter = prop.getWriteMethod(); 558 if (setter == null) { 559 throw new Exception (prop.getName() + " is a read only property"); 560 } 561 562 Class propclass = prop.getPropertyType(); 563 Object [] args = { null }; 564 565 if (propclass == String .class) { 566 args[0] = getParameter(prop.getName()); 567 } else if ((propclass == Integer .class) || (propclass == Integer.TYPE)) { 568 args[0] = getParameterAsInteger(prop.getName()); 569 } else if ((propclass == Long .class) || (propclass == Long.TYPE)) { 570 args[0] = getParameterAsLong(prop.getName()); 571 } else if ((propclass == Boolean .class) || (propclass == Boolean.TYPE)) { 572 args[0] = new Boolean (getParameterAsBoolean(prop.getName())); 573 } else if ((propclass == Double .class) || (propclass == Double.TYPE)) { 574 args[0] = getParameterAsDouble(prop.getName()); 575 } else if (propclass == String [].class) { 576 args[0] = getParameterValues(prop.getName()); 577 } else if (propclass == Object .class) { 578 args[0] = getParameter(prop.getName()); 579 } else if (propclass == Date .class) { 580 args[0] = getParameterAsDate(prop.getName()); 581 } else if (propclass == java.util.Locale .class) { 582 args[0] = new java.util.Locale (getParameter(prop.getName())); 583 } else if (propclass == java.util.TimeZone .class) { 584 args[0] = 585 java.util.TimeZone.getTimeZone(getParameter(prop.getName())); 586 } else { 587 throw new Exception ("property " + prop.getName() 588 + " is of unsupported type " + propclass.toString()); 589 } 590 591 setter.invoke(bean, args); 592 } 593 594 protected void clearPage() throws IOException { 595 createPageObject(); 596 this.redirectionURL = null; 597 } 598 599 602 protected void createPageObject() throws IOException { 603 this.page_ = new Page(this); 604 } 605 606 private String getCallerClassName() { 607 StackTraceElement [] ste = new Exception ().getStackTrace(); 608 if ((ste == null) || (ste.length < 3)) { 609 return null; 610 } else { 611 return ste[2].getClassName(); 612 } 613 } 614 615 } 616 | Popular Tags |