JasperReports IO - Dynamically Change DataAdaptor

0

Hi,

I'm new to Jasper Reports and have been following the getting started guides and reading the help sections.
I've run into a road block that I cannot figure out or get more information on.

I have 100s of databases with the same schema and need to be able to run reports against any of them on demand.
Ideally by passing a parameter to the REST service instructing it which data-adaptor to use (if I could pass just the connection username and password this would be even better).

This is my current repo structure:

    repository\samples
        dataadaptors
            DataAdaptor_Local_TestA.xml
            DataAdaptor_Local_TestB.xml
        images
        JR-INF
        reports
            Test.jrxml

DataAdaptor_Local_TestA.xml Contents:

<?xml version="1.0" encoding="UTF-8" ?>
<jdbcDataAdapter class="net.sf.jasperreports.data.jdbc.JdbcDataAdapterImpl"><name>DataAdatptor_Local_TestA</name><driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver><username>someuser1</username><password>somepassword1</password><savePassword>true</savePassword><url>jdbc:sqlserver://192.168.1.111\SQLExpress;databaseName=testa</url><database></database><serverAddress></serverAddress></jdbcDataAdapter>

Test.jrxml Contents:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 7.9.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928  -->
<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="test" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e1ad0c8e-458d-4b19-b236-1c651be4f4ab">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="/samples/dataadaptors/DataAdaptor_Local_TestA.xml"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
 
    <propertyExpression name="net.sf.jasperreports.data.adapter"><![CDATA[$P{DataAdapterLocation}]]></propertyExpression>
    <parameter name="DataAdapterLocation" class="java.lang.String" evaluationTime="Early">
        <defaultValueExpression><![CDATA["/samples/dataadaptors/DataAdaptor_Local_TestA.xml"]]></defaultValueExpression>
    </parameter>
 
    <queryString>
        <![CDATA[SELECT ID, Name FROM Horses]]>
    </queryString>
    <field name="ID" class="java.lang.Integer">
        <property name="com.jaspersoft.studio.field.name" value="ID"/>
        <property name="com.jaspersoft.studio.field.label" value="ID"/>
    </field>
    <field name="Name" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.name" value="Name"/>
        <property name="com.jaspersoft.studio.field.label" value="Name"/>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="260" y="0" width="40" height="30" uuid="badde751-7b4f-49eb-ac8e-a2c318e26775"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <text><![CDATA[Test]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="22" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="50" height="20" uuid="494ec343-9350-463c-8675-c9fe95b10777">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="8faf7309-c7d7-45b8-8541-9b97c6707cbc"/>
                </reportElement>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement x="50" y="0" width="504" height="20" uuid="056bb2d0-b8df-4311-acf9-bce07422fcce">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b6a20da9-58db-43bf-9aeb-34001b896224"/>
                </reportElement>
                <text><![CDATA[Name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="50" height="20" uuid="5e12a933-59dc-459c-8a95-1cb93536471b">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="8faf7309-c7d7-45b8-8541-9b97c6707cbc"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="50" y="0" width="504" height="20" uuid="1ddc7158-df57-478d-b0fa-e93c653a4bbf">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="b6a20da9-58db-43bf-9aeb-34001b896224"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="4" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Test.jrxml Snippet:

<propertyExpression name="net.sf.jasperreports.data.adapter"><![CDATA[$P{DataAdapterLocation}]]></propertyExpression>
<parameter name="DataAdapterLocation" class="java.lang.String" evaluationTime="Early">
<defaultValueExpression><![CDATA["/samples/dataadaptors/DataAdaptor_Local_TestA.xml"]]></defaultValueExpression>
</parameter>

Changing the default value expression above from TestA.xml to TestB.xml by hand runs the reports correctly against each respective database.

How do I replace the reference to DataAdapterLocation dynamically? Is there a better way to do this.

Any help would be appreciated.

AlanR's picture
2
Joined: Jun 8 2021 - 5:51am
Last seen: 3 weeks 1 day ago

To answer my own question it looks like I was pretty close with the above.

I had come across the propertyExpression on a seperate Jasper Reports server query so had actually had it in place in my sample above.

To switch dataAdapters with the above code it was as simple as passing DataAdapterLocation = /samples/dataadaptors/DataAdaptor_Local_TestB.xml when making my rest call.

AlanR - 1 month 6 days ago

0 Answers:

No answers yet
Feedback
randomness