Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the List of primitive type objects as datasource for subreport

I need to pass to my subreport a dataSource with help of master report's List<String> parameter. I don't know what is a type of dataSource is correct and how to get value in subreport.

The snippet from my master report:

<parameter name="seznamPriloh" class="java.util.List" isForPrompting="false"/>
....
<subreport>
    <reportElement x="0" y="56" width="555" height="76"/>
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{seznamPriloh})]]></dataSourceExpression>
            <subreportExpression><![CDATA[cz.alis.keong.jasjdr.reporting.ReportCompiler.compile("R79_SeznamPriloh")]]></subreportExpression>
</subreport>

The snippet from my subreport:

<detail>
    <band height="23">
        <textField>
            <reportElement x="56" y="3" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{}]]></textFieldExpression>
        </textField>
    </band>
</detail>

Please advise me how to pass the datasource to subreport via master report's parameter of java.util.List<String> type.

Edit: 08.14 14:20 Add Tags to Java

Edit: 08.14 15:30 relates with How do I print a list of strings contained within another list in iReport? and works for 4.5.0 And List<String>

like image 324
Perlos Avatar asked Aug 14 '12 09:08

Perlos


2 Answers

You have to specify what field you're using in your sub report. You are passing $F{} and as you are passing a List<String> as a DataSource you should put $F{_THIS}. Of course you have to add a field with that name too, only doing that you can use the expression $F{somefield}.

like image 178
Diego Urenia Avatar answered Nov 04 '22 17:11

Diego Urenia


You can use this datasource expression for passing java.util.List (via parameter) to subreport:

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{seznamPriloh})]]></dataSourceExpression>

The working sample, master report:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="listParam" class="java.util.List"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["<subreport_dir>"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT id, street, city FROM address]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="STREET" class="java.lang.String"/>
    <field name="CITY" class="java.lang.String"/>
    <detail>
        <band height="57" splitType="Stretch">
            <frame>
                <reportElement x="0" y="0" width="539" height="57"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <subreport>
                    <reportElement x="0" y="32" width="523" height="17"/>
                    <subreportParameter name="cityParam">
                        <subreportParameterExpression><![CDATA[$F{CITY}]]></subreportParameterExpression>
                    </subreportParameter>
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listParam})]]></dataSourceExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport_list_as_param.jasper"]]></subreportExpression>
                </subreport>
                <textField>
                    <reportElement x="300" y="0" width="208" height="20"/>
                    <box leftPadding="10"/>
                    <textElement>
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA["City: " + $F{CITY}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="100" y="0" width="200" height="20"/>
                    <box leftPadding="10"/>
                    <textElement>
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Street: " + $F{STREET}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="0" y="0" width="100" height="20"/>
                    <box leftPadding="10"/>
                    <textElement>
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Id: " + $F{ID}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
</jasperReport>

The subreport:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="cityParam" class="java.lang.String"/>
    <field name="id" class="java.lang.Integer"/>
    <field name="station" class="java.lang.String"/>
    <field name="city" class="java.lang.String"/>
    <filterExpression><![CDATA[$F{city}.equals($P{cityParam})]]></filterExpression>
    <title>
        <band height="39">
            <textField>
                <reportElement x="220" y="14" width="161" height="20"/>
                <box leftPadding="10"/>
                <textElement>
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA["City param: " + $P{cityParam}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <box leftPadding="10"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20"/>
                <box leftPadding="10"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{station}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <noData>
        <band height="50">
            <textField>
                <reportElement x="220" y="17" width="161" height="20"/>
                <box leftPadding="10"/>
                <textElement>
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <textFieldExpression><![CDATA["No data for city param: " + $P{cityParam}]]></textFieldExpression>
            </textField>
        </band>
    </noData>
</jasperReport>

The Java code for passing List:

Map<String, Object> params = new HashMap<String, Object>();

List<TestBean> beansList = new ArrayList<TestBean>();

// The TestBean class constructor is: 
//public TestBean(String city, Integer id, String station)
TestBean bean = new TestBean("Dallas", 10, "Central park st.");
beansList.add(bean);

bean = new TestBean("Dallas", 11, "Railway st.");
beansList.add(bean);

bean = new TestBean("Dallas", 12, "Market st.");
beansList.add(bean);

bean = new TestBean("Lyon", 20, "Airport st.");
beansList.add(bean);

params.put("listParam", beansList);

JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDemoHsqldbConnection());

JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);

The result will be (view of generated PDF file):

Generated result in PDF format


You can look at the implementations of net.sf.jasperreports.engine.JRDataSource. The most appropriate for your case are: JRBeanCollectionDataSource and JRBeanArrayDataSource. As you can see they are both Bean-based.

I think you can easily convert your List<String> to the List<StringBean>.

Or you can implement your own JRDataSource.

like image 9
Alex K Avatar answered Nov 04 '22 17:11

Alex K