The data model

The data model is based on a simple SQL table, Call, that contains columns for the ID, client name, phone number, city, start time, duration, and status. The following is a JPA Entity representing this table:

@Entity
@Data
public class Call {

@Id
@GeneratedValue
private Long id;

private String client;

private String phoneNumber;

@Enumerated(EnumType.STRING)
private City city;

private LocalDateTime startTime;

private Integer duration;

@Enumerated(EnumType.STRING)
private Status status;
}

Status and City are simple Java enums that define some test values:

public enum Status {
RECEIVED, MISSED
}

public enum City {
BOGOTA, TURKU, LONDON, BERLIN, HELSINKI, TOKYO, SAN_FRANCISCO, SIDNEY, LAGOS, VANCOUVER, SANTIAGO, BEIJING
}

Notice the @Enumerated annotations in the city and status fields of the Call class. This is used to persist the value as a string in the database instead of an integer representing the value, which allows us to use simpler SQL queries for the reports.

We'll use two persistence frameworks in this application. For parts of the application that require saving data or running business logic, we'll use JPA. For reports data, we'll use MyBatis. In your applications you can, of course, use only one framework. The reason behind choosing MyBatis for report generation is that it's a great fit for constructing and maintaining complex SQL queries. SQL, in turn, is a powerful language and a perfect fit for reporting. The ability to copy an SQL query from your code and run it directly on a SQL client eases implementation and maintenance as you can quickly see the data you'd get in a report without having to compile or execute the application. Each report has its own data transfer model (DTO), a class that encapsulates the data to be rendered in a report in a convenient format. The advantage of this is that we don't have to query extra data not used in the report and so free the web server from data processing to some extent.

The configuration of both frameworks is implemented in the JPAService and MyBatisService classes and the persistence.xml and mybatis-config.xml files. A file-based H2 database is used by default, but you'll find configuration examples for MySQL and PostgreSQL as comments in the configuration files.

You can find the complete source code of this chapter's example in the Data-centric-Applications-with-Vaadin-8chapter-08 Maven project of the source code that accompanies this book.

Since a report viewer doesn't make sense without data, the example application includes a random data generator that populates the Call table with random data. When the table is empty, the generator will fill it with initial data representing phone calls made in the past 6 months at a rate of one million calls per year. If the table is not empty, the generator will "fill" the time span between the time of the last call in the table and the current time using the same rate. Additionally, the generator runs a background thread that inserts random data at runtime. This generator is meant to simulate a real-life situation in which data is constantly inserted into the database, sometimes even when the application is not running. You can find the implementation in the DataGenerator class. The DataGenerator functionality is invoked from a ServletContextListener that is defined in the WebConfig class. The initial time span and the rate used in the generator is configurable via parameters, in case you want to use different values.

..................Content has been hidden....................

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