How to avoid using a lots of ternary operators for translation of value to text - jasper-reports

I am using JasperReports to create some reports of a Django web application.
Let's say that I have a model that has an id and a value. This value is a Django choice so I end up in my database with only the keys and not the values (the values are in my code). To make it more clear for people that don't use Django, I end up with something like that in my database:
id value
1 'GD'
2 'VG'
3 'VG'
4 'VG'
5 'GD'
6 'AV'
7 'GD'
8 'AV'
I want to display Good instead of 'GD', Average instead of 'AV' and Very Good instead of VG in my report. I know that this can be done with two equally not desirable to me options:
Create a new table in my database that has key - value and join that in the JR query. I really hate this because I'd need to create around 10 such tables.
Use the ternary operator to display the correct value:
field.equals("GD")?"Good":(field.equals("AV")?"Average":(field.equals("VG")?"Very good":"-"))
I also hate this because it would be very complicated if I had, for instance, 10 key-value pairs.
My ideal solution would be to define a dictionary (HashMap) variable in my report that would contain all the key value pairs and then just do a DICTIONARY.get(field) to represent the field value. Can this be done ? Can you possibly propose another, better solution?
Please don't tell me to alter my database design, I know that some people won't like but it perfect for my needs.

You can solve this issue with help of Java. For example, the Guava library can help us in solving this task.
Using report's parameter
We can add the parameter of java.util.Map type and use it for extracting the value by the key (it can be the value field from your sample)
The sample:
<?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="using_map" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e566b23d-ca97-472e-9cc3-8073573f4537">
<import value="com.google.common.collect.*"/>
<import value="com.google.common.base.*"/>
<parameter name="values" class="java.util.Map" isForPrompting="false">
<defaultValueExpression><![CDATA[new ImmutableMap.Builder<String, String>().put("GD", "Good").put("AV", "Average").build()]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.String"/>
<field name="value" class="java.lang.String"/>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="200" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value from Map]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$P{values}.get($F{value})]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
In this sample we use ImmutableMap.Builder class for creating and filling the Map.
The result will be (via iReport preview):
As you can see that the third columns contains null value for values that are not in Map.
Note: Don't forget to add Guava library to classpath.
Using scriptlet
You can do the same with help of Scriptlets.
You can write the simple Java class with static method, for example for getting value from the Map.
Using internationalization mechanism
May be the simplest way is to use report's internationalization support.
You can attach the properties file with values to your report.
The sample of 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="localization" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="marks" uuid="a5d74b61-8d62-41ac-b874-76d6f40da79e">
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.String"/>
<field name="value" class="java.lang.String"/>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="200" y="0" width="114" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value from Properties]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="200" y="0" width="114" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[str($F{value})]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The marks.properties file:
GD=Good
AV=Average
VG=Very Good
And the result will be (with iReport preview):
I've used the str() method and the $R{} syntax.

Related

Sum columns based on custom group (Jasper Reports) [duplicate]

