public Representation build() {
validateState();
// Representation is a DTO so requires a default constructor
DefaultRepresentationFactory factory = new DefaultRepresentationFactory();
final Representation representation;
// Check for pagination links
if (paginationOptional.isPresent()) {
Pagination pagination = paginationOptional.get();
// Build a self URI with pagination parameters
URI paginatedSelf = UriBuilder
.fromUri(self)
.queryParam("pn", pagination.getCurrentPage())
.queryParam("ps",pagination.getResultsPerPage())
.build();
representation = factory.newRepresentation(paginatedSelf);
representation
.withLink("first", UriBuilder
.fromUri(self)
.queryParam("pn", 1)
.queryParam("ps", pagination.getResultsPerPage())
.build())
.withLink("previous", UriBuilder
.fromUri(self)
.queryParam("pn", pagination.getPreviousPage())
.queryParam("ps", pagination.getResultsPerPage())
.build())
.withLink("current", UriBuilder
.fromUri(self)
.queryParam("pn", pagination.getPreviousPage())
.queryParam("ps", pagination.getResultsPerPage())
.build())
.withLink("next", UriBuilder
.fromUri(self)
.queryParam("pn", pagination.getNextPage())
.queryParam("ps", pagination.getResultsPerPage())
.build())
.withLink("last", UriBuilder
.fromUri(self)
.queryParam("pn", pagination.getTotalPages())
.queryParam("ps", pagination.getResultsPerPage())
.build());
} else {
representation = factory.newRepresentation(self);
}
// Add any links
for (Map.Entry<String, String> entry : links.entrySet()) {
representation.withLink(entry.getKey(), entry.getValue());