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

The following examples show how to use org.springframework.data.domain.Page#getNumber() . 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 okta-jhipster-microservices-oauth-example 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 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 4
Source File: PaginationUtil.java    From ehcache3-samples 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 5
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 6
Source File: PaginationUtil.java    From okta-jhipster-microservices-oauth-example 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 7
Source File: PaginationUtil.java    From jhipster-microservices-example 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 8
Source File: PaginationUtil.java    From tutorials with MIT License 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 9
Source File: PaginationUtil.java    From tutorials with MIT License 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 tutorials with MIT License 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 okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
public static <T> HttpHeaders generateSearchPaginationHttpHeaders(String query, Page<T> page, String baseUrl) {
    String escapedQuery;
    try {
        escapedQuery = URLEncoder.encode(query, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    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()) + "&query=" + escapedQuery + ">; rel=\"next\",";
    }
    // prev link
    if ((page.getNumber()) > 0) {
        link += "<" + generateUri(baseUrl, page.getNumber() - 1, page.getSize()) + "&query=" + escapedQuery + ">; rel=\"prev\",";
    }
    // last and first link
    int lastPage = 0;
    if (page.getTotalPages() > 0) {
        lastPage = page.getTotalPages() - 1;
    }
    link += "<" + generateUri(baseUrl, lastPage, page.getSize()) + "&query=" + escapedQuery + ">; rel=\"last\",";
    link += "<" + generateUri(baseUrl, 0, page.getSize()) + "&query=" + escapedQuery + ">; rel=\"first\"";
    headers.add(HttpHeaders.LINK, link);
    return headers;
}
 
Example 12
Source File: BaseMovableService.java    From es with Apache License 2.0 5 votes vote down vote up
public void doReweight(Page<M> page) {
    int totalElements = (int) page.getTotalElements();
    List<M> moves = page.getContent();

    int firstElement = page.getNumber() * page.getSize();

    for (int i = 0; i < moves.size(); i++) {
        M move = moves.get(i);
        move.setWeight((totalElements - firstElement - i) * stepLength);
        update(move);
    }

}
 
Example 13
Source File: Pagination.java    From Fame with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <S> Pagination<S> of(Page<S> page) {
    return new Pagination(
            page.getNumber(),
            page.getSize(),
            page.getTotalElements(),
            page.getTotalPages(),
            page.getSort().toString(),
            page.getContent()
    );
}
 
Example 14
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 15
Source File: PaginationUtils.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
public static HttpHeaders generatePaginationHttpHeaders(Page<?> page, String baseUrl) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Total-Count", "" + page.getTotalElements());

    if (!StringUtils.isEmpty(baseUrl)) {
        Pagination pagination = new Pagination();
        int lastPage = 0;
        if (page.getTotalPages() > 0) {
            lastPage = page.getTotalPages() - 1;
        }
        if (page.getNumber() + 1 < page.getTotalPages()) {
            pagination.setNext(generateUri(baseUrl, page.getNumber() + 1, page.getSize()));
        }

        if (page.getNumber() > 0) {
            pagination.setPrev(generateUri(baseUrl, page.getNumber() - 1, page.getSize()));
        }
        pagination.setLast(generateUri(baseUrl, lastPage, page.getSize()));
        pagination.setFirst(generateUri(baseUrl, 0, page.getSize()));
        try {
            headers.add("X-Pagination", PAGINATION_OBJECT_MAPPER.writeValueAsString(pagination));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return headers;
}
 
Example 16
Source File: SearchData.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public SearchData(Page<T> page){
	this.topicList = page.getContent() ;
	this.totalPages = page.getTotalPages();
	this.totalElements = page.getTotalElements();
	this.size = page.getSize();
	this.number= page.getNumber();
	this.numberOfElements = page.getNumberOfElements();
}
 
Example 17
Source File: PageUtils.java    From thymeleaf-spring-data-dialect with Apache License 2.0 4 votes vote down vote up
public static int getFirstItemInPage(final Page<?> page) {
    return page.getSize() * page.getNumber() + 1;
}
 
Example 18
Source File: PageResp.java    From ElementVueSpringbootCodeTemplate with Apache License 2.0 4 votes vote down vote up
public PageResp(Page<T> page) {
	this.rows = page.getContent();
	this.page = page.getNumber() + 1;
	this.pagesize = page.getSize();
	this.total = page.getTotalElements();
}
 
Example 19
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> getPostsByTag(Long id, int page, int size) {
	AppUtils.validatePageNumberAndSize(page, size);

	Tag tag = tagRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(TAG, ID, id));

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

	Page<Post> posts = postRepository.findByTags(Arrays.asList(tag), 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());
}
 
Example 20
Source File: TagServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 3 votes vote down vote up
@Override
public PagedResponse<Tag> getAllTags(int page, int size){
	AppUtils.validatePageNumberAndSize(page, size);
	
	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");
	
	Page<Tag> tags = tagRepository.findAll(pageable);
	
	List<Tag> content = tags.getNumberOfElements() == 0 ? Collections.emptyList() : tags.getContent();
	
	return new PagedResponse<Tag>(content, tags.getNumber(), tags.getSize(), tags.getTotalElements(), tags.getTotalPages(), tags.isLast());
}