Incompatible java.lang.Integer value assigned to parameter SUBREPORT_DIR - jasper-reports

Let me depict a situation: I have a report A and subreports B and C. A uses B and B uses C. Default value of SUBREPORT_DIR in A is $P{CUSTOM_SUBREPORT_DIR}. In A.jrxml I see:
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA[$P{CUSTOM_SUBREPORT_DIR}]]></defaultValueExpression>
</parameter>
<parameter name="CUSTOM_SUBREPORT_DIR" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
I also need a proper value of SUBREPORT_DIR in B so I passed it using iReport (Subreport properties/Parameters) so in A.jrxml I have:
<subreportParameter name="SUBREPORT_DIR">
<subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
</subreportParameter>
And when I'm trying to generate report A I get net.sf.jasperreports.engine.JRException: Incompatible java.lang.Integer value assigned to parameter SUBREPORT_DIR in the B dataset.
I'm sure that $P{SUBREPORT_DIR} isn't an integer and type of SUBREPORT_DIR parameter in B subreport is String. All reports I made using iReport 4.0.2.

You can try the following (I do it in this way and it works):
Define the parameters in report A:
<parameter name="SUBREPORT_B_DIR" class="java.lang.String" isForPrompting="false" />
<parameter name="SUBREPORT_C_DIR" class="java.lang.String" isForPrompting="false" />
Define subreport B in report A. Here I use DataSource for report B from parameter. Also I send a parameter to subreport B with the subreport C directory and another parameter with DataSource for report C.
<subreport>
<reportElement x="0" y="16" width="583" height="10" />
<subreportParameter name="SUBREPORT_C_DIR">
<subreportParameterExpression><![CDATA[$P{SUBREPORT_C_DIR}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="SubReportCDataSource">
<subreportParameterExpression><![CDATA[$P{SubReportCDataSource}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{SubReportBDataSource}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_B_DIR} + "B.jasper"]]></subreportExpression>
</subreport>
Finally, in subreport B, define subreport C:
<subreport>
<reportElement x="0" y="16" width="583" height="10" />
<dataSourceExpression><![CDATA[$P{SubReportCDataSource}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_C_DIR} + "C.jasper"]]></subreportExpression>
</subreport>

Related

iReport variable expression returns null

Here's the simple code in XML view of print form:
<variable name="TITLE" class="java.lang.String">
<variableExpression><![CDATA[$F{APP_NUMBER}.replaceAll( "app_", "" )]]></variableExpression>
</variable>
If I use this variable in form, it returns null (while using "$V{TITLE}"). But if I remove replaceAll method, it works and returns correct value.
What is wrong with expression? Should I use something instead of "<variableExpression>"?
If you are printing this variable in Title band change its evaluation time to "Report"
<title>
<band height="79" splitType="Stretch">
**<textField evaluationTime="Report">**
<reportElement x="143" y="43" width="100" height="30" uuid="4fc0ce80-ced5-448e-bdd1-1571b3bc788f"/>
<textFieldExpression><![CDATA[$V{TITLE}]]></textFieldExpression>
</textField>
</band>
</title>

In JasperStudio, I need to display the text on (n-1)th page alone

In JasperStudio, I need to display the text only on (n-1)th page. unfortunately I am unable to do that.
I have tried doing it with variables below
<variable name="nthpage" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[$V{PAGE_NUMBER}]]></variableExpression>
</variable>
and
<variable name="n-1thpage" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[($V{PAGE_NUMBER}-1)]]></variableExpression>
</variable>
and for the report text field expression used as
<textField evaluationTime="Report">
<reportElement x="383" y="124" width="136" height="16" uuid="3193a49a-7354-44c2-a5e7-62336419a190"/>
<textFieldExpression><![CDATA[$V{n-1thpage}==($V{nthpage})?"Hi":"Hello"]]></textFieldExpression>
</textField>
I have tried with evaluationTime="Auto". But it's not working for me.
How can I achieve this custom behavior?
You need to use the Print When Expression e.g. to print a report element on the 10th page only fill the Print When Expression with the value $V{PAGE_NUMBER} == 10

How to create a single pdf containing multiple invoices

