org.springframework.hateoas.PagedModel.PageMetadata Java Examples

The following examples show how to use org.springframework.hateoas.PagedModel.PageMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskController.java    From taskana with Apache License 2.0 6 votes vote down vote up
@GetMapping(path = Mapping.URL_TASKS)
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<TaskanaPagedModel<TaskSummaryRepresentationModel>> getTasks(
    @RequestParam MultiValueMap<String, String> params) throws InvalidArgumentException {
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Entry to getTasks(params= {})", params);
  }

  TaskQuery query = taskService.createTaskQuery();
  query = applyFilterParams(query, params);
  query = applySortingParams(query, params);

  PageMetadata pageMetadata = getPageMetadata(params, query);
  List<TaskSummary> taskSummaries = getQueryList(query, pageMetadata);

  TaskanaPagedModel<TaskSummaryRepresentationModel> pagedModels =
      taskSummaryRepresentationModelAssembler.toPageModel(taskSummaries, pageMetadata);
  ResponseEntity<TaskanaPagedModel<TaskSummaryRepresentationModel>> response =
      ResponseEntity.ok(pagedModels);
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Exit from getTasks(), returning {}", response);
  }

  return response;
}
 
Example #2
Source File: AbstractRessourcesAssembler.java    From taskana with Apache License 2.0 6 votes vote down vote up
protected PagedResources<?> addPageLinks(
    PagedResources<?> pagedResources, PageMetadata pageMetadata) {
  UriComponentsBuilder original = getBuilderForOriginalUri();
  pagedResources.add(
      (Link.of(original.replaceQueryParam("page", 1).toUriString())).withRel("first"));
  pagedResources.add(
      (Link.of(original.replaceQueryParam("page", pageMetadata.getTotalPages()).toUriString()))
          .withRel("last"));
  if (pageMetadata.getNumber() > 1L) {
    pagedResources.add(
        (Link.of(original.replaceQueryParam("page", pageMetadata.getNumber() - 1L).toUriString()))
            .withRel("prev"));
  }

  if (pageMetadata.getNumber() < pageMetadata.getTotalPages()) {
    pagedResources.add(
        (Link.of(original.replaceQueryParam("page", pageMetadata.getNumber() + 1L).toUriString()))
            .withRel("next"));
  }

  return pagedResources;
}
 
Example #3
Source File: WorkbasketController.java    From taskana with Apache License 2.0 6 votes vote down vote up
@GetMapping(path = Mapping.URL_WORKBASKET)
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<TaskanaPagedModel<WorkbasketSummaryRepresentationModel>> getWorkbaskets(
    @RequestParam MultiValueMap<String, String> params) throws InvalidArgumentException {
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Entry to getWorkbaskets(params= {})", params);
  }

  WorkbasketQuery query = workbasketService.createWorkbasketQuery();
  query = applySortingParams(query, params);
  applyFilterParams(query, params);

  PageMetadata pageMetadata = getPageMetadata(params, query);
  List<WorkbasketSummary> workbasketSummaries = getQueryList(query, pageMetadata);
  TaskanaPagedModel<WorkbasketSummaryRepresentationModel> pagedModels =
      workbasketSummaryRepresentationModelAssembler.toPageModel(
          workbasketSummaries, pageMetadata);

  ResponseEntity<TaskanaPagedModel<WorkbasketSummaryRepresentationModel>> response =
      ResponseEntity.ok(pagedModels);
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Exit from getWorkbaskets(), returning {}", response);
  }

  return response;
}
 
Example #4
Source File: ClassificationController.java    From taskana with Apache License 2.0 6 votes vote down vote up
@GetMapping(path = Mapping.URL_CLASSIFICATIONS)
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<TaskanaPagedModel<ClassificationSummaryRepresentationModel>>
    getClassifications(@RequestParam MultiValueMap<String, String> params)
        throws InvalidArgumentException {
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Entry to getClassifications(params= {})", params);
  }

  ClassificationQuery query = classificationService.createClassificationQuery();
  query = applySortingParams(query, params);
  applyFilterParams(query, params);

  PageMetadata pageMetadata = getPageMetadata(params, query);
  List<ClassificationSummary> classificationSummaries = getQueryList(query, pageMetadata);

  ResponseEntity<TaskanaPagedModel<ClassificationSummaryRepresentationModel>> response =
      ResponseEntity.ok(
          classificationSummaryRepresentationModelAssembler.toPageModel(
              classificationSummaries, pageMetadata));
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Exit from getClassifications(), returning {}", response);
  }

  return response;
}
 
