Designing the report

Let's start with the implementation of a simple report, the Worldwide Calls in the Last Hour report, shown in the following screenshot:

To create a report using DynamicJasper, you have to create an object of type DynamicReport. This is done by using the DynamicReportBuilder class, which provides methods to add the title, header, columns, and other elements that form the report. The DynamicReportBuilder class implements the builder pattern to allow creating the report step by step and a method to build the DynamicReport instance. There are several subclasses of DynamicReportBuilder; we'll follow the examples given in the official documentation and use the FastReportBuilder class.

We can start by configuring the title and header information, enabling full page width, setting the text to show when there's no data, and enabling a background color for odd rows:

DynamicReport report = new FastReportBuilder()
.setTitle("Worldwide Calls in the Last Hour")
.addAutoText("CONFIDENTIAL", AutoText.POSITION_HEADER, AutoText.ALIGMENT_LEFT, 200, new Style())
.addAutoText(LocalDateTime.now().toString(), AutoText.POSITION_HEADER, AutoText.ALIGNMENT_RIGHT, 200, new Style())
.setUseFullPageWidth(true)
.setWhenNoData("(no calls)", new Style())
.setPrintBackgroundOnOddRows(true)
.build();

Notice how, after configuring the report, we end the sentence by calling the build method, which returns an instance of DynamicReport. All configuration calls happen between the instantiation (new FastReportBuilder()) and the call to build().

The report data is defined by columns. Columns are configured with the addColumn method. The addColumn method accepts an instance of type AbstractColumn that we can create by using ColumnBuilder, also a builder class. The following snippet of code demonstrates how to create the seven columns that make up the report:

DynamicReport report = new FastReportBuilder()
...
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("city", City.class)
.setTitle("City")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("client", String.class)
.setTitle("Client")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("phoneNumber", String.class)
.setTitle("Phone number")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("startTime", LocalDateTime.class)
.setTitle("Date")
.setTextFormatter(DateTimeFormatter.ISO_DATE.toFormat())
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("startTime", LocalDateTime.class)
.setTextFormatter(DateTimeFormatter.ISO_LOCAL_TIME.toFormat())
.setTitle("Start time")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("duration", Integer.class)
.setTitle("Minutes")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("status", Status.class)
.setTitle("Status").build())
.build();

For each column, we have to specify the name of the corresponding Java property in the CallDto class and its type. We can also specify the title and text formatters when needed.

The DynamicReport instance defines the visual structure of the report. With this in place, we can create a JasperPrint object, which represents a page-oriented document that can be later exported to multiple formats. We first need to get the data from the service class, and then pass the DynamicReport instance and the data to the generateJasperPrint method of the DynamicJasperHelper class:

List<CallDto> calls = ReportsService.lastHourCalls();
JasperPrint print = DynamicJasperHelper.generateJasperPrint(report, new ClassicLayoutManager(), calls);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset