Difference between revisions of "Using JasperReports with opentaps"
LeonTorres (talk | contribs) |
LeonTorres (talk | contribs) |
||
Line 11: | Line 11: | ||
An in depth [[Tutorial_iReports | tutorial on using iReports is available]]. | An in depth [[Tutorial_iReports | tutorial on using iReports is available]]. | ||
− | After designing the report, you will most likely be making further programmatic | + | ==Editing the Report== |
+ | |||
+ | After designing the report, you will most likely be making further modifications. There are two types of modifications, display and programmatic. Display adjustments are done with iReport. Programmatic adjustments are usually made by editing the XML directly. An example of a common programmatic control is to prevent the footer from being displayed in formats that are not paginated, such as an Excel spreadsheet or plain text file. | ||
+ | |||
+ | A typical development cycle for a report might be as follows, | ||
# Set up a data source with sample data for the report in iReport | # Set up a data source with sample data for the report in iReport | ||
# Define the way the report is to be displayed using iReport | # Define the way the report is to be displayed using iReport | ||
# Define parameters that should be dynamic and provide defaults in iReport | # Define parameters that should be dynamic and provide defaults in iReport | ||
− | # Edit the XML directly to add additional programmatic controls | + | # Edit the XML directly to add additional programmatic controls |
− | |||
# Trial run the report in opentaps | # Trial run the report in opentaps | ||
# Make further design adjustments in iReport and to XML directly | # Make further design adjustments in iReport and to XML directly | ||
− | + | One thing to remember is that iReport will overwrite any changes you make directly to the XML. Save a backup whenever using iReport. | |
==Serving a Simple Report Directly== | ==Serving a Simple Report Directly== | ||
+ | First ensure that the following handler is defined in the controller.xml for the target application, | ||
+ | <handler name="jasperreports" type="view" class="org.opentaps.common.reporting.jasper.JasperReportsViewHandler"/> | ||
+ | Next, define a view-map that will trigger the report generation. Use the content-type attribute to specify the output format. For example, if we want the URI MyReport to serve MyReport.jrxml as a PDF, | ||
+ | <view-map name="MyReport" type="jasperreports" page="/reports/MyReport.jrxml" content-type="application/pdf" encoding="none"/> | ||
− | + | The location of the .jrxml resource is relative to the <tt>webapp/</tt> directory in your application. The following formats are available, | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | * text/xml | |
− | + | * text/html | |
− | + | * text/csv (CSV comma delimited file) | |
− | + | * application/rtf (RTF) | |
− | + | * text/plain (Plain Text) | |
− | + | * application/vnd.oasis.opendocument.text (Open Document Text) | |
+ | * application/vnd.ms-excel (Microsoft Excel Spreadsheet) | ||
− | + | ==Parametrizing Reports== | |
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: | 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: |
Revision as of 19:54, 29 February 2008
Contents
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.
An in depth tutorial on using iReports is available.
Editing the Report
After designing the report, you will most likely be making further modifications. There are two types of modifications, display and programmatic. Display adjustments are done with iReport. Programmatic adjustments are usually made by editing the XML directly. An example of a common programmatic control is to prevent the footer from being displayed in formats that are not paginated, such as an Excel spreadsheet or plain text file.
A typical development cycle for a report might be as follows,
- Set up a data source with sample data for the report in iReport
- Define the way the report is to be displayed using iReport
- Define parameters that should be dynamic and provide defaults in iReport
- Edit the XML directly to add additional programmatic controls
- Trial run the report in opentaps
- Make further design adjustments in iReport and to XML directly
One thing to remember is that iReport will overwrite any changes you make directly to the XML. Save a backup whenever using iReport.
Serving a Simple Report Directly
First ensure that the following handler is defined in the controller.xml for the target application,
<handler name="jasperreports" type="view" class="org.opentaps.common.reporting.jasper.JasperReportsViewHandler"/>
Next, define a view-map that will trigger the report generation. Use the content-type attribute to specify the output format. For example, if we want the URI MyReport to serve MyReport.jrxml as a PDF,
<view-map name="MyReport" type="jasperreports" page="/reports/MyReport.jrxml" content-type="application/pdf" encoding="none"/>
The location of the .jrxml resource is relative to the webapp/ directory in your application. The following formats are available,
- text/xml
- text/html
- text/csv (CSV comma delimited file)
- application/rtf (RTF)
- text/plain (Plain Text)
- application/vnd.oasis.opendocument.text (Open Document Text)
- application/vnd.ms-excel (Microsoft Excel Spreadsheet)
Parametrizing Reports
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.