Java Code Examples for org.springframework.data.domain.Page#getTotalPages()

The following examples show how to use org.springframework.data.domain.Page#getTotalPages() . 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: PaginationUtil.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
public static <T> HttpHeaders generatePaginationHttpHeaders(Page<T> page, String baseUrl) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Total-Count", Long.toString(page.getTotalElements()));
        String link = "";
        if ((page.getNumber() + 1) < page.getTotalPages()) {
            link = "<" + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + ">; rel=\"next\",";
        }
        // prev link
        if ((page.getNumber()) > 0) {
            link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + ">; rel=\"prev\",";
        }
        // last and first link
        int lastPage = 0;
        if (page.getTotalPages() > 0) {
            lastPage = page.getTotalPages() - 1;
        }
        link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + ">; rel=\"last\",";
        link += "<" + generateUri(baseUrl, 0, page.getSize()) + ">; rel=\"first\"";
        headers.add(HttpHeaders.LINK, link);
        return headers;
    }
 
Example 2
Source File: PaginationUtil.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
public static <T> HttpHeaders generatePaginationHttpHeaders(Page<T> page, String baseUrl) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Total-Count", Long.toString(page.getTotalElements()));
        String link = "";
        if ((page.getNumber() + 1) < page.getTotalPages()) {
            link = "<" + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + ">; rel=\"next\",";
        }
        // prev link
        if ((page.getNumber()) > 0) {
            link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + ">; rel=\"prev\",";
        }
        // last and first link
        int lastPage = 0;
        if (page.getTotalPages() > 0) {
            lastPage = page.getTotalPages() - 1;
        }
        link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + ">; rel=\"last\",";
        link += "<" + generateUri(baseUrl, 0, page.getSize()) + ">; rel=\"first\"";
        headers.add(HttpHeaders.LINK, link);
        return headers;
    }
 
Example 3
Source File: PaginationUtil.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page<?> page, String baseUrl)
    throws URISyntaxException {

    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Total-Count", "" + page.getTotalElements());
    String link = "";
    if ((page.getNumber() + 1) < page.getTotalPages()) {
        link = "<" + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + ">; rel=\"next\",";
    }
    // prev link
    if ((page.getNumber()) > 0) {
        link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + ">; rel=\"prev\",";
    }
    // last and first link
    int lastPage = 0;
    if (page.getTotalPages() > 0) {
        lastPage = page.getTotalPages() - 1;
    }
    link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + ">; rel=\"last\",";
    link += "<" + generateUri(baseUrl, 0, page.getSize()) + ">; rel=\"first\"";
    headers.add(HttpHeaders.LINK, link);
    return headers;
}
 
Example 4
Source File: BookController.java    From tutorials with MIT License 6 votes vote down vote up
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
    final int currentPage = page.orElse(1);
    final int pageSize = size.orElse(5);

    Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));

    model.addAttribute("bookPage", bookPage);

    int totalPages = bookPage.getTotalPages();
    if (totalPages > 0) {
        List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
            .boxed()
            .collect(Collectors.toList());
        model.addAttribute("pageNumbers", pageNumbers);
    }

    return "listBooks.html";
}
 
Example 5
Source File: UseCase3DTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<UseCase3DTO> findAll(PageRequestByExample<UseCase3DTO> req) {
    Example<UseCase3> example = null;
    UseCase3 useCase3 = toEntity(req.example);

    if (useCase3 != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(UseCase3_.dummy.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(useCase3, matcher);
    }

    Page<UseCase3> page;
    if (example != null) {
        page = useCase3Repository.findAll(example, req.toPageable());
    } else {
        page = useCase3Repository.findAll(req.toPageable());
    }

    List<UseCase3DTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example 6
Source File: PaginationUtil.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page page, String baseUrl) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Total-Count", Long.toString(page.getTotalElements()));
        String link = "";
        if ((page.getNumber() + 1) < page.getTotalPages()) {
            link = "<" + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + ">; rel=\"next\",";
        }
        // prev link
        if ((page.getNumber()) > 0) {
            link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + ">; rel=\"prev\",";
        }
        // last and first link
        int lastPage = 0;
        if (page.getTotalPages() > 0) {
            lastPage = page.getTotalPages() - 1;
        }
        link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + ">; rel=\"last\",";
        link += "<" + generateUri(baseUrl, 0, page.getSize()) + ">; rel=\"first\"";
        headers.add(HttpHeaders.LINK, link);
        return headers;
    }
 