I feed an xml file to JasperReports in order to generate a single invoice. This works fine.
However, I would also like to generate multiple invoices (i.e. all invoices for a given user) from a single xml file. The output should be just a single pdf file.
The XML file looks like this
<orders>
<order>
...
</order>
<order>
...
</order>
<order>
...
</order>
</orders>
Is this possible with JasperReports by just editing the jrxml file? I know that I could generate a pdf for each invoice by calling JasperReports in a loop. But I'd like to avoid this since it would require modifing a lot of code.
Update
I use the Jasper-Rails plugin for RoR to generate the reports. https://github.com/fortesinformatica/jasper-rails/blob/master/lib/jasper-rails/jasper_reports_renderer.rb
An xml for a single invoice is simply <order> ... </order>
The jrxml (note: xPath has already been changed to reflect mkl's approach) http://pastebin.com/R0vnrQgU
It is possible to generate a single PDF with multiple invoices if it before was possible to generate a PDF for a single invoice.
It is not clear how many changes to the JRXML are required. Unfortunately the OP did not provide a JRXML. To illustrate a simple case:
If the order merely contains some static amount of data and the JRXML, therefore, only essentially works on one data set, there is hardly anything to change. e.g.:
Let's assume you have original XMLs similar to this sample:
<order>
<name>Mr. Smith</name>
<quantity>2</quantity>
<item>bike</item>
<price>200</price>
</order>
and an original JRXML similar to this sample:
<queryString language="xPath">
<![CDATA[/order]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.String">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="item" class="java.lang.String">
<fieldDescription><![CDATA[item]]></fieldDescription>
</field>
<field name="price" class="java.lang.String">
<fieldDescription><![CDATA[price]]></fieldDescription>
</field>
<variable name="aggregated" class="java.lang.Number">
<variableExpression><![CDATA[java.lang.Integer.parseInt($F{quantity}) * java.lang.Float.parseFloat($F{price})]]></variableExpression>
</variable>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="535" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Dear " + $F{name} + ","]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="40" width="100" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="40" width="235" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="335" y="40" width="100" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="75" width="535" height="35"/>
<textElement/>
<textFieldExpression><![CDATA["Please pay " + $V{aggregated} + " soon."]]></textFieldExpression>
</textField>
<textField>
<reportElement x="435" y="40" width="100" height="20" backcolor="#CCCCCC"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{aggregated}]]></textFieldExpression>
</textField>
</band>
</detail>
Now for multi-invoice XMLs like this sample:
<orders>
<order>
<name>Mr. Smith</name>
<quantity>2</quantity>
<item>bike</item>
<price>200</price>
</order>
<order>
<name>Mr. Fisher</name>
<quantity>3</quantity>
<item>box</item>
<price>10</price>
</order>
</orders>
all you have to do in the JRXML is to change the xPath expression to match all order elements and add a page break between data sets:
...
<queryString language="xPath">
<![CDATA[//order]]>
</queryString>
...
<detail>
<band height="125" splitType="Stretch">
...
<break>
<reportElement x="0" y="124" width="535" height="1"/>
</break>
</band>
</detail>
Even if the order contains more dynamic data, e.g. a variable number of items, it might be just as easy as long as the order element defines the main data set (the variable number of items might be handled by some table element working on a subdataset).
Update
After the OP provided his JRXML file, it turns out that it essentially is of the simple type indicated above. A small correction is required, though:
The OP's JRXML uses only two sections, Title and Page Footer. The latter indeed is used for page footer material but the former is not used for a title but instead for the actual invoice body, i.e. for material which shall be printed not once (as a title) but as many times as there are data sets! To make this ready for multi-invoice outputs, therefore, the title band first has to be moved to become a detail band:
...
</background>
<title>
<band height="616" splitType="Stretch">
...
</band>
</title>
<pageFooter>
...
becomes
...
</background>
<detail>
<band height="616" splitType="Stretch">
...
</band>
</detail>
<pageFooter>
...
And due to the sheer size of this band, this already suffices to make the JRXML ready for multi-order inputs: A page break occurs automatically before the next detail section with information for the next data set because not more than one of these bands fits on a page and the band splitType is "Stretch".
The output into a single pdf isn't the point.
The key is how to use page breaks for each invoice and the use of the headers/bands, I think.

Just one pie in report chart if parameter values are equal

Was created pie 3d chart. It has several params. For example, one of them call "errors" and another - "success". Both is Integer. If values are different, for example 10 and 9, all work fine, but if values are equal, it draw like one pie, and in label expression writed only one value.
In this image I set 10 errors and 10 success, but as can be seen, JasperReports draw just 10 success.
I check it in iReport and by generation report in Java, but results was same.
One more example.
<parameter name="access" class="java.lang.Number"/> set 10
<parameter name="configChange" class="java.lang.Number"/> set 10
<parameter name="creating" class="java.lang.Number"/> set 5
<parameter name="deleting" class="java.lang.Number"/> set 10
<parameter name="updating" class="java.lang.Number"/> set 5
<parameter name="objRequest" class="java.lang.Number"/> set 7
<parameter name="unknown" class="java.lang.Number"/> set 8
Result:
jrxml-file:
<pie3DChart>
<chart isShowLegend="true" renderType="draw" theme="default">
<reportElement uuid="1ea2fe12-8478-48a3-8a16-828cb17bc242" positionType="FixRelativeToBottom" mode="Opaque" x="0" y="50" width="430" height="280" backcolor="#CCFFFF"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<pieSeries>
<keyExpression><![CDATA[$P{errors}]]></keyExpression>
<valueExpression><![CDATA[$P{errors}]]></valueExpression>
<labelExpression><![CDATA["Error"]]></labelExpression>
</pieSeries>
<pieSeries>
<keyExpression><![CDATA[$P{success}]]></keyExpression>
<valueExpression><![CDATA[$P{success}]]></valueExpression>
<labelExpression><![CDATA["Success"]]></labelExpression>
</pieSeries>
</pieDataset>
<pie3DPlot depthFactor="0.2">
<plot backgroundAlpha="0.1" foregroundAlpha="0.6">
<seriesColor seriesOrder="0" color="#FF0000"/>
<seriesColor seriesOrder="1" color="#0066FF"/>
</plot>
<itemLabel/>
</pie3DPlot>
</pie3DChart>

Grouping records in JasperReports

I have a problem with JasperReports. I want to group the records depending on one specific column's value.
For example input data:
Name--email--PledgeType--amount
aaa--aa#yahoo.com--1--20.00
bbb--bb#yahoo.com--2--30.00
ccc--cc#gmai.com--1--35.00
ddd--dd#gmai.com--2-- 40.00
The output report will be grouped by the "PledgeType" value (1, 2, ... number):
Total for group one: 55.00
Name email amount
aaa aa#yahoo.com 20.00
ccc cc#gmai.com 35.00
------------------------------------
Total for group two: 70.00
Name email amount
bbb bb#yahoo.com 30.00
ddd dd#gmai.com 40.00
Can JasperReports solve this problem? how?
You can define grouping in JasperReports. JasperReports calculates the total for you, there is comfortable way to add groups and totals. Here an overview what you need to do in iReport.
To add the group
modify your query to order by pledgeType - JasperReports requires the data sorted according to your grouping.
right click on the report in the report inspector and choose Add Report Group.
Follow the wizard, set as group name PledgeType and choose Group by the following report object where you select the field PledgeType. Click next. Check Add the group header and click Finish.
To add the total
right click on variables in the report inspector and choose Add Variable.
In the properties panel choose this configuration: Variable class: BigDecimal, Calculation: Sum, ResetType: Group, ResetGroup PledgeType, Variable Expression: $F{amount}.
Drag & drop the variable into the group header in the report designer. Click on the field and change: Text field expression: "Total for group " + $F{PledgeType} + ": " + $V{totalPledge}, Expression Class: java.lang.String. Evaluation time: Group. Evaluation Group: PledgeType.
Info: The evaluation time decides when a variable gets evaluated, i.e. when the sum of the calculation will be shown. If you set it to group it means 'once the group processing is completed'.
Attached the generated report and the JRXML.
The JRXML is created with iReport 5.0 - however, if you follow the steps above it should work with JR v 2+
<?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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ce08fe1c-1543-4460-8613-7f03b200082b">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select * from
(select 'aaa' as Name, 'aa#yahoo.com' as email, 1 as PledgeType, 20.00 as amount
union select 'bbb', 'bb#yahoo.com' ,2, 30.00
union select 'ccc', 'cc#gmai.com' ,1, 35.00
union select 'ddd', 'dd#gmai.com' ,2, 40.00) tbl
order by PledgeType]]>
</queryString>
<field name="Name" class="java.lang.String"/>
<field name="email" class="java.lang.String"/>
<field name="PledgeType" class="java.lang.Long"/>
<field name="amount" class="java.math.BigDecimal"/>
<variable name="totalPledge" class="java.math.BigDecimal" resetType="Group" resetGroup="PledgeType" calculation="Sum">
<variableExpression><![CDATA[$F{amount}]]></variableExpression>
</variable>
<group name="PledgeType">
<groupExpression><![CDATA[$F{PledgeType}]]></groupExpression>
<groupHeader>
<band height="61">
<textField evaluationTime="Group" evaluationGroup="PledgeType">
<reportElement uuid="401c7b3b-af73-4d40-8982-9c1692eb7085" x="0" y="21" width="555" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Total for group " + $F{PledgeType} + ": " + $V{totalPledge}]]></textFieldExpression>
</textField>
<staticText>
<reportElement uuid="87cd0d21-014d-4e6c-a54a-006165a38414" x="0" y="41" width="185" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement uuid="bd0fc2f5-4963-4c9d-a9be-3659be06e436" x="185" y="41" width="185" height="20"/>
<textElement/>
<text><![CDATA[email]]></text>
</staticText>
<staticText>
<reportElement uuid="5d5d7ce1-5353-4f83-91b4-57725b0c922b" x="370" y="41" width="185" height="20"/>
<textElement/>
<text><![CDATA[amount]]></text>
</staticText>
</band>
</groupHeader>
</group>
<detail>
<band height="20">
<textField>
<reportElement uuid="5b325da6-7c56-4357-8808-911dad16ec53" x="0" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="0bc06b28-7b8c-4af9-997a-714d1599def1" x="185" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="e5504bb9-c3c0-4135-94c6-7ea935f97cb6" x="370" y="0" width="185" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>