Jaspersoft's Data Adapters
ernesto ongaro
july 2013
Session Overview
- Why Data Adapters?
- How to configure & use a Data Adapter
- Using in JasperReports Server
- Resources + Q&A
Why Data Adapters?
What is a data adapter?
-
A way to define a source for
JasperReports
using a configuration
file
- Different from a traditional datasource which is typically invoked like this:
public void myConnection(){ String HOST = "jdbc:mysql://localhost:3306/db"; String USER = "user"; String PASS = "pass"; conn = DriverManager.getConnection(HOST, USER, PASS);
JasperFillManager.fillReportToFile("report.jasper", parameters, myConnection());
- Data Adapters typically use an empty data source, instead defined in a file:
JasperFillManager.fillReportToFile("report.jasper", null, new JREmptyDataSource(5));
Why use them?
- Traditionally iReport has supported alternative datasource from JDBC like XML, CSV, XLS, JSON
- JasperReports Server traditionally doesn't support them out of the box (as of JRS 5.1)
- With DataAdapters we can support them in the server!
- Another DataAdapter feature: we can support multiple data sources in one report, example:
- Parent report and sub-report with two sources
- Sub-datasets with different sources
How do they work?
Supported Data Adapters:
-
BeanDataAdapter
-
ClasspathAwareDataAdapter
-
CsvDataAdapter
-
DataSourceDataAdapter
-
DataSourceProviderDataAdapter
-
EjbqlDataAdapter
-
EmptyDataAdapter
-
HibernateDataAdapter
-
JdbcDataAdapter
-
JndiDataAdapter
-
JsonDataAdapter
-
MondrianDataAdapter, XmlaDataAdapter
-
QueryExecuterDataAdapter
-
SpringHibernateDataAdapter
-
XlsDataAdapter, XlsxDataAdapter
- XmlDataAdapter, RemoteXmlDataAdapter
Data Source Adapter File Example:
<?xml version="1.0" encoding="UTF-8"?> <csvDataAdapter class="net.sf.jasperreports.data.csv.CsvDataAdapterImpl"> <name>csv</name> <fileName>/data/CsvDataSource.csv</fileName> <recordDelimiter> </recordDelimiter> <useFirstRowAsHeader>false</useFirstRowAsHeader> <queryExecuterMode>true</queryExecuterMode> <datePattern>yyyy-MM-dd HH:mm:ss</datePattern> <columnNames>address</columnNames> <columnNames>latitude</columnNames> <columnNames>longitude</columnNames> <columnNames>size</columnNames> <columnNames>color</columnNames> <columnNames>url</columnNames> <columnNames>icon</columnNames> <columnNames>iconurl</columnNames> <columnNames>iconwidth</columnNames> <columnNames>iconheight</columnNames> </csvDataAdapter>
Note: fileName can be in file system or repo:
Referencing Data Adapters from JRXML:
As "main report" dataset:
<jasperReport
xmlns="http://jasperreports.sourceforge.net/jasperreports"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
name="CustomersReport" pageWidth="595" pageHeight="842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50">
<property name="net.sf.jasperreports.data.adapter" value="/data/myAdapter.xml"/>
As "sub-dataset":
<subDataset name="MarkersDataset">
<property name="net.sf.jasperreports.data.adapter" value="/data/myAdapter.xml"/>
Note: files can be referenced in file system (example) or in repo: syntax
Using in JasperReports Server
Why different?
Remember these aren't standard datasources...the report will not have a "data source" attached
Steps from Jaspersoft Studio:
- Create a new data adapter
- Publish to server (hint drag and drop!)
-
Set net.sf.jasperreports.data.adapter repo:/path of adapter
- Publish report (set to "No Datasource")
(DEMO)
Note: File datasource have to be published with (xml) extension - csv, xls, etc not supported
Note2: You might need to add some jars from JSS for all data adapters to work
Examples in Server:
Examples ship in /reports/Interactive:
Limitations:
- Can not use in Ad-hoc topics (as of 5.1)
- This is planned soon!
- No UI for upload/management on server
Resources:
Questions?
Thank you!!!!
Jaspersoft Data Adapters
By ernestoo
Jaspersoft Data Adapters
- 7,808