ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Dynamic Creation of Reports with Apache Formatting Objects
Pages: 1, 2, 3, 4

Accessing Data Content

Data producers access data from data sources and convert the data into well-formed XML representing the report data content. In the creation of a report, one or more data producers will access specific types of data from one or more data sources. For simplicity, it is best to make each data producer type correspond to only one data source.



Data producers are created by the report-creation subsystem while parsing a report template. Report templates define the type of data producer needed to fill in the data content. This framework is designed to read through a report template using a SAX parser and to look for a <reporting> tag. Each <reporting> tag has an attribute that defines the class name of the data producer to use. The sample code includes a simple report template called customerNumbers.xml. It defines one data producer class to be created.

<?xml version="1.0" encoding="UTF-8"?>
<addresses>
   <reporting producer="com.company.reporting.dataproducers.CustomerDataProducer">
   </reporting>
</addresses>

ReportCreation creates a TemplateHandler, which extends the SAX handler. It handles callbacks as the XML is read by an XML parser. When tags are parsed, the startElement method is called. If a <reporting tag is found, a DataProducerFactory is created and a data producer of the type defined in the tag attribute is requested to be created by the factory. This newly-created data producer is used to create the data content by calling the process method. The handler streams the resulting data content on to an output stream.


Figure 5. Class used to created data content with DataProducers.

From the included example code and database, the resulting XML with the data content looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<addresses>
   <customer>
      <customername>SuperCom</customername>
      <customerphone>305-777-4632</customerphone>
   </customer>
   <customer>
      <customername>LivingstonEnterprises</customername>
      <customerphone>305-456-8888</customerphone>
   </customer>
   <customer>
      <customername>Oak Computers</customername>
      <customerphone>214-999-1234</customerphone>
   </customer>
   <customer>
      <customername>MicroApple</customername>
      <customerphone>555-275-9900</customerphone>
   </customer>
   <customer>
      <customername>HostProCom</customername>
      <customerphone>650-456-8876</customerphone>
   </customer>
</addresses>

This is only the data content for the report to be generated. Styles can now be independently applied to the data.

Notification

Notification implementation used for this example is Java Mail. The reporting system uses Java Mail to email messages about the completion status of a report if the asynchronous request interface is used. A properties file defines the required values for mail notification. A notification message is defined based on the completion status of report generation and sent to the specified user.

Putting it All Together

The code included with this article implements the design described. The code represents a working framework to be used for dynamic report generation. It can be extended and evolve as needed. All source code, libraries used, compiled class files, and sample database are included. Executing the code can be done with a test client or from a servlet running in a Web server environment.

Test Client

There is a com.company.reporting.Tester class defined that can be run as a unit tester. This class takes the name of a properties file as an input argument. There is a reporting.properties file included. The properties file defines the location of template file, style sheet, and output directories. It also contains the properties used for email notification properties. The tester is run from the source directory using:

java -cp .;../lib/activation.jar;../lib/avalon-frameworkcvs20020315.jar;
           ../lib/fop.jar;../lib/mail.jar;../lib/pbclient35RE.jar;
           ../lib/xalan-2.3.1.jar;../lib/xercesImpl-2.0.1.jar 
           com.company.reporting.Tester reporting.properties 

Related Reading

Apache: The Definitive Guide
By Ben Laurie, Peter Laurie

Servlet

The example also includes a servlet that calls the reporting service. The ReportRequester.html file provides a form allowing the selection of multiple styles and output formats to be applied to a report. The report can be returned synchronously to the requesting client browser or stored as a file. The sample code class files and .jar files need to be deployed to the Web server's WEB-INF directory and configured according to Web server specifications.

Database

The sample uses a sample PointBase 3.5 database for data. If you wish to use the sample database, the demo database server must be downloaded and installed. The pbclient35RE.jar archive includes all of the client driver code needed to run the sample. Alternatively, you could use another database and associated software and write your own data producers to connect and access data content.

Summary

With the help of some excellent, freely-available open source software libraries and a few sleepless nights of development, it was possible to develop a framework that supports creating reports dynamically from database content. This framework also provides a mechanism to apply different document styles and output formats. The basic requirements that defined what the framework is supposed to perform, an overall architecture, design details, and an implementation of the framework are provided. The result is a basis for a reporting solution using some of today's defined standards and available open source tools. The architecture is meant to be extensible so security features can be added, multiple data sources can be incorporated, and new output formats can be included. There is a bit of a learning curve to understand all of the technologies used, but the working implementation provided shortens the time to deploy a working system.

Resources

Additional XML Resources

Kevin Hartig is a senior Java architect at Sun's Professional Services Java Center.


Return to ONJava.com.