iReport Ultimate Guide - Nimsoft Library

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

13.07.2015 Views

iReport Ultimate GuideCode Example 11-9Interface JRQueryExecuterFactory provided by UR, continued* These parameters will be created as system-defined parameters for each* report/dataset having a query of this type.* The returned array should contain consecutive pairs of parameter* names and parameter classes* (e.g. {"Param1", String.class, "Param2", "List.class"}).* @return array of built-in parameter names and types associated* with this query type*/public Object[] getBuiltinParameters();/*** Creates a query executer.* This method is called at fill time for reports/datasets having a* query supported by* this factory.* @param dataset the dataset containing the query, fields, etc.* @param parameters map of value parameters (instances of* {@link JRValueParameter JRValueParameter})* indexed by name** @return a query executer* @throws JRException*/public JRQueryExecuter createQueryExecuter(JRDataset dataset, Map parameters) throws JRException;/*** Decides whether the query executers created by this factory support* a query parameter type.* This check is performed for all $P{..} parameters in the query.** @param className the value class name of the parameter* @return whether the parameter value type is supported*/public boolean supportsQueryParameterType(String className);}There are three methods to implement: getBuiltinParameters, createQueryExecuter, andsupportsQueryParameterType:• The first method returns an array containing names and types of built-in parameters that the query executer makesavailable. This feature is useful when the query is executed against some kind of session object or against a connection toan external entity, such as a database or a server.For example, the query executer factory for SQL provides the built-in parameter REPORT_CONNECTION, storing thejava.sql.Connection instance used to execute the query. This object can be used by subreports to execute their SQLqueries. Similarly, the query executer factory for HQL provides as a parameter the Hibernate session required to performthe query.• The second method (createQueryExecuter) is responsible for creating the query executer instance, making it possiblythe most important one of the three methods.212

Data Sources and Query Executers• Finally, you can filter the accepted parameter types by implementing the supportsQueryParameterType method,which returns true if the class name given as an argument is accepted, and false otherwise.In this implementation, you will not return any built-in parameter, and you will accept all types of parameters (actually, yourquery executer factory ignores any $P{} directives in the query).Here is the code:Code Example 11-10 CustomQueryExecuterFactory source codeimport java.io.File;import java.util.Map;import net.sf.jasperreports.engine.JRDataset;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.query.JRQueryExecuter;import net.sf.jasperreports.engine.query.JRQueryExecuterFactory;/**** @version $Id: CustomQueryExecuterFactory.java 0 2009-12-08 11:45:45 CET gtoffoli $* @author Giulio Toffoli (giulio@jaspersoft.com)**/public class CustomQueryExecuterFactory implements JRQueryExecuterFactory {public Object[] getBuiltinParameters() {return new Object[]{};}public JRQueryExecuter createQueryExecuter(JRDataset jrd, Map map)throws JRException {File directory = null;try {directory = new File(jrd.getQuery().getText());} catch (Exception ex){throw new JRException(ex);}}return new CustomQueryExecuter(directory);}public boolean supportsQueryParameterType(String string) {return true;}The only relevant portion of this implementation is the createQueryExecuter method, which looks into the dataset passedas argument for the query string. We assume that the query is a directory path (remember that our data source lists the filescontained in a specified directory path). With the directory path we instance a CustomQueryExecuter, the class that willmake use of the parsed query (or the File object created starting from the query).If you would like to add support for parameters in the query string, this may be the right place to implement the parameters’parsing and replacement. We have everything we need: the query string, the dataset, and the map with the values of the213

<strong>iReport</strong> <strong>Ultimate</strong> <strong>Guide</strong>Code Example 11-9Interface JRQueryExecuterFactory provided by UR, continued* These parameters will be created as system-defined parameters for each* report/dataset having a query of this type.* The returned array should contain consecutive pairs of parameter* names and parameter classes* (e.g. {"Param1", String.class, "Param2", "List.class"}).* @return array of built-in parameter names and types associated* with this query type*/public Object[] getBuiltinParameters();/*** Creates a query executer.* This method is called at fill time for reports/datasets having a* query supported by* this factory.* @param dataset the dataset containing the query, fields, etc.* @param parameters map of value parameters (instances of* {@link JRValueParameter JRValueParameter})* indexed by name** @return a query executer* @throws JRException*/public JRQueryExecuter createQueryExecuter(JRDataset dataset, Map parameters) throws JRException;/*** Decides whether the query executers created by this factory support* a query parameter type.* This check is performed for all $P{..} parameters in the query.** @param className the value class name of the parameter* @return whether the parameter value type is supported*/public boolean supportsQueryParameterType(String className);}There are three methods to implement: getBuiltinParameters, createQueryExecuter, andsupportsQueryParameterType:• The first method returns an array containing names and types of built-in parameters that the query executer makesavailable. This feature is useful when the query is executed against some kind of session object or against a connection toan external entity, such as a database or a server.For example, the query executer factory for SQL provides the built-in parameter REPORT_CONNECTION, storing thejava.sql.Connection instance used to execute the query. This object can be used by subreports to execute their SQLqueries. Similarly, the query executer factory for HQL provides as a parameter the Hibernate session required to performthe query.• The second method (createQueryExecuter) is responsible for creating the query executer instance, making it possiblythe most important one of the three methods.212

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

Saved successfully!

Ooh no, something went wrong!