Difference between revisions of "Using JasperReports with opentaps"

From Opentaps Wiki
Jump to navigationJump to search
m
Line 1: Line 1:
With opentaps, you can define reports with [http://jasperreports.sourceforge.net/ JasperReports] either manually or using the [http://www.jasperforge.org/sf/projects/ireport iReport] report writer and then upload them to the opentaps server for processing.   
+
__TOC__
 +
 
 +
==Overview==
 +
 
 +
JasperReports is a powerful reporting engine that provides a way to design a report once and generate output in numerous formats including PDF, plain text, HTML and Excel.  The opentaps system provides a convenient way to embed and serve these reports from within an application.  It also provides a means to automate the creation of input fields for Jasper parameters so that you can create dynamic reports on the fly.  Reports can also be served as desired without having to use the features provided by opentaps.
 +
 
 +
==Designing Reports with iReport==
 +
 
 +
A JasperReport is defined in an XML language called JasperReport XML.  The schema is complex and lengthly, particularily when it comes to defining the appearance and layout.  It is highly recommended to use [http://www.jasperforge.org/sf/projects/ireport iReport], which is a visual report designer that writes these XML files.  The primary use of iReport is to design the visual aspect of the report.  After designing the report, you will most likely be making further programmatic modifications to the XML directly.  It is useful to think of the development of a report as follows,
 +
 
 +
1.  Set up a data source with sample data for the report in iReport
 +
2.  Define the way the report is to be displayed using iReport
 +
3.  Define parameters that should be dynamic and provide defaults in iReport
 +
4.  Edit the XML directly to add additional programmatic controls, such as not printing a page footer if the report is not PDF
 +
5.  Edit the XML directly and add hooks to let it be embedded in opentaps
 +
6.  Trial run the report in opentaps
 +
7.  Make further design adjustments in iReport and to XML directly
 +
 
 +
==Serving a Simple Report Directly==
 +
 
 +
 
 +
 
 +
 
 +
 
 +
==Designing a Report==
 +
 
 +
With opentaps, you can define reports with [http://jasperreports.sourceforge.net/ JasperReports] either manually or using the [http://www.jasperforge.org/sf/projects/ireport iReport] report writer and then upload them to the opentaps server for processing.  It is recommended to use iReport.
 +
 
 +
Once you have a report XML file,
 +
 
 +
 
 +
 
 +
 
  
 
To use Jasper in your custom application you should incorporate the view handler in your controller.xml:
 
To use Jasper in your custom application you should incorporate the view handler in your controller.xml:

Revision as of 19:37, 29 February 2008

Overview

JasperReports is a powerful reporting engine that provides a way to design a report once and generate output in numerous formats including PDF, plain text, HTML and Excel. The opentaps system provides a convenient way to embed and serve these reports from within an application. It also provides a means to automate the creation of input fields for Jasper parameters so that you can create dynamic reports on the fly. Reports can also be served as desired without having to use the features provided by opentaps.

Designing Reports with iReport

A JasperReport is defined in an XML language called JasperReport XML. The schema is complex and lengthly, particularily when it comes to defining the appearance and layout. It is highly recommended to use iReport, which is a visual report designer that writes these XML files. The primary use of iReport is to design the visual aspect of the report. After designing the report, you will most likely be making further programmatic modifications to the XML directly. It is useful to think of the development of a report as follows,

1. Set up a data source with sample data for the report in iReport 2. Define the way the report is to be displayed using iReport 3. Define parameters that should be dynamic and provide defaults in iReport 4. Edit the XML directly to add additional programmatic controls, such as not printing a page footer if the report is not PDF 5. Edit the XML directly and add hooks to let it be embedded in opentaps 6. Trial run the report in opentaps 7. Make further design adjustments in iReport and to XML directly

Serving a Simple Report Directly

Designing a Report

With opentaps, you can define reports with JasperReports either manually or using the iReport report writer and then upload them to the opentaps server for processing. It is recommended to use iReport.

Once you have a report XML file,



To use Jasper in your custom application you should incorporate the view handler in your controller.xml:

 <handler name="jasperreports" type="view" class="org.opentaps.common.reporting.jasper.JasperReportsViewHandler"/>

This view handler will render the jasper reports XML file in a variety of formats available. Then, upload your reports into the opentaps webapp/ in a sub-directory and define view-map for your report:

  <view-map name="MyReport" type="jasperreports" page="/reports/MyReport.jrxml" content-type="application/pdf" encoding="none"/>

Possible values for content-type are text/xml (XML), text/html (HTML), text/csv (CSV), application/rtf (RTF), text/plain (TXT), application/vnd.oasis.opendocument.text (ODT), and application/vnd.ms-excel (XLS) This content-type is a default content type in case the report itself does not specify one.

When the report is run on the opentaps server, the data could be prepared by a beanshell (.bsh) script and generated using the entity engine. This would override the SQL in the jrxml itself. To use a beanshell script, change the request-handler in the opentaps controller.xml to reference it, like this:

<request-map uri="myReport">
    <security https="true" auth="true"/>
    <event type="bsf" path="/reports/" invoke="myReport.bsh"/>
    <response name="success" type="view" value="myReport"/>
    <response name="error" type="view" value="myReportSetup"/>
</request-map> 

In your beanshell script, if you put a jrDatasource into the request as an attribute like this:

 // set up my query
 iterator = delegator.findListIteratorByCondition("EntityXXXX", new EntityConditionList(whereConditions, EntityOperator.AND), null, selectList, null, null);
 jrDataSource = new JREntityListIteratorDataSource(iterator);
 request.setAttribute("jrDataSource", jrDataSource);

opentaps will use the jrDatasource and ask JasperReports to fill in the report with these parameters. Otherwise, it will use the JDBC connection for the "org.ofbiz" entity group, which is basically everything, as the JDBC connection for your report.

You can also reference internationalized your jasper reports by using $R{...} syntax, such as:

   $R{FinancialsReport.TotalSales.Title}

which would reference the FinancialsReport.TotalSales.Title in the FinancialUILabels.properties file. $R{...} is a resource bundle which uses the opentaps UtilMessages.getUiLabels(..) method to get the UI labels from opentaps. It can be used for text fields in Jasper reports.