ReviewIn the previous section, we have laid down the functional specs of the application. In this section, we will discuss the project's structure and write the Java classes in layers.
Table of ContentsPart 1: Introduction and Functional Specs
Part 2: Java classes
Part 3: Jasper layout with iReport
Part 4: HTML and jqGrid
Part 5: XML configuration
Part 6: Running the Application
Project StructureOur application is a Maven project and therefore follows Maven structure. As we create the classes, we've organized them in logical layers: domain, repository, service, and controller.
Here's a preview of our project's structure:
DomainThis layer contains two domain classes, User and Role. They represent our database tables, user and role respectively. Because we're developing a JPA-based repository, both classes must be annotated with JPA annotations.
Data Transfer Object (DTO)This layer contains four DTO classes:
- UserDto is a POJO for mapping User objects to and from the presentation layer
- StatusResponse is a POJO for sending boolean responses to the presentation layer
- JqgridResponse is a container for UserDto objects for sending records to the jqGrid table. It contains information regarding the number of rows, current page, and total pages.
- JasperDto is a POJO for mapping User objects into something that Jasper can process easily (This is used for reporting purposes only.)
Note: We won't display the contents of UserDto, StatusReponse, and JqgridResponse here since we've already discussed that on a different tutorial (see Spring MVC 3.1, jqGrid, and Spring Data JPA Integration Guide (Part 2))
ControllerThis layer contains two controllers, MediatorController and UserController.
- MediatorController is responsible for redirecting requests from the root path to the Users page
- UserController is responsible for handling user related requests, including download requests
Let's describe the methods available from UserController:
- The records() method returns a list of UserDto objects as JSON strings. This will be displayed on the presentation layer as an interactive grid of data with the help of jqGrid plugin.
- checkDownloadProgress() method is used to check if a download token is present or not. See Download Token Algorithm below
- getDownloadToken() method is used to retrieve a download token. See Download Token Algorithm below
- download() method performs the actual report download by delegating it to the DownloadService.
Download Token Algorithm
(Inspired by Detect when browser receives file download)
This is basically a fancy way of adding a progress box in an AJAX environment, so that we can display the status of a download requests. In a typical AJAX requests, the call is perform asynchronously, and the user does not know if the requests is being processed unless we provide a progress report. Unfortunately, you can't use AJAX directly for file downloads.
However, you can provide an illusion of a progress report through the Download Token Algorithm:
- User clicks on download button. A progress box appears.
- Download button performs an AJAX requests to retrieve a download token
- Once a download token has been received, a call to the actual download URL is performed. (This is not an AJAX requests)
- Periodically perform an AJAX requests to check if the download token retrieved earlier still exists. If not, the download has started.
- If the download token does not exists anymore, close the progress box.
RepositoryThis layer contains a single interface, UserRepository. This is our data access object (DAO). With the help of Spring Data JPA, Spring will automatically provide the actual implementation.
What is Spring Data JPA?
Spring JPA is part of the umbrella Spring Data project that makes it easy to easily implement JPA based repositories.
Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.
ServiceThis layer contains the following services:
- TokenService: This is responsible for providing and managing download tokens. See the Download Token Algorithm above.
- JasperDatasourceService: Wraps a list of objects into a JRBeanCollectionDataSource. This is the datasource for our Jasper reports.
- ExporterService: This is responsible for exporting a Jasper report into different formats, such as Pdf and Excel.
- DownloadService: This is responsible for the actual download requests and writing of reports to the output stream
The DownloadService is the heart of the download process. The algorithm is as follows:
- Add report parameters
- Retrieve template
- Convert template to JasperDesign
- Compile design to JasperReport
- Create the JasperPrint object
- Create an output byte stream where data will be written
- Export report
- Write to reponse stream
- JqgridFilter is a Java representation of a jqGrid filter
- JqgridObjectMapper is used to convert a jqGrid filter to a JqgridFilter object
- UserMapper is used to map User objects to UserDto objects
- TraceInterceptor is an AOP-based utility class to help us debug our application. This is a subclass of CustomizableTraceInterceptor. See Spring Data JPA FAQ
Note: We won't display the contents of JqgridFilter, JqgridObjectMapper, UserMapper, and TraceInterceptor here since we've already discussed them on a different tutorial. See Spring Data JPA FAQ
NextIn the next section, we will create the Jasper report layout and design it using iReport. Click here to proceed.
Share the joy:
Subscribe by reader Subscribe by email Share