1 14 package org.compiere.print; 15 16 import java.io.*; 17 import java.util.*; 18 import java.sql.*; 19 import javax.xml.transform.stream.*; 20 21 import java.awt.print.*; 22 import javax.print.*; 23 import javax.print.attribute.*; 24 import javax.print.attribute.standard.*; 25 import javax.print.event.*; 26 27 import org.apache.ecs.*; 28 import org.apache.ecs.xhtml.*; 29 import org.apache.log4j.Logger; 30 31 import org.compiere.util.*; 32 import org.compiere.model.*; 33 import org.compiere.print.layout.*; 34 35 import com.qoppa.pdfPrinter.PDFPrinterJob; 38 39 47 public class ReportEngine implements PrintServiceAttributeListener 48 { 49 55 public ReportEngine (Properties ctx, MPrintFormat pf, MQuery query) 56 { 57 if (pf == null) 58 throw new IllegalArgumentException ("ReportEngine - no PrintFormat"); 59 log.debug(pf + " - " + query); 60 m_ctx = ctx; 61 m_printFormat = pf; 62 setQuery(query); } 65 66 private Logger log = Logger.getLogger (getClass()); 67 68 69 private Properties m_ctx; 70 71 72 private MPrintFormat m_printFormat; 73 74 private MQuery m_query; 75 76 private PrintData m_printData; 77 78 private LayoutEngine m_layout = null; 79 80 private String m_printerName = Ini.getProperty(Ini.P_PRINTER); 81 82 private View m_view = null; 83 84 85 90 protected void setPrintFormat (MPrintFormat pf) 91 { 92 m_printFormat = pf; 93 if (m_layout != null) 94 { 95 setPrintData(); 96 m_layout.setPrintFormat(pf, false); 97 m_layout.setPrintData(m_printData, m_query, true); } 99 if (m_view != null) 100 m_view.revalidate(); 101 } 103 108 protected void setQuery (MQuery query) 109 { 110 m_query = query; 111 if (query == null) 112 return; 113 setPrintData(); 115 if (m_layout != null) 116 m_layout.setPrintData(m_printData, m_query, true); 117 if (m_view != null) 118 m_view.revalidate(); 119 } 121 125 public MQuery getQuery() 126 { 127 return m_query; 128 } 130 135 private void setPrintData() 136 { 137 if (m_query == null) 138 return; 139 DataEngine de = new DataEngine(m_printFormat.getLanguage()); 140 setPrintData(de.getPrintData (m_ctx, m_printFormat, m_query)); 141 } 144 148 public void setPrintData (PrintData printData) 149 { 150 if (printData == null) 151 return; 152 m_printData = printData; 153 } 155 156 157 160 private void layout() 161 { 162 if (m_printFormat == null) 163 throw new IllegalStateException ("ReportEngine.layout - no print format"); 164 if (m_printData == null) 165 throw new IllegalStateException ("ReportEngine.layout - no print data"); 166 m_layout = new LayoutEngine (m_printFormat, m_printData, m_query); 167 } 169 173 protected LayoutEngine getLayout() 174 { 175 if (m_layout == null) 176 layout(); 177 return m_layout; 178 } 180 184 public String getName() 185 { 186 return m_printFormat.getName(); 187 } 189 193 public MPrintFormat getPrintFormat() 194 { 195 return m_printFormat; 196 } 198 202 public Properties getCtx() 203 { 204 return m_layout.getCtx(); 205 } 207 211 public int getRowCount() 212 { 213 return m_printData.getRowCount(); 214 } 216 220 public int getColumnCount() 221 { 222 if (m_layout != null) 223 return m_layout.getColumnCount(); 224 return 0; 225 } 227 228 229 233 public View getView() 234 { 235 if (m_layout == null) 236 layout(); 237 if (m_view == null) 238 m_view = new View (m_layout); 239 return m_view; 240 } 242 243 244 251 public void print (boolean withDialog, int copies, boolean isDocument, String printerName) 252 { 253 log.info("print - Copies=" + copies + ", Document=" + isDocument + ", Printer=" + printerName); 254 if (m_layout == null) 255 layout(); 256 Print print = new Print (m_layout, false); 257 PrintRequestAttributeSet prats = m_layout.getPaper().getPrintRequestAttributeSet(); 259 if (isDocument || copies < 1) 261 prats.add (new Copies(1)); 262 else 263 prats.add (new Copies(copies)); 264 Locale locale = Language.getLanguage().getLocale(); 265 prats.add(new JobName(m_printFormat.getName(), locale)); 266 prats.add(PrintUtil.getJobPriority(m_layout.getPageCount(), copies, true)); 267 268 PrinterJob job = getPrinterJob(printerName); 270 job.setPageable(print); 272 if (withDialog && !job.printDialog(prats)) 274 return; 275 276 boolean printCopy = isDocument && copies > 1; 278 PrintUtil.print(job, prats, false, printCopy); 279 280 if (printCopy) 282 { 283 log.info("print - Copy " + (copies-1)); 284 print = new Print (m_layout, true); prats.add(new Copies(copies-1)); 286 job = getPrinterJob(printerName); 287 job.setPageable (print); 289 PrintUtil.print(job, prats, false, false); 290 } 291 } 293 297 public void attributeUpdate(PrintServiceAttributeEvent psae) 298 { 299 311 System.out.println(psae); 312 PrintUtil.dump (psae.getAttributes()); 313 } 315 316 321 private PrinterJob getPrinterJob (String printerName) 322 { 323 if (printerName != null && printerName.length() > 0) 324 return CPrinter.getPrinterJob(printerName); 325 return CPrinter.getPrinterJob(m_printerName); 326 } 328 332 public void pageSetupDialog () 333 { 334 if (m_layout == null) 335 layout(); 336 m_layout.pageSetupDialog(getPrinterJob(m_printerName)); 337 if (m_view != null) 338 m_view.revalidate(); 339 } 341 345 public void setPrinterName(String printerName) 346 { 347 if (printerName == null) 348 m_printerName = Ini.getProperty(Ini.P_PRINTER); 349 else 350 m_printerName = printerName; 351 } 353 357 public String getPrinterName() 358 { 359 return m_printerName; 360 } 362 363 364 371 public boolean createHTML (File file, boolean onlyTable, Language language) 372 { 373 try 374 { 375 Language lang = language; 376 if (lang == null) 377 lang = Language.getLanguage(); 378 FileWriter fw = new FileWriter (file, false); 379 return createHTML (new BufferedWriter(fw), onlyTable, lang); 380 } 381 catch (FileNotFoundException fnfe) 382 { 383 log.error("createHTML(f) - " + fnfe.toString()); 384 } 385 catch (Exception e) 386 { 387 log.error("createHTML(f)", e); 388 } 389 return false; 390 } 392 399 public boolean createHTML (Writer writer, boolean onlyTable, Language language) 400 { 401 try 402 { 403 table table = new table(); 404 for (int row = -1; row < m_printData.getRowCount(); row++) 407 { 408 tr tr = new tr(); 409 table.addElement(tr); 410 if (row != -1) 411 m_printData.setRowIndex(row); 412 for (int col = 0; col < m_printFormat.getItemCount(); col++) 414 { 415 MPrintFormatItem item = m_printFormat.getItem(col); 416 if (item.isPrinted()) 417 { 418 if (row == -1) 420 { 421 th th = new th(); 422 tr.addElement(th); 423 th.addElement(Util.maskHTML(item.getPrintName(language))); 424 } 425 else 426 { 427 td td = new td(); 428 tr.addElement(td); 429 Object obj = m_printData.getNode(new Integer (item.getAD_Column_ID())); 430 if (obj == null) 431 td.addElement(" "); 432 else if (obj instanceof PrintDataElement) 433 { 434 String value = ((PrintDataElement)obj).getValueDisplay(language); td.addElement(Util.maskHTML(value)); 436 } 437 else if (obj instanceof PrintData) 438 { 439 } 441 else 442 log.error("createHTML - Element not PrintData(Element) " + obj.getClass()); 443 } 444 } } } 448 PrintWriter w = new PrintWriter(writer); 450 if (onlyTable) 451 table.output(w); 452 else 453 { 454 XhtmlDocument doc = new XhtmlDocument(); 455 doc.appendBody(table); 456 doc.output(w); 457 } 458 w.flush(); 459 w.close(); 460 } 461 catch (Exception e) 462 { 463 log.error("createHTML(w)", e); 464 } 465 return false; 466 } 468 469 470 471 478 public boolean createCSV (File file, char delimiter, Language language) 479 { 480 try 481 { 482 FileWriter fw = new FileWriter (file, false); 483 return createCSV (new BufferedWriter(fw), delimiter, language); 484 } 485 catch (FileNotFoundException fnfe) 486 { 487 log.error("createCSV(f) - " + fnfe.toString()); 488 } 489 catch (Exception e) 490 { 491 log.error("createCSV(f)", e); 492 } 493 return false; 494 } 496 503 public boolean createCSV (Writer writer, char delimiter, Language language) 504 { 505 if (delimiter == 0) 506 delimiter = '\t'; 507 try 508 { 509 for (int row = -1; row < m_printData.getRowCount(); row++) 511 { 512 StringBuffer sb = new StringBuffer (); 513 if (row != -1) 514 m_printData.setRowIndex(row); 515 516 boolean first = true; for (int col = 0; col < m_printFormat.getItemCount(); col++) 519 { 520 MPrintFormatItem item = m_printFormat.getItem(col); 521 if (item.isPrinted()) 522 { 523 if (first) 525 first = false; 526 else 527 sb.append(delimiter); 528 if (row == -1) 530 createCSVvalue (sb, delimiter, 531 m_printFormat.getItem(col).getPrintName(language)); 532 else 533 { 534 Object obj = m_printData.getNode(new Integer (item.getAD_Column_ID())); 535 String data = ""; 536 if (obj == null) 537 ; 538 else if (obj instanceof PrintDataElement) 539 { 540 data = ((PrintDataElement)obj).getValueDisplay(null); } 542 else if (obj instanceof PrintData) 543 { 544 } 545 else 546 log.error("createCSV - Element not PrintData(Element) " + obj.getClass()); 547 createCSVvalue (sb, delimiter, data); 548 } 549 } } writer.write(sb.toString()); 552 writer.write(Env.NL); 553 } writer.flush(); 556 writer.close(); 557 } 558 catch (Exception e) 559 { 560 log.error("createCSV(w)", e); 561 } 562 return false; 563 } 565 572 private void createCSVvalue (StringBuffer sb, char delimiter, String content) 573 { 574 if (content == null || content.length() == 0) 576 return; 577 boolean needMask = false; 579 StringBuffer buff = new StringBuffer (); 580 char chars[] = content.toCharArray(); 581 for (int i = 0; i < chars.length; i++) 582 { 583 char c = chars[i]; 584 if (c == '"') 585 { 586 needMask = true; 587 buff.append(c); } else if (!needMask && (c == delimiter || !Character.isLetterOrDigit(c))) 590 needMask = true; 591 buff.append(c); 592 } 593 594 if (needMask) 596 sb.append('"').append(buff).append('"'); 597 else 598 sb.append(buff); 599 } 601 602 603 608 public boolean createXML (File file) 609 { 610 try 611 { 612 FileWriter fw = new FileWriter (file, false); 613 return createXML (new BufferedWriter(fw)); 614 } 615 catch (FileNotFoundException fnfe) 616 { 617 log.error("createXML(f) - " + fnfe.toString()); 618 } 619 catch (Exception e) 620 { 621 log.error("createXML(f)", e); 622 } 623 return false; 624 } 626 631 public boolean createXML (Writer writer) 632 { 633 try 634 { 635 m_printData.createXML(new StreamResult(writer)); 636 writer.flush(); 637 writer.close(); 638 } 639 catch (Exception e) 640 { 641 log.error("createXML(w)", e); 642 } 643 return false; 644 } 646 647 648 653 public File getPDF() 654 { 655 return getPDF(null); 656 } 658 663 public File getPDF (File file) 664 { 665 if (file == null) 666 { 667 try 668 { 669 file = File.createTempFile ("ReportEngine", ".pdf"); 670 } 671 catch (Exception ex) 672 { 673 log.error("getFile", ex); 674 } 675 } 676 if (createPDF (file)) 677 return file; 678 return null; 679 } 681 686 public boolean createPDF (File file) 687 { 688 try 689 { 690 if (file == null) 691 file = File.createTempFile ("ReportEngine", ".pdf"); 692 693 if (m_layout == null) 695 layout (); 696 Print print = new Print (m_layout, false); 697 698 PrintRequestAttributeSet prats = m_layout.getPaper (). 700 getPrintRequestAttributeSet (); 701 prats.add (new JobName (file.getAbsolutePath (), Locale.getDefault ())); 702 prats.add (new Destination (file.toURI ())); 704 705 711 if (false) 712 Class.forName("com.qoppa.pdfPrinter.PDFPrinterJob"); 713 PDFPrinterJob job = (PDFPrinterJob)PDFPrinterJob.getPrinterJob (); 715 job.setPrintable(print, m_layout.getPageFormat()); 717 log.debug("createPDF - " + file.getAbsolutePath() + " - " + file.length()); 718 job.print(file.getAbsolutePath()); 719 log.info("createPDF - " + file.getAbsolutePath() + " - " + file.length()); 720 721 722 744 745 return true; 746 } 747 catch (ClassNotFoundException cnf) 748 { 749 log.error("Download http://www.qoppa.com/demo/jPDFPrinter.jar and save it in the lib directory"); 750 } 751 catch (Exception e) 752 { 753 log.error("createPDF", e); 754 } 755 return false; 757 } 759 760 761 766 public boolean createPS (File file) 767 { 768 try 769 { 770 return createPS (new FileOutputStream(file)); 771 } 772 catch (FileNotFoundException fnfe) 773 { 774 log.error("createPS(f) - " + fnfe.toString()); 775 } 776 catch (Exception e) 777 { 778 log.error("createPS(f)", e); 779 } 780 return false; 781 } 783 788 public boolean createPS (FileOutputStream fos) 789 { 790 try 791 { 792 String outputMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType(); 793 DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; 794 StreamPrintServiceFactory[] spsfactories = 795 StreamPrintServiceFactory.lookupStreamPrintServiceFactories(docFlavor, outputMimeType); 796 if (spsfactories.length == 0) 797 { 798 log.error("createPS(fos) - No StreamPrintService"); 799 return false; 800 } 801 StreamPrintService sps = spsfactories[0].getPrintService(fos); 804 if (m_layout == null) 806 layout(); 807 Print print = new Print (m_layout, false); 808 sps.createPrintJob().print(print, new HashPrintRequestAttributeSet()); 810 fos.flush(); 812 fos.close(); 813 } 814 catch (Exception e) 815 { 816 log.error("createPS(fos)", e); 817 } 818 return false; 819 } 821 822 823 824 828 public static void main(String [] args) 829 { 830 org.compiere.Compiere.startupClient(); 831 int AD_Table_ID = 100; 833 MQuery q = new MQuery("AD_Table"); 834 q.addRestriction("AD_Table_ID", "<", 108); 835 MPrintFormat f = MPrintFormat.createFromTable(Env.getCtx(), AD_Table_ID); 837 ReportEngine re = new ReportEngine(Env.getCtx(), f, q); 838 re.layout(); 839 846 re.print(true, 1, false, "Epson Stylus COLOR 900 ESC/P 2"); re.print(false, 1, false, "Epson Stylus COLOR 900 ESC/P 2"); System.exit(0); 850 } 851 } | Popular Tags |