The value of variable displayed as null instead of formatted date - jasper-reports

Hello I'm having issues displaying a string date of this format "01-Nov-16" as "01-11-2016" null is what's returned. I have tried all I know. Below is my exact xml.
<variable name="S_DATE" class="java.util.Date">
<variableExpression><![CDATA[new java.text.SimpleDateFormat("dd-MMM-yy",java.util.Locale.US).parse($P{startdate})]]></variableExpression>
</variable>
<textField isStretchWithOverflow="true" pattern="dd-MM-yyyy" isBlankWhenNull="false">
<reportElement stretchType="RelativeToTallestObject" x="205" y="0" width="102" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" forecolor="#000000" uuid="560349d8-0bb8-44f0-90ce-41ffb71544cc"/>
<textElement textAlignment="Left">
<font size="10"/>
</textElement>
<textFieldExpression><![CDATA[$V{S_DATE}]]></textFieldExpression>
</textField>
The type of startdate parameter is String

First approach
Try to use parameter instead of variable. No need to use the "power" and features of variables in this case.
Sample
<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 date" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="dateAsString" class="java.lang.String">
<defaultValueExpression><![CDATA["01-Nov-2016"]]></defaultValueExpression>
</parameter>
<parameter name="dateParam" class="java.util.Date">
<defaultValueExpression><![CDATA[new SimpleDateFormat("dd-MMM-yy", Locale.US).parse($P{dateAsString})]]></defaultValueExpression>
</parameter>
<title>
<band height="79" splitType="Stretch">
<textField pattern="dd-MM-yyyy">
<reportElement x="130" y="20" width="250" height="30"/>
<textFieldExpression><![CDATA[$P{dateParam}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
Output result
The result in Jaspersoft Studio will be:
Second approach
Change the resetType of the variable. The valid type for your case will be None. The right expression will be:
<variable name="S_DATE" class="java.util.Date" resetType="None">
<variableExpression><![CDATA[new SimpleDateFormat("dd-MMM-yy", Locale.US).parse($P{startdate})]]></variableExpression>
</variable>

Related

How to repeat textfield by parameter in jaspersoft?

I have a text for a document, that can repeat itself for more than 100 times, what I was trying to do is putting a lot of textfield's and then putting a print when expression so that only the quantity of the parameters that I gave would repeat.
The problem is that it can repeat more times than I thought. I like to pass as parameter the number of times it should repeat, not using datasource.
An easy way to repeat text would be to actually use a datasource, the JREmptyDataSource in constructor you can define how many records you like.
I will show an example using the jr:list component to repeat text, but you could use the jr:table component or a subreport instead.
Pass to this component an empty datasource with the number of records you like (hence the parameter defined):
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{NR_REPEAT})]]></dataSourceExpression>
Full 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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="716e18ba-d975-40fa-9be2-89f43a4ab69c">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="RepeatDataset" uuid="185d5db2-6f6b-4c9a-9905-e7a554b6a8fa">
<queryString>
<![CDATA[]]>
</queryString>
</subDataset>
<parameter name="NR_REPEAT" class="java.lang.Integer">
<defaultValueExpression><![CDATA[5]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="22">
<textField>
<reportElement x="0" y="0" width="260" height="20" uuid="b0a2bf35-f015-4b85-aa87-b5587e48de10"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["NR. REPEAT IS: " + $P{NR_REPEAT}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="31" splitType="Stretch">
<componentElement>
<reportElement x="0" y="0" width="550" height="22" uuid="0d5f8ed2-c697-4337-b0b0-2411da8cc5fa">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="RepeatDataset" uuid="e98a25f7-2d4e-403e-8199-0d3573bb3a3e">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{NR_REPEAT})]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="22" width="550">
<textField>
<reportElement x="0" y="0" width="550" height="22" uuid="21984a90-5517-4c8b-82f0-1fe682728830">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT} + " - Hello world"]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>
Output setting NR_REPEAT to 5

How to print text for each iteration in the detail except for the last one?

