Alex
Alex

Reputation: 33

JavaBean DataSource doesn't get passed to the sub-sub-report

I'm having trouble finding out how to correctly pass a JavaBean DS to a subsubreport. I have the following Java code:

     JRDataSource javaBeansKapitelDS = new JRBeanCollectionDataSource(BeanFactory.generateKapitelCollection());
     jasperReport = JasperCompileManager.compileReport("JRXML/Subreports.jrxml");
     jasperUnterkapitelReport = JasperCompileManager.compileReport("JRXML/Subreports_subreport1.jrxml");
     jasperEntryReport = JasperCompileManager.compileReport("JRXML/Subreports_subreport1_subreport1.jrxml");

     params.put("SUB_DATASOURCE", BeanFactory.generateUnterKapitelCollection());
     params.put("SUB_SUB_DATASOURCE", BeanFactory.generateEntryCollection());

     jasperPrint = JasperFillManager.fillReport(jasperReport, params, javaBeansKapitelDS);
     JasperExportManager.exportReportToPdfFile(jasperPrint, "output/TestJAVABeansDS.pdf");

In the main report i have a report, which has a subreport, which in turn has its own subreport. In the main report i set the Datasource of the subreport as DataSource Expression new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_DATASOURCE}) and works just fine!

In the subreport i tried doing the same thing for the subsubreport (new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_SUB_DATASOURCE})) but i can't pass the SUB_SUB_DATASOURCE parameter from the main report to the subreport in order to use it there. If i define in the main report a parameter for the subreport:

<subreportParameter name="SUB_SUB_DATASOURCE"> <subreportParameterExpression><![CDATA[$P{SUB_SUB_DATASOURCE}]]></subreportParameterExpression>

i get an exception

Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class jasperreports.datasource.Entry'

...

Fill 1: exception

net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean :

... 

I'm using the latest JasperReports library 5.5.1

So my question is: how do i pass to the subreport the JavaBeansDS in order to use it there for the subsubreport?

Upvotes: 0

Views: 1799

Answers (1)

GenericJon
GenericJon

Reputation: 8986

The solution is pretty simple. You are already passing the parameters to the subreport, you just need to do the same again within the subreport to pass it to the subsubreport. Lets call your reports A, B and C. A is the main report, which contains B, which contains C.

Report A contains the following parameters (which you set from Java by calling params.put):

<parameter name="SUB_DATASOURCE" class="java.util.Collection" />
<parameter name="SUB_SUB_DATASOURCE" class="java.util.Collection" />

Report A also contains the first subreport component:

<subreport>
    ...
    <subreportParameter name="SUB_DATASOURCE">
        <subreportParameterExpression><![CDATA[$P{SUB_DATASOURCE}]]></subreportParameterExpression>
    </subreportParameter>
    <subreportParameter name="SUB_SUB_DATASOURCE">
        <subreportParameterExpression><![CDATA[$P{SUB_SUB_DATASOURCE}]]></subreportParameterExpression>
    </subreportParameter>
    <dataSourceExpression><![CDATA new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_DATASOURCE})]]></dataSourceExpression>
    ...
</subreport>

Report B contains the same parameters, passed down from report A:

<parameter name="SUB_DATASOURCE" class="java.util.Collection" />
<parameter name="SUB_SUB_DATASOURCE" class="java.util.Collection" />

You now need to pass SUB_SUB_DATASOURCE to report C by including it as a subreportParameter again. So report B will contain the second subreport component as follows:

<subreport>
    ...
    <subreportParameter name="SUB_SUB_DATASOURCE">
        <subreportParameterExpression><![CDATA[$P{SUB_SUB_DATASOURCE}]]></subreportParameterExpression>
    </subreportParameter>
    <dataSourceExpression><![CDATA new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_SUB_DATASOURCE})]]></dataSourceExpression>
    ...
</subreport>

If you want to use the parameter in Report C, you can then include it as follows:

<parameter name="SUB_SUB_DATASOURCE" class="java.util.Collection" />

Upvotes: 2

Related Questions