Using JasperReports with opentaps

From Opentaps Wiki
Revision as of 20:50, 26 October 2007 by Sichen (talk | contribs)
Jump to navigationJump to search

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.

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.