Saturday, July 21, 2007

JasperReports Scriptlet Example

Back in October 2005, I posted some directions on how to create a Scriptlet. Since that project, I had some experience with Eclipse BIRT, but never used JasperReports again. But that post was very popular and _lots_ of people wrote comments or send me an email asking for examples. I never had the time or energy to search my old projects and find the example that motivated that post until today (finally!). This code is in production since November 2005, although it's been a long time since I was called by the company for any maintenance.

So, below you can find an example class and some report excerpts. Refer to the previous post for more details on how they were built. I will also send both files to everyone who wrote a comment or sent me an email (if I find all emails here). In case you want to receive these files, please drop me a note that I will send whenever I have some spare time.

PriorityScriptlet.java

package br.com.specchia.cerville.sac.returns;

import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;

public class PriorityScriptlet extends JRDefaultScriptlet {
public String priorityDescription()
throws JRScriptletException {
Integer priority =
(Integer) this.getFieldValue("priority");

if (priority.intValue() == 1)
return "Importante";
if (priority.intValue() == 2)
return "Normal";
if (priority.intValue() == 3)
return "Baixa";

return "Nenhuma";
}
}


priority_list.jrxml - excerpt 1
(scriptletClass line was manually inserted)

<jasperReport
name="priority_list"
columnCount="1"
printOrder="Vertical"
orientation="Portrait"
pageWidth="595"
pageHeight="842"
columnWidth="535"
columnSpacing="0"
leftMargin="30"
rightMargin="30"
topMargin="20"
bottomMargin="20"
whenNoDataType="NoPages"
scriptletClass="br.com.(...).PriorityScriptlet"
isTitleNewPage="false"
isSummaryNewPage="false">


priority_list.jrxml - excerpt 2
(where the method is called - I used iReport to edit this)

<textFieldExpression class="java.lang.String">
<![CDATA[" " +
$V{PriorityNumber} + ". " +
$P{REPORT_SCRIPTLET}.priorityDescription()]]>
</textFieldExpression>
Post a Comment