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

The following examples show how to use org.springframework.data.domain.Page#getNumberOfElements() . 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: PhotoServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public PagedResponse<PhotoResponse> getAllPhotos(int page, int size) {
	AppUtils.validatePageNumberAndSize(page, size);

	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);
	Page<Photo> photos = photoRepository.findAll(pageable);

	List<PhotoResponse> photoResponses = new ArrayList<>(photos.getContent().size());
	for (Photo photo : photos.getContent()) {
		photoResponses.add(new PhotoResponse(photo.getId(), photo.getTitle(), photo.getUrl(),
				photo.getThumbnailUrl(), photo.getAlbum().getId()));
	}

	if (photos.getNumberOfElements() == 0) {
		return new PagedResponse<>(Collections.emptyList(), photos.getNumber(), photos.getSize(),
				photos.getTotalElements(), photos.getTotalPages(), photos.isLast());
	}
	return new PagedResponse<>(photoResponses, photos.getNumber(), photos.getSize(), photos.getTotalElements(),
			photos.getTotalPages(), photos.isLast());

}
 
Example 2
Source File: ActionStatusMsgBeanQuery.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a list of {@link ProxyActionStatus} for presentation layer from
 * page of {@link ActionStatus}.
 *
 * @param actionBeans
 *            page of {@link ActionStatus}
 * @return list of {@link ProxyActionStatus}
 */
private List<ProxyMessage> createProxyMessages(final Page<String> messages) {
    final List<ProxyMessage> proxyMsgs = new ArrayList<>(messages.getNumberOfElements());

    Long idx = messages.getNumber() * ((long) messages.getSize());
    for (final String msg : messages) {
        idx++;
        final ProxyMessage proxyMsg = new ProxyMessage();
        proxyMsg.setMessage(msg);
        proxyMsg.setId(String.valueOf(idx));
        proxyMsgs.add(proxyMsg);
    }

    if (messages.getTotalElements() == 1L && StringUtils.isEmpty(proxyMsgs.get(0).getMessage())) {
        proxyMsgs.get(0).setMessage(noMessageText);
    }

    return proxyMsgs;
}
 
Example 3
Source File: PostServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PagedResponse<Post> getPostsByCreatedBy(String username, int page, int size) {
	validatePageNumberAndSize(page, size);
	User user = userRepository.findByUsername(username)
			.orElseThrow(() -> new ResourceNotFoundException(USER, USERNAME, username));
	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);
	Page<Post> posts = postRepository.findByCreatedBy(user.getId(), 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 4
Source File: PageInfo.java    From graphql-java-demo with MIT License 5 votes vote down vote up
PageInfo(Page page) {
    this.totalElements = page.getTotalElements();
    this.totalPages = page.getTotalPages();
    this.numberOfElements = page.getNumberOfElements();
    this.pageNumber = page.getNumber();
    this.pageSize = page.getSize();
}
 
Example 5
Source File: AbstractRelatedContentResource.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected ResultListDataRepresentation createResultRepresentation(Page<RelatedContent> results) {
    List<RelatedContentRepresentation> resultList = new ArrayList<RelatedContentRepresentation>(results.getNumberOfElements());

    for (RelatedContent content : results) {
        resultList.add(createRelatedContentResponse(content));
    }
    
    ResultListDataRepresentation result = new ResultListDataRepresentation(resultList);
    result.setTotal(results.getTotalElements());
    return result;
}
 
Example 6
Source File: MovieResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/customFind/page/{size}/{num}")
public String customFind(@PathParam("size") int pageSize, @PathParam("num") int pageNum) {
    Page<Movie> page = movieRepository.customFind(
            PageRequest.of(pageNum, pageSize, Sort.Direction.ASC, "title"));
    return page.hasNext() + " / " + page.getNumberOfElements();
}
 
Example 7
Source File: SongResource.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@GET
@Produces("application/json")
@Path("/all")
public String all() {
    Pageable wholePage = Pageable.unpaged();
    Page<Song> page = songRepository.findAll(wholePage);
    return page.hasPrevious() + " - " + page.hasNext() + " / " + page.getNumberOfElements();
}
 
Example 8
Source File: BillController.java    From logistics-back with MIT License 5 votes vote down vote up
@ApiOperation(value = "分页查询单据信息", notes = "分页查询单据信息")
@RequestMapping(value = "/findByPage", method = RequestMethod.GET)
public Result findAllByPage(@RequestParam("pageNum") int pageNum, @RequestParam("limit") int limit) {
	
	Pageable pageable = PageRequest.of(pageNum-1, limit);
	Page<BillInfo> page = billService.findAllByPage(pageable);
	Result result = new Result(200, "SUCCESS", page.getNumberOfElements(), page.getContent());
	return result;
	
}
 
Example 9
Source File: PostServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PagedResponse<Post> getPostsByCategory(Long id, int page, int size) {
	AppUtils.validatePageNumberAndSize(page, size);
	Category category = categoryRepository.findById(id)
			.orElseThrow(() -> new ResourceNotFoundException(CATEGORY, ID, id));

	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);
	Page<Post> posts = postRepository.findByCategory(category.getId(), 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 10
Source File: LocalRegistryService.java    From openvsx with Eclipse Public License 2.0 5 votes vote down vote up
private List<SearchEntryJson> toSearchEntries(Page<ExtensionSearch> page, int size, int offset) {
    if (offset > 0 || size < page.getNumberOfElements())
        return CollectionUtil.map(
                Iterables.limit(Iterables.skip(page.getContent(), offset), size),
                this::toSearchEntry);
    else
        return CollectionUtil.map(page.getContent(), this::toSearchEntry);
}
 
Example 11
Source File: PlatformPageImpl.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PlatformPageImpl(Page page) {
    super(page.getContent(),
            new PageRequest(page.getNumber(), page.getSize(), page.getSort()),
            page.getTotalElements());
    this.number = page.getNumber();
    this.size = page.getSize();
    this.totalPages = page.getTotalPages();
    this.numberOfElements = page.getNumberOfElements();
    this.totalElements = page.getTotalElements();
    this.first = page.isFirst();
    this.last = page.isLast();
    this.content = page.getContent();
    this.sort = page.getSort();
}
 
Example 12
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 13
Source File: TodoServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PagedResponse<Todo> getAllTodos(UserPrincipal currentUser, int page, int size) {
	validatePageNumberAndSize(page, size);
	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);

	Page<Todo> todos = todoRepository.findByCreatedBy(currentUser.getId(), pageable);
	
	List<Todo> content = todos.getNumberOfElements() == 0 ? Collections.emptyList() : todos.getContent();

	return new PagedResponse<>(content, todos.getNumber(), todos.getSize(), todos.getTotalElements(),
			todos.getTotalPages(), todos.isLast());
}
 
Example 14
Source File: MyPage.java    From White-Jotter with MIT License 5 votes vote down vote up
public MyPage(Page<T> page) {
    this.content = page.getContent();
    this.totalElements = page.getTotalElements();
    this.pageNumber = page.getPageable().getPageNumber();
    this.pageSize = page.getPageable().getPageSize();
    this.numberOfElements = page.getNumberOfElements();
}
 
Example 15
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 16
Source File: PersonResource.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@GET
@Path("/name/joinedOrder/{name}/page/{size}/{num}")
public String byNamePage(@PathParam("name") String name, @PathParam("size") int pageSize, @PathParam("num") int pageNum) {
    Page<Person> page = personRepository.findByNameOrderByJoined(name, PageRequest.of(pageNum, pageSize));
    return page.hasPrevious() + " - " + page.hasNext() + " / " + page.getNumberOfElements();
}
 
Example 17
Source File: AlbumServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public PagedResponse<AlbumResponse> getAllAlbums(int page, int size){
       AppUtils.validatePageNumberAndSize(page, size);

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

       Page<Album> albums = albumRepository.findAll(pageable);

       if (albums.getNumberOfElements() == 0){
           return new PagedResponse<>(Collections.emptyList(), albums.getNumber(), albums.getSize(), albums.getTotalElements(), albums.getTotalPages(), albums.isLast());
       }
       
       List<AlbumResponse> albumResponses = Arrays.asList(modelMapper.map(albums.getContent(), AlbumResponse[].class));
       
       return new PagedResponse<>(albumResponses, albums.getNumber(), albums.getSize(), albums.getTotalElements(), albums.getTotalPages(), albums.isLast());
   }
 
Example 18
Source File: CategoryServiceImpl.java    From Spring-Boot-Blog-REST-API with GNU Affero General Public License v3.0 3 votes vote down vote up
@Override
public PagedResponse<Category> getAllCategories(int page, int size){
	AppUtils.validatePageNumberAndSize(page, size);
	
	Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");
	
	Page<Category> categories = categoryRepository.findAll(pageable);
	
	List<Category> content = categories.getNumberOfElements() == 0 ? Collections.emptyList() : categories.getContent();
	
	return new PagedResponse<Category>(content, categories.getNumber(), categories.getSize(), categories.getTotalElements(), categories.getTotalPages(), categories.isLast());
}
 
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());
}