From: <max...@us...> - 2009-10-06 07:23:21
|
Revision: 3885 http://uni-d.svn.sourceforge.net/uni-d/?rev=3885&view=rev Author: max_brod Date: 2009-10-06 07:23:08 +0000 (Tue, 06 Oct 2009) Log Message: ----------- [UNI-479] Added config setting: "pdfReports" (a list of report names separated by commas) - for these reports, the report will be requested and printed as pdf. Modified Paths: -------------- trunk/Uni-d/tool/src/main/java/be/unid/tool/print/PrintWorker.java trunk/Uni-d/tool/src/main/java/be/unid/tool/print/configuration/PrintSectionSettings.java Modified: trunk/Uni-d/tool/src/main/java/be/unid/tool/print/PrintWorker.java =================================================================== --- trunk/Uni-d/tool/src/main/java/be/unid/tool/print/PrintWorker.java 2009-10-05 14:54:38 UTC (rev 3884) +++ trunk/Uni-d/tool/src/main/java/be/unid/tool/print/PrintWorker.java 2009-10-06 07:23:08 UTC (rev 3885) @@ -38,16 +38,16 @@ import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; -import javax.print.PrintService; +import javax.print.*; import java.awt.print.PrinterJob; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; +import java.io.*; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; +import com.lowagie.tools.Executable; + /** * Class that provides the logic of the print application * @@ -58,8 +58,10 @@ extends Worker<PrintConfiguration> { public static final String REPORT_TYPE_PRINT = "print"; + public static final String REPORT_TYPE_PDF = "pdf"; private static final int NO_RETRIES_FIND_PRINTER = 3; private static final int SLEEP_FIND_PRINTER = 10000; + private static final String DEFAULT_PDF_TEMP_FILE = "c:/tmp.pdf"; private URL reportURL; private ArrayList<String> classes; @@ -68,6 +70,8 @@ private PrintService printService; private String forcedPortraitReports[]; private String forcedLandscapeReports[]; + private String pdfReports[]; + private String pdfTempFile; public PrintWorker( PrintConfiguration cfg, Id id ) { @@ -79,6 +83,13 @@ printService = getPrintService( cfg.getPrintSectionSettings().getPrinter() ); forcedPortraitReports = cfg.getPrintSectionSettings().getForcedPortraitReports(); forcedLandscapeReports = cfg.getPrintSectionSettings().getForcedLandscapeReports(); + pdfReports = cfg.getPrintSectionSettings().getPdfReports(); + pdfTempFile = cfg.getPrintSectionSettings().getPdfTempFile(); + if ( pdfTempFile == null || pdfTempFile.length() == 0 ) + { + pdfTempFile = DEFAULT_PDF_TEMP_FILE; + if ( log.isDebugEnabled() ) log.debug( "pdfTempFile is blank. Using default." ); + } if ( log.isDebugEnabled() ) { log.debug( "Listing all existing printers:" ); @@ -161,6 +172,18 @@ { try { + boolean pdfReport = false; + if ( pdfReports != null ) + { + for ( String reportName : pdfReports ) + { + if ( reportName.equals( info.getReportName() ) ) + { + pdfReport = true; + break; + } + } + } if ( log.isDebugEnabled() ) { log.debug( "found report for printing " + info.getReportName() + " : " + info ); @@ -172,7 +195,7 @@ PostMethod post = new PostMethod( reportURL.toString() ); post.setParameter( "file", info.getReportName() ); - post.setParameter( "type", REPORT_TYPE_PRINT ); + post.setParameter( "type", ( pdfReport ) ? REPORT_TYPE_PDF : REPORT_TYPE_PRINT ); post.setParameter( "username", username ); post.setParameter( "password", password ); post.setParameter( "language", info.getReportLanguage() ); @@ -190,31 +213,56 @@ httpConn.executeMethod( post ); InputStream in = post.getResponseBodyAsStream(); - JasperPrint print = (JasperPrint) ( new ObjectInputStream( in ) ).readObject(); + if ( !pdfReport ) + { + JasperPrint print = (JasperPrint) ( new ObjectInputStream( in ) ).readObject(); - if ( print.getPages().size() == 0 ) + if ( print.getPages().size() == 0 ) + { + if ( log.isDebugEnabled() ) + { + log.debug( + "Report " + info.getReportName() + " has no pages. Report creation date: " + + info.getTime() ); + } + listener.notifyInfo( ToolListener.STATUS_ENDED, className, + i18n.txt( "nopages", workerId ) ); + listener.notifyPersistentMessage( + i18n.txt( "finished", workerId ) + " " + className + " " + + i18n.txt( "nopages", workerId ) ); + return false; + } + listener.notifyInfo( ToolListener.STATUS_ENDED, className, i18n.txt( "ok", workerId ) ); + listener + .notifyPersistentMessage( i18n.txt( "finished", workerId ) + " " + className + " " + + i18n.txt( "ok", workerId ) ); + + if ( log.isDebugEnabled() ) log.debug( "print report " + info.getReportName() + " : " + info ); + PrintService ps = getPrintService( info.getPrinterName() ); + new Printer( print, ps != null ? ps : printService ) + .print( forcedPortraitReports, forcedLandscapeReports ); + } + else { - if ( log.isDebugEnabled() ) + FileOutputStream file = new FileOutputStream( pdfTempFile ); + BufferedInputStream is = new BufferedInputStream( in ); + byte[] buf = new byte[4 * 1024]; + int bytesRead; + while ( ( bytesRead = is.read( buf ) ) != -1 ) { - log.debug( - "Report " + info.getReportName() + " has no pages. Report creation date: " + info.getTime() ); + file.write( buf, 0, bytesRead ); } - listener.notifyInfo( ToolListener.STATUS_ENDED, className, - i18n.txt( "nopages", workerId ) ); - listener.notifyPersistentMessage( - i18n.txt( "finished", workerId ) + " " + className + " " + - i18n.txt( "nopages", workerId ) ); - return false; + is.close(); + file.close(); + Executable ex = new Executable(); + ex.printDocumentSilent( pdfTempFile, true ); + + /*PrintService printService=getPrintService( info.getPrinterName() ); + if (printService==null) printService= PrintServiceLookup.lookupDefaultPrintService(); + DocPrintJob printerJob = printService.createPrintJob(); + SimpleDoc simpleDoc = new SimpleDoc( new File( "C:/test.pdf" ).toURL(), DocFlavor.URL.PDF, null ); + printerJob.print( simpleDoc, null ); */ } - listener.notifyInfo( ToolListener.STATUS_ENDED, className, i18n.txt( "ok", workerId ) ); - listener - .notifyPersistentMessage( i18n.txt( "finished", workerId ) + " " + className + " " + - i18n.txt( "ok", workerId ) ); - - if ( log.isDebugEnabled() ) log.debug( "print report " + info.getReportName() + " : " + info ); - PrintService ps = getPrintService( info.getPrinterName() ); - new Printer( print, ps != null ? ps : printService ) - .print( forcedPortraitReports, forcedLandscapeReports ); return true; } Modified: trunk/Uni-d/tool/src/main/java/be/unid/tool/print/configuration/PrintSectionSettings.java =================================================================== --- trunk/Uni-d/tool/src/main/java/be/unid/tool/print/configuration/PrintSectionSettings.java 2009-10-05 14:54:38 UTC (rev 3884) +++ trunk/Uni-d/tool/src/main/java/be/unid/tool/print/configuration/PrintSectionSettings.java 2009-10-06 07:23:08 UTC (rev 3885) @@ -55,13 +55,16 @@ public static final String INI_SETTINGS_SECTION_ATTRIBUTE_PRINTER = "printer"; public static final String INI_SETTINGS_SECTION_ATTRIBUTE_FORCED_PORTRAIT_REPORTS = "forcedPortraitReports"; public static final String INI_SETTINGS_SECTION_ATTRIBUTE_FORCED_LANDSCAPE_REPORTS = "forcedLandscapeReports"; + public static final String INI_SETTINGS_SECTION_ATTRIBUTE_PDF_REPORTS = "pdfReports"; + public static final String INI_SETTINGS_SECTION_ATTRIBUTE_PDF_TEMP_FILE = "pdfTempFile"; - private long scanPeriod; private String printer; private URL reportURL; private String forcedPortraitReports[]; private String forcedLandscapeReports[]; + private String pdfReports[]; + private String pdfTempFile; public PrintSectionSettings( INIFile ini, String sectionPrefix, SectionGeneral cfg ) throws ToolException @@ -100,13 +103,19 @@ if ( value != null ) printer = value; // forced portrait reports - value=section.get(INI_SETTINGS_SECTION_ATTRIBUTE_FORCED_PORTRAIT_REPORTS); - if (value!=null) forcedPortraitReports=value.split( ","); + value = section.get( INI_SETTINGS_SECTION_ATTRIBUTE_FORCED_PORTRAIT_REPORTS ); + if ( value != null ) forcedPortraitReports = value.split( "," ); // forced portrait reports - value=section.get(INI_SETTINGS_SECTION_ATTRIBUTE_FORCED_LANDSCAPE_REPORTS); - if (value!=null) forcedLandscapeReports=value.split( ","); + value = section.get( INI_SETTINGS_SECTION_ATTRIBUTE_FORCED_LANDSCAPE_REPORTS ); + if ( value != null ) forcedLandscapeReports = value.split( "," ); + // reports to be printed as pdf's + value = section.get( INI_SETTINGS_SECTION_ATTRIBUTE_PDF_REPORTS ); + if ( value != null ) pdfReports = value.split( "," ); + + // path to save pdf file to print + pdfTempFile = section.get( INI_SETTINGS_SECTION_ATTRIBUTE_PDF_TEMP_FILE ); } private void initURL( SectionGeneral cfg, INIFile ini, String resolvedSectionId ) @@ -167,4 +176,14 @@ { return forcedLandscapeReports; } + + public String[] getPdfReports() + { + return pdfReports; + } + + public String getPdfTempFile() + { + return pdfTempFile; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |