The following sample data sources are found in the <js‑install>/samples/customDataSource directory:
| • | Custom Bean Data Source |
The following sample data sources are found in the <js‑install>/samples/customDataSource-pro directory (commercial versions only). These data sources include support for Domains:
| • | My Custom Data Source PRO |
| • | Cassandra Query Data Source |
| • | MongoDB Query Data Source |
| • | Spark Query Data Source |
Prerequisites
Java Development Kit
The examples are provided as Java source files. To work with the examples, you must install the Java Development Kit (JDK). Ensure that the JAVA_HOME environment variable points to a full JDK installation. Custom data sources are supported only with JDK 1.6.
Apache Ant
Ant is installed as part of the JasperReports Server installation process. Run Ant using the following command:
Linux: | <js-install>/apache-ant/bin/ant <ant-arguments> |
Windows: | <js-install>apache-antbinant.bat <ant-arguments> |
If you cannot find Ant on your system, you can download it from http://ant.apache.org. The bundled Apache Ant is version 1.8.1; this version or higher is recommended.
Installing the Custom Data Source Examples
Each sample directory includes:
| • | readme.txt: Text file describing how to build the examples. |
| • | build.xml: The Ant build file. |
| • | src: Java source directory. |
| • | webapp: A directory containing other files required by the examples, such as JSPs and Spring configuration files, which are copied directly to the JasperReports Server web application directory. |
| • | reports: A directory containing example JRXML files that use the sample custom data sources. |
To install the samples in your JasperReports Server web application:
| 1. | At the command line, change directories to the custom data source sample directory (<js‑install>/samples/customDataSource or <js-install>/samples/customDataSource-pro). |
| 2. | Edit build.xml and set the webAppDir property to the root of the JasperReports Server web application. |
| 3. | Run the Ant command (as described in Prerequisites) with no arguments; this executes the default target, which is named deploy. The deploy target initiates these actions: |
| • | Compiles the Java source under the src directory. |
| • | Deploys the compiled Java class files to the web application. |
| • | Deploys files under the webapp directory to the web application. |
| 4. | Restart the application server. |
The example custom data sources are now available from the New Data Source page in JasperReports Server.
To test the samples and create or view reports:
| 1. | Log on to JasperReports Server as an administrator. |
| 2. | If you are testing the webscraper report, you must register its query executer factory. See Webscraper Custom Data Source for more information. |
| 3. | Open the New Data Source page (for example, by selecting Create > Data Source for the main menu), enter the required values for the data source you selected, and save the data source. See the JasperReports Server Administrator Guide for more information about creating data sources. |
| 4. | If you are testing a sample from the <js‑install>/samples/customDataSource directory, upload the associated report from the <js‑install>/samples/customDataSource/reports directory and view the report. |
| 5. | If you are testing a sample from the <js‑install>/samples/customDataSource-pro directory, create a Domain using the Data Source you just created and then create an Ad Hoc View from your new Domain. |
Custom Bean Data Sources
There are two custom bean data source implementations, each of which creates a data source from a collection of Java beans declared in the source code.
| • | My Custom Data Source shows how to create a basic custom bean data source. Its Spring bean definition file is in <js-install>/samples/customDataSource/webapp/WEB-INF/applicationContext‑sampleCDS.xml. Jaspersoft provides an example report that uses this data source; it is called sampleCDS.jrxml and is located in the <js-install>/samples/customDataSource/reports directory. |
| • | My Custom Data Source PRO adds Domain support to a basic custom bean data source. Its Spring bean definition file is in <js-install>/samples/customDataSource/webapp/WEB-INF/applicationContext-sampleCDSWithMetaData.xml. To use this example in a report, create a Domain and then an Ad Hoc View. |
Webscraper Custom Data Source
The webscraper custom data source implementation fetches a web page, decodes its HTML, and extracts selected data that is turned into field values in the data source. Its Spring bean definition file is located in <js‑install>/samples/customDataSource/webapp/WEB-INF/applicationContext-webscraperDS.xml.
The webscraper data source configuration includes these elements:
| • | URL: An HTTP URL that refers to the HTML page containing the desired content. |
| • | DOM path: An XPath expression that locates HTML elements to be turned into rows in the data source. |
| • | Field paths: XPath expressions for each field defined in the JRXML. JasperReports Server uses these paths to locate the field value in each row selected by the DOM path. |
The implementation creates a data source by:
| • | Using the URL to issue a GET request for an HTML page. |
| • | Using the DOM path to select XML elements from the converted response. |
| • | Creating a new data source row for each selected element. |
| • | Determining the context for each field based on its field path. |
| • | The data source takes two parameters: the URL of the web page and the XPath that determines how elements in the HTML page become rows in the data source. The parameters can either be specified by a data source definition in the repository or by a query string in the JRXML. The <js-install>/samples/reports/webscrapertest.jrxml report has no query. Instead, it relies on an instance of the custom data source that you must create in the repository. Set the URL and DOM Path according the website you are trying to access. |
| Web sites are frequently redesigned and the URLs used by any website are subject to change. This can affect your reports. |
In order to use the webscraper data source, you must first register the webscraper query executer factory. One way to do this is as follows:
| 1. | Open the file <js-install>/WEB-INF/classes/jasperreports.properties for editing |
| 2. | Add the following at the end of the file: |
# registering query executer for webscraperQEtest.jrxml examplenet.sf.jasperreports.query.executer.factory.webscraper=example.cds.WebScraperQueryExecuterFactory[/code] |
| 4. | Restart your application server. |
For more information about registering query executors, see the Report Query section in the JasperReports Library Ultimate Guide.
Custom Bean Data Sources
The My Custom Data Source PRO implementation creates a data source from a collection of Java beans declared in the source code. Its Spring bean definition file is in <js-install>/samples/customDataSource/webapp/WEB-INF/applicationContext‑sampleCDS.xml. Jaspersoft provides an example report that uses this data source; it is called simpleCDS.jrxml and is located in the <js-install>/samples/customDataSource/reports directory.
JDBC Custom Data Sources with Domain Support
The examples in the <js‑install>/samples/customDataSource-pro directory implement Domain support using the CustomDomainMetaData class. The JDBC Query data source lets you connect to a JDBC data source, enter any query that returns a single table, and use that in a Domain. The Cassandra, MongoDB, and Spark data sources are specific instances of the JDBC Query data source, where the driver is hard-coded and the required connection information is specifically requested on the New Data Source page.
The JDBC custom data source implementation establishes the JDBC connection and extracts the data needed to build a data source. It also uses the necessary metadata from the query to build a Domain. Its Spring bean definition file is located in <js-install>/samples/customDataSource-pro/webapp/WEB-INF/applicationContext-JdbcQueryDataSource.xml.
The JDBC custom data source implementation uses a JDBCQueryDataSourceDefinition
class, based on JdbcDataAdapterImpl
, to allow the user to enter database connection information and a JDBC query. It then creates a data source as follows:
| • | Build a custom data source using the JasperReports Library JDBC Data Adapter. |
| • | Create a JDBC connection based on the driver, url, username, and password entered by the user using JDBCQueryDataSourceService |
| • | Executing the user-defined query and retrieving the metadata layer necessary for Domain support using JRJdbcQueryExecuterFactory. |
| • | Creating a new data source using JDBCQueryDataSourceService. |
The following steps describe how to create a data source from the examples and use it in a Domain and an Ad Hoc view.
To create a custom data source using a query example:
| 1. | Create a data source, for example, by choosing Create > Data Source from the main menu. |
The New Data Source page is displayed.
| 2. | Select the type of data source you want to create from the Type menu. |
The fields on the page change to prompt for the connection information required for your data source.
| 3. | Fill in the required connection information and enter the query you want to use. |
| 5. | When prompted, enter a name and location for your data source and click Save. |
To use a sample custom data source in a Domain:
| 1. | Create a Domain, for example, by choosing Create > Domain from the main menu, and use the query-based data source as the data source. Choose a save location. |
| 2. | In the Domain Designer, click Edit with Domain Designer... |
| 3. | The only tab available in the Domain Designer is the Display tab. In this tab, you can select fields, change labels, change field type (field/measure), and set default summary functions. |
| By default, the Domain only contains fields (Dimensions). You can create measures in the Domain Designer or in the Ad Hoc view. |
| 4. | Click OK, then click Submit. |
Once the Domain has been created, you can add support for Domain pre-filters and calculated fields in the Domain Design file. See the JasperReports Server User Guide for more information on creating and editing a Domain design file.
Using the Domain in an Ad Hoc View:
| 1. | Create an Ad Hoc view using the Domain you created. |
| 2. | If no measures are available, right-click on any numeric field and select Use as Measure. |
| 3. | Drag fields and measures to the canvas to create your Ad Hoc view. |
Recommended Comments
There are no comments to display.