DZone

Today I’d like to show you a simple table HTML report created with only thirteen lines of source code, written in a single command line using the Java programming language. Let’s pretend the data provision service already exists. If we wanted to offer our users some simple (sequential) content filtering for a table with column sorting, our implementation will have to add a few extra lines of code. To optimize this filtering, we’ll need to dig deeper into the backend. The web component takes its data from an object of type Stream, and so the maximum volume of presented data is limited only by the capabilities of our web browser. For my data source I used a freely available list of hotels in CSV format and it shouldn’t matter that some of the information it contains isn’t up to date. Let’s take a look at the body of the doGet() method of our trusty old Java servlet for those thirteen lines of code we mentioned. We’ll assemble the model of our table using the ReportBuilder class from the Ujorm framework.

Java

 

<div class="codeMirror-code–wrapper" data-code="new ReportBuilder("Simple Hotel Report")
.add(hotel -> hotel.getName(), "Hotel", NAME).sortable(true)
.add(hotel -> hotel.getCity().getName(), "City", CITY).sortable()
.add(hotel -> hotel.getStreet(), "Street").sortable()
.add(hotel -> hotel.getPrice(), "Price").sortable()
.add(hotel -> hotel.getCurrency(), "Currency")
.add(hotel -> hotel.getPhone(), "Phone")
.add(hotel -> hotel.getStars(), "Stars").sortable()
.setFooter(e -> e.addText("Data source: ").addLinkedText(HOTELBASE, HOTELBASE))
.build(input, output, builder -> selectHotels(builder,
DEFAULT_ROW_LIMIT,
NAME.of(input),
CITY.of(input)));” data-lang=”text/x-java”>

xxxxxxxxxx
1

14

 

1

new ReportBuilder<Hotel>("Simple Hotel Report")

2

        .add(hotel -> hotel.getName(), "Hotel", NAME).sortable(true)

3

        .add(hotel -> hotel.getCity().getName(), "City", CITY).sortable()

4

        .add(hotel -> hotel.getStreet(), "Street").sortable()

5

        .add(hotel -> hotel.getPrice(), "Price").sortable()

6

        .add(hotel -> hotel.getCurrency(), "Currency")

7

        .add(hotel -> hotel.getPhone(), "Phone")

8

        .add(hotel -> hotel.getStars(), "Stars").sortable()

9

        .setFooter(e -> e.addText("Data source: ").addLinkedText(HOTELBASE, HOTELBASE))

10

        .build(input, output, builder -> selectHotels(builder,

11

                        DEFAULT_ROW_LIMIT,

12

                        NAME.of(input),

13

                        CITY.of(input)));

Each call to the add() method adds another column to our table. The first argument of this method contains the code for getting the contents of a table cell, the second contains the name of the column header. If we want to filter the column, we also pass a constant representing the HTTP parameter of the web page. To support sorting, we add a call to the sortable() method, whose (optional) parameter determines the direction of sorting, but this is only for graphical representation of the data, the following method takes care of the actual sorting of the data. Due to licensing terms and conditions, we have to add a link to the original data project, inserted in the footer using the setFooter() method. The title of the HTML page is generated implicitly from the text provided in the constructor of the builder, however, using the setHeader() method, we can replace it with our own implementation. The build() method then calls the data source method, with the attached code:

Source: DZone