Search in sources :

Example 1 with PageMetadata

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);
}
Also used : PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) TaskSummaryResourcesAssembler(pro.taskana.rest.resource.mapper.TaskSummaryResourcesAssembler) TaskSummaryResource(pro.taskana.rest.resource.TaskSummaryResource) ResponseEntity(org.springframework.http.ResponseEntity) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskQuery(pro.taskana.TaskQuery) TaskSummary(pro.taskana.TaskSummary) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with PageMetadata

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;
}
Also used : PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException)

Example 3 with 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);
}
Also used : PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) ResponseEntity(org.springframework.http.ResponseEntity) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) WorkbasketSummaryResourcesAssembler(pro.taskana.rest.resource.mapper.WorkbasketSummaryResourcesAssembler) WorkbasketQuery(pro.taskana.WorkbasketQuery) WorkbasketSummaryResource(pro.taskana.rest.resource.WorkbasketSummaryResource) WorkbasketSummary(pro.taskana.WorkbasketSummary) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with PageMetadata

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);
}
Also used : PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) EmbeddedWrapper(org.springframework.hateoas.core.EmbeddedWrapper)

Aggregations

PageMetadata (org.springframework.hateoas.PagedResources.PageMetadata)4 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)3 ResponseEntity (org.springframework.http.ResponseEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 EmbeddedWrapper (org.springframework.hateoas.core.EmbeddedWrapper)1 TaskQuery (pro.taskana.TaskQuery)1 TaskSummary (pro.taskana.TaskSummary)1 WorkbasketQuery (pro.taskana.WorkbasketQuery)1 WorkbasketSummary (pro.taskana.WorkbasketSummary)1 TaskSummaryResource (pro.taskana.rest.resource.TaskSummaryResource)1 WorkbasketSummaryResource (pro.taskana.rest.resource.WorkbasketSummaryResource)1 TaskSummaryResourcesAssembler (pro.taskana.rest.resource.mapper.TaskSummaryResourcesAssembler)1 WorkbasketSummaryResourcesAssembler (pro.taskana.rest.resource.mapper.WorkbasketSummaryResourcesAssembler)1