Example #5
Source File: StarbucksClient.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void discoverStoreSearch() {

	Traverson traverson = new Traverson(URI.create(String.format(SERVICE_URI, port)), MediaTypes.HAL_JSON);

	// Set up path traversal
	TraversalBuilder builder = traverson. //
			follow("stores", "search", "by-location");

	// Log discovered
	log.info("");
	log.info("Discovered link: {}", builder.asTemplatedLink());
	log.info("");

	Map<String, Object> parameters = new HashMap<>();
	parameters.put("location", "40.740337,-73.995146");
	parameters.put("distance", "0.5miles");

	PagedModel<EntityModel<Store>> resources = builder.//
			withTemplateParameters(parameters).//
			toObject(new PagedModelType<EntityModel<Store>>() {});

	PageMetadata metadata = resources.getMetadata();

	log.info("Got {} of {} stores: ", resources.getContent().size(), metadata.getTotalElements());

	StreamSupport.stream(resources.spliterator(), false).//
			map(EntityModel::getContent).//
			forEach(store -> log.info("{} - {}", store.name, store.address));
}
 
Example #6
Source File: TaskanaPagingAssembler.java    From taskana with Apache License 2.0 5 votes vote down vote up
default TaskanaPagedModel<D> toPageModel(Iterable<T> entities, PageMetadata pageMetadata) {
  return StreamSupport.stream(entities.spliterator(), false)
      .map(this::toModel)
      .collect(
          Collectors.collectingAndThen(
              Collectors.toList(), l -> new TaskanaPagedModel<>(getProperty(), l, pageMetadata)));
}
 
Example #7
Source File: PagedResources.java    From taskana with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the pagination metadata.
 *
 * @return the metadata
 */
@JsonProperty("page")
public PageMetadata getMetadata() {
  if (Objects.isNull(metadata)) {
    Collection<T> contentCollection = getContent();
    return new PageMetadata(contentCollection.size(), 0, contentCollection.size());
  }
  return metadata;
}
 
Example #8
Source File: AbstractPagingController.java    From taskana with Apache License 2.0 5 votes vote down vote up
protected <T> List<T> getQueryList(BaseQuery<T, ?> query, PageMetadata pageMetadata) {
  List<T> resultList;
  if (pageMetadata != null) {
    resultList = query.listPage((int) pageMetadata.getNumber(), (int) pageMetadata.getSize());
  } else {
    resultList = query.list();
  }
  return resultList;
}
 
Example #9
Source File: AbstractPagingController.java    From taskana with Apache License 2.0 5 votes vote down vote up
protected PageMetadata getPageMetadata(
    MultiValueMap<String, String> params, BaseQuery<?, ?> query) throws InvalidArgumentException {
  PageMetadata pageMetadata = null;
  if (hasPagingInformationInParams(params)) {
    // paging
    long totalElements = query.count();
    pageMetadata = initPageMetadata(params, totalElements);
    validateNoInvalidParameterIsLeft(params);
  } else {
    // not paging
    validateNoInvalidParameterIsLeft(params);
  }
  return pageMetadata;
}
 
Example #10
Source File: WorkbasketAccessItemRepresentationModelAssembler.java    From taskana with Apache License 2.0 5 votes vote down vote up
public TaskanaPagedModel<WorkbasketAccessItemRepresentationModel> toPageModelForSingleWorkbasket(
    String workbasketId,
    List<WorkbasketAccessItem> workbasketAccessItems,
    PageMetadata pageMetadata)
    throws NotAuthorizedException, WorkbasketNotFoundException {
  TaskanaPagedModel<WorkbasketAccessItemRepresentationModel> pageModel =
      toPageModel(workbasketAccessItems, pageMetadata);
  pageModel.add(
      linkTo(methodOn(WorkbasketController.class).getWorkbasketAccessItems(workbasketId))
          .withSelfRel());
  pageModel.add(
      linkTo(methodOn(WorkbasketController.class).getWorkbasket(workbasketId))
          .withRel("workbasket"));
  return pageModel;
}
 
