04.06.2015 Views

iReport Ultimate Guide - Docs.nimsoft.com

iReport Ultimate Guide - Docs.nimsoft.com

iReport Ultimate Guide - Docs.nimsoft.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong><br />

Groovy is a full language for the Java 2 Platform. Inside the Groovy language you can use all classes and JARs that are<br />

available for Java. Table 3-4 <strong>com</strong>pares some typical JasperReports expressions written in Java and Groovy.<br />

Table 3-4<br />

Groovy and Java code samples<br />

Expression Java Groovy<br />

Field $F{field_name} $F{field_name}<br />

Sum of two<br />

double fields<br />

new Double($F{f1}.doubleValue() + $F{f2}.doubleValue())<br />

$F{f1} + $F{f2}<br />

Comparison of<br />

numbers<br />

new Boolean($F{f}.intValue() == 1) $F{f} == 1<br />

Comparison of<br />

strings<br />

new Boolean($F{f} != null && $F{f}.equals("test"))<br />

$F{f} == "test"<br />

The following is a correct Groovy expression:<br />

new JREmptyDataSource($F{num_of_void_records})<br />

JREmptyDataSource is a class of JasperReports that creates an empty record set (meaning with the all fields set to null). You<br />

can see how you can instance this class (a pure Java class) in Groovy without any problem. At the same time, Groovy allows<br />

you to use a simple expression like this one:<br />

5+5<br />

The language automatically encapsulates the primitive value 10 (the result of that expression) in a proper object. Actually, you<br />

can do more: you can treat this value as an object of type String and create an expression such as:<br />

5 + 5+ ”my value”<br />

Whether or not such an expression resolves to a rational value, it is still a legal expression and the result will be an object of<br />

type String with the value:<br />

10 my value<br />

Hiding the difference between objects and primitive values, Groovy allows the <strong>com</strong>parison of different types of objects and<br />

primitive values, such as the legal expression:<br />

$F{Name} == “John”<br />

This expression returns true or false, or, again:<br />

$F{Age} > 18<br />

Returns true if the Age object interpreted as a number is greater than<br />

18.<br />

“340” < 100 Always returns false.<br />

“340”.substring(0,2) < 100<br />

Always returns true (since the substring method call will produce the<br />

string “34”, which is less than 100).<br />

Groovy provides a way to greatly simplify expressions and never <strong>com</strong>plains about null objects that can crash a Java expression<br />

throwing a NullPointerException. It really does open the doors of JasperReports to people who don’t know Java.<br />

3.8 Using JavaScript as a Language for Expressions<br />

JavaScript is a popular scripting language with a syntax very similar to Java and Groovy. The support for JavaScript has been<br />

requested for a long time from the <strong>com</strong>munity and was finally introduced in JasperReports 3.1.2, using the open source Rhino<br />

JavaScript implementation.<br />

JavaScript has a set of functions and object methods that in some cases differ from Java and Groovy. For example, the method<br />

String.startsWith(...) does not exist in JavaScript. The good news is that you can still use Java objects in JavaScript. A<br />

simple example is:<br />

42

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!