Jump to content
Changes to the Jaspersoft community edition download ×

Scriptlet Help - I'm just missing something..


2005 IR Help

Recommended Posts

By: Hugh Blandford - global

Scriptlet Help - I'm just missing something..

2005-08-02 05:50

Hi all,

 

thanks for the time to read this. I am trying to use a scriptlet to massage some of my dates. I pass a date string through to the report as a parameter. I want to assign a date string one month and one day earlier to a variable. I have written a scriptlet (my java coding is bad :-( ) that does this. When the boxes are in the details area of the report my scriptlet works fine (see example below, it works running without a data source). I just cannot work out how to get this to work when the fields are in any other area eg the column header.

 

What I really need to understand is what I need to change to make this work when the fields are in the column header or a group header. I have the iReport PDF manual (an excellent investment) but unfortunately my skills are not great enough to make this part of iReport work for me just yet.

 

My coding is poor and I haven't been able to work out what code I need to put in to which method! I tried modifying my code and calling myPrevious from the beforeColumInit method but had no joy.

 

Is there some way that the Scriptlet code can be debugged/stepped through?

 

Here is my example report with the two fields in and below the Scriptlet java file:

 

JXRML ---------------------

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="untitled_report_1"

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="untitled_report_1Scriptlet"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="1" />

<import value="java.util.*" />

<import value="net.sf.jasperreports.engine.*" />

<import value="net.sf.jasperreports.engine.data.*" />

<parameter name="BillPeriod" isForPrompting="false" class="java.lang.String">

<defaultValueExpression ><![CDATA[new String("2005-07-21")]]></defaultValueExpression>

</parameter>

<variable name="BillPeriodPrev" class="java.lang.String" resetType="Report" calculation="Nothing">

<variableExpression><![CDATA[((untitled_report_1Scriptlet)$P{REPORT_SCRIPTLET}).getPrevious()]]></variableExpression>

<initialValueExpression><![CDATA[new String("")]]></initialValueExpression>

</variable>

<background>

<band height="0" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="50" isSplitAllowed="true" >

</band>

</title>

<pageHeader>

<band height="50" isSplitAllowed="true" >

</band>

</pageHeader>

<columnHeader>

<band height="30" isSplitAllowed="true" >

</band>

</columnHeader>

<detail>

<band height="100" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

mode="Opaque"

x="300"

y="24"

width="110"

height="27"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$P{BillPeriod}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

<reportElement

mode="Opaque"

x="55"

y="24"

width="110"

height="27"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-3"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$V{BillPeriodPrev}]]></textFieldExpression>

</textField>

</band>

</detail>

<columnFooter>

<band height="30" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</pageFooter>

<lastPageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</lastPageFooter>

<summary>

<band height="50" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

 

---------------------

Here is my java file:

---------------------

import net.sf.jasperreports.engine.*;

import java.util.Calendar;

import java.lang.String;

import java.util.Date;

import java.text.SimpleDateFormat;

 

 

public class untitled_report_1Scriptlet extends it.businesslogic.ireport.IReportScriptlet {

String billingPeriod = new String();

Date billPeriod = new Date();

String prevBillPeriod = new String();

Calendar billCal = Calendar.getInstance();

SimpleDateFormat reverseDate = new SimpleDateFormat("yyyy-MM-dd");

 

public String getPrevious() {

try{

billingPeriod = ((java.lang.String)getParameterValue("BillPeriod"));

}catch(net.sf.jasperreports.engine.JRScriptletException jrse) {

System.err.println("Parameter assignment error: "+jrse.getMessage());

}

try{

billPeriod = reverseDate.parse(billingPeriod);

}catch(java.text.ParseException pe){

System.err.println("Parse exception: "+pe.getMessage());

}

billCal.setTime(billPeriod);

billCal.add(Calendar.DAY_OF_MONTH, -1);

billCal.add(Calendar.MONTH, -1);

prevBillPeriod = (reverseDate.format(billCal.getTime()));

return prevBillPeriod;

}

 

/** Creates a new instance of JRIreportDefaultScriptlet */

 

 

 

 

 

 

 

 

 

 

 

/** Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

public void afterColumnInit() throws JRScriptletException

{

super.beforeColumnInit();

getPrevious();

}

/** End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

public void afterDetailEval() throws JRScriptletException

{

super.afterDetailEval();

getPrevious();

}

/** End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

public void afterGroupInit(String groupName) throws JRScriptletException

{

super.afterGroupInit(groupName);

getPrevious();

}

/** End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

public void afterPageInit() throws JRScriptletException

{

super.afterPageInit();

getPrevious();

}

/** End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

public void afterReportInit() throws JRScriptletException

{

getPrevious();

}

/** End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

public void beforeColumnInit() throws JRScriptletException

{

getPrevious();

}

/** End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

public void beforeDetailEval() throws JRScriptletException

{

getPrevious();

}

/** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment*/

 

/** End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

public void beforeGroupInit(String groupName) throws JRScriptletException

{

 

}

/** End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

public void beforePageInit() throws JRScriptletException

{

 

}

/** End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

public void beforeReportInit() throws JRScriptletException

{

 

}

 

/** End EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

 

}

 

 

 

---------------------------

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...