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());
}
// Add the bean
if (beanOptional.isPresent()) {
representation.withBean(beanOptional.get());
}
// Add the embedded entries
for (Map.Entry<String, List<Representation>> entry : embedded.entrySet()) {
List<Representation> embeddedList = entry.getValue();
// Duplicated keys will be represented as an array
for (Representation embedded : embeddedList) {
representation.withRepresentation(entry.getKey(), embedded);
}
}
isBuilt = true;