Nick Cameo Nick Cameo - 5 months ago 31
Java Question

Jasper Subreport

Struggling with this for a week, and yes I progressed slightly thanks to previous posts on the same subject however, not completely.

Two reports:

Master Report - Contains company info
Sub Report - Contains 1..* company orders in detail list


The data source is built from pojo array lists:

Master Report:

<parameter name="SUB_REPORT" class="java.lang.Object"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="companyName" class="java.lang.String">
<fieldDescription><![CDATA[companyName]]></fieldDescription>
</field>
<field name="companyOrders" class="java.util.List">
<fieldDescription><![CDATA[companyOrders]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<pageHeader>
<band height="214" splitType="Stretch">
<textField>
<reportElement x="415" y="5" width="40" height="14" uuid="f03490b8-4ab1-4d61-a0d6-f43214dd1c1c"/>
<textElement>
<font isBold="false"/>
<paragraph lineSpacing="Single" lineSpacingSize="2.0"/>
</textElement>
<textFieldExpression><![CDATA["Date:"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="1" y="110" width="99" height="14" uuid="e82847e9-7235-45d6-9eeb-0f5d2fc2622c"/>
<textElement>
<font isBold="false"/>
<paragraph lineSpacing="Single" lineSpacingSize="2.0"/>
</textElement>
<textFieldExpression><![CDATA["Business Name:"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="113" y="110" width="252" height="15" uuid="68e891b9-cd70-4b65-90f3-b11866446f4a"/>
<textFieldExpression><![CDATA[$F{companyName}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band height="17" splitType="Stretch">
<staticText>
<reportElement x="0" y="2" width="100" height="15" uuid="309582cb-bde5-4a37-a97b-c9058fc5e184"/>
<text><![CDATA[Order Num]]></text>
</staticText>
<staticText>
<reportElement x="110" y="2" width="100" height="15" uuid="a6fb5059-8126-4ae2-be9c-a5d0ed700ef9"/>
<text><![CDATA[Order Amount]]></text>
</staticText>
<staticText>
<reportElement x="220" y="2" width="100" height="15" uuid="198d15de-1484-41cc-b2b0-66b8a75c106d"/>
<text><![CDATA[Order Date]]></text>
</staticText>
<staticText>
<reportElement x="369" y="2" width="100" height="15" uuid="d142e6aa-0ff5-416f-b367-d639a287aadd"/>
<text><![CDATA[Order Paid]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="28" splitType="Stretch">
<subreport>
<reportElement x="1" y="4" width="468" height="21" uuid="77507436-37c6-4f8e-bc13-3997be8ffbee"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{companyOrders})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUB_REPORT}]]></subreportExpression>
</subreport>
</band>
</detail>


Sub Report:

<field name="orderNum" class="java.lang.Long">
<fieldDescription><![CDATA[orderNum]]></fieldDescription>
</field>
<field name="orderTotal" class="java.lang.Double">
<fieldDescription><![CDATA[orderTotal]]></fieldDescription>
</field>
<field name="orderDate" class="java.sql.Timestamp">
<fieldDescription><![CDATA[orderDate]]></fieldDescription>
</field>
<field name="orderPaid" class="java.lang.Integer">
<fieldDescription><![CDATA[orderPaid]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="24" splitType="Stretch">
<textField>
<reportElement x="0" y="3" width="100" height="15" uuid="d356cc51-325b-43c3-ab2d-dc9f5041d3e4"/>
<textFieldExpression><![CDATA[$F{orderNum}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="3" width="100" height="15" uuid="d91fd44f-12f9-4c67-aaca-24c7efa6c75c"/>
<textFieldExpression><![CDATA[$F{orderTotal}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="222" y="3" width="100" height="15" uuid="af910428-2309-4159-ba5e-c29748f00b6f"/>
<textFieldExpression><![CDATA[$F{orderDate}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="369" y="3" width="100" height="15" uuid="2921437b-8146-41e4-86fa-6b5ec39f4d65"/>
<textFieldExpression><![CDATA[$F{orderPaid}]]></textFieldExpression>
</textField>
</band>
</detail>


The java code that I use to compile and print the reports at runtime:

CompanyMapper cm = new CompanyMapper();
this.company = cm.getCompanyByID((Long) httpRequest.getSession().getAttribute("userCompanyID"));
List<ACompany> c = new ArrayList<ACompany>();
List<AOrder> companyOrders = getOrders(getParameterInt("orderStatus"));

c.add(this.company);

String reportFolder = httpRequest.getServletContext().getRealPath("/").replace('\\', '/') + "reports/";
String invoice = reportFolder + ReportManager.getProperty("orders");
String orders = reportFolder + ReportManager.getProperty("orderList");
File invoiceFile = new File(invoice);
File orderFile = new File(orders);
msgHelper = new MessageHelper();

JasperReport jasperMasterReport = JasperCompileManager.compileReport(invoiceFile.getAbsolutePath());
JasperReport jasperSubReport = JasperCompileManager.compileReport(orderFile.getAbsolutePath());

Map<String,Object> parameters = new HashMap<String,Object>();
parameters.put("SUB_REPORT", jasperSubReport);
parameters.put("SUB_REPORT_DATA", new JRBeanCollectionDataSource(companyOrders));

JasperFillManager.fillReport(jasperSubReport, parameters, new JRBeanCollectionDataSource(companyOrders));
JasperPrint print = JasperFillManager.fillReport(jasperMasterReport, parameters, new JRBeanCollectionDataSource(c));


Company info show up fine in the Master report however, no orders in details are printed from the sub report. When I print the subreport, there is data (ie, detail of orders) showing up as expected. I just want to pass the subreport in the detail section of the master, and everything will be perfect.

Answer

If you want to use "companyOrders" as a field in your reports you have to put it in your "ACompany" POJO, more or less like that :

public class DataBean {
    private String companyName;
    private List<AOrder> companyOrders;
}
Comments