11.02.2013 Views

iReport Ultimate Guide

iReport Ultimate Guide

iReport Ultimate Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

it was still valid because the user was not using a new tag in the XML. With the move to an XML schema, the JRXML output<br />

of <strong>iReport</strong> 3.1.1 and newer can only be compiled with a JasperReports 3.1.0 or later.<br />

3.5 Expressions<br />

Though I designed <strong>iReport</strong> to be useful for non-technical report designers, many settings in a report are defined using formulas<br />

(such as conditions to hide an element, special calculations, text processing, and so on) that require a minimum knowledge of<br />

a scripting language.<br />

Fortunately, formulas can be written in at least three languages, two of which (JavaScript and Groovy) are pretty simple and<br />

can be used without knowledge of programming methods.<br />

All of the formulas in JasperReports are defined through expressions. The default expression language is Java, but I suggest<br />

that you design your projects with JavaScript or Groovy. Both hide a lot of the Java complexity and are definitively the<br />

languages to use if you don’t know Java. The language is a property of the document, so, to set it, select the document root<br />

node in the Outline view and choose your language in the Language property in the Properties view. We will go through all<br />

the languages in the following sections, but let’s concentrate for a moment on our definition of an “expression,” in particular<br />

the type you will declare for it and why that is important in JasperReports.<br />

An expression is just a formula that operates on some values and returns a result. Think of an expression as the formula you<br />

might define for a spreadsheet cell. A cell can have a simple value or you can use a complex formula that refers to other<br />

values; in a spreadsheet you would refer to values contained in other cells, whereas in JasperReports you will use the report<br />

fields, parameters, and variables. The main point is that whatever you have in your expression, when it is computed it gives a<br />

value as result (which can be null; that’s still a value).<br />

3.5.1 The Type of an Expression<br />

The type of an expression is the nature of the value resulting from it; the type is determined by the context in which the<br />

expression is used. For example, if your expression is used to evaluate a condition, the type of the expression should be<br />

Boolean (true or false); if you are creating an expression that should be displayed in a textfield, it will probably be a String or<br />

a number (Integer or Double). We could simplify the declaration of types by limiting them to text, numbers, Booleans, and<br />

generic object values. Unfortunately, JasperReports is a bit more formal and in many cases you have to be very precise when<br />

setting the type of your expression.<br />

So far, we are discussing only Java types (regardless of the language used). Some of the most important types are:<br />

As noted, if the expression is used to determine the value of a condition that determines, for instance, whether an element<br />

should be printed, the return type will be java.lang.Boolean; to create it, you need an expression that returns an instance of<br />

38<br />

java.lang.Boolean Defines an Object that represents a boolean value such as true and<br />

false<br />

java.lang.Byte Defines an Object that represents a byte<br />

java.lang.Short Defines an Object that represents an short integer<br />

java.lang.Integer Defines an Object that represents integer numbers<br />

java.lang.Long Defines an Object that represents long integer numbers<br />

java.lang.Float Defines an Object that represents floating point numbers<br />

java.lang.Double Defines an Object that represents real numbers<br />

java.lang.String Defines an Object that represents a text<br />

java.util.Date Defines an Object that represents a date or a timestamp<br />

java.lang.Object A generic java Object

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

Saved successfully!

Ooh no, something went wrong!