Advertisement
Guest User

Untitled

a guest
Jun 29th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. public void stampaReport(final String nomeFile, long p1, long p2, boolean autoprint) {
  2.         String path = "/com/reports";
  3.         final String reportFile = path + File.separatorChar + nomeFile;
  4.         final HashMap parameters = new HashMap();
  5.         if (p1 > 0 && p2 > 0) {
  6.             parameters.put("numlista", p1);
  7.             parameters.put("anno", p2);
  8.         } else if (p1 > 0 && p2 <= 0) {
  9.             parameters.put("id", p1);
  10.         }
  11.  
  12.         if (autoprint) {
  13.             Connection conn = null;
  14.             try {
  15.                 InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
  16.                 if (report == null) {
  17.                     mainWindow.showNotification("No report!");
  18.                     return;
  19.                 }
  20.                 conn = getDbHelp().getJDBCConnectionPool().reserveConnection();
  21.                 JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
  22.                 conn.commit();
  23.                 HtmlExporter exporterHTML = new HtmlExporter();
  24.                 final ByteArrayOutputStream output = new ByteArrayOutputStream();
  25.  
  26.                 SimpleExporterInput exporterInput = new SimpleExporterInput(print);
  27.                 exporterHTML.setExporterInput(exporterInput);
  28.                 HtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(output);
  29.                 exporterHTML.setExporterOutput(exporterOutput);
  30.  
  31.                 SimpleHtmlExporterConfiguration exporterConfig = new SimpleHtmlExporterConfiguration();
  32.                 exporterConfig.setHtmlFooter("<script type='text/javascript'>window.print();</script>");
  33.                 exporterHTML.setConfiguration(exporterConfig);
  34.  
  35.                 exporterHTML.exportReport();
  36.                 output.flush();
  37.  
  38.                 StreamResource.StreamSource source = new StreamResource.StreamSource() {
  39.                     public InputStream getStream() {
  40.                         byte[] b = null;
  41.                         b = output.toByteArray();
  42.  
  43.                         return new ByteArrayInputStream(b);
  44.                     }
  45.                 };
  46.  
  47.                 StreamResource resource = new StreamResource(source, "report.html", getInstance());
  48.                 resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"report.html\"");
  49.                 resource.setMIMEType("text/html");
  50.                 resource.setCacheTime(0);
  51.  
  52.                 mainWindow.open(resource, "_new");
  53.             } catch (Exception e) {
  54.                 e.printStackTrace();
  55.             } finally {
  56.                 getDbHelp().getJDBCConnectionPool().releaseConnection(conn);
  57.             }
  58.         } else {
  59.             try {
  60.                 StreamResource.StreamSource source = new StreamResource.StreamSource() {
  61.  
  62.                     public InputStream getStream() {
  63.                         byte[] b = null;
  64.                         Connection conn = null;
  65.                         try {
  66.                             InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
  67.                             if (report == null) {
  68.                                 mainWindow.showNotification("No report!");
  69.                                 return null;
  70.                             }
  71.                             conn = getDbHelp().getJDBCConnectionPool().reserveConnection();
  72.                             b = JasperRunManager.runReportToPdf(report, parameters, conn);
  73.                             conn.commit();
  74.                         } catch (Exception e) {
  75.                             e.printStackTrace();
  76.                         } finally {
  77.                             getDbHelp().getJDBCConnectionPool().releaseConnection(conn);
  78.                         }
  79.  
  80.                         return new ByteArrayInputStream(b);
  81.                     }
  82.                 };
  83.  
  84.                 StreamResource resource = new StreamResource(source, "report.pdf", getInstance());
  85.                 resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"report.pdf\"");
  86.                 resource.setMIMEType("application/pdf");
  87.                 resource.setCacheTime(0);
  88.  
  89.                 mainWindow.open(resource, "_new");
  90.             } catch (Exception e) {
  91.                 e.printStackTrace();
  92.             }
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement