Advertisement
csaffi

Vaadin Jasper Report

Jun 30th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. public static void stampaReport(final String nomeFile, long p1) {
  2.         String path = "/com/myapp/reports/";
  3.         final String reportFile = path + nomeFile;
  4.         final HashMap parameters = new HashMap();
  5.         parameters.put("id", p1);
  6.        
  7.         try {
  8.             StreamResource.StreamSource source = new StreamResource.StreamSource() {
  9.  
  10.                 public InputStream getStream() {
  11.                     byte[] b = null;
  12.                     Connection conn = null;
  13.                     try {
  14.                         InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
  15.                         if (report == null) {
  16.                             Notification.show("No report!", Type.ERROR_MESSAGE);
  17.                             return null;
  18.                         }
  19.                         conn = getDbHelp().getJDBCConnectionPool().reserveConnection();
  20.                         b = JasperRunManager.runReportToPdf(report, parameters, conn);
  21.                         conn.commit();
  22.                     } catch (Exception e) {
  23.                         e.printStackTrace();
  24.                     } finally {
  25.                         getDbHelp().getJDBCConnectionPool().releaseConnection(conn);
  26.                     }
  27.  
  28.                     return new ByteArrayInputStream(b);
  29.                 }
  30.             };
  31.  
  32.             StreamResource resource = new StreamResource(source, "report.pdf");
  33.             resource.getStream().setParameter("Content-Disposition", "attachment;filename=\"report.pdf\"");
  34.             resource.setMIMEType("application/pdf");
  35.             resource.setCacheTime(0);
  36.            
  37.             Window w = new Window();
  38.             w.setModal(true);
  39.             w.setCloseShortcut(KeyCode.ESCAPE, null);
  40.             w.setResizable(false);
  41.             //w.setClosable(false);
  42.             w.setHeight(90.0f, Unit.PERCENTAGE);
  43.             w.setWidth(90.0f, Unit.PERCENTAGE);
  44.            
  45.             BrowserFrame e = new BrowserFrame("Print", resource);
  46.             e.setSizeFull();
  47.            
  48.             VerticalLayout layout = new VerticalLayout();
  49.             layout.setSizeFull();
  50.             layout.addComponent(e);
  51.            
  52.             w.setContent(layout);
  53.             UI.getCurrent().addWindow(w);
  54.             w.focus();
  55.         } catch (Exception e) {
  56.             e.printStackTrace();
  57.             Notification.show("Error during report generation!", Type.ERROR_MESSAGE);
  58.         }
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement