Develop advanced reports on JBoss with Pentaho

Pentaho Reporting is an easy-to-use, open source, lightweight suite of Java projects built for report generation. In this tutorial we will show how you can embed Pentaho Reporting into a Web application deployed on JBoss AS.

Pantaho Reporting is an advanced reporting solution which is part of the Pentaho suite. Pentaho reporting is made up of a Reporting Designer and a standard Api for accessing resources.

Your your first step is to download Pentaho Report Designer. Surf to http://reporting.pentaho.org, and follow the link to download the Report Designer.

Once you’ve downloaded the binary distribution of the Report Designer, create a
directory on your machine and unzip the contents of the package into that directory.

Now from the unpacked archive launch the shell report-designer.bat or report-designer.sh if you are running Linux.

pentaho report jboss reporting

The Report Designer first snapshot will ask if you want to create a Report using the Wizard or a New Report. We’ll create a “New Report” using the latter option.

Our report will render data from an Oracle table. Choose from the Menu : Data | Add DataSource | JDBC.

There you have a few set of SampleData available but we want to simulate a real world example, so we will add a new DataSource by clicking on the (+) button.

pentaho report jboss reporting

Next screen requires to enter the JDBC connection details which should be quite straightforward to you. At the end of the process hit Test Connection to verify that the connection is correctly configured.

pentaho report jboss reporting

Please note that Oracle driver doesn’t ship in the default lib of the Report Designer. You have to add the appropriate driver in the report-designer/lib before launching the Designer.
 
Ok. At this point you can return to the JDBC DataSource window and add one Query which will be used by your report. (Consider adding a Max preview row if the amount of rows is too much large to display in a single report).

pentaho report jboss reporting tutorial

Ok. We’re almost done. We have one query definition which will be used in our Report. In the main Designer window you can design your Report in full What-You-See-Is-What-You-Get (WYSIWYG) mode.

Select the “Data” tab in the upper right section of the Designer. There you can see in the Data Sets tree the list of your DataSets. You should find your query there as well.

pentaho report jboss reporting tutorial

At this point you actually draw your report. In the left side of your screen you have objects, labels and graphics which can be drag on the sections of the report. From the Data tree you can drag the single field.

pentaho report jboss reporting tutorial

In this sample we have added four labels in the Report Header and the relative fields in the Details section. Very intuitive and easy. Reminds me good old Microsoft Access, isn’t it 🙂 ?

That’s all. You can preview your report by clicking on the green arrow (Run) in the Toolbar. Don’t forget to save it as Report Bundle Definition (.prpt). This file will be directly accessed by your Java Api.

pentaho report jboss reporting tutorial

Ok Done with the Report Designer. Now we will code a Java Servlet to access your Report. Create a new Web application and add the following libraries (which can be found in the lib folder of Pentaho Designer) :

  • report-designer/lib/itext-1.5.2.jar
  • report-designer/lib/poi-3.0.1-jdk122-final-20071014.jar
  • report-designer/lib/libxml-1.1.5.jar
  • report-designer/lib/libbase-1.1.5.jar
  • report-designer/lib/libcss-1.1.3.jar
  • report-designer/lib/libdocbundle-1.1.6.jar
  • report-designer/lib/libfonts-1.1.5.jar
  • report-designer/lib/libformat-1.1.5.jar
  • report-designer/lib/libformula-1.1.5.jar
  • report-designer/lib/libformula-ui-1.1.5.jar
  • report-designer/lib/libloader-1.1.5.jar
  • report-designer/lib/libpixie-1.1.5.jar
  • report-designer/lib/librepository-1.1.5.jar
  • report-designer/lib/libserializer-1.1.5.jar
  • report-designer/lib/libsparkline-1.1.4.jar
  • report-designer/lib/pentaho-reporting-engine-classic-extensions-3.6.0-GA.jar
  • report-designer/lib/pentaho-reporting-engine-classic-core-3.6.0-GA.jar

As you can see, basically to access Pentaho reports you have to add iText and Poi libraries, then the set of “libX” which are used to by the Report and finally the reporting Engine core libraries and extensions.

Now add a Servlet to your Web project. The purpose of this Servlet is to render the Report just created using HTML format:

package test;

import java.io.IOException;
import java.net.URL;

import javax.servlet.*;
import javax.servlet.http.*;

import org.pentaho.reporting.engine.classic.core.*;
import org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.PdfReportUtil;
import org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlReportUtil;

import org.pentaho.reporting.libraries.resourceloader.*;

 
public class ReportServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    public ReportServlet() {
        super();
        
    }
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        // TODO: Initialize the Reporting Engine
        ClassicEngineBoot.getInstance().start();
        }
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResourceManager manager = new ResourceManager();
        manager.registerDefaults();
        String reportPath = "file:" +
        this.getServletContext().getRealPath("sampleReport.prpt");
        
        try {
            Resource res = manager.createDirectly(new URL(reportPath),
                    MasterReport.class);
            MasterReport report = (MasterReport) res.getResource();
                         HtmlReportUtil.createStreamHTML(report, response.getOutputStream());
                         
        } catch (Exception e) {
             e.printStackTrace();
        }
    }

     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

}
In this example, the Report is loaded via the ServletContext’s RealPath method which scans for the file in the root directory of the Web application.
Report.war
?   sampleReport.prpt
?
?
????WEB-INF
?   web.xml
?
????classes
?   ?   
?   ?
?   ????test
?           ReportServlet.class
?
????lib
itext-1.5.2.jar
libbase-1.1.5.jar
libcss-1.1.3.jar
libdocbundle-1.1.6.jar
libfonts-1.1.5.jar
libformat-1.1.5.jar
libformula-1.1.5.jar
libformula-ui-1.1.5.jar
libloader-1.1.5.jar
libpixie-1.1.5.jar
librepository-1.1.5.jar
libserializer-1.1.5.jar
libsparkline-1.1.4.jar
libxml-1.1.5.jar
pentaho-reporting-engine-classic-core-3.6.0-GA.jar
pentaho-reporting-engine-classic-extensions-3.6.0-GA.jar
poi-3.0.1-jdk122-final.jar-20071014.jar

Here an HTML file report was generated. Adding output support for other formats is just as easy. For example, using a PDF Report just requires:

response.setContentType(“application/pdf”);  
PdfReportUtil.createPDF(report, response.getOutputStream());

or if you prefer an Excel Report:

response.setContentType(“application/vnd.ms-excel”);
ExcelReportUtil.createXLS(report, response.getOutputStream());

That’s all. Verify that your Servlet correctly renders the Report and enjoy further reading at: http://www.pentaho.com/products/reporting/

Found the article helpful? if so please follow us on Socials