Before exploring the data sources provided by Jaspersoft Studio, it's important to understand how the JRDataSource interface works. Every JRDataSource must implement both these methods:
public boolean next()
public Object getFieldValue(JRField jrField)
The public boolean next() method returns true if the cursor is positioned correctly in the subsequent record, false if no more records are available The public Object getFieldValue(JRField jrField) method is useful for moving a virtual cursor to the next record. In fact, data supplied by a JRDataSource is ideally organized into records as in a table. .
Every time JasperReports executes the public boolean next() method, all the fields declared in the report are filled and all the expressions (starting from those associated with the variables) are calculated again. Subsequently, JasperReports determines whether to print the header of a new group, to go to a new page, and so on. When next returns false, the report is ended by printing all final bands (Group Footer, Column Footer, Last Page Footer, and Summary). The method can be called as many times as there are records present (or represented) from the data source instance.
The method public Object getFieldValue(JRField jrField) is called by JasperReports after a call to next results in a true value. In particular, it's executed for every single field declared in the report. In the call, a JRField object is passed as a parameter. It's used to specify the name, the description and the type of the field from which to obtain the value (all this information, depending on the specific data source implementation, can be combined to extract the field value).
The type of the value returned by the public Object getFieldValue(JRField jrField) method has to be adequate for that declared in the JRField parameter, except when a null is returned. If the type of the field was declared as java.lang.Object, the method can return an arbitrary type. In this case, if required, a cast can be used in the expressions. A cast is a way to dynamically indicate the type on an object, the syntax of a cast is:
(type)object
in example:
(com.jaspersoft.ireport.examples.beans.PersonBean)$F{my_person}
Usually a cast is required when you need to call a method on the object that belongs to a particular class.
Recommended Comments
There are no comments to display.