@RequestMapping(produces = {MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<PagedResources<StreamMetrics>> fetchMetrics( @RequestParam(value = "name", defaultValue = "") String name) { Collection<StreamMetrics> entries = service.toStreamMetrics(name); int totalPages = (entries.size() == 0) ? 0 : entries.size() / entries.size(); PagedResources.PageMetadata pageMetadata = new PagedResources.PageMetadata(entries.size(), 0, entries.size(), totalPages); PagedResources<StreamMetrics> pagedResources = new PagedResources<>(entries, pageMetadata, ControllerLinkBuilder.linkTo(MetricsCollectorEndpoint.class).withRel(Link.REL_SELF)); return new ResponseEntity<>(pagedResources, HttpStatus.OK); }
return "Error when registering applications from " + uri + ": " + e.getMessage(); long numRegistered = registered.getMetadata().getTotalElements(); return (applications.keySet().size() == numRegistered) ? String.format("Successfully registered applications: %s", applications.keySet()) PagedResources<AppRegistrationResource> registered = appRegistryOperations().importFromResource(uri, force); return String.format("Successfully registered %d applications from '%s'", registered.getMetadata().getTotalElements(), uri);
final int currentPage = (int) resources.getMetadata().getNumber(); final int totalPages = (int) resources.getMetadata().getTotalPages();
jgen.writeNumberField("hydra:totalItems", metadata.getTotalElements());
categories.add(dto); }); resources = new PagedResources<>(categories, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageCategory.getTotalElements(), pageCategory.getTotalPages()));
items.add(dto); }); resources = new PagedResources<>(items, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageItem .getTotalElements(), pageItem.getTotalPages()));
}); resources = new PagedResources<>(users, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageUser .getTotalElements(), pageUser.getTotalPages()));
categories.add(dto); }); resources = new PagedResources<>(categories, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageCategory.getTotalElements(), pageCategory.getTotalPages()));
items.add(dto); }); resources = new PagedResources<>(items, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageItem .getTotalElements(), pageItem.getTotalPages()));
categories.add(dto); }); resources = new PagedResources<>(categories, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageCategory.getTotalElements(), pageCategory.getTotalPages()));
categories.add(dto); }); resources = new PagedResources<>(categories, new PagedResources.PageMetadata(tempSize, (tempPage + 1), pageCategory.getTotalElements(), pageCategory.getTotalPages()));
return "Error when registering applications from " + uri + ": " + e.getMessage(); long numRegistered = registered.getMetadata().getTotalElements(); return (applications.keySet().size() == numRegistered) ? String.format("Successfully registered applications: %s", applications.keySet()) PagedResources<AppRegistrationResource> registered = appRegistryOperations().importFromResource(uri, force); return String.format("Successfully registered %d applications from '%s'", registered.getMetadata().getTotalElements(), uri);
/** * Return a paged collection of job executions for a given job. * * @param jobName name of the job * @param pageable If not provided will default to page 0 and a page size of 20 * @return Collection of JobExecutionInfo */ @RequestMapping(value = "", method = RequestMethod.GET, params = "jobname") @ResponseStatus(HttpStatus.OK) public PagedResources<JobExecutionInfoResource> executionsForJob(@RequestParam("jobname") String jobName, Pageable pageable) { Collection<JobExecutionInfoResource> result = new ArrayList<JobExecutionInfoResource>(); try { for (JobExecution jobExecution : jobService.listJobExecutionsForJob(jobName, pageable.getOffset(), pageable.getPageSize())) { result.add(jobExecutionInfoResourceAssembler.toResource(new JobExecutionInfo(jobExecution, timeZone))); } return new PagedResources<JobExecutionInfoResource>(result, new PageMetadata(pageable.getPageSize(), pageable.getPageNumber(), jobService.countJobExecutionsForJob(jobName))); } catch (NoSuchJobException e) { throw new NoSuchBatchJobException(jobName); } }
/** * Converts HATEOAS resources into Spring Data page. * * @param resources Paged resources. * @param request Pagination specification. * @param <ResourceType> Collection element type. * @return Paged result. */ public static <ResourceType> Page<ResourceType> buildPageFromResources( PagedResources<? extends ResourceType> resources, Pageable request ) { return new PageImpl<>( new ArrayList<>(resources.getContent()), request, resources.getMetadata().getTotalElements() ); } }
/** * List all job executions in a given range. If no pagination is provided, * the default {@code PageRequest(0, 20)} is passed in. See {@link org.springframework.data.web.PageableHandlerMethodArgumentResolver} * for details. * * @param pageable If not provided will default to page 0 and a page size of 20 * @return Collection of JobExecutionInfoResource */ @RequestMapping(value = { "" }, method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public PagedResources<JobExecutionInfoResource> list(Pageable pageable) throws NoSuchJobException { Collection<JobExecutionInfoResource> resources = new ArrayList<JobExecutionInfoResource>(); for (JobExecution jobExecution : jobService.listJobExecutions(pageable.getOffset(), pageable.getPageSize())) { Job job = jobLocator.getJob(jobExecution.getJobInstance().getJobName()); final JobExecutionInfoResource jobExecutionInfoResource = getJobExecutionInfoResource(jobExecution, job.isRestartable()); resources.add(jobExecutionInfoResource); } return new PagedResources<JobExecutionInfoResource>(resources, new PageMetadata(pageable.getPageSize(), pageable.getPageNumber(), jobService.countJobExecutions())); }
@Test public void testPagedResources() { List<Address> addresses = new ArrayList<Address>(); for (int i = 0; i < 4; i++) { addresses.add(new Address()); } PagedResources<Address> addressResources = new PagedResources<Address>(addresses, new PageMetadata(2, 0, addresses.size())); addressResources.add(new Link("http://example.com/addresses", "self")); SirenEntity entity = new SirenEntity(); sirenUtils.toSirenEntity(entity, addressResources); String json = objectMapper.valueToTree(entity) .toString(); with(json).assertThat("$.entities", hasSize(4)); with(json).assertThat("$.entities[0].properties.city.postalCode", equalTo("74199")); with(json).assertThat("$.entities[3].properties.city.name", equalTo("Donnbronn")); with(json).assertThat("$.links", hasSize(1)); }
public ExtendedPageMetadata toExtendedPageMetadata(long skipCount, PagedResources.PageMetadata basePageMetadata) { long totalPages = basePageMetadata.getTotalPages(); int skipCountRemainder = Math.toIntExact(skipCount % basePageMetadata.getSize()); if (skipCountRemainder != 0) { // exclude the first page, which has a different size than other pages int firstPageSize = skipCountRemainder; long totalElementsNotInTheFirstPage = basePageMetadata.getTotalElements() - firstPageSize; // then calculate the number of pages other than the first one and increment it by one (the first page) totalPages = new PagedResources.PageMetadata(basePageMetadata.getSize(), basePageMetadata.getNumber(), totalElementsNotInTheFirstPage).getTotalPages() + 1; } return new ExtendedPageMetadata(skipCount, basePageMetadata.getSize(), basePageMetadata.getNumber(), basePageMetadata.getTotalElements(), totalPages ); }
public AlfrescoPageMetadata toAlfrescoPageMetadata(PagedResources.PageMetadata basePageMetadata, long count) { long skipCount = basePageMetadata.getNumber() * basePageMetadata.getSize(); if (basePageMetadata instanceof ExtendedPageMetadata) { skipCount = ((ExtendedPageMetadata) basePageMetadata).getSkipCount(); } // the page number starts from zero, so it's necessary to increment by one before comparing with total pages return new AlfrescoPageMetadata(skipCount, basePageMetadata.getSize(), count, basePageMetadata.getTotalPages() > basePageMetadata.getNumber() + 1, basePageMetadata.getTotalElements()); } }
/** * Creates a new {@link PageMetadata} instance from the given {@link Page}. * * @param page must not be {@literal null}. * @return */ private PageMetadata asPageMetadata(Page<?> page) { Assert.notNull(page, "Page must not be null!"); int number = pageableResolver.isOneIndexedParameters() ? page.getNumber() + 1 : page.getNumber(); return new PageMetadata(page.getSize(), number, page.getTotalElements(), page.getTotalPages()); }
@NotNull private static PageMetadata convertUberDataToPageMetaData(UberData uberData) { int size = 0; int number = 0; int totalElements = 0; int totalPages = 0; List<UberData> content = uberData.getData(); if (content != null) { for (UberData data : content) { String name = data.getName(); switch (name) { case "size": size = (int) data.getValue(); break; case "number": number = (int) data.getValue(); break; case "totalElements": totalElements = (int) data.getValue(); break; case "totalPages": totalPages = (int) data.getValue(); break; default: } } } return new PageMetadata(size, number, totalElements, totalPages); }