Thursday, 27 November 2008

The Definitive Guide to JasperReports - Part4

Couple of Weeks back i started reading The Definitive Guide to JasperReports By Teodor Danciu , Lucian Chirita.

Nice book written by Teodor Danciu , Lucian Chirita.

I wanted to share few qoutations i found from this book found from the next 2 chapters (7-8).

1) JRXML

JRXML is the name we use when referring to XML files that represent the definition of a JasperReports template and that comply with the mentioned DTD structure. When working with JRXML report templates, JasperReports uses its own internal DTD files to validate the XML content it receives for processing. If the XML validation passes, it means that the supplied report design corresponds to the JasperReports-required XML structure and syntax, and the engine is able to generate the compiled version of the report design.
Valid JRXML report templates always point to the JasperReports internal DTD files for validation.
If the DTD reference is not specified, report compilation will fail abruptly. This should not be a big problem since the DTD reference is always the same and can simply be copied from previous report templates. To start with, you can copy it from the supplied samples.

2) Page Orientation

The orientation attribute determines whether the documents use the Portrait or the Landscape format. JasperReports requires you to adapt the page width and the page height when switching from Portrait documents to Landscape, and vice versa. For example, assume that you want to create an A4 report using the Portrait layout.

An A4 report has approximately this size:

pageWidth="595" pageHeight="842" orientation="Portrait"

If you decide to use the Landscape layout for your A4 document, you must be sure to modify the page width and page height accordingly, as follows:

pageWidth="842" pageHeight="595" orientation="Landscape"

This is because JasperReports has to know exactly the absolute width and height of the pages it will draw on, and does not necessarily consider the value supplied in the orientation attribute, at least not at report-filling time.

This orientation attribute is useful only at report-printing time to inform the printer about the page orientation, and in some special exporters. The default page orientation is "Portrait".

3) Expressions

Expressions are a powerful feature of JasperReports. They can be used to declare report variables that perform various calculations, group data on the report, specify report text field content, or further customize the appearance of report objects.

4) Built-In Functions

When JasperReports was internationalized, some of the data and message-formatting logic was placed inside the engine itself to avoid forcing users to rely on external utility classes.Report expressions can perform method calls on various objects that are available during report filling, such as parameters, fields, or variable values, but can also call methods on a special object that is already available as the this reference. This is the calculator object presented in the previous section. It has public utility methods that are ready to use inside report expressions.

Currently, there are only a few utility methods of the calculator object available as built-in functions inside report expressions. These are the following:

• msg: This function offers a convenient way to format messages based on the current report locale, just as you would normally do when using a java.text.MessageFormat instance. Furthermore, several signatures for this function take up to three message parameters in order to make the formatting functionality easier to use.

• str: This function is the equivalent of the $R{} syntax. It gives access to locale-specific resources from the associated resource bundle.

5) Conditional Expressions

As the Java language documentation states, an expression is a series of variables, operators,and method calls (constructed according to the syntax of the language) that evaluate to a single value.

So even if you rely on the Java language for writing report expressions, you cannot use Java statements like if else, for, or while.

However, quite often an expression must return a value that is calculated based on a condition or even multiple conditions. To accomplish this, use the conditional operator ?:. You can even nest this operator inside a Java expression to obtain the desired output based on multiple conditions.

The following text field displays No data if the value for the quantity field is null:


$F{quantity} == null ? "No data" : String.valueOf($F{quantity})


6) BLOB and CLOB Support

When the SQL query retrieves data from table columns that have large binary or large char data types and are mapped to java.sql.Blob and java.sql.Clob values through JDBC, the current data source implementation tries to simplify the data by using intelligent field mapping.

For instance, in most cases, BLOB columns are used to store images in the database that the application might need to use inside a report. If the report field that maps a given BLOB column from the java.sql.ResultSet is of type java.awt.Image, the data source will try to read from the java.sql.Blob instance and load an image object using a java.awt.MediaTracker. Or, if very large chunks of text are stored in large character columns inside the database,then the data source will try to read the text from the database and load it in java.lang.String objects, in case the corresponding report field was declared as being of type String. The supported mappings are as follows:

• BLOB: java.sql.Blob, java.io.InputStream, and java.awt.Image
• CLOB: java.sql.Clob, java.io.InputStream, java.io.Reader, and java.lang.String

About the Author

Teodor Danciu is the founder and architect of the JasperReports library, the most popular open source reporting tool, and is now working for JasperSoft. Before joining JasperSoft, Teodor worked for almost 8 years with several French IT companies as a software engineer and team leader on ERP and other medium-to-large database-related enterprise applications using mainly Java technologies and the J2EE platform. Teodor holds a B.S. in computer science from the Academy of Economic Studies in Bucharest.

Lucian Chirita joined Teodor Danciu at JasperSoft in the development of JasperReports library back in 2005, and he quickly became one of the main contributors to the project. Prior to joining JasperSoft, Lucian had 3 years of software engineering experience on several Java/J2EE products and applications. His contributions to JasperReports include support for crosstabs and integration with data query technologies such as Hibernate and OLAP. Lucian holds an M.S. in computer science from the University of Bucharest.

No comments: