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, Pageable pageable, long total) 

Source Link

Document

Constructor of PageImpl .

Usage

From source file:com.ethercamp.harmony.service.ContractsService.java

/**
 * Get contract storage entries./*from   w w w .ja v a  2 s .  c o m*/
 *
 * @param hexAddress - address of contract
 * @param path - nested level of fields
 * @param pageable - for paging
 */
public Page<StorageEntry> getContractStorage(String hexAddress, String path, Pageable pageable) {
    final byte[] address = Hex.decode(hexAddress);
    final ContractEntity contract = Optional.ofNullable(contractsStorage.get(address))
            .map(bytes -> contractFormat.decode(bytes))
            .orElseThrow(() -> new RuntimeException("Contract sources not found"));

    final StoragePage storagePage = getContractData(hexAddress, contract.getDataMembers(), Path.parse(path),
            pageable.getPageNumber(), pageable.getPageSize());

    final PageImpl<StorageEntry> storage = new PageImpl<>(storagePage.getEntries(), pageable,
            storagePage.getTotal());

    return storage;
}

From source file:com.jiwhiz.rest.site.WebsiteRestControllerTest.java

@Test
public void getRecentPublicBlogPosts_ShouldReturnRecentBlogPosts() throws Exception {
    Pageable pageable = new PageRequest(0, 4);

    when(blogPostRepositoryMock.findByPublishedIsTrueOrderByPublishedTimeDesc(any(Pageable.class)))
            .thenReturn(new PageImpl<BlogPost>(getTestPublishedBlogPostList(), pageable, 2));

    mockMvc.perform(get(API_ROOT + URL_SITE_RECENT_BLOGS)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$._embedded.blogPostList", hasSize(2)))
            .andExpect(jsonPath("$._embedded.blogPostList[0].id", is(BLOGS_1_ID)))
            .andExpect(jsonPath("$._embedded.blogPostList[0].title", is(BLOGS_1_TITLE)))
            .andExpect(jsonPath("$._embedded.blogPostList[1].id", is(BLOGS_2_ID)))
            .andExpect(jsonPath("$._embedded.blogPostList[1].title", is(BLOGS_2_TITLE)))
            //check links
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_SITE_RECENT_BLOGS)));

    verify(blogPostRepositoryMock, times(1)).findByPublishedIsTrueOrderByPublishedTimeDesc(pageable);
    verifyNoMoreInteractions(blogPostRepositoryMock);

}

From source file:com.luna.common.repository.support.SimpleBaseRepository.java

/**
 * Reads the given {@link javax.persistence.TypedQuery} into a {@link org.springframework.data.domain.Page} applying the given {@link org.springframework.data.domain.Pageable} and
 * {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param query    must not be {@literal null}.
 * @param spec     can be {@literal null}.
 * @param pageable can be {@literal null}.
 * @return//w w w.  ja  v  a 2s  .  c o m
 */
protected Page<M> readPage(TypedQuery<M> query, Pageable pageable, Specification<M> spec) {

    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

    Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
    List<M> content = total > pageable.getOffset() ? query.getResultList() : Collections.<M>emptyList();

    return new PageImpl<M>(content, pageable, total);
}

From source file:com.sishuok.es.common.repository.support.SimpleBaseRepository.java

/**
 * Reads the given {@link javax.persistence.TypedQuery} into a {@link org.springframework.data.domain.Page} applying the given {@link org.springframework.data.domain.Pageable} and
 * {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param query    must not be {@literal null}.
 * @param spec     can be {@literal null}.
 * @param pageable can be {@literal null}.
 * @return//from  w w w .jav  a  2s  .c o  m
 */
private Page<M> readPage(TypedQuery<M> query, Pageable pageable, Specification<M> spec) {

    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

    Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
    List<M> content = total > pageable.getOffset() ? query.getResultList() : Collections.<M>emptyList();

    return new PageImpl<M>(content, pageable, total);
}

From source file:com.ethlo.geodata.GeodataServiceImpl.java

@Override
public Page<GeoLocationDistance> findNear(Coordinates point, int maxDistanceInKilometers, Pageable pageable) {
    final Iterator<Long> ids = rTree.findNear(point, maxDistanceInKilometers, pageable);
    final List<GeoLocationDistance> content = new LinkedList<>();
    while (ids.hasNext()) {
        final long id = ids.next();
        final GeoLocationDistance e = new GeoLocationDistance();
        final GeoLocation location = findById(id);
        if (location != null) {
            e.setLocation(location);//from w  w w .  j  av a2  s .com
            final double distance = DistanceUtil.distance(location.getCoordinates(), point);
            e.setDistance(distance);
            content.add(e);
        }
    }
    final long total = content.size();
    return new PageImpl<>(content, pageable, total);
}

From source file:com.alliander.osgp.acceptancetests.devicemanagement.RetrieveReceivedEventNotificationsSteps.java

