Jump to content
We've recently updated our Privacy Statement, available here ×

Varible incrementing incorrectly


dcherk

Recommended Posts

I am using JasperReports 1.2.4 and iReport 1.2.4.

 

I am trying to create a variable "public_notes" that is incremented every time the group "coverageTypeCode" changes.

 

I have added such a variable using iReport, and this is what I got (added line breaks for formatting):

 

Code:
<variable name="public_notes" 
class="java.lang.String"
resetType="Group"
incrementType="Group"
incrementGroup="coverageTypeCode"
resetGroup="applicationId"
calculation="Nothing">
<variableExpression>
<![CDATA[$V{public_notes} + ( $P{public_notes}.containsKey( $F{coverage_type_code} ) ? ($V{public_notes}.length() > 0 ? "<BR/>" : "" ) + $P{public_notes}.get( $F{coverage_type_code} ) : "" )]]>
</variableExpression>
<initialValueExpression>
<![CDATA[""]]>
</initialValueExpression>
</variable>

I had assumed that since incrementType="Group" and incrementGroup="coverageTypeCode", the variableExpression would only be evaluated when the group changed. But I am finding that it is being evaluated with every record change instead.

 

Am I missing something obvious? I am attaching the entire file for reference.

 

Thanks,

Dave Cherkassky

DJiNN Software Inc.

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi,

 

You should not reference the variable inside its own expression.

The variable expression is called with every row in order to make group break estimations.

 

What you need is to implement a variable incrementer (JRIncrementer and JRIncrementerFactory interfaces) and associate it to your variable using the incrementerFactoryClass attribute.

Or, you could use a scriptlet to increment the variable.

Basically it is about teaching the JR engine to sum up String values. You'll probably use calculation="Sum" for your variable with custom incrementer, instead of "Nothing".

 

Our /demo/samples/scriptlet sample shows a similar String concatenation.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

Thanks, Teodor.

 

You've given me something to think about. Just a quick follow-up. I changed the variable not to refer to itself and to use calculation="Sum":

 

Code:
<variable name="public_notes" 
class="java.lang.String"
resetType="Group"
incrementType="Group"
incrementGroup="coverageTypeCode"
resetGroup="applicationId"
calculation="Sum">
<variableExpression>
<![CDATA[( $P{public_notes}.containsKey( $F{coverage_type_code} ) ? "<BR/>" + $P{public_notes}.get( $F{coverage_type_code} ) : "" )]]>
</variableExpression>
<initialValueExpression>
<![CDATA[""]]>
</initialValueExpression>
</variable>

With this expression the report does not work properly either: only the value from the last group is used (i.e. there is no string concatenation).

 

Would I be correct in saying that's the case because calculation="Sum" can't apply to Strings? Or should that work, and I have something else wrong...

 

(It would be great if I could avoid writing a scriptlet or making this any more complicated than it has to be).

 

 

All the best,

Dave Cherkassky.

Link to comment
Share on other sites

Yes, that worked.

 

For completeness, here is the final answer:

 

Code:
<variable name="public_notes" 
class="java.lang.String" resetType="Group"
incrementerFactoryClass="com.aimcompanies.website.reports.RenewalPublicNotesIncrementorFactory"
incrementType="Group"
incrementGroup="coverageTypeCode"
resetGroup="applicationId"
calculation="Sum">
<variableExpression>
<![CDATA[( $P{public_notes}.containsKey( $F{coverage_type_code} ) ? ( String )$P{public_notes}.get( $F{coverage_type_code} ) : null )]]>
</variableExpression>
<initialValueExpression>
<![CDATA[""]]>
</initialValueExpression>
</variable>

 

and the RenewalPublicNotesIncrementorFactory is:

 

Code:
[code]public class RenewalPublicNotesIncrementorFactory extends JRAbstractExtendedIncrementerFactory {

public static JRExtendedIncrementer INCREMENTOR = new JRAbstractExtendedIncrementer() {
public Object increment( JRCalculable variablePublicNotes, Object publicNote,
AbstractValueProvider abstractValueProvider ) throws JRException {

String publicNotes = ( String )variablePublicNotes.getIncrementedValue();
if( publicNotes == null ) {
publicNotes = ( String )initialValue();
}

if( publicNote != null && publicNote.trim().length() > 0 ) ) {
if( publicNotes.length() == 0 ) {
publicNotes = ( String )publicNote;
} else {
publicNotes = publicNotes + "<BR/>" + publicNote;
}
}

return publicNotes;
}

public Object initialValue() {
return "";
}
};

public JRExtendedIncrementer getExtendedIncrementer( byte calculation ) {
if ( calculation == JRVariable.CALCULATION_SUM ) {
return INCREMENTOR;
} else {
throw new UnsupportedOperationException(
"RenewalPublicNotesIncrementorFactory can only do Sum calculations" );
}
}
}

 

Many thanks,

Dave Cherkassky

Link to comment
Share on other sites

  • 10 years later...

Hi, 

I have one question about Matrix/Crosstab reports in JasperReports. I would like to show Departmentwise, which Salesmen, Clerks, Analysts and Managers are working. For example, see below table. There are multiple values in Measure column.   Since, I am beginner and have no idea how to print multiple values in Measure field.   

I am getting single value means only name of one employee.  Not all clerks and salemen are displayed. Even though I choosed 'Nothing' option in the wizard for Measure Field.  

I have got an idea somehow that need to implement custom incrementer class and that will be used inside property 'Incrementer Factory class'. But unfortunately, no idea how to start and write that custom class using 'JRAbstractExtendedIncrementer' and 'JRExtendedIncrementer' interfaces.

Does anyone have any report sample using them or any other idea to display such information ??  

Looking forward to your replies.

 CLERKMANAGERPRESIDENTANALYSTSALESMAN
ACCOUNTINGMILLERCLARKKINGJAMESNull
RESEARCHADAMS
SMITH
PETER
JONESNullNullNull
SALESJAMESBLAKENullNullALLEN
MARTIN
TURNER
WARD

 

Thank you.

 

Regards,

Eddie

 
Link to comment
Share on other sites

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...