I have a subreport that iterates names. I want to print a the text "or" on every loop, except for the last one. Right now I'm doing a suggested solution in this question, but it still prints in the last iterate.
Here is a sample of what I have right now:
There is still an extra or in the end of the subreport.
Current subreport source code I have right now:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<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="paiwi-signatories-name-only-subreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8f474d5a-0cca-4c5f-807e-71af8848276f">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="signatoryJSON"/>
<queryString language="json">
<![CDATA[signatoryNames]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="address" class="java.lang.String"/>
<variable name="signatoryCurrentCount" class="java.lang.Integer" resetType="None">
<variableExpression><![CDATA[$V{nameGroup_COUNT}]]></variableExpression>
</variable>
<group name="nameGroup">
<groupExpression><![CDATA[$F{name}]]></groupExpression>
</group>
<detail>
<band height="36" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement positionType="Float" x="-20" y="-20" width="260" height="20" uuid="220a59b9-595b-4149-badd-f4a1ecff36c5"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField evaluationTime="Auto" isBlankWhenNull="true">
<reportElement positionType="Float" x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="afda4fcc-78fc-46f9-8c5c-2e0c4f04dfa5"/>
<textFieldExpression><![CDATA[$V{signatoryCurrentCount}.equals($V{nameGroup_COUNT}) ? "or" : ""]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
How can I avoid to print the last textField after last record?
You can try a simple thing, put the 2 variables in your report, I think you already know how many pages your report will contain, and then change the printWhenExpression statement to the value of your page, for example your report contains 20 pages, then in your variable change the print condition to PAGE_NUMBER==20 and PAGE_NUMBER < 20 for the other one.
Good luck.
If you like to leverage evalutationTime="Auto"
Variables will be evaluated at a time corresponding to their reset type
From EvaluationTimeEnum
You need 2 variables with different reset time. Adapting to your example a solution can be:
Setting your signatoryCurrentCount to the REPORT_COUNT but reseting everytime your group change (hence on every record in your example)
<variable name="signatoryCurrentCount" class="java.lang.Integer" resetType="Group" resetGroup="nameGroup">
<variableExpression><![CDATA[$V{REPORT_COUNT}]]></variableExpression>
</variable>
Then you compare this with the built in variable $V{REPORT_COUNT} that have reset type report
<textFieldExpression><![CDATA[$V{signatoryCurrentCount}<$V{REPORT_COUNT} ? "or" : ""]]></textFieldExpression>
To understand the expression with reset type "Auto", the signatoryCurrentCount will have 1,2,3 (reset's at group) and $V{REPORT_COUNT} always 3 (reset at report).
Full Example
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="paiwi-signatories-name-only-subreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8f474d5a-0cca-4c5f-807e-71af8848276f">
<queryString language="json">
<![CDATA[signatoryNames]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="address" class="java.lang.String"/>
<variable name="signatoryCurrentCount" class="java.lang.Integer" resetType="Group" resetGroup="nameGroup">
<variableExpression><![CDATA[$V{REPORT_COUNT}]]></variableExpression>
</variable>
<group name="nameGroup">
<groupExpression><![CDATA[$F{name}]]></groupExpression>
</group>
<detail>
<band height="36" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement positionType="Float" x="-20" y="-20" width="260" height="20" uuid="220a59b9-595b-4149-badd-f4a1ecff36c5"/>
<textElement>
<font fontName="Times New Roman" size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField evaluationTime="Auto" isBlankWhenNull="true">
<reportElement positionType="Float" x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" uuid="afda4fcc-78fc-46f9-8c5c-2e0c4f04dfa5"/>
<textFieldExpression><![CDATA[$V{signatoryCurrentCount}<$V{REPORT_COUNT} ? "or" : ""]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
output
Do note: using evaluationTime="Auto" is resource intensive for jasper report, in most cases I would suggest that users pass in datasource the totale number of records, the report will be generated faster.

Is it possible to make a custom sorting (not ascending or descending) with help of engine?

I have a data to be sorted, but it should be a custom sorting defined by mine rules.
For example I need to print values in this order:
B
C
A
D
It is not an ascending or descending sorting.
Is it possible to do this?
First of all, it is possible to make sorting via JasperReports engine (without help of query language, for example SQL).
This can be done with help of sortField attribute of main dataset or subDataset.
The sample of report without sorting
The jrxml for report without sorting:
<?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="Non sorted data" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="ds">
<field name="value" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
</subDataset>
<summary>
<band height="15">
<componentElement>
<reportElement x="0" y="0" width="311" height="15"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="ds">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Arrays.asList("def", "jkl", "abc", "ghi"))]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="15" width="311">
<textField>
<reportElement x="0" y="0" width="311" height="15"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</summary>
</jasperReport>
The result in Jaspersoft Studio (JSS) will be:
After adding sorting with help of sortField the template will be:
<?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="Sorted data" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="ds">
<field name="value" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<sortField name="value"/>
</subDataset>
<summary>
<band height="15">
<componentElement>
<reportElement x="0" y="0" width="311" height="15"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="ds">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Arrays.asList("def", "jkl", "abc", "ghi"))]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="15" width="311">
<textField>
<reportElement x="0" y="0" width="311" height="15"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</summary>
</jasperReport>
and the result in JSS:
The custom sorting can be implemented in several ways
1. Using sortField attribute
The sortField attribute may deal not only with fields, but also with variables.
If it possible, we can move the sorting logic here (to variable).
The simple example:
<?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="Data with custom sorting" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<subDataset name="ds">
<field name="value" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<sortField name="valueForSorting" type="Variable"/>
<variable name="valueForSorting" class="java.lang.Integer">
<variableExpression><![CDATA["a".equalsIgnoreCase($F{value}) ? 3 : "b".equalsIgnoreCase($F{value}) ? 1 : "c".equalsIgnoreCase($F{value}) ? 2 : "d".equalsIgnoreCase($F{value}) ? 4 : 0]]></variableExpression>
</variable>
</subDataset>
<summary>
<band height="15">
<componentElement>
<reportElement x="0" y="0" width="311" height="15"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="ds">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Arrays.asList("d", "c", "b", "a"))]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="15" width="311">
<textField>
<reportElement x="0" y="0" width="311" height="15"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</summary>
</jasperReport>
The result in JSS will be:
2. Using query tricks
In some cases using CASE operator can help us to sort data like we want.
For example:
SELECT value,
CASE
WHEN value = 'a' THEN 3
WHEN value = 'b' THEN 1
WHEN value = 'c' THEN 2
WHEN value = 'd' THEN 4
ELSE 0
END AS valueForSorting
FROM someTable
3. Custom datasources
The using custom datasource is another way to sort data. We can sort data in Java code and then pass datasource to the report.
4. Using crosstab
The crosstab component is using mechanism of sorting data - we can set the custom behaviour for sorting.

