I am attempting to fill my jasper report from an ArrayList
, so I have created String parameters for this. Then I have this method:
public void formatPurchaseOrder(String orderNumber, String reportdir) {
ArrayList all = PurchaseOrders.listPurchaseOrderDetails(orderNumber);
int size = all.size();
ClassLoader classLoader = getClass().getClassLoader();
JREmptyDataSource datasource = new JREmptyDataSource();
Map parameters = new HashMap();
for (int i = 0; i < size; i++) {
ArrayList one = (ArrayList) all.get(i);
parameters.put("PRODUCT_NAME", (String) one.get(2));
parameters.put("PRODUCT_UNITS", (String) one.get(5));
parameters.put("UNIT_COST", (String) one.get(4));
parameters.put("TOTAL_COST", (String) one.get(6));
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd_MMM_YYYY");
InputStream url = classLoader.getResourceAsStream("com/orig/stock/jrxml/porder.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(url);
reportDestination = reportdir + "/Statement_" + orderNumber + "_" + sdf.format(new Date()) + ".pdf";
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);
JasperExportManager.exportReportToPdfFile(jasperPrint, reportDestination);
} catch (Exception asd) {
System.out.println(asd.getMessage());
}
}
My report is only having one row of data when the array has several records. Could I be overwriting the other values or what am I doing wrong?
My 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="porder" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="05f8fa07-cc11-4951-91d0-927112a15a81">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="PRODUCT_NAME" class="java.lang.String"/>
<parameter name="PRODUCT_UNITS" class="java.lang.String"/>
<parameter name="UNIT_COST" class="java.lang.String"/>
<parameter name="TOTAL_COST" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="8" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="177" y="15" width="100" height="20" uuid="41147e24-6284-475c-b00c-96e980c79bbc"/>
<text><![CDATA[PURCHASE ORDER]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="15" y="41" width="100" height="20" uuid="9dfd8b36-bc9b-49de-b9a9-b6d769d412d6"/>
<text><![CDATA[PRODUCT NAME]]></text>
</staticText>
<staticText>
<reportElement x="136" y="41" width="100" height="20" uuid="0eb87f16-3251-4736-9c7a-8ec69652da8c"/>
<text><![CDATA[PRODUCT UNITS]]></text>
</staticText>
<staticText>
<reportElement x="242" y="41" width="100" height="20" uuid="8a5f0b4c-b468-4791-9a5c-7b99431bef00"/>
<text><![CDATA[UNIT COST]]></text>
</staticText>
<staticText>
<reportElement x="353" y="41" width="100" height="20" uuid="7aadd922-f446-4e8b-b16b-fa61545936d6"/>
<text><![CDATA[TOTAL COST]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="23" splitType="Stretch">
<textField>
<reportElement x="14" y="3" width="112" height="20" uuid="faddcb7e-d047-4a35-9296-dd8a7d384e57"/>
<textFieldExpression><![CDATA[$P{PRODUCT_NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="138" y="3" width="100" height="20" uuid="ee974e76-1698-4d41-95ca-e3254e6d8537"/>
<textFieldExpression><![CDATA[$P{PRODUCT_UNITS}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="243" y="3" width="100" height="20" uuid="7de5517f-5baa-489b-a4ad-b152df7523fb"/>
<textFieldExpression><![CDATA[$P{UNIT_COST}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="355" y="3" width="100" height="20" uuid="02a9f9a9-a81b-4f71-9e38-85b0ebdd0937"/>
<textFieldExpression><![CDATA[$P{TOTAL_COST}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
You have some confusion about what is the parameters
and what is the datasource
.
Your loop
for (int i = 0; i < size; i++) {
ArrayList one = (ArrayList) all.get(i);
parameters.put("PRODUCT_NAME", (String) one.get(2));
}
has no sense, you will set the parameter PRODUCT_NAME
to last value (its a Map<String,Object>
, on one key you can only have one value) and furthermore you should not use this when trying to pass a datasource, but only to pass parameters (for query, for path to images ecc.)
To generate a datasource from you List<List<String>>
you can use the ListOfArrayDataSource, but you need to convert it to a List<Object[]>
. I however would have converted it to a List<MyBean>
and used the JRBeanCollectionDataSource. The MyBean
class is similar to
public class MyBean {
private String productName;
... all other fields
private double totalCost;
... getters and setters
}
and then define in your jrxml these as fields instead of parameter
<field name="productName" class="java.lang.String"/>
<field name="totalCost" class="java.lang.Double"/>
Hence its the datasource that will be iterated in your detail band not the parameters.