Example 7
Source File: UseCase2DTOService.java    From celerio-angular-quickstart with Apache License 2.0 6 votes vote down vote up
@Transactional(readOnly = true)
public PageResponse<UseCase2DTO> findAll(PageRequestByExample<UseCase2DTO> req) {
    Example<UseCase2> example = null;
    UseCase2 useCase2 = toEntity(req.example);

    if (useCase2 != null) {
        ExampleMatcher matcher = ExampleMatcher.matching() //
                .withMatcher(UseCase2_.dummy.getName(), match -> match.ignoreCase().startsWith());

        example = Example.of(useCase2, matcher);
    }

    Page<UseCase2> page;
    if (example != null) {
        page = useCase2Repository.findAll(example, req.toPageable());
    } else {
        page = useCase2Repository.findAll(req.toPageable());
    }

    List<UseCase2DTO> content = page.getContent().stream().map(this::toDTO).collect(Collectors.toList());
    return new PageResponse<>(page.getTotalPages(), page.getTotalElements(), content);
}
 
Example 8
Source File: PaginationUtil.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 6 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page<?> page, String baseUrl)
    throws URISyntaxException {

    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Total-Count", "" + page.getTotalElements());
    String link = "";
    if ((page.getNumber() + 1) < page.getTotalPages()) {
        link = "<" + (new URI(baseUrl + "?page=" + (page.getNumber() + 1) + "&size=" + page.getSize())).toString() + ">; rel=\"next\",";
    }
    // prev link
    if ((page.getNumber()) > 0) {
        link += "<" + (new URI(baseUrl + "?page=" + (page.getNumber() - 1) + "&size=" + page.getSize())).toString() + ">; rel=\"prev\",";
    }
    // last and first link
    int lastPage = 0;
    if (page.getTotalPages() > 0) {
        lastPage = page.getTotalPages() - 1;
    }
    link += "<" + (new URI(baseUrl + "?page=" + lastPage + "&size=" + page.getSize())).toString() + ">; rel=\"last\",";
    link += "<" + (new URI(baseUrl + "?page=" + 0 + "&size=" + page.getSize())).toString() + ">; rel=\"first\"";
    headers.add(HttpHeaders.LINK, link);
    return headers;
}
 
Example 9
Source File: PaginationUtil.java    From cubeai with Apache License 2.0 6 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page page, String baseUrl) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Total-Count", Long.toString(page.getTotalElements()));
        String link = "";
        if ((page.getNumber() + 1) < page.getTotalPages()) {
            link = "<" + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + ">; rel=\"next\",";
        }
        // prev link
        if ((page.getNumber()) > 0) {
            link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + ">; rel=\"prev\",";
        }
        // last and first link
        int lastPage = 0;
        if (page.getTotalPages() > 0) {
            lastPage = page.getTotalPages() - 1;
        }
        link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + ">; rel=\"last\",";
        link += "<" + generateUri(baseUrl, 0, page.getSize()) + ">; rel=\"first\"";
        headers.add(HttpHeaders.LINK, link);
        return headers;
    }
 
Example 10
Source File: PaginationUtil.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page page, String baseUrl) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Total-Count", Long.toString(page.getTotalElements()));
        String link = "";
        if ((page.getNumber() + 1) < page.getTotalPages()) {
            link = "<" + generateUri(baseUrl, page.getNumber() + 1, page.getSize()) + ">; rel=\"next\",";
        }
        // prev link
        if ((page.getNumber()) > 0) {
            link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + ">; rel=\"prev\",";
        }
        // last and first link
        int lastPage = 0;
        if (page.getTotalPages() > 0) {
            lastPage = page.getTotalPages() - 1;
        }
        link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + ">; rel=\"last\",";
        link += "<" + generateUri(baseUrl, 0, page.getSize()) + ">; rel=\"first\"";
        headers.add(HttpHeaders.LINK, link);
        return headers;
    }
 
