Labels

Saturday, 3 August 2013

JASPER REPORT

 

Architecture

As shown in the above figure JasperReports architecture is based on declarative XML files which by convention have an extension of jrxml that contains the report layout. A lot of third-party design tools were produced to generate your jrxml file in a smooth way (like iReport or JasperAssistant) Design file is supposed to be filled by report's result which is fetched from database, XML files, Java collection, Comma-separated values or Models. Jasper can communicate with those data-sources and more, it can merge any number of data-sources together and manipulates the results of any combinations. This communication goes through JDBC, JNDI, XQuery, EJBQL, Hibernate or existing Oracle PL/SQL. You also can define your own data-source class and pass it to jasper engine directly. After defining your report design layout in jrxml format and determining your data source(s) jasper engine does the rest of work. It compiles your design file and fills it with results fetched from data-source and generates your report to the chosen exporting format (PDF, Excel, HTML, XML, RTF, TXT …, etc.)

Report Definition file structure (jrxml):

Jasper design file –jrxml- contains the following elements:
  • <jasperReport>: the root element.
  • <title>: its contents are printed only once at the beginning of the report
  • <pageHeader> - its contents are printed at the beginning of every page in the report.
  • <detail> - contains the body of the report, repeated by n number of results
  • <pageFooter> - its contents are printed at the bottom of every page in the report.
  • <band> - defines a report section, all of the above elements contain a band element as its only child element.
Only the root element is mandatory, the rest of elements are optional.

 

Environment

To set up working environment we need to download JasperReport jar file from the following URL: http://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579
And add the following jars to your project classpath:
  • jasperreports-2.0.4.jar
  • commons-digester-1.7.jar
  • commons-collections-2.1.jar (commons-collections.jar)
  • commons-logging-1.0.2.jar
  • commons-beanutils.jar
  • iText-2.0.7.jar (used infor PDF exporting)

Sample application

At this section we'll introduce a sample application that generates PDF, HTML and Excel files contain the results of our report which is built over Oracle database contains the following table:
ITEM
ITEM_ID ---- NUMBER(5) --- NOT NULL
CATEOGRY_ID ---- NUMBER(5) --- NOT NULL
ITEM_NAME ---- VARCHAR2(50) --- NOT NULL
ITEM_DESCIPTION ---- VARCHAR2(200)
ITEM_AMOUNT ---- NUMBER(5) ---- NOT NULL

Result: Report should retrieve the items with amount less than or equal 100 item.
We're going to divide the work into two steps:
  1. Generate the report design (jrxml file).
  2. Implement application that assigns data source, compiles jrxml file and exports result in the chosen format.

Source: http://java.dzone.com/articles/java-reporting-part-2.

No comments:

Post a Comment