First of all, the records are shown in the table by table component but not in the report one.
The results looks like this:
YEARS MONTHS SUMMONTH SUMQUARTER
----- ------ -------- ----------
2009 Jan 130984 432041
Feb 146503
Mar 154554
Apr 147917 435150
May 131822
Jun 155411
Jul 144000 424806
Aug 130369
Sep 150437
Oct 112137 400114
Nov 152057
Dec 135920
=====================================
Jan-Dec 1692111
=====================================
2010 Jan 139927 417564
Feb 154940
Mar 122697
Apr 163257 413305
May 124999
Jun 125049
Jul 145127 427612
Aug 138804
Sep 143681
Oct 143398 406381
Nov 125351
Dec 137632
=====================================
Jan-Dec 1664862
=====================================
The sumquarter column shows the sum of each quarter in year.
They are not printed when it repeated the value of the field.
The question is how to group the column of sumquarter, so that the first printed repeated value in each row to join the next repeated value to become a single cell until it meets the non-repeated value?
You can simply see it in an image.
Below is the image that the table shows and the solution that I preferred is to group those 3 months of sum into a single cell.
Here is the image:
You can use this sample:
<?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="year_sum_quarter" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="year" class="java.lang.Integer"/>
<field name="month" class="java.lang.String"/>
<field name="sum" class="java.lang.Integer"/>
<field name="q" class="java.lang.Integer"/>
<variable name="yearSum" class="java.lang.Integer" resetType="Group" resetGroup="yearGroup" calculation="Sum">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
<variable name="qSum" class="java.lang.Integer" resetType="Group" resetGroup="quaterGroup" calculation="Sum">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
<group name="yearGroup">
<groupExpression><![CDATA[$F{year}]]></groupExpression>
<groupFooter>
<band height="20">
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA["Jan-Dec, " + $F{year}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20"/>
<box leftPadding="0">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$V{yearSum}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="300" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
</band>
</groupFooter>
</group>
<group name="quaterGroup">
<groupExpression><![CDATA[$F{year} + $F{q}]]></groupExpression>
</group>
<columnHeader>
<band height="50">
<staticText>
<reportElement x="100" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Month]]></text>
</staticText>
<staticText>
<reportElement x="0" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Year]]></text>
</staticText>
<staticText>
<reportElement x="200" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Month Sum]]></text>
</staticText>
<staticText>
<reportElement x="300" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Quarter Sum]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="300" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
<textField>
<reportElement x="0" y="0" width="100" height="20">
<printWhenExpression><![CDATA[$V{yearGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{month}]]></textFieldExpression>
</textField>
<textField evaluationTime="Group" evaluationGroup="quaterGroup" isBlankWhenNull="false">
<reportElement stretchType="RelativeToBandHeight" isPrintRepeatedValues="false" x="300" y="0" width="100" height="20" printWhenGroupChanges="quaterGroup">
<printWhenExpression><![CDATA[$V{quaterGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$V{qSum}]]></textFieldExpression>
</textField>
<line>
<reportElement x="300" y="0" width="100" height="1" printWhenGroupChanges="quaterGroup">
<printWhenExpression><![CDATA[$V{quaterGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
</line>
</band>
</detail>
</jasperReport>
The result will be (in pdf format):
In this sample I've used two elements in the Detail band for the Year column:
one textField with only vertical borders and with printWhenExpression: "$V{yearGroup_COUNT} == 1" property (I'm show it only once for the whole yearGroup) and one staticText without any text and with only vertical borders.
I've used three elements in the Detail band for the Quarter Sum column:
one textField with only vertical borders and with printWhenExpression: "$V{quaterGroup_COUNT} == 1" property (I'm show it only once for the whole quaterGroup), one staticText without any text and with only vertical borders and the line element for drawing horizontal border with printWhenExpression: "$V{quaterGroup_COUNT} == 1" property.

How to display one group per page using Jasper Reports?

I'm using iReport to create a Jasper Report template, and I need to display one group per page:
Group 1: (Page One)
-Field1 Field2 Field3
Group 2: (Page Two)
-Field1 Field2 Field3
And so on.
I tried setting the Maximize Band Height property to true in the "Group Footer" band using iReport, but some empty pages are added when I try to review.
You can use isStartNewPage attribute of group.
You can add Group Header band and
1) set "Start on a new page" attribute as true
2) set band height as 0 (if you don't need it).
The working sample
The jrxml file:
<?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="group_break" language="groovy" pageWidth="400" pageHeight="400" columnWidth="360" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2bcb6f3d-2169-44f6-9e8a-db54ea21b5e0">
<queryString>
<![CDATA[SELECT DOCUMENTID, PRODUCTID, PRICE FROM POSITIONS ORDER BY DOCUMENTID]]>
</queryString>
<field name="DOCUMENTID" class="java.lang.Integer"/>
<field name="PRODUCTID" class="java.lang.Integer"/>
<field name="PRICE" class="java.math.BigDecimal"/>
<group name="docId" isStartNewPage="true">
<groupExpression><![CDATA[$F{DOCUMENTID}]]></groupExpression>
<groupHeader>
<band/>
</groupHeader>
</group>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="111" height="20" uuid="596f5fb4-7e6d-4e7e-88bf-b9c37d0fb1a1"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center"/>
<text><![CDATA[Position]]></text>
</staticText>
<staticText>
<reportElement x="111" y="0" width="111" height="20" uuid="4884f663-00cf-4b5f-9cc1-05d698b8154a"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center"/>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="222" y="0" width="111" height="20" uuid="26c8d18f-2281-405c-a41b-bddcbcdebdbb"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center"/>
<text><![CDATA[Price]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="111" height="20" uuid="fb6369eb-4b92-41dd-b5b8-94a9210bf315"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textFieldExpression><![CDATA[$F{DOCUMENTID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="111" y="0" width="111" height="20" uuid="9fcbb785-06fa-4f7a-bc6d-301cc8db4388"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textFieldExpression><![CDATA[$F{PRODUCTID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="222" y="0" width="111" height="20" uuid="0d18c9fe-5a70-4890-b94d-f26d88bc97da"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textFieldExpression><![CDATA[$F{PRICE}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="50">
<textField>
<reportElement x="0" y="20" width="80" height="20" uuid="47486fe9-e21f-4913-a9c4-cef0d3e2df39"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="80" y="20" width="40" height="20" uuid="0f7df66b-bbe6-4373-962e-519584c44541"/>
<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
The result
The output result generated in iReport via preview mode

Conditional Horizontal line in jasperreport between results

I'm using jasperreport 4.7.0
I have a query where I order by name then date .
Now the clients wants that when the name changes that we add an horizontal line (see attached img - red line)
Is there a way to accomplish this without duplicating the query and the fields ?
Result :
For solving your task you can use Data Grouping.
The possible steps (there are a lot of way to reach your target) for adding line are:
Create the report Group (via context menu Add Report Group in iReport) on field
Add Group Footer Band
Add Rectangle element to the Group Footer Band
Set Height, Forecolor and Backcolor for this Rectangle - drawing "red pencil line"
The sample:
<?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="line_in_group" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f1394ead-7ad6-4371-979d-5a13d1bdde4d">
<queryString>
<![CDATA[SELECT id, city, street FROM address ORDER BY city]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="CITY" class="java.lang.String"/>
<field name="STREET" class="java.lang.String"/>
<group name="cityGroup">
<groupExpression><![CDATA[$F{CITY}]]></groupExpression>
<groupFooter>
<band height="2">
<rectangle>
<reportElement uuid="6564e743-2a45-4b51-89a5-e3ec6aee291f" x="0" y="0" width="300" height="2" forecolor="#FF0000" backcolor="#FF0000"/>
</rectangle>
</band>
</groupFooter>
</group>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[City]]></text>
</staticText>
<staticText>
<reportElement uuid="77860b22-95f6-41b6-955a-f8991843e221" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Street]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="7e375aa3-fab5-4761-bab9-a0570a5442b1" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The result will be (via preview in iReport):
In this sample I've create a group for the field CITY.

How do I set number format with custom Grouping and Decimal separators

For example: I have 9999.99 and I want to display 9.999,99 in my report.
I try to set custom pattern is #.##0,00;-#.##0,00 but it does not working
My textfield:
The work of pattern depends on Locale settings. The Grouping and Decimal separators are defined in Locale.
If you want be free of regional (Locale) settings you can use this scriptlet:
package utils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class CustomDecimalFormatter {
public static String format(BigDecimal value, String pattern, char decimalSeparator, char groupingSeparator) {
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
otherSymbols.setDecimalSeparator(decimalSeparator);
otherSymbols.setGroupingSeparator(groupingSeparator);
DecimalFormat df = new DecimalFormat(pattern, otherSymbols);
return df.format(value);
}
}
This scriptlet allows to set pattern, custom Grouping and Decimal separators.
The jrxml file:
<?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="format_decimal" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<import value="utils.CustomDecimalFormatter"/>
<queryString>
<![CDATA[SELECT id, cost*100 as cost from product]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="COST" class="java.math.BigDecimal"/>
<columnHeader>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="154" height="50"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true"/>
</textElement>
<text><![CDATA[The result of using
classical pattern.
Depends on System locale]]></text>
</staticText>
<staticText>
<reportElement x="154" y="0" width="191" height="50"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center">
<font isBold="true"/>
</textElement>
<text><![CDATA[The result of using the sriptlet]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField pattern="#,##0.00;#,##0.00-">
<reportElement x="0" y="0" width="154" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="154" y="0" width="191" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[CustomDecimalFormatter.format($F{COST}, "#,##0.00;#,##0.00-", ',', '.')]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
I've set the Grouping and Decimal separators and the pattern (#,##0.00;#,##0.00-).
The result will be:
Note
Don't forget to add class (jar) with sriptlet to classpath.

How to generate separate pages for each group? [duplicate]

This question already has an answer here:
New page for every group in iReport
(1 answer)
Closed 2 years ago.
Story:
Two Domain Objects:
class JasperProject {
private String ...
}
class JasperProjectGroup {
private String ...
private List<JasperProject> ...
}
Code for Jasper:
Map<String, Object> parameters = new HashMap<String, Object>();
List<JasperProjectGroup> groups = buildGroups();
parameters.put("groups", groups);
InputStream jasperReportInputStream = ProjectStatusReportGenerator.class.getClassLoader().getResourceAsStream(
PROJECT_STATUS_JASPER_REPORT_FILENAME);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportInputStream, parameters,
new JREmptyDataSource());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
return outputStream.toByteArray();
I want to create a pdf report with tables which each group table on each separate page (e.g. table for group 1 on page1, table for group 2 on page2).
However, when I pass the groups as parameter to Jasper template, it display just one table contains all the groups on the pdf report.
XML code for jrxml
First xml:
<?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="timesheets" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="groups" class="java.util.Collection"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="800" splitType="Stretch">
<subreport runToBottom="true">
<reportElement x="0" y="0" width="515" height="800"/>
<dataSourceExpression><![CDATA[(new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{groups}))]]></dataSourceExpression>
<subreportExpression><![CDATA[net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class.getClassLoader().getResourceAsStream("projectgroup.jasper")]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
Second template (projectgroup.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="projectgroup" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="TableDataset1">
<field name="projectName" class="java.lang.String"/>
<field name="bookedHours" class="java.lang.String"/>
<field name="predictedHours" class="java.lang.String"/>
<field name="burnedHours" class="java.lang.String"/>
<field name="percentageComplete" class="java.lang.String"/>
<field name="bookedHoursBurned" class="java.lang.String"/>
</subDataset>
<field name="jasperProjectStatusReports" class="java.util.List"/>
<detail>
<band height="50" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="515" height="50"/>
<dataSourceExpression><![CDATA[(new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{jasperProjectStatusReports}))]]></dataSourceExpression>
<subreportExpression><![CDATA[net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class.getClassLoader().getResourceAsStream("projectgroup_table.jasper")]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
Third template xml (projectgroup_table.jasper):
<?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="projectgroup_table" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="groupName" class="java.lang.String"/>
<field name="projectName" class="java.lang.String"/>
<field name="bookedHours" class="java.lang.String"/>
<field name="predictedHours" class="java.lang.String"/>
<field name="burnedHours" class="java.lang.String"/>
<field name="percentageComplete" class="java.lang.String"/>
<field name="bookedHoursBurned" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="20" splitType="Stretch">
<textField>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
<staticText>
<reportElement mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Booked]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Predicted]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="300" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Burned]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="400" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Complete]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="500" y="0" width="95" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Booked Burned]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="21" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{projectName}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="0" rightPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{bookedHours}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="200" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{predictedHours}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="300" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{burnedHours}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="400" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{percentageComplete}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="500" y="1" width="95" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{bookedHoursBurned}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Are there any ideas about that? thanks.
Yes this can be done. If you expand the detail field by using ireprt... by this when once your one group got printed there will be no space for other group, so that next group will be on next page.
Its jasper report's built-in functionality.