Jasper reports on Vaadin sample

Introduction

I meet JasperReports some years ago and I liked this report library; this year I did need to implement a report on a personal project using Vaadin, but surprisingly I was not able to found a sample of this, so I did this little sample and article.

First, you will need a JDK Maven and Mysql in order to try the sample, and you can download the code here: http://sourceforge.net/projects/jrtutorial/files/VaadinJRSample/

There is a README.txt file you can follow in order to run the sample, basically you need to:

  1. Create database running resources/database.sql on Mysql or MariaDB

  2. Compile the entire project: run “mvn install”.

  3. Deploy the application in Jetty: run “mvn jetty:run”

  4. Go to http://localhost:8080/ in your browser

Implementation

Let’s see the sample code step by step.
The data is only a person table with some data.
The main class MyUI.java has two UI components (the report generating button and a list component used to show current data in database.):

Java

  1. final Button reportGeneratorButton = new Button("Generate report");
  2. layout.addComponent(reportGeneratorButton);
  3. layout.addComponent(new PersonList());

The list is implemented on PersonList.java, I am using a FilteringTable (https://vaadin.com/directory/component/filteringtable), that loads the data using a Vaadin SQLContainer:

Java

  1. SQLContainer container=null;
  2. TableQuery tq = new TableQuery("person", new ConnectionUtil().getJDBCConnectionPool());
  3. container = new SQLContainer(tq);
  4. filterTable = buildPagedTable(container);

And the SQLContainer is provided with a JDBCConnectionPool created from a properties file (resources/database.properties):

Java

  1. Properties prop=PropertiesUtil.getProperties();
  2. public JDBCConnectionPool getJDBCConnectionPool(){
  3. JDBCConnectionPool pool = null;
  4. try {
  5. pool = new SimpleJDBCConnectionPool(
  6. prop.getProperty("database.driver"),
  7. prop.getProperty("database.url"),
  8. prop.getProperty("database.userName"),
  9. prop.getProperty("database.password"));
  10. } catch (SQLException e) {
  11. e.printStackTrace();
  12. }
  13. return pool;

The report generation is implemented on ReportGenerator class, this class loads the report template:

Java

  1. File templateFile=new File(templatePath);
  2. JasperDesign jasperDesign = JRXmlLoader.load(templateFile);

Compile report template:

Java

  1. jasperReport = JasperCompileManager.compileReport(jasperDesign);

Fill report with data:

Java

  1. HashMap fillParameters=new HashMap();
  2. JasperPrint jasperPrint = JasperFillManager.fillReport(
  3. jasperReport,
  4. fillParameters,
  5. conn);

Export the jasperPrint object to Pdf format:

Java

  1. JRPdfExporter exporter = new JRPdfExporter();
  2. exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
  3. exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
  4. exporter.exportReport();

And finally execute all the logic to generate the report and sent it to an OutputStream:

Java

  1. JasperDesign jasperDesign=loadTemplate(templatePath);
  2. setTempDirectory(templatePath);
  3. JasperReport jasperReport=compileReport(jasperDesign);
  4. JasperPrint jasperPrint=fillReport(jasperReport, conn);
  5. exportReportToPdf(jasperPrint, outputStream);

But all the logic at ReportGenerator.java is called from the ReportUtil class, this class is the responsible to connect Vaadin layer with ReportGenerator layer. There are two methods: the first one is prepareForPdfReport, this method creates a database connection, generates the report as a StreamResource (calling the another method) and finally extends the source button with a FileDownloader component in order to upload the generated report stream, so all the uploading magic is done by FileDownloader extension (https://vaadin.com/api/com/vaadin/server/FileDownloader.html):

Java

  1. Connection conn=new ConnectionUtil().getSQLConnection();
  2. reportOutputFilename+=("_"+getDateAsString()+".pdf");
  3. StreamResource myResource =createPdfResource(conn,reportTemplate,reportOutputFilename);
  4. FileDownloader fileDownloader = new FileDownloader(myResource);
  5. fileDownloader.extend(buttonToExtend);

The second method createPdfResource, uses ReportGenerator class in order to return the generated report as a StreamResource:

Java

  1. return new StreamResource(new StreamResource.StreamSource() {
  2. @Override
  3. public InputStream getStream () {
  4. ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
  5. ReportGenerator reportGenerator=new ReportGenerator();
  6. try {
  7. reportGenerator.executeReport(baseReportsPath+templatePath, conn, pdfBuffer);
  8. } catch (JRException e) {
  9. e.printStackTrace();
  10. }
  11. return new ByteArrayInputStream(
  12. pdfBuffer.toByteArray());
  13. }
  14. }, reportFileName);

So, in order to call the report generator process when only need to call ReportUtil like we did in ‘MyUI.java’:

Java

  1. final Button reportGeneratorButton = new Button("Generate report");
  2. new ReportsUtil().prepareForPdfReport("/reports/PersonListReport.jrxml",
  3. "PersonList",
  4. reportGeneratorButton);

Finally, the jasper report design can be found in the WEB-INF/personListReport.jrxml file

This is a picture of the sample running and the generated report:

Running sample

And that’s all, I expect to help someone with this sample, thanks for reading.