1 64 65 package com.jcorporate.expresso.services.html; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.misc.StringUtil; 69 import com.jcorporate.expresso.ext.report.ExpressoReport; 70 import com.jcorporate.expresso.ext.report.ReportException; 71 72 import javax.servlet.http.HttpServletRequest ; 73 import javax.servlet.http.HttpServletResponse ; 74 import java.io.BufferedOutputStream ; 75 import java.io.FileOutputStream ; 76 import java.io.IOException ; 77 import java.io.OutputStream ; 78 import java.io.PrintWriter ; 79 import java.util.ArrayList ; 80 import java.util.Date ; 81 import java.util.HashMap ; 82 import java.util.Hashtable ; 83 import java.util.Iterator ; 84 import java.util.Map ; 85 import java.util.StringTokenizer ; 86 import java.util.Vector ; 87 88 89 100 public class ReportPage 101 extends HtmlElement implements ExpressoReport { 102 103 private String thisClass = this.getClass().getName() + "."; 104 private String reportTitle = null; 105 private Page myPage = null; 106 private Table currentTable = null; 107 private String fileName = null; 108 private HashMap myParams = null; 109 private Vector defaultParams = new Vector (1); 110 private String dbName = ""; 111 private String reportCode = ""; 112 113 118 public ReportPage() 119 throws HtmlException { 120 myPage = new Page("Report Page"); 121 } 122 123 124 129 public void setReportCode(String newCode) { 130 reportCode = newCode; 131 } 132 133 139 public void populateDefaultValues() throws DBException { 140 } 141 142 147 public String getReportCode() { 148 return reportCode; 149 } 150 151 159 public ReportPage(String newFileName, String newTitle) 160 throws HtmlException { 161 this(); 162 setFileName(newFileName); 163 setTitle(newTitle); 164 } 165 166 172 public synchronized void addLine(String newLine) 173 throws HtmlException { 174 sendLine(newLine); 175 } 176 177 178 185 protected void addParam(String newCode, String newDescrip, String newType, 186 String newDefaultValue) 187 throws HtmlException { 188 ReportPageParam newParam = new ReportPageParam(newCode, newDescrip, 189 newType, 190 newDefaultValue); 191 defaultParams.addElement(newParam); 192 } 193 194 195 202 public synchronized void addRow(String rowString) 203 throws HtmlException { 204 Row myRow = new Row(); 205 currentTable.add(myRow); 206 207 StringTokenizer stk = new StringTokenizer (rowString, "|"); 208 209 while (stk.hasMoreTokens()) { 210 myRow.add(new Cell(new Text(stk.nextToken()))); 211 } 212 } 213 214 215 220 public synchronized void addTimeStamp() 221 throws HtmlException { 222 Date Now = new Date (System.currentTimeMillis()); 223 myPage.add(new Paragraph(new Text(Now.toString()))); 224 } 225 226 227 233 public synchronized void close() 234 throws HtmlException { 235 save(); 236 } 237 238 239 public synchronized void display(HttpServletRequest req, 240 HttpServletResponse res, String charset) 241 throws HtmlException { 242 if (StringUtil.notNull(charset).equals("")) { 243 charset = "ISO-8859-1"; 244 } 245 246 res.setContentType("text/html; charset=" + charset); 247 248 try { 249 PrintWriter out = res.getWriter(); 250 display(out); 251 } catch (IOException ie) { 252 throw new HtmlException(ie); 253 } 254 } 255 256 257 263 public void display(PrintWriter out) 264 throws HtmlException { 265 myPage.display(out); 266 } 267 268 269 276 protected void display(PrintWriter out, int depth) 277 throws HtmlException { 278 display(out); 279 } 280 281 286 public synchronized void endTable() 287 throws HtmlException { 288 myPage.add(currentTable); 289 currentTable = null; 290 } 291 292 293 299 public Table getCurrentTable() 300 throws HtmlException { 301 return currentTable; 302 } 303 304 305 310 public String getDBName() { 311 return dbName; 312 } 313 314 315 320 public Vector getDefaultParams() { 321 return (Vector ) defaultParams.clone(); 322 } 323 324 325 334 public synchronized String getDefaultValue(String parameterName) { 335 StringUtil.assertNotBlank(parameterName, 336 "getDefaultValue(String) parameter: parameterName cannot be blank"); 337 for (Iterator i = defaultParams.iterator(); i.hasNext();) { 338 ReportPageParam oneParam = (ReportPageParam) i.next(); 339 if (parameterName.equals(oneParam.getCode())) { 340 return oneParam.getDefaultValue(); 341 } 342 } 343 344 throw new IllegalArgumentException (parameterName + " is not a parameter in the report"); 345 } 346 347 348 353 protected Page getPage() { 354 return myPage; 355 } 356 357 364 public String getParam(String paramCode) 365 throws HtmlException { 366 String myName = (thisClass + "getParam(String)"); 367 368 if (myParams == null) { 369 throw new HtmlException(myName + ":No parameters defined for " + 370 " this report. Can't get parameter '" + 371 paramCode + "'"); 372 } 373 374 String retVal = (String ) myParams.get(paramCode); 375 376 if (retVal == null) { 377 throw new HtmlException(myName + ":No such parameter as '" + 378 paramCode + "'"); 379 } 380 381 return retVal; 382 } 383 384 385 391 public synchronized Hashtable getParams() 392 throws HtmlException { 393 if (myParams == null) { 394 return new Hashtable (1); 395 } else { 396 return new Hashtable (myParams); 397 } 398 } 399 400 401 406 public String getTitle() { 407 return reportTitle; 408 } 409 410 415 public void save() 416 throws HtmlException { 417 String myName = (thisClass + "save()"); 418 419 try { 420 FileOutputStream fout = new FileOutputStream (fileName); 421 BufferedOutputStream bout = new BufferedOutputStream (fout); 422 PrintWriter out = new PrintWriter (bout); 423 myPage.display(out); 424 } catch (IOException ie) { 425 throw new HtmlException(myName + ":I/O Error writing " + fileName + 426 ":" + ie.getMessage()); 427 } 428 } 429 430 431 438 private void sendLine(String theLine) 439 throws HtmlException { 440 myPage.add(new Text(theLine)); 441 } 442 443 444 447 public synchronized void setDBName(String newDBName) { 448 dbName = StringUtil.notNull(newDBName); 449 } 450 451 457 public synchronized void setFileName(String newFileName) 458 throws HtmlException { 459 fileName = newFileName; 460 } 461 462 463 470 public synchronized void setFileName(String newFileName, String newTitle) 471 throws HtmlException { 472 setTitle(newTitle); 473 setFileName(newFileName); 474 } 475 476 477 483 public synchronized void setParams(Hashtable newParams) 484 throws HtmlException { 485 if (newParams != null) { 486 myParams = new HashMap (newParams); 487 } 488 } 489 490 491 496 public synchronized void setTitle(String newTitle) { 497 reportTitle = newTitle; 498 } 499 500 507 public synchronized void startTable(String caption, String colHeaders) 508 throws HtmlException { 509 currentTable = new Table(); 510 currentTable.setCaption(caption); 511 currentTable.setBorder(1); 512 currentTable.addHeading(colHeaders); 513 } 514 515 516 525 public void printReport(OutputStream os) throws ReportException, java.io.IOException { 526 try { 527 java.io.PrintWriter printWriter = new java.io.PrintWriter (os); 528 this.display(printWriter); 529 printWriter.flush(); 530 } catch (HtmlException ex) { 531 throw new ReportException("Error producing report", ex); 532 } 533 } 534 535 540 public void setReportParameters(Map parameters) { 541 if (myParams == null) { 542 myParams = new HashMap (parameters.size()); 543 } 544 if (defaultParams != null) { 546 for (Iterator i = this.defaultParams.iterator(); i.hasNext();) { 547 ReportPageParam param = (ReportPageParam) i.next(); 548 myParams.put(param.getCode(), param.getDefaultValue()); 549 } 550 } 551 552 553 for (Iterator i = parameters.keySet().iterator(); i.hasNext();) { 554 String key = (String ) i.next(); 555 String value = (String ) parameters.get(key); 556 if (value != null && value.length() > 0) { 557 myParams.put(key, value); 558 } 559 } 560 } 561 562 568 protected Map getReportParameters() { 569 return myParams; 570 } 571 572 577 public void setDataContext(String newDataContext) { 578 dbName = newDataContext; 579 } 580 581 586 protected String getDataContext() { 587 return dbName; 588 } 589 590 595 public String getReportMimeType() { 596 return "text/html"; 597 } 598 599 608 public String getReportFileExtension() { 609 return "html"; 610 } 611 612 620 public java.util.List getParameterNames() { 621 if (defaultParams != null && defaultParams.size() > 0) { 622 ArrayList al = new ArrayList (defaultParams.size()); 623 for (Iterator i = defaultParams.iterator(); i.hasNext();) { 624 ReportPageParam oneparams = (ReportPageParam) i.next(); 625 al.add(oneparams.getCode()); 626 } 627 return al; 628 } else { 629 return null; 630 } 631 } 632 633 } 634 635 | Popular Tags |