Siguiente ‹UC: CRUD Empleados
Next ›UC: Get Reports

Use Case: Paged Queries

Class: net.hrider.api.usecases.Pagination.java
Some of the API collections can be too large to obtain them in a single request. This happens for example with the employees and the appraisals. In this cases, the API paginates the result returning only a small part of results by request.
To navigate through the collection the 'query' parameters used are: page, to indicate a specific page number, and pageSize to indicate the number of elements wanted to be returned per page. If not specified, the default values are page 1 and pageSize 50.

Since the Hrider API follows the HATEOS principle, we can navigate the collection using its links next and previous without having to worry about creating the URI for the next or the previous page. Example:

First create some employees (later we will obtain them in a paginated list):

Link employeesLink = company.getLinks().getEmployees();
service.create(employeesLink, buildEmployee("EMP1", "Employee 1", "[email protected]"));
service.create(employeesLink, buildEmployee("EMP2", "Employee 2", "[email protected]"));
service.create(employeesLink, buildEmployee("EMP3", "Employee 3", "[email protected]"));        


Make a paginated query from 2 to 2 employees (pageSize = 2) and print each page:

Link link = company.getLinks().getEmployees().addQueryParam("pageSize", "2");
EmployeesCollection collection = service.get(EmployeesCollection.class, link);
int page = 1;
printPage(page++, collection.getEmbedded().getEmployees());
while (collection.getLinks().getNext() != null) {
    collection = service.get(EmployeesCollection.class, collection.getLinks().getNext());
    printPage(page++, collection.getEmbedded().getEmployees());
}

As seen, using the next link it is possible go page by page as long as next is not null. The obtained result is the expected: a first page with 2 employees, and a last page with the remaining employee:

Page: 1
ID                  Code      Email               Name                
------------------- --------- ------------------  ------------------  
184916402857051713  EMP1      [email protected]       EMPLOYEE 1          
184916402968200770  EMP2      [email protected]       EMPLOYEE 2          


Page: 2
ID                  Code      Email               Name                
------------------- --------- ------------------  ------------------  
184916403059426883  EMP3      [email protected]       EMPLOYEE 3          

Siguiente ‹UC: CRUD Empleados
Next ›UC: Get Reports