@DomainStep("(.*) received event notifications")
public void givenNumberReceivedEventNotifications(final String count) {
    LOGGER.info("GIVEN: {} received event notifications", count);

    final List<Event> eventList = new ArrayList<Event>();
    int totalPages = 0;
    if (count != null && count != "EMPTY") {
        for (int i = 0; i < Math.min(Integer.parseInt(count),
                Math.min(this.request.getPageSize(), PAGESIZELIMIT)); i++) {
            eventList.add(new Event(this.device, this.event.getEventType(), this.event.getDescription(),
                    this.event.getIndex()));
        }/*  w  w w . j ava 2 s  . com*/
        totalPages = Integer.parseInt(count);
    }
    this.eventsPage = new PageImpl<Event>(eventList,
            new PageRequest(this.request.getPage(), Math.min(this.request.getPageSize(), PAGESIZELIMIT)),
            totalPages);

    when(this.eventRepositoryMock.findAll(Matchers.<Specifications<Event>>any(), any(PageRequest.class)))
            .thenReturn(this.eventsPage);
}

From source file:com.jiwhiz.rest.site.WebsiteRestControllerTest.java

@Test
public void getRecentPublicCommentPosts_ShouldReturnRecentCommentPosts() throws Exception {
    Pageable pageable = new PageRequest(0, 4);

    when(commentPostRepositoryMock.findByStatusOrderByCreatedTimeDesc(eq(CommentStatusType.APPROVED),
            any(Pageable.class)))
                    .thenReturn(new PageImpl<CommentPost>(getTestApprovedCommentPostList(), pageable, 2));

    mockMvc.perform(get(API_ROOT + URL_SITE_RECENT_COMMENTS)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaTypes.HAL_JSON))
            .andExpect(jsonPath("$._embedded.commentPostList", hasSize(2)))
            .andExpect(jsonPath("$._embedded.commentPostList[0].id", is(COMMENTS_1_ID)))
            .andExpect(jsonPath("$._embedded.commentPostList[0].content", is(COMMENTS_1_CONTENT)))
            .andExpect(jsonPath("$._embedded.commentPostList[1].id", is(COMMENTS_2_ID)))
            .andExpect(jsonPath("$._embedded.commentPostList[1].content", is(COMMENTS_2_CONTENT)))
            //check links
            .andExpect(jsonPath("$._links.self.href", endsWith(URL_SITE_RECENT_COMMENTS)));

    verify(commentPostRepositoryMock, times(1)).findByStatusOrderByCreatedTimeDesc(CommentStatusType.APPROVED,
            pageable);/*from  w  w w. j  a  v  a2  s.  c om*/
    verifyNoMoreInteractions(commentPostRepositoryMock);

}

From source file:com.frank.search.solr.core.ResultHelper.java

static <T> Map<Object, GroupResult<T>> convertGroupQueryResponseToGroupResultMap(Query query,
        Map<String, Object> objectNames, QueryResponse response, SolrTemplate solrTemplate, Class<T> clazz) {

    GroupResponse groupResponse = response.getGroupResponse();
    SolrDocumentList sdl = response.getResults();

    if (groupResponse == null) {
        return Collections.emptyMap();
    }//from   www  . j a  v  a2  s . c o m

    Map<Object, GroupResult<T>> result = new LinkedHashMap<Object, GroupResult<T>>();

    List<GroupCommand> values = groupResponse.getValues();
    for (GroupCommand groupCommand : values) {

        List<GroupEntry<T>> groupEntries = new ArrayList<GroupEntry<T>>();

        for (Group group : groupCommand.getValues()) {

            SolrDocumentList documentList = group.getResult();
            List<T> beans = solrTemplate.convertSolrDocumentListToBeans(documentList, clazz);
            Page<T> page = new PageImpl<T>(beans, query.getGroupOptions().getPageRequest(),
                    documentList.getNumFound());
            groupEntries.add(new SimpleGroupEntry<T>(group.getGroupValue(), page));
        }

        int matches = groupCommand.getMatches();
        Integer ngroups = groupCommand.getNGroups();
        String name = groupCommand.getName();

        PageImpl<GroupEntry<T>> page;
        if (ngroups != null) {
            page = new PageImpl<GroupEntry<T>>(groupEntries, query.getPageRequest(), ngroups.intValue());
        } else {
            page = new PageImpl<GroupEntry<T>>(groupEntries);
        }

        SimpleGroupResult<T> groupResult = new SimpleGroupResult<T>(matches, ngroups, name, page);
        result.put(name, groupResult);
        if (objectNames.containsKey(name)) {
            result.put(objectNames.get(name), groupResult);
        }
    }

    return result;
}

From source file:es.fdi.reservas.reserva.business.boundary.ReservaService.java

public Page<Reserva> getReservasPaginadasUser(PageRequest pageRequest, Long idUsuario) {
     List<Reserva> lista = reserva_repository.findByUserId(idUsuario);
     Page<Reserva> pagina = new PageImpl<Reserva>(lista, pageRequest, 5);
     return pagina;
 }