Example #11
Source File: WorkbasketSummaryRepresentationModelAssembler.java    From taskana with Apache License 2.0 5 votes vote down vote up
@PageLinks(Mapping.URL_WORKBASKET_ID_DISTRIBUTION)
public TaskanaPagedModel<WorkbasketSummaryRepresentationModel> toDistributionTargetPageModel(
    List<WorkbasketSummary> workbasketSummaries, PageMetadata pageMetadata) {
  return workbasketSummaries.stream()
      .map(this::toModel)
      .collect(
          Collectors.collectingAndThen(
              Collectors.toList(),
              list -> new TaskanaPagedModel<>(DISTRIBUTION_TARGETS, list, pageMetadata)));
}
 
Example #12
Source File: WorkbasketAccessItemController.java    From taskana with Apache License 2.0 5 votes vote down vote up
/**
 * This GET method return all workbasketAccessItems that correspond the given data.
 *
 * @param params filter, order and access ids.
 * @return all WorkbasketAccesItemResource.
 * @throws NotAuthorizedException if the user is not authorized.
 * @throws InvalidArgumentException if some argument is invalid.
 */
@GetMapping(path = Mapping.URL_WORKBASKET_ACCESS_ITEMS)
public ResponseEntity<TaskanaPagedModel<WorkbasketAccessItemRepresentationModel>>
    getWorkbasketAccessItems(@RequestParam MultiValueMap<String, String> params)
        throws NotAuthorizedException, InvalidArgumentException {
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Entry to getWorkbasketAccessItems(params= {})", params);
  }

  WorkbasketAccessItemQuery query = workbasketService.createWorkbasketAccessItemQuery();
  getAccessIds(query, params);
  applyFilterParams(query, params);
  query = applySortingParams(query, params);

  PageMetadata pageMetadata = getPageMetadata(params, query);
  List<WorkbasketAccessItem> workbasketAccessItems = getQueryList(query, pageMetadata);

  TaskanaPagedModel<WorkbasketAccessItemRepresentationModel> pagedResources =
      workbasketAccessItemRepresentationModelAssembler.toPageModel(
          workbasketAccessItems, pageMetadata);

  ResponseEntity<TaskanaPagedModel<WorkbasketAccessItemRepresentationModel>> response =
      ResponseEntity.ok(pagedResources);
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Exit from getWorkbasketAccessItems(), returning {}", response);
  }

  return response;
}
 
Example #13
Source File: PageLinksAspect.java    From taskana with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Around("@annotation(pro.taskana.resource.rest.PageLinks) && args(data, page, ..)")
public <T extends RepresentationModel<? extends T> & ProceedingJoinPoint>
    RepresentationModel<T> addLinksToPageResource(
        ProceedingJoinPoint joinPoint, List<?> data, PageMetadata page) throws Throwable {
  HttpServletRequest request =
      ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
  PageLinks pageLinks = method.getAnnotation(PageLinks.class);
  String relativeUrl = pageLinks.value();
  UriComponentsBuilder original = originalUri(relativeUrl, request);
  RepresentationModel<T> resourceSupport = (RepresentationModel<T>) joinPoint.proceed();
  resourceSupport.add(Link.of(original.toUriString()).withSelfRel());
  if (page != null) {
    resourceSupport.add(
        Link.of(original.replaceQueryParam("page", 1).toUriString())
            .withRel(IanaLinkRelations.FIRST));
    resourceSupport.add(
        Link.of(original.replaceQueryParam("page", page.getTotalPages()).toUriString())
            .withRel(IanaLinkRelations.LAST));
    if (page.getNumber() > 1) {
      resourceSupport.add(
          Link.of(original.replaceQueryParam("page", page.getNumber() - 1).toUriString())
              .withRel(IanaLinkRelations.PREV));
    }
    if (page.getNumber() < page.getTotalPages()) {
      resourceSupport.add(
          Link.of(original.replaceQueryParam("page", page.getNumber() + 1).toUriString())
              .withRel(IanaLinkRelations.NEXT));
    }
  }
  return resourceSupport;
}
 
Example #14
Source File: CustomerStub.java    From microservice with Apache License 2.0 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public PagedModel<Customer> getAll() {
	return new PagedModel<Customer>(Arrays.asList(new Customer(42,
			"Eberhard", "Wolff", "[email protected]",
			"Unter den Linden", "Berlin")), new PageMetadata(1, 0, 1));
}
 