Example 11
Source File: PaginationUtil.java    From ServiceCutter with Apache License 2.0 6 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page<?> page, String baseUrl)
    throws URISyntaxException {

    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Total-Count", "" + page.getTotalElements());
    String link = "";
    if ((page.getNumber() + 1) < page.getTotalPages()) {
        link = "<" + (new URI(baseUrl +"?page=" + (page.getNumber() + 1) + "&size=" + page.getSize())).toString() + ">; rel=\"next\",";
    }
    // prev link
    if ((page.getNumber()) > 0) {
        link += "<" + (new URI(baseUrl +"?page=" + (page.getNumber() - 1) + "&size=" + page.getSize())).toString() + ">; rel=\"prev\",";
    }
    // last and first link
    link += "<" + (new URI(baseUrl +"?page=" + (page.getTotalPages() - 1) + "&size=" + page.getSize())).toString() + ">; rel=\"last\",";
    link += "<" + (new URI(baseUrl +"?page=" + 0 + "&size=" + page.getSize())).toString() + ">; rel=\"first\"";
    headers.add(HttpHeaders.LINK, link);
    return headers;
}
 
Example 12
Source File: TPage.java    From issue-management with MIT License 5 votes vote down vote up
public void setStat(Page page, List<T> list) {
    this.number = page.getNumber();
    this.size = page.getSize();
    this.sort = page.getSort();
    this.totalPages = page.getTotalPages();
    this.totalElements = page.getTotalElements();
    this.content = list;
}
 
Example 13
Source File: CommentServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PagedResponse<Comment> getAllComments(Long postId, int page, int size) {
	AppUtils.validatePageNumberAndSize(page, size);
	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");

	Page<Comment> comments = commentRepository.findByPostId(postId, pageable);

	return new PagedResponse<Comment>(comments.getContent(), comments.getNumber(), comments.getSize(),
			comments.getTotalElements(), comments.getTotalPages(), comments.isLast());
}
 
Example 14
Source File: NotificationService.java    From production-ready-microservices-starter with MIT License 5 votes vote down vote up
public NotificationsResponse listByPage(Pageable pageable) {

        Page<Notification> notificationPage = notificationRepository.findAll(pageable);

        long totalElements = notificationPage.getTotalElements();
        int totalPage = notificationPage.getTotalPages();
        int size = notificationPage.getSize();
        int page = notificationPage.getNumber();

        List<NotificationResponse> notificationResponseList = new ArrayList<>();
        for (Notification notification : notificationPage.getContent()) {
            NotificationResponse notificationResponse = new NotificationResponse();
            notificationResponse.setId(notification.getId());
            notificationResponse.setContent(notification.getContent());
            notificationResponse.setType(NotificationResponse.Type.valueOf(notification.getType().toString()));
            notificationResponse.setCreatedAt(notification.getCreatedAt());
            notificationResponse.setAdditionalProperties(notification.getAdditionalProperties());
            notificationResponseList.add(notificationResponse);
        }

        return NotificationsResponse.builder()
                .items(notificationResponseList)
                .page(page)
                .size(size)
                .totalPages(totalPage)
                .totalElements(totalElements)
                .build();

    }
 
Example 15
Source File: CourseController.java    From itweet-boot with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String list(@RequestParam(value = "page", defaultValue = "0") Integer page,Model model) {
    if(page !=0) page = page -1;

    Page<Course> courseList = courseService.list(page);
    model.addAttribute("courseList",courseList);

    PageUtils pageUtils = new PageUtils("/admin/course/list?",page,courseList.getTotalPages(),courseList.getTotalElements(),itweetProperties.getPagSize());
    model.addAttribute("pb",pageUtils);

    return "admin/course/list";
}
 
Example 16
Source File: TagController.java    From itweet-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 文章标签按Name查询
 * @param page
 * @param model
 * @return
 */
