1 package org.enhydra.snapper.presentation; 2 10 11 import java.sql.SQLException ; 12 import java.util.ArrayList ; 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 import java.util.StringTokenizer ; 16 17 18 import org.enhydra.xml.xmlc.XMLObject; 19 import org.enhydra.snapper.Log; 20 21 import org.w3c.dom.Attr ; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.NamedNodeMap ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 import org.w3c.dom.html.HTMLElement; 28 import org.w3c.dom.html.HTMLInputElement; 29 30 import com.lutris.appserver.server.httpPresentation.HttpPresentation; 31 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms; 32 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 33 import com.lutris.appserver.server.sql.DBTransaction; 35 import com.lutris.util.Config; 36 import org.enhydra.dods.DODS; 37 38 43 public abstract class BasePO implements HttpPresentation { 44 45 46 49 protected DBTransaction dbTransaction = null; 50 51 52 55 protected HttpPresentationComms comms = null; 56 57 58 62 Exception errorObj; 63 64 67 protected String infoText; 68 69 72 protected String errorText; 73 74 77 protected int pageId = -1; 78 79 82 85 public static String paramDelimiter = ";"; 86 87 90 public static String ENCODING = "UTF-8"; 91 92 97 public DBTransaction getTransaction() { 98 return this.dbTransaction; 99 } 100 101 105 public void setError(Exception err) { 106 if (this.errorObj == null) { 107 Log.logException(err); 108 this.errorObj = err; 109 } 110 } 111 112 117 public void setError(Exception err, boolean override) { 118 if (override) 119 this.errorObj = err; 120 else if (this.errorObj == null) 121 this.errorObj = err; 122 } 123 124 128 public Exception getError() { 129 return this.errorObj; 130 } 131 132 137 public void clearError() { 138 this.errorObj = null; 139 } 140 141 145 146 147 153 protected String getParamValue(String parameters, String paramName) { 154 String retVal = ""; 155 if (parameters != null && !parameters.equals("")) { 156 StringTokenizer st = new StringTokenizer (parameters, BasePO.paramDelimiter); 157 while (st.hasMoreTokens()) { 158 String param = st.nextToken(); 159 String paramNameIn = param.substring(0, param.indexOf("=")); 160 if (paramName.equalsIgnoreCase(paramNameIn)) { 161 retVal = param.substring(param.indexOf("=") + 1); 162 } 163 } 164 } 165 return retVal; 166 } 167 168 173 protected String formParameterList(String [] arg) { 174 StringBuffer retVal = new StringBuffer (""); 175 for (int i = 0; i < arg.length; i++) { 176 if (i != 0) 177 retVal.append(paramDelimiter); 178 retVal.append(arg[i]); 179 } 180 return retVal.toString(); 181 } 182 183 188 protected String [] reformParameterList(String str) { 189 StringTokenizer tokenizer = new StringTokenizer (str, paramDelimiter); 190 ArrayList list = new ArrayList (); 191 while (tokenizer.hasMoreTokens()) 192 list.add(tokenizer.nextToken()); 193 return (String []) list.toArray(new String [0]); 194 } 195 196 201 protected String getStartSearchURL(String searchPath) { 202 StringTokenizer tokenizer = new StringTokenizer (searchPath, "/"); 203 String returnPage = ""; 204 if (tokenizer.hasMoreTokens()) 205 returnPage = tokenizer.nextToken(); 206 207 return returnPage; 208 } 209 210 215 protected String getLevelUp(String searchPath) { 216 String levelUpSearchPath = ""; 217 int lastIndexOfSlach = searchPath.lastIndexOf("/"); 218 if (lastIndexOfSlach != -1) 219 levelUpSearchPath = searchPath.substring(0, lastIndexOfSlach); 220 else 221 levelUpSearchPath = ""; 222 223 return levelUpSearchPath; 224 } 225 226 231 protected String getReturnURL(String searchPath) { 232 String returnPage = ""; 233 StringTokenizer tokenizer = new StringTokenizer (searchPath, "/"); 234 while (tokenizer.hasMoreTokens()) 235 returnPage = tokenizer.nextToken(); 236 237 return returnPage; 238 } 239 240 245 protected String getFromURL(String searchPath) { 246 searchPath = getLevelUp(searchPath); 247 if (searchPath.equals("")) 248 return searchPath; 249 250 return getReturnURL(searchPath); 251 } 252 253 261 protected void addHiddenInputField(String name, String value, HTMLElement root) { 262 HTMLInputElement field = (HTMLInputElement) root.getOwnerDocument().createElement("INPUT"); 263 field.setAttribute("type", "hidden"); 264 field.setAttribute("name", name); 265 field.setAttribute("value", value); 266 root.appendChild(field); 267 } 268 269 276 protected HTMLInputElement createInputField(Hashtable attributes, Document document) { 277 HTMLInputElement field = (HTMLInputElement) document.createElement("INPUT"); 278 Enumeration keys = attributes.keys(); 279 String key = ""; 280 String value = ""; 281 while (keys.hasMoreElements()) { 282 key = keys.nextElement().toString(); 283 value = attributes.get(key).toString(); 284 field.setAttribute(key, value); 285 } 286 return field; 287 } 288 289 298 protected void addHiddenInputFields(String paramNamePrefix, HTMLElement root) throws HttpPresentationException { 299 300 Enumeration params = this.comms.request.getParameterNames(); 301 while (params.hasMoreElements()) { 302 String name = (String ) params.nextElement(); 303 if (name.startsWith(paramNamePrefix)) 304 this.addHiddenInputField(name, this.comms.request.getParameter(name), root); 305 else 306 continue; 307 } 308 309 } 310 311 324 protected void addHiddenInputFields(String paramNamePrefix, HTMLElement root, 325 String [] exludePrefix) throws HttpPresentationException { 326 327 if (exludePrefix == null || exludePrefix.length == 0) 328 this.addHiddenInputFields(paramNamePrefix, root); 329 else { 330 Enumeration params = this.comms.request.getParameterNames(); 331 while (params.hasMoreElements()) { 332 String name = (String ) params.nextElement(); 333 if (name.startsWith(paramNamePrefix) && !this.isIn(name, exludePrefix)) { 334 this.addHiddenInputField(name, this.comms.request.getParameter(name), root); 335 } 336 else { 337 continue; 338 } 339 } 340 } 341 } 342 343 public boolean isIn(String searchVal, String [] definedGroup) { 344 if (definedGroup == null || definedGroup.equals("")) { 345 return false; 346 } 347 for (int i = 0; i < definedGroup.length; i++) { 348 if (searchVal == null && definedGroup[i] == null) { 349 return true; 350 } 351 else if (searchVal != null && searchVal.equalsIgnoreCase(definedGroup[i])) { 352 return true; 353 } 354 } 355 return false; 356 } 357 358 364 public void run(HttpPresentationComms comms) throws HttpPresentationException { 365 366 boolean doCommit = true; 367 try { 368 initTransaction(); 369 comms.request.getHttpServletRequest().setCharacterEncoding(BasePO.ENCODING); 370 this.comms = comms; 371 372 comms.response.setEncoding(BasePO.ENCODING); 373 XMLObject dom = this.getDOM(); 374 375 378 379 if (dom != null) 380 comms.response.writeDOM(dom); 381 } 382 catch (Exception ex) { 383 doCommit = false; 384 Log.logException(ex); 385 386 } 387 finally { 388 if (doCommit && this.getError() == null) { 391 try{ 392 this.commitTransaction(); 393 } catch (Exception e) {} 394 } else { 395 if(getError() != null) 396 Log.logException(getError()); 397 try{ 398 this.releaseTransaction(); 399 } catch (Exception e){} 400 } 401 } 402 } 403 404 412 public void removeIdAttrFromTree(Node rootNode) { 413 414 if (rootNode.hasAttributes()) { 415 NamedNodeMap nMap = rootNode.getAttributes(); 416 for (int i = 0; i < nMap.getLength(); i++) { Node attribFromList = nMap.item(i); 418 if (attribFromList.getNodeName().equalsIgnoreCase("id")) { 419 Element parent = ( (Attr ) attribFromList).getOwnerElement(); 420 parent.removeAttributeNode( (Attr ) attribFromList); 421 break; } 423 } 424 } 425 426 if (rootNode.hasChildNodes()) { 427 NodeList nList = rootNode.getChildNodes(); 428 for (int i = 0; i < nList.getLength(); i++) { 429 Node fromList = nList.item(i); 430 if (fromList.getNodeType() == Node.ELEMENT_NODE) { removeIdAttrFromTree(fromList); 433 } 434 } 435 } 436 } 437 438 443 protected Config getAppConfiguration() { 444 return this.comms.application.getConfig(); 445 } 446 447 451 protected void listAllParameters() { 452 try { 453 Enumeration paramNames = this.comms.request.getParameterNames(); 454 while (paramNames.hasMoreElements()) { 455 String parameter = (String ) paramNames.nextElement(); 456 String value = (String )this.comms.request.getParameter(parameter); 457 org.enhydra.snapper.Log.logToFile("parameter = " + parameter + ", value = " + value); 458 } 459 } 460 catch (Exception ex) { 461 } 462 } 463 464 471 protected abstract XMLObject getDOM() throws Exception ; 472 473 475 476 479 private void initTransaction() throws Exception { 480 try { 481 482 this.dbTransaction = DODS.getDatabaseManager().createTransaction(); 483 } 484 catch (Exception ex) { 485 throw new Exception ("Could not init transaction", ex); 486 } 487 } 488 489 495 private void commitTransaction() throws Exception { 496 try { 497 this.dbTransaction.commit(); 498 } 499 catch (Throwable ex) { 500 Log.logException(ex); 501 throw new Exception ("Could not commit transaction", ex); 502 } 503 finally { 504 this.releaseTransaction(); 505 } 506 } 507 508 514 520 521 private void releaseTransaction() throws Exception { 522 try { 523 dbTransaction.release(); 524 } 525 catch (Exception ex) { 526 throw new Exception ("Could not commit transaction", ex); 527 } 528 } 529 530 531 public void writeTransaction() throws SQLException { 532 dbTransaction.write(); 533 } 534 535 public static void printFreeMemory(String prefix) { 536 System.out.println("========================================================"); 537 System.out.println(prefix + ", free memory = " + (Runtime.getRuntime().freeMemory() / 1024) / 1024 + " MB"); 538 System.out.println(prefix + ", max memory = " + (Runtime.getRuntime().maxMemory() / 1024) / 1024 + " MB"); 539 System.out.println(prefix + ", total memory = " + (Runtime.getRuntime().totalMemory() / 1024) / 1024 + " MB"); 540 } 541 542 public static void show(String s) { 543 javax.swing.JOptionPane.showConfirmDialog(null, s); 544 } 545 546 553 protected boolean getBoolParameter(String name) { 554 boolean retVal = false; 555 String value = null; 556 try { 557 value = this.comms.request.getParameter(name); 558 if (value != null && value.equals("true")) 559 retVal = true; 560 } 561 catch (Exception e) { 562 retVal = false; 563 } 564 return retVal; 565 } 566 567 } | Popular Tags |