use of org.springframework.hateoas.PagedResources.PageMetadata in project taskana by Taskana.
the class TaskController method getTasks.
@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<PagedResources<TaskSummaryResource>> getTasks(@RequestParam MultiValueMap<String, String> params) throws InvalidArgumentException, NotAuthorizedException {
TaskQuery query = taskService.createTaskQuery();
query = applyFilterParams(query, params);
query = applySortingParams(query, params);
PageMetadata pageMetadata = null;
List<TaskSummary> taskSummaries = null;
String page = params.getFirst(PAGING_PAGE);
String pageSize = params.getFirst(PAGING_PAGE_SIZE);
if (page != null && pageSize != null) {
// paging
long totalElements = query.count();
pageMetadata = initPageMetadata(pageSize, page, totalElements);
taskSummaries = query.listPage((int) pageMetadata.getNumber(), (int) pageMetadata.getSize());
} else if (page == null && pageSize == null) {
// not paging
taskSummaries = query.list();
} else {
throw new InvalidArgumentException("Paging information is incomplete.");
}
TaskSummaryResourcesAssembler taskSummaryResourcesAssembler = new TaskSummaryResourcesAssembler();
PagedResources<TaskSummaryResource> pagedResources = taskSummaryResourcesAssembler.toResources(taskSummaries, pageMetadata);
return new ResponseEntity<>(pagedResources, HttpStatus.OK);
}
use of org.springframework.hateoas.PagedResources.PageMetadata in project taskana by Taskana.
the class AbstractPagingController method initPageMetadata.
protected PageMetadata initPageMetadata(String pagesizeParam, String pageParam, long totalElements) throws InvalidArgumentException {
long pagesize;
long page;
try {
pagesize = Long.valueOf(pagesizeParam);
page = Long.valueOf(pageParam);
} catch (NumberFormatException e) {
throw new InvalidArgumentException("page and pagesize must be a integer value.");
}
PageMetadata pageMetadata = new PageMetadata(pagesize, page, totalElements);
if (pageMetadata.getNumber() > pageMetadata.getTotalPages()) {
// unfortunately no setter for number
pageMetadata = new PageMetadata(pagesize, pageMetadata.getTotalPages(), totalElements);
}
return pageMetadata;
}
use of org.springframework.hateoas.PagedResources.PageMetadata in project taskana by Taskana.
the class WorkbasketController method getWorkbaskets.
@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<PagedResources<WorkbasketSummaryResource>> getWorkbaskets(@RequestParam(value = "sortBy", defaultValue = "name", required = false) String sortBy, @RequestParam(value = "order", defaultValue = "asc", required = false) String order, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "nameLike", required = false) String nameLike, @RequestParam(value = "key", required = false) String key, @RequestParam(value = "keyLike", required = false) String keyLike, @RequestParam(value = "descLike", required = false) String descLike, @RequestParam(value = "owner", required = false) String owner, @RequestParam(value = "ownerLike", required = false) String ownerLike, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "requiredPermission", required = false) String requiredPermission, @RequestParam(value = "page", required = false) String page, @RequestParam(value = "pagesize", required = false) String pageSize) throws InvalidArgumentException {
WorkbasketQuery query = workbasketService.createWorkbasketQuery();
addSortingToQuery(query, sortBy, order);
addAttributeFilter(query, name, nameLike, key, keyLike, descLike, owner, ownerLike, type);
addAuthorizationFilter(query, requiredPermission);
PageMetadata pageMetadata = null;
List<WorkbasketSummary> workbasketSummaries = null;
if (page != null && pageSize != null) {
// paging
long totalElements = query.count();
pageMetadata = initPageMetadata(pageSize, page, totalElements);
workbasketSummaries = query.listPage((int) pageMetadata.getNumber(), (int) pageMetadata.getSize());
} else if (page == null && pageSize == null) {
// not paging
workbasketSummaries = query.list();
} else {
throw new InvalidArgumentException("Paging information is incomplete.");
}
WorkbasketSummaryResourcesAssembler assembler = new WorkbasketSummaryResourcesAssembler();
PagedResources<WorkbasketSummaryResource> pagedResources = assembler.toResources(workbasketSummaries, pageMetadata);
return new ResponseEntity<>(pagedResources, HttpStatus.OK);
}
use of org.springframework.hateoas.PagedResources.PageMetadata in project spring-data-commons by spring-projects.
the class PagedResourcesAssembler method toEmptyResource.
private PagedResources<?> toEmptyResource(Page<?> page, Class<?> type, Optional<Link> link) {
Assert.notNull(page, "Page must must not be null!");
Assert.isTrue(!page.hasContent(), "Page must not have any content!");
Assert.notNull(type, "Type must not be null!");
Assert.notNull(link, "Link must not be null!");
PageMetadata metadata = asPageMetadata(page);
EmbeddedWrapper wrapper = wrappers.emptyCollectionOf(type);
List<EmbeddedWrapper> embedded = Collections.singletonList(wrapper);
return addPaginationLinks(new PagedResources<>(embedded, metadata), page, link);
}
Aggregations