Skip to content

Foundset Based Reports Guidelines

Gabi edited this page Apr 13, 2020 · 2 revisions

The usage of a foundset as a datasource for a report is one of the core features of the plugin. In the case of a foundset based report, when exporting the report (i.e. using the @runReport@ function of the plugin), the report filling step of the export process is executed on the client. This situation requires special handling of resources (subreports/images/style templates). The current wiki page describes how resource loading works in this context, with an emphasis on the server-client scenario. IMPORTANT NOTE: In what follows, we are using Java based expressions inside reports. Although "Groovy is reported to be fully Java compatible":http://community.jaspersoft.com/wiki/creating-expressions-using-groovy, several problems have been reported over time, regarding the usage of Groovy as reporting language and Java expressions inside a report. Therefore, we recommend that you set the report scripting language to Java when using Java based expressions inside the report.

Subreports

If a report contains a subreport, a call to the JasperReportsResourceLoader.loadReport function should be included in the subreportExpression as follows:

<pre>
<subreport>
   ...
   <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[com.servoy.plugins.jasperreports.JasperReportsResourceLoader.loadReport("subreport.jrxml")]]></subreportExpression>
</subreport>
</pre>

In plugin versions above 4.0.0, the subreport given as a parameter should be a jrxml file, not a jasper (compiled report) file anymore (which was a requirement for the "3.2 version"). Compilation will be done under the hood by the plugin. If a compiled jasper file already exists, then that one will be used. It is recommended to let the plugin do the compiling in this scenario, due to classpath and other resource loading problems which might arise if compilation is done with iReport (which might not have all necessary libraries in its classpath, nor access to all needed resources). Therefore it is better to have no compiled subreport jasper files and only pass the jrxml to @loadReport@ in order for the plugin to do the compilation. The jrxml file must reside in a location relative to the (server set) reports directory. For instance:

<pre>
<subreport>
   <dataSourceExpression><![CDATA[$F{customers_to_orders}]]></dataSourceExpression>
   <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[com.servoy.plugins.jasperreports.JasperReportsResourceLoader.loadReport("/subreports/subreport.jrxml")]]></subreportExpression>
</subreport>
</pre>

where @subreports@ is location relative to the reports directory.

It is possible to use report parameters as well, in the @loadReport@ call, as follows:

<pre>
<subreport>
   <dataSourceExpression><![CDATA[$F{customers_to_orders}]]></dataSourceExpression>
   <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[com.servoy.plugins.jasperreports.JasperReportsResourceLoader.loadReport($P{SUBREPORT_DIR} + "subreport.jrxml")]]></subreportExpression>
</subreport>
</pre>

@SUBREPORT_DIR@ is a parameter sent via the @runReport@ call and that refers to a reports directory relative location. Also, in the snippet above, we added a foundset datasource to the subreport, where @customers_to_orders@ is a Servoy defined relation, specified as the datasource (to the @runReport@ call on the main report) and a field of type @net.sf.jasperreports.engine.JRDataSource@.

If you want to use the foundset datasource of the main report as the datasource for the subreport, simply use the JasperReports parameter @$P{REPORT_DATA_SOURCE}@ and pass that as the datasource given to the subreport, in the main report (be careful, if using iReport, to set, in the subreport properties, the connection type to 'Use a datasource expression') :

<pre>
<subreport>
   <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
   <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[com.servoy.plugins.jasperreports.JasperReportsResourceLoader.loadReport($P{SUBREPORT_DIR} + "subreport.jrxml")]]></subreportExpression>
</subreport>
</pre>

Images

When using images in the report, the image files must reside in a location relative to the extra directories setting (specified locations), so they must follow the same relative location rule as subreports, but corresponding to the extra directories server setting. When exporting to all formats, except 'view'/'print', mark the image @isLazy@ property in the jrxml file as true. The image expression value should designate a file that resides at a location as explained above. In the case of using images and exporting to 'view'/'print', the @JasperReportsResourceLoader.loadImage@ function should be used, inside the report, as follows:

<pre>
<imageExpression class="java.awt.Image">
    <![CDATA[com.servoy.plugins.jasperreports.JasperReportsResourceLoader.loadImage("image.gif")]]>
</imageExpression>
</pre>

Please note that the imageExpression class should be of type @java.awt.Image@.

When loading images inside subreports of foundset based main reports, the same guidelines as above must be followed, with the exception that in this case, the @JasperReportsResourceLoader.loadImage@ should be used all the time, for all exports formats.

For showing media fields from the foundset, they should be declared of type @java.lang.Object@ and @JRImageLoader.loadAwtImageFromBytes@ should be used to create the image. See the example bellow:

<pre>
<field name="product_image" class="java.lang.Object">
...
<image>
  <reportElement x="0" y="10" width="150" height="150"/>
  <imageExpression class="java.awt.Image"><![CDATA[net.sf.jasperreports.engine.util.JRImageLoader.getInstance(DefaultJasperReportsContext.getInstance()).loadAwtImageFromBytes((byte[])$F{product_image})]]>
  </imageExpression>
</image>
</pre>

Styles

Loading of style templates in a foundset based report is similar to that of images. The corresponding function to load style resources is @JasperReportsResourceLoader.loadStyle@. It should be used, inside the report, as follows:

<pre>
...
<template><![CDATA[com.servoy.plugins.jasperreports.JasperReportsResourceLoader.loadStyle("style.jrtx")]]></template>
...
</pre>

The style must reside one of the extra resource directories. The loading of styles inside subreports of foundset based (main) reports should follow the same guidelines to that of image loading (i.e. the @loadStyle@ function should be used at all times, for all export formats).

Useful links:

Clone this wiki locally