Example usage for org.springframework.data.domain PageImpl PageImpl

List of usage examples for org.springframework.data.domain PageImpl PageImpl

Introduction

In this page you can find the example usage for org.springframework.data.domain PageImpl PageImpl.

Prototype

public PageImpl(List<T> content) 

Source Link

Document

Creates a new PageImpl with the given content.

Usage

From source file:org.jtalks.jcommune.service.transactional.TransactionalTopicFetchService.java

/**
 * {@inheritDoc}//from   www .j  av a2s .c o  m
 */
@Override
public Page<Topic> searchByTitleAndContent(String phrase, String page) {
    JCUser currentUser = userService.getCurrentUser();

    List<Long> allowedBranchesIds = this.getDao().getAllowedBranchesIds(currentUser);

    if (!StringUtils.isEmpty(phrase) && !allowedBranchesIds.isEmpty()) {
        int pageSize = currentUser.getPageSize();
        PageRequest pageRequest = new PageRequest(page, pageSize);
        // hibernate search refuses to process long string throwing error
        String normalizedPhrase = StringUtils.left(phrase, 50);

        return searchDao.searchByTitleAndContent(normalizedPhrase, pageRequest, allowedBranchesIds);
    }
    return new PageImpl<>(Collections.<Topic>emptyList());
}

From source file:org.jtalks.jcommune.service.transactional.TransactionalTopicFetchServiceTest.java

@Test
public void testGetAllTopicsPastLastDay() throws NotFoundException {
    String pageNumber = "1";
    int pageSize = 20;
    List<Topic> expectedList = Collections.nCopies(2, new Topic(user, "title"));
    Page<Topic> expectedPage = new PageImpl<>(expectedList);
    when(topicDao.getTopicsUpdatedSince(Matchers.<DateTime>any(), Matchers.<PageRequest>any(), eq(user)))
            .thenReturn(expectedPage);//  w  w w  .j  av  a 2s  .co  m
    user.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(user);

    Page<Topic> actualPage = topicFetchService.getRecentTopics(pageNumber);

    assertNotNull(actualPage);
    assertEquals(expectedPage, actualPage);
    verify(topicDao).getTopicsUpdatedSince(Matchers.<DateTime>any(), Matchers.<PageRequest>any(), eq(user));
}

From source file:org.jtalks.jcommune.service.transactional.TransactionalTopicFetchServiceTest.java

@Test
public void testGetUnansweredTopics() {
    String pageNumber = "1";
    int pageSize = 20;
    List<Topic> expectedList = Collections.nCopies(2, new Topic(user, "title"));
    Page<Topic> expectedPage = new PageImpl<>(expectedList);
    when(topicDao.getUnansweredTopics(Matchers.<PageRequest>any(), eq(user))).thenReturn(expectedPage);
    user.setPageSize(pageSize);/*w w w  .  j  av a  2 s.c  o  m*/
    when(userService.getCurrentUser()).thenReturn(user);

    Page<Topic> actualPage = topicFetchService.getUnansweredTopics(pageNumber);
    assertNotNull(actualPage);
    assertEquals(actualPage, expectedPage);
}

From source file:org.jtalks.jcommune.service.transactional.TransactionalTopicFetchServiceTest.java

@Test
public void testGetTopics() {
    String pageNumber = "50";
    Branch branch = createBranch();//w w  w  .  j a  v  a  2  s. co  m
    Page<Topic> expectedPage = new PageImpl<>(Collections.<Topic>emptyList());

    JCUser currentUser = new JCUser("current", null, null);
    currentUser.setPageSize(50);
    when(userService.getCurrentUser()).thenReturn(currentUser);
    when(topicDao.getTopics(Matchers.any(Branch.class), Matchers.any(PageRequest.class)))
            .thenReturn(expectedPage);

    Page<Topic> actualPage = topicFetchService.getTopics(branch, pageNumber);

    assertEquals(actualPage, expectedPage, "Service returned incorrect data for one page of topics");
    verify(topicDao).getTopics(Matchers.any(Branch.class), Matchers.any(PageRequest.class));
}

From source file:org.jtalks.jcommune.web.controller.UserProfileControllerTest.java

@Test
public void showUserPostListShouldShowThemToUser() throws NotFoundException {
    JCUser user = new JCUser("username", "email", "password");
    user.setPageSize(5);/*www.  j a v a  2  s .  c o  m*/
    Post post = mock(Post.class);
    Topic topic = mock(Topic.class);
    List<Post> posts = new ArrayList<>();
    posts.add(post);
    Page<Post> postsPage = new PageImpl<>(posts);
    //
    when(userService.getByUsername("username")).thenReturn(user);
    when(breadcrumbBuilder.getForumBreadcrumb()).thenReturn(new ArrayList<Breadcrumb>());
    when(postService.getPostsOfUser(any(JCUser.class), anyString())).thenReturn(postsPage);
    when(userService.getCurrentUser()).thenReturn(user);
    when(post.getTopic()).thenReturn(topic);

    ModelAndView mav = profileController.showUserPostList(user.getId(), "1");

    verify(userService).get(user.getId());
    verify(converter).convertPostPageToPostDtoPage(any(Page.class));
    assertViewName(mav, "userPostList");
    assertModelAttributeAvailable(mav, "user");
    assertModelAttributeAvailable(mav, "breadcrumbList");
    assertModelAttributeAvailable(mav, "user");
    assertModelAttributeAvailable(mav, "postsPage");
}

From source file:org.springframework.xd.dirt.util.PagingUtility.java

/**
 * Get the paged data of the given list.
 *
 * @param pageable the paging information, must not be null
 * @param list the list of content to paginate, must not be null
 * @return the paginated implementation of the given list of <T>
 * @throws PageNotFoundException in case an invalid page is requested
 *///from w w w .j av  a 2s .co m
public Page<T> getPagedData(Pageable pageable, List<T> list) throws PageNotFoundException {

    Assert.notNull(pageable, "Pagination info can't be null.");
    Assert.notNull(list, "The provided list must not be null.");

    final int offset = pageable.getOffset();
    final int to = Math.min(list.size(), offset + pageable.getPageSize());

    if (offset > to) {
        throw new PageNotFoundException(String.format("Page %s does not exist.", pageable.getPageNumber()));
    }

    if (CollectionUtils.isEmpty(list)) {
        return new PageImpl<T>(list);
    }

    Collections.sort(list);

    final List<T> data = list.subList(offset, to);
    return new PageImpl<T>(data, pageable, list.size());
}