jr.
jr.

Reputation: 4675

How to access jasper reports properties from a report to specify ROOT_DIR location on each developer machine?

I have a report where I define ROOT_DIR as a parameter. I pass this in when I render the reports in my servlet.

Something like this:

 <parameter name="ROOT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["/Some/Path/To/Reports"]]></defaultValueExpression>
</parameter>
<parameter name="IMAGES_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA[$P{ROOT_DIR}+"/images"]]></defaultValueExpression>
</parameter>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA[$P{ROOT_DIR}+"/"]]></defaultValueExpression>
</parameter>

My issue is that we now have more developers working on the reports, so I would like to find a way to not have everyone changing the defaultValueExpression on ROOT_DIR with each commit to their git workspace / home directory.

I was thinking that we could use a property in iReport (Preferences -> iReport -> Jasper Report Properties Tab), but I can't seem to find a way to reference those values from the defaultValueExpression?

Is there any way to access these properties from within a report? Or another suggestions to accomplish this?

Upvotes: 3

Views: 8009

Answers (1)

Alex K
Alex K

Reputation: 22857

It can be done with help of resource bundles.

You can create resource file and then use "variables" with help of $R{} syntax.

The sample

The folder structure is:

reports
    images
        Number1.png

The properties file (folders.properties):

ROOT_DIR=/reports

The name of resource file is set with help of report's attribute resourceBundle.

In my sample I've set attribute like this: resourceBundle="folders".

You can do it, for example in iReport:

enter image description here

The jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report33" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="folders" uuid="6c23de18-90a0-4246-85ab-1a6fae930bd4">
    <parameter name="IMAGES_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[$R{ROOT_DIR}+"/images"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="88" splitType="Stretch">
            <image scaleImage="RealSize">
                <reportElement uuid="7745c4a6-ce7e-4fd8-aae9-4752488dd038" x="325" y="19" width="64" height="56"/>
                <imageExpression><![CDATA[$P{IMAGES_DIR} + "/Number1.png"]]></imageExpression>
            </image>
        </band>
    </title>
</jasperReport>

The report's design in iReport:

enter image description here

The result will be (via preview in iReport):

enter image description here

Every member from your developer team can change path (set by ROOT_DIR key) in properties file and this solution will work without using Java code and templates recompiling.


Notes:

  1. The properties file should be in classpath.
  2. Information about Internationalized Report Templates.

Upvotes: 2

Related Questions