Example #15
Source File: TaskHistoryEventListResourceAssembler.java    From taskana with Apache License 2.0 4 votes vote down vote up
public TaskHistoryEventListResource toResources(
    List<HistoryEventImpl> historyEvents, PageMetadata pageMetadata) {

  TaskHistoryEventResourceAssembler assembler = new TaskHistoryEventResourceAssembler();
  List<TaskHistoryEventResource> resources =
      new ArrayList<>(assembler.toCollectionModel(historyEvents).getContent());
  TaskHistoryEventListResource pagedResources =
      new TaskHistoryEventListResource(resources, pageMetadata);

  pagedResources.add(Link.of(this.getOriginal().toUriString()).withSelfRel());
  if (pageMetadata != null) {
    pagedResources.add(linkTo(TaskHistoryEventController.class).withRel("allTaskHistoryEvent"));
    pagedResources.add(
        Link.of(this.getOriginal().replaceQueryParam("page", 1).toUriString())
            .withRel(IanaLinkRelations.FIRST));
    pagedResources.add(
        Link.of(
                this.getOriginal()
                    .replaceQueryParam("page", pageMetadata.getTotalPages())
                    .toUriString())
            .withRel(IanaLinkRelations.LAST));
    if (pageMetadata.getNumber() > 1) {
      pagedResources.add(
          Link.of(
                  this.getOriginal()
                      .replaceQueryParam("page", pageMetadata.getNumber() - 1)
                      .toUriString())
              .withRel(IanaLinkRelations.PREV));
    }
    if (pageMetadata.getNumber() < pageMetadata.getTotalPages()) {
      pagedResources.add(
          Link.of(
                  this.getOriginal()
                      .replaceQueryParam("page", pageMetadata.getNumber() + 1)
                      .toUriString())
              .withRel(IanaLinkRelations.NEXT));
    }
  }

  return pagedResources;
}
 
Example #16
Source File: CatalogStub.java    From microservice with Apache License 2.0 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public PagedModel<Item> getAll() {
	return new PagedModel<Item>(
			Arrays.asList(new Item(1, "iPod", 42.0)), new PageMetadata(1,
					0, 1));
}
 
Example #17
Source File: TaskHistoryEventListResource.java    From taskana with Apache License 2.0 4 votes vote down vote up
public TaskHistoryEventListResource(
    Collection<TaskHistoryEventResource> content, PageMetadata metadata, Iterable<Link> links) {
  super(content, metadata, links);
}
 
Example #18
Source File: TaskHistoryEventListResource.java    From taskana with Apache License 2.0 4 votes vote down vote up
public TaskHistoryEventListResource(
    Collection<TaskHistoryEventResource> content, PageMetadata metadata, Link... links) {
  super(content, metadata, links);
}
 
Example #19
Source File: TaskHistoryEventController.java    From taskana with Apache License 2.0 4 votes vote down vote up
@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<TaskHistoryEventListResource> getTaskHistoryEvents(
    @RequestParam MultiValueMap<String, String> params) throws InvalidArgumentException {
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug("Entry to getTaskHistoryEvents(params= {})", params);
  }

  HistoryQuery query = simpleHistoryService.createHistoryQuery();
  query = applySortingParams(query, params);
  applyFilterParams(query, params);

  PageMetadata pageMetadata = null;
  List<HistoryEventImpl> historyEvents;
  final String page = params.getFirst(PAGING_PAGE);
  final String pageSize = params.getFirst(PAGING_PAGE_SIZE);
  params.remove(PAGING_PAGE);
  params.remove(PAGING_PAGE_SIZE);
  validateNoInvalidParameterIsLeft(params);
  if (page != null && pageSize != null) {
    long totalElements = query.count();
    pageMetadata = initPageMetadata(pageSize, page, totalElements);
    historyEvents = query.listPage((int) pageMetadata.getNumber(), (int) pageMetadata.getSize());
  } else if (page == null && pageSize == null) {
    historyEvents = query.list();
  } else {
    throw new InvalidArgumentException("Paging information is incomplete.");
  }

  TaskHistoryEventListResourceAssembler assembler = new TaskHistoryEventListResourceAssembler();
  TaskHistoryEventListResource pagedResources =
      assembler.toResources(historyEvents, pageMetadata);

  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug(
        "Exit from getTaskHistoryEvents(), returning {}",
        new ResponseEntity<>(pagedResources, HttpStatus.OK));
  }

  return new ResponseEntity<>(pagedResources, HttpStatus.OK);
}
 