@RequestMapping(value = "/select",method = RequestMethod.GET)
public String select(@RequestParam(value = "name") String name, @RequestParam(value = "page", defaultValue = "0") Integer page,Model model) {
    if(page !=0) page = page -1;
    Page<Tag> tagList = tagService.selectByName(page,name);
    model.addAttribute("tagList",tagList);

    PageUtils pageUtils = new PageUtils("/admin/tag/select?name="+ name+"&",page,tagList.getTotalPages(),tagList.getTotalElements(),itweetProperties.getPagSize());
    model.addAttribute("pb",pageUtils);

    model.addAttribute("name",name);
    return "admin/article/t_list";
}
 
Example 17
Source File: UserPagingAndSortingRepositoryTest.java    From springbootexamples with Apache License 2.0 5 votes vote down vote up
/**
 *   分页查询
 * new PageRequest(页数, 每页的数量)
 */
@Test
public void findAllByPage() {
	//
	Page<User> userPage = userPagingAndSortingRepository.findAll(new PageRequest(1, 2));
	userPage.getNumber();// 页数
	userPage.getContent();// 分页的数据
	userPage.getTotalPages();// 总共的页数
	System.out.println("number:" + userPage.getNumber() + "Countet" + userPage.getContent().size() + "TotalPages"
			+ userPage.getTotalPages());
	Stream<User> stream = userPage.get();
	stream.forEach((user)->{
		System.out.println(user.getId()+"#"+user.getName());
	});
}
 
Example 18
Source File: AlbumServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PagedResponse<Album> getUserAlbums(String username, int page, int size){
       User user = userRepository.findByUsername(username).orElseThrow(() -> new ResourceNotFoundException(USER_STR, USERNAME_STR, username));
       Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);

       Page<Album> albums = albumRepository.findByCreatedBy(user.getId(), pageable);
       
       List<Album> content = albums.getNumberOfElements() > 0 ? albums.getContent() : Collections.emptyList();
       
       return new PagedResponse<>(content, albums.getNumber(), albums.getSize(), albums.getTotalElements(), albums.getTotalPages(), albums.isLast());
   }
 
Example 19
Source File: SagaExecutionQueryService.java    From servicecomb-saga-actuator with Apache License 2.0 5 votes vote down vote up
public SagaExecutionQueryResult querySagaExecution(String pageIndex, String pageSize,
    String startTime, String endTime) throws ParseException {

  Date start = "NaN-NaN-NaN NaN:NaN:NaN".equals(startTime) ? new Date(0) : this.dateFormat.parse(startTime);
  Date end = "NaN-NaN-NaN NaN:NaN:NaN".equals(endTime) ? new Date() : this.dateFormat.parse(endTime);

  List<SagaExecution> requests = new ArrayList<>();
  Page<SagaEventEntity> startEvents = repo.findByTypeAndCreationTimeBetweenOrderByIdDesc(
      SagaStartedEvent.class.getSimpleName(), start, end,
      new PageRequest(Integer.parseInt(pageIndex), Integer.parseInt(pageSize)));
  for (SagaEventEntity event : startEvents) {
    SagaEventEntity endEvent = repo
        .findFirstByTypeAndSagaId(SagaEndedEvent.class.getSimpleName(), event.sagaId());
    SagaEventEntity abortedEvent = repo
        .findFirstByTypeAndSagaId(TransactionAbortedEvent.class.getSimpleName(), event.sagaId());

    requests.add(new SagaExecution(
        event.id(),
        event.sagaId(),
        event.creationTime(),
        endEvent == null ? 0 : endEvent.creationTime(),
        endEvent == null ? "Running" : abortedEvent == null ? "OK" : "Failed"));
  }

  return new SagaExecutionQueryResult(Integer.parseInt(pageIndex), Integer.parseInt(pageSize),
      startEvents.getTotalPages(), requests);
}
 
Example 20
Source File: PostServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 3 votes vote down vote up
@Override
public PagedResponse<Post> getAllPosts(int page, int size) {
	validatePageNumberAndSize(page, size);

	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);

	Page<Post> posts = postRepository.findAll(pageable);

	List<Post> content = posts.getNumberOfElements() == 0 ? Collections.emptyList() : posts.getContent();

	return new PagedResponse<>(content, posts.getNumber(), posts.getSize(), posts.getTotalElements(),
			posts.getTotalPages(), posts.isLast());
}