iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate Guideparameters). Another solution would be to pass all this information to the query executer implementation and delegate thequery parsing to it.The query executer has a simple interface, too. Again, there are three methods:• One that will produce the JRDataSource to fill the report (createDatasource).• One to clean up everything at the end of the execution (close).• And a method to interrupt the query execution (cancelQuery).Code Example 11-11 Query executer interface/*** Query executer interface.* An implementation of this interface is created when the input data* of a report/dataset is specified by a query.* The implementation will run the query and create a JRDataSource* from the result.* The query executers would usually be initialized by a JRQueryExecuterFactory* with the query and the parameter values.*/public interface JRQueryExecuter{/*** Executes the query and creates a JRDataSource out of the result.** @return a JRDataSource wrapping the query execution result.* @throws JRException*/public JRDataSource createDatasource() throws JRException;/*** Closes resources kept open during the datasource iteration.* This method is called after the report is filled or the dataset is* iterated.* If a resource is not needed after the datasource has been created,* it should be released at the end of createDatasource.*/public void close();/*** Cancels the query if it’s currently running.* This method will be called from a different thread if the client* decides to cancel the filling process.** @return true iff the query was running and it has been* cancelled* @throws JRException*/public boolean cancelQuery() throws JRException;}The very simple query executer we are creating will do nothing when the close and the cancelQuery methods are invoked.The main method, createDatasource, will create an instance of CustomDataSource, providing the report query as a path214

Data Sources and Query Executersof the directory name to list. The aim of the operation, in fact, is to return a list of file names encapsulated in a bean array datasource.Our CustomQueryExecuter will look like the following:Code Example 11-12 The source of our QueryExecuter implementationpackage samples.queryexecuter;import java.io.File;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;import net.sf.jasperreports.engine.query.JRQueryExecuter;public class CustomQueryExecuter implements JRQueryExecuter {File directory = null;public CustomQueryExecuter(File directory){this.directory = directory;}public JRDataSource createDatasource() throws JRException {// Creates a list of files and present them using the CustomDataSourceif (directory != null && directory.exists() && directory.isDirectory()){File[] files = directory.listFiles();return new CustomDataSource(files);}throw new JRException("Invalid directory!");}public void close() {// Nothing to do in this implementation}}public boolean cancelQuery() throws JRException {// Too fast to be interrupted... :-)return false;}Up to now we have created the CustomQueryExecuterFactory and the CustomQueryExecuter, which uses a class calledCustomDataSource. This class extends the JRBeanArrayDataSource. In this sample, we may just use aJRBeanArrayDataSource, but the implementation of another custom data source can be useful to introduce the next task:creating and using a FieldsProvider.215

Data Sources and Query Executersof the directory name to list. The aim of the operation, in fact, is to return a list of file names encapsulated in a bean array datasource.Our CustomQueryExecuter will look like the following:Code Example 11-12 The source of our QueryExecuter implementationpackage samples.queryexecuter;import java.io.File;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;import net.sf.jasperreports.engine.query.JRQueryExecuter;public class CustomQueryExecuter implements JRQueryExecuter {File directory = null;public CustomQueryExecuter(File directory){this.directory = directory;}public JRDataSource createDatasource() throws JRException {// Creates a list of files and present them using the CustomDataSourceif (directory != null && directory.exists() && directory.isDirectory()){File[] files = directory.listFiles();return new CustomDataSource(files);}throw new JRException("Invalid directory!");}public void close() {// Nothing to do in this implementation}}public boolean cancelQuery() throws JRException {// Too fast to be interrupted... :-)return false;}Up to now we have created the CustomQueryExecuterFactory and the CustomQueryExecuter, which uses a class calledCustomDataSource. This class extends the JRBeanArrayDataSource. In this sample, we may just use aJRBeanArrayDataSource, but the implementation of another custom data source can be useful to introduce the next task:creating and using a FieldsProvider.215

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

Saved successfully!

Ooh no, something went wrong!