Calculate sum of column in JasperReports and calculate on based of that value

I have data something like below which are fetched by query for JasperReports.
The column1 has data:
12,
21,
23,
321,
23
Now I want to show the percentage in column2 for each row's value e.g for row 1
(100 * 12) / sum of (column1)
It should be done for all the rows.
How can i do that in JasperReports?
Add a variable(e.g. v_sum) to get the sum of the column(eg. EMPLOYEEID).
<variable name="v_sum" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{EMPLOYEEID}]]></variableExpression>
</variable>
And select the filed of column 2 where you want to get the % value and select property:-
Evaluation Time- Auto
Example report, you can run this with Sample Database(HSQLDB):-
<?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="76072389-4335-4fd9-b45a-111b679776c9">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select EMPLOYEEID from orders]]>
</queryString>
<field name="EMPLOYEEID" class="java.lang.Integer"/>
<variable name="v_sum" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{EMPLOYEEID}]]></variableExpression>
</variable>
<columnHeader>
<band height="28" splitType="Stretch">
<staticText>
<reportElement uuid="00bd99b4-690f-4eea-aebe-08b0cdcfcec2" x="15" y="6" width="100" height="20"/>
<textElement/>
<text><![CDATA[EmployeeID]]></text>
</staticText>
<staticText>
<reportElement uuid="6396c710-73d9-407f-ba02-4aceb524cb75" x="172" y="6" width="100" height="20"/>
<textElement/>
<text><![CDATA[%]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="dbbdb209-dbd4-4dbb-a454-32968c31ec79" x="14" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{EMPLOYEEID}]]></textFieldExpression>
</textField>
<textField evaluationTime="Auto">
<reportElement uuid="f14e9e42-5486-4204-974c-c2c148ab73c7" x="172" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[(100*$F{EMPLOYEEID}) / $V{v_sum}]]></textFieldExpression>
</textField>
</band>
</detail>

display column header in jasper reports

Trying to create a jasper report, I can see the data when I export to xls or csv. But column header is missing, not sure whats wrong with it, following is my report. Thanks in Advance.
<?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="scireport" language="groovy" printOrder="Horizontal" pageWidth="5000" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="4960" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
<![CDATA[select PICKUP_DATE from SCI_PLDDATA_VIEW order by EVENT_TIMESTAMP DESC]]>
</queryString>
<field name="PICKUP_DATE" class="java.sql.Timestamp">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<group name="stopid">
<groupHeader>
<band height="50"/>
</groupHeader>
</group>
<columnHeader>
<band height="44" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="900" height="20"/>
<textElement>
<font size="14" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$R{jasper.PICKUP_DATE}]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="34" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="MM-dd-yyyy HH:mm" isBlankWhenNull="true">
<reportElement x="0" y="0" width="900" height="20"/>
<textElement/>
<textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{PICKUP_DATE}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
You are fetching data from database , your value will come in field. You should use $F{} instead of $R{} in column header.