perloc perloc - 4 months ago 47
Java Question

Subreport field is still null even if the dataset is assigned in DynamicReports

I am using the DynamicReports (4.1.0) and iReport (5.6.0) to generate pdf files. I have created a subreport in iReport which has a pie chart. After I run the code via DynamicReports, the created PDF does not show this piechart. Here is the code:

HashMap<String,Object> map = new HashMap<String,Object>();

map.put("id", "157"); //Actually, this will not be used since I pass this value statically in subreport.jrxml
Object[] array = new Object[1];
array[0] = map;
JRMapArrayDataSource ds = new JRMapArrayDataSource(array);
InputStream is = JasperTemplateDesignReport2.class.getResourceAsStream("mainreport.jrxml");

try {
report()
.setTemplateDesign(is)
.setDataSource(ds)
.show();

} catch (DRException e) {
e.printStackTrace();
}


subreport.jrxml:

<?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="templatereport2_subreport3" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isTitleNewPage="true" uuid="3dc0ebf2-660d-4d0c-af85-ccae1f01bdee">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="id" class="java.lang.String"/>
<queryString>
<![CDATA[SELECT
count(status) AS count,
case when status=TRUE then 'Succeeded'else 'Failed'end AS status
FROM
"dbtable" dbtable
WHERE
id = 157
GROUP BY
status]]>
</queryString>
<field name="count" class="java.lang.Long"/>
<field name="status" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="351" splitType="Stretch">
<pie3DChart>
<chart evaluationTime="Report">
<reportElement x="40" y="24" width="415" height="244" uuid="a43afa44-c4e5-4ad5-b06e-8922128bcdaf"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{status}]]></keyExpression>
<valueExpression><![CDATA[$F{count}]]></valueExpression>
</pieDataset>
<pie3DPlot>
<plot/>
<itemLabel/>
</pie3DPlot>
</pie3DChart>
<textField>
<reportElement x="134" y="292" width="100" height="20" uuid="2726affe-1fef-45a1-abc8-8da8c9856e42"/>
<textFieldExpression><![CDATA[$F{count}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="20" y="292" width="100" height="20" uuid="fa4e41c0-b4f4-443c-ab9e-3cf048a3f520"/>
<textFieldExpression><![CDATA[$F{status}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>


mainreport.jrxml

<?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="templatedesign2" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="575" leftMargin="10" rightMargin="10" topMargin="10" bottomMargin="10" uuid="8c9654e9-6770-46c4-a4d0-4bdf99195b70">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="229"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["D:\\DynamicReports\\src\\subreport\\"]]></defaultValueExpression>
</parameter>
<field name="id" class="java.lang.String"/>
<group name="Intro">
<groupHeader>
<band height="203">
<subreport isUsingCache="false">
<reportElement x="0" y="69" width="575" height="77" uuid="eb90e8b6-2860-4c94-ad10-3eb5031502fc"/>
<subreportParameter name="SUBREPORT_DIR">
<subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="id">
<subreportParameterExpression><![CDATA[$F{id}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "templatereport2_subreport3.jasper"]]></subreportExpression>
</subreport>
</band>
</groupHeader>
</group>
<pageFooter>
<band height="79" splitType="Stretch"/>
</pageFooter>
</jasperReport>


Any ideas?

Answer

I have modified the Java code as the following, then the Pie chart started to be displayed on the created PDF:

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/myDB", "username", "password");
HashMap<String,Object> parmsMap = new HashMap<String,Object>();
parmsMap.put("id", "157");
String sourceFileName = 
                "D://DynamicReports//src//subreport//masterreport.jasper";
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, parmsMap, conn);
JasperViewer.viewReport(jasperPrint, false);
conn.close();

I believe the key thing is sending the connection which worked for me.