Jasper Reports in ADF
July 16th, 2010 | Posted by Dorin Simina in Oracle ADF | 5 Comments
An application using statistical data requires often to generate reports (PDF, XLS etc.) in order to provide a visual document with the selected information.
There are some tools to provide report generation, and I’d like to present you a comprehensive environment – Jasper with its template builder iReports.
First of all I’ll start with the Jasper configuration regarding template building and data source provider. The best way to simulate information retrieved from DB is to create database beans and a database provider class to return List of data. My example builds an report with the data: name, department, job, e-mail and address and creates a chart with the employee distribution over the existing departments.
public class EmployeeBean {
private String name;
private String department;
private String job;
private String email;
private String address;
private List<DistributionByDepartment> distribution;
}
public class DistributionByDepartment {
private String department;
private int noOfEmployees;
}
public class PopulateBean {
public static List createBeanCollection() {
return employeeList;
}
}
The project with demo data is exported as .jar and is added to the Jasper’s class path. Now we can configure a JavaBean set datasource with the corresponding factory class and retrieve data method like in the picture:

We can start building our template by editing the query to access data from EmployeeBean. The group header band it will be set with the expression $F{department} and the detail band will contain information about employee name, job, e-mail and address. In order to display the chart we must build a secondary datasource based on information retrieved from distribution field.
The connection is established with the following directive and the key expression is set to $F{department} and the value to $F{noOfEmployees}:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{distribution})
The chart display has some problems regarding image quality so there must be build a new style layout with anti-alias properties. The report preview looks like:

The most challenging part is to integrate our final template in ADF with the corresponding data source. The first step is to include a series of jars for Jasper management (the full list is specified in the attached project’s libraries or in the following image). Do not forget the jar for chart theme and the fonts jar to support internationalization. Now we can invoke the API to build all kinds of reports: PDF, XLS, CSV.

I’ll focus on building a PDF report based on the attached template, with mock-up data. The business is exposed in a manged bean, and the code looks like:
public void generateReport(ActionEvent actionEvent) throws FileNotFoundException {
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(PopulateBean.createBeanCollection());
Map parameters = new HashMap();
try {
InputStream is = new FileInputStream(new File("Source path to template.jrxml"));
OutputStream os=new FileOutputStream(new File("Resulting report.pdf"));
JasperDesign jasperDesign = JRXmlLoader.load(is);
JasperReport jasperReport =
JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport, parameters, ds);
JasperExportManager.exportReportToPdfStream(jasperPrint, os);
} catch (JRException e) {
e.printStackTrace();
}
}
We can transmit parameters to out template through parameters variable. If we want to build XLS reports we use the following flow presented below. Here you have the possibility to create multiple sheets to display data and configure cell parameters.
JasperDesign jasperDesign = JRXmlLoader.load(is);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);
JExcelApiExporter jExcelApiExporter = new JExcelApiExporter();
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
jExcelApiExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jExcelApiExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
jExcelApiExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
jExcelApiExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
jExcelApiExporter.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING,"utf-8");
jExcelApiExporter.setParameter(JExcelApiExporterParameter.MAXIMUM_ROWS_PER_SHEET, 50000);
jExc1elApiExporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
jExcelApiExporter.setParameter(JExcelApiExporterParameter.SHEET_NAMES, new String[]{"Employees"});
jExcelApiExporter.exportReport();
The project is available here (template and data source) and the ADF integration here.



[...] filename. For more information related to the Jasper integration in ADF you can see my other post here. The source code for generating the report on the OutputStream is presented in the [...]
[...] Veja o artigo clicando no link a seguir: http://www.gebs.ro/blog/oracle/jasper-reports-in-adf/ [...]
So, doesn’t your submit button stay grayed out after you submit?
By pressing the button to generate report it will stay disabled after the proccess is completed?
Took me round to browse all of the comments, but I unequivocally enjoyed the object. It proved to be unconditionally useful to me and I am certain to all the commenters here! It’s in particular nice whenever you cannot just be informed, but additionally engaged! I am certain you had enjoyment writing this being.