13.07.2015 Views

iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library

SHOW MORE
SHOW LESS
  • No tags were found...

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>Groovy is a full language for the Java 2 Platform. Inside the Groovy language you can use all classes and JARs that areavailable for Java. Table 3-4 compares some typical UR expressions written in Java and Groovy.Table 3-4 Groovy and Java code samplesExpression Java GroovyField $F{field_name} $F{field_name}Sum of twodouble fieldsnew Double($F{f1}.doubleValue() + $F{f2}.doubleValue())$F{f1} + $F{f2}Comparison ofnumbersnew Boolean($F{f}.intValue() == 1) $F{f} == 1Comparison ofstringsnew Boolean($F{f} != null && $F{f}.equals("test"))$F{f} == "test"The following is a correct Groovy expression:new JREmptyDataSource($F{num_of_void_records})JREmptyDataSource is a class of UR that creates an empty record set (meaning with the all fields set to null). Youcan see how you can instance this class (a pure Java class) in Groovy without any problem. At the same time, Groovy allowsyou to use a simple expression like this one:5+5The language automatically encapsulates the primitive value 10 (the result of that expression) in a proper object. Actually, youcan do more: you can treat this value as an object of type String and create an expression such as:5 + 5+ ”my value”Whether or not such an expression resolves to a rational value, it is still a legal expression and the result will be an object oftype String with the value:10 my valueHiding the difference between objects and primitive values, Groovy allows the comparison of different types of objects andprimitive values, such as the legal expression:$F{Name} == “John”This expression returns true or false, or, again:$F{Age} > 18Returns true if the Age object interpreted as a number is greater than18.“340” < 100 Always returns false.“340”.substring(0,2) < 100Always returns true (since the substring method call will produce thestring “34”, which is less than 100).Groovy provides a way to greatly simplify expressions and never complains about null objects that can crash a Java expressionthrowing a NullPointerException. It really does open the doors of UR to people who don’t know Java.3.8 Using JavaScript as a Language for ExpressionsJavaScript is a popular scripting language with a syntax very similar to Java and Groovy. The support for JavaScript has beenrequested for a long time from the community and was finally introduced in JasperReports 3.1.2, using the open source RhinoJavaScript implementation.JavaScript has a set of functions and object methods that in some cases differ from Java and Groovy. For example, the methodString.startsWith(...) does not exist in JavaScript. The good news is that you can still use Java objects in JavaScript. Asimple example is:42

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

Saved successfully!

Ooh no, something went wrong!