Example #20
Source File: TaskSummaryRepresentationModelAssembler.java    From taskana with Apache License 2.0 4 votes vote down vote up
@PageLinks(Mapping.URL_TASKS)
public TaskanaPagedModel<TaskSummaryRepresentationModel> toPageModel(
    List<TaskSummary> taskSummaries, PageMetadata pageMetadata) {
  return TaskanaPagingAssembler.super.toPageModel(taskSummaries, pageMetadata);
}
 
Example #21
Source File: TaskanaPagedModel.java    From taskana with Apache License 2.0 4 votes vote down vote up
public PageMetadata getMetadata() {
  return metadata;
}
 
Example #22
Source File: TaskCommentRepresentationModelAssembler.java    From taskana with Apache License 2.0 4 votes vote down vote up
@Override
@PageLinks(Mapping.URL_TASK_COMMENTS)
public TaskanaPagedModel<TaskCommentRepresentationModel> toPageModel(
    Iterable<TaskComment> taskComments, PageMetadata pageMetadata) {
  return TaskanaPagingAssembler.super.toPageModel(taskComments, pageMetadata);
}
 
Example #23
Source File: CustomerStub.java    From microservice-kubernetes with Apache License 2.0 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public PagedModel<Customer> getAll() {
	return new PagedModel<Customer>(Arrays.asList(new Customer(42,
			"Eberhard", "Wolff", "[email protected]",
			"Unter den Linden", "Berlin")), new PageMetadata(1, 0, 1));
}
 
Example #24
Source File: ClassificationSummaryRepresentationModelAssembler.java    From taskana with Apache License 2.0 4 votes vote down vote up
@Override
@PageLinks(Mapping.URL_CLASSIFICATIONS)
public TaskanaPagedModel<ClassificationSummaryRepresentationModel> toPageModel(
    Iterable<ClassificationSummary> entities, PageMetadata pageMetadata) {
  return TaskanaPagingAssembler.super.toPageModel(entities, pageMetadata);
}
 
Example #25
Source File: WorkbasketAccessItemRepresentationModelAssembler.java    From taskana with Apache License 2.0 4 votes vote down vote up
@PageLinks(Mapping.URL_WORKBASKET_ACCESS_ITEMS)
public TaskanaPagedModel<WorkbasketAccessItemRepresentationModel> toPageModel(
    List<WorkbasketAccessItem> workbasketAccessItems, PageMetadata pageMetadata) {
  return TaskanaPagingAssembler.super.toPageModel(workbasketAccessItems, pageMetadata);
}
 
Example #26
Source File: WorkbasketSummaryRepresentationModelAssembler.java    From taskana with Apache License 2.0 4 votes vote down vote up
@Override
@PageLinks(Mapping.URL_WORKBASKET)
public TaskanaPagedModel<WorkbasketSummaryRepresentationModel> toPageModel(
    Iterable<WorkbasketSummary> entities, PageMetadata pageMetadata) {
  return TaskanaPagingAssembler.super.toPageModel(entities, pageMetadata);
}
 
Example #27
Source File: CatalogStub.java    From microservice-kubernetes with Apache License 2.0 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public PagedModel<Item> getAll() {
	return new PagedModel<Item>(
			Arrays.asList(new Item(1, "iPod", 42.0)), new PageMetadata(1,
					0, 1));
}
 
Example #28
Source File: PagedResources.java    From taskana with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link PagedResources} from the given content {@link PageMetadata} and {@link
 * Link}s.
 *
 * @param content must not be {@literal null}.
 * @param metadata the metadata
 * @param links the links
 */
public PagedResources(Collection<T> content, PageMetadata metadata, Iterable<Link> links) {
  super();
  this.content = content;
  this.metadata = metadata;
  this.add(links);
}
 
Example #29
Source File: TaskanaPagedModel.java    From taskana with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link TaskanaPagedModel} from the given content.
 *
 * @param property property which will be used for serialization.
 * @param content must not be {@literal null}.
 * @param metadata the metadata. Can be null. If null, no metadata will be serialized.
 */
public TaskanaPagedModel(
    TaskanaPagedModelKeys property, Collection<? extends T> content, PageMetadata metadata) {
  this.content = content;
  this.metadata = metadata;
  this.key = property;
}
 
Example #30
Source File: PagedResources.java    From taskana with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link PagedResources} from the given content, {@link PageMetadata} and {@link
 * Link}s (optional).
 *
 * @param content must not be {@literal null}.
 * @param metadata the metadata
 * @param links the links
 */
public PagedResources(Collection<T> content, PageMetadata metadata, Link... links) {
  this(content, metadata, Arrays.asList(links));
}