/**
   * Gets the link node of the next page.
   *
   * @param page the pagination object
   * @param contextPath current context path of servlet
   * @return the node
   */
  private Element getNextElement(final Page<?> page, final String contextPath) {
    final Element result = new Element("li");

    String url;
    if (!page.hasNext()) {
      int pageNumber = page.getTotalPages() - 1;
      url = getPagedParams(contextPath, pageNumber < 0 ? 0 : pageNumber, page.getSize());
    } else {
      url = getPagedParams(contextPath, page.getNumber() + 1, page.getSize());
    }

    final Element a = new Element("a");
    a.setAttribute("href", url);
    result.addChild(a);

    final Element icon = new Element("i");
    icon.setAttribute("class", "glyphicon glyphicon-triangle-right");

    a.addChild(icon);

    if (!page.hasNext()) {
      result.setAttribute("class", "disabled");
    }

    return result;
  }
  /**
   * Gets the link node of the previous page.
   *
   * @param page the pagination object
   * @param contextPath current context path of servlet
   * @return the node
   */
  private Element getPreviousElement(final Page<?> page, final String contextPath) {
    final Element result = new Element("li");

    String url;
    if (!page.hasPrevious()) {
      url = getPagedParams(contextPath, 0, page.getSize());
    } else {
      url = getPagedParams(contextPath, page.getNumber() - 1, page.getSize());
    }

    final Element a = new Element("a");
    a.setAttribute("href", url);
    result.addChild(a);

    final Element icon = new Element("i");
    icon.setAttribute("class", "glyphicon glyphicon-triangle-left");

    a.addChild(icon);

    if (!page.hasPrevious()) {
      result.setAttribute("class", "disabled");
    }

    return result;
  }
示例#3
0
  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;
  }
 private void addLastLink(Page<T> page, String pageParam, String sizeParam) {
   if (page.isLast()) {
     Link link =
         buildPageLink(pageParam, page.getTotalPages(), sizeParam, page.getSize(), Link.REL_LAST);
     add(link);
   }
 }
 private void addNextLink(Page<T> page, String pageParam, String sizeParam) {
   if (page.hasNext()) {
     Link link =
         buildPageLink(pageParam, page.getNumber() + 1, sizeParam, page.getSize(), Link.REL_NEXT);
     add(link);
   }
 }
  /**
   * Add all bean properties from the supplied bean to the representation
   *
   * @param value
   * @return
   */
  public ResponseEntityBuilder<ReadableRepresentation> withPage(
      Page<?> value, String uriTemplate, String... includeFields) {
    String[] fields = requestedFields == null ? includeFields : requestedFields;

    // Extract page data such as size, page number
    representation.withProperty("size", value.getSize());
    representation.withProperty("number", value.getNumber());
    representation.withProperty("numberOfElements", value.getNumberOfElements());
    representation.withProperty("totalElements", value.getTotalElements());

    // Next/back links
    if (value.hasNextPage()) {
      buildNextLink(representation, request);
    }
    if (value.hasPreviousPage()) {
      buildPreviousLink(representation, request);
    }

    // Build the content of the page
    for (Object object : value.getContent()) {
      Representation content = converter.convert(object, new UriTemplate(uriTemplate), fields);
      this.representation.withRepresentation("content", content);
    }
    return this;
  }
示例#7
0
  public WebPageDto(Page<T> page, String url) {
    this.url = url;
    this.totalPages = page.getTotalPages();
    this.totalElements = page.getTotalElements();
    this.firstPage = page.isFirstPage();
    this.lastPage = page.isLastPage();
    this.hasPreviousPage = page.hasPreviousPage();
    this.hasNextPage = page.hasNextPage();
    this.size = page.getSize();
    this.number = page.getNumber() + 1;
    this.content = page.getContent();

    int start, size;
    if (totalPages <= DEFAULT_PAGES) {
      start = 1;
      size = totalPages;
    } else {
      if (number <= DEFAULT_PAGES - DEFAULT_PAGES / 2) {
        start = 1;
        size = DEFAULT_PAGES;
      } else if (number >= totalPages - DEFAULT_PAGES / 2) {
        start = totalPages - DEFAULT_PAGES + 1;
        size = DEFAULT_PAGES;
      } else {
        start = number - DEFAULT_PAGES / 2;
        size = DEFAULT_PAGES;
      }
    }

    for (int i = 0; i < size; i++) {
      items.add(new PageItem(start + i, (start + i) == number));
    }
  }
 private void addPreviousLink(Page<T> page, String pageParam, String sizeParam) {
   if (page.hasPrevious()) {
     Link link =
         buildPageLink(
             pageParam, page.getNumber() - 1, sizeParam, page.getSize(), Link.REL_PREVIOUS);
     add(link);
   }
 }
  @Test
  public void testeListWithPagination() {
    User bruno = admin("bruno").build();
    City cidade = city("São Paulo").build();
    Hotel c1 = hotel("A-Hotel", cidade).build();
    Hotel c2 = hotel("B-Hotel", cidade).build();
    Hotel c3 = hotel("C-Hotel", cidade).build();
    Hotel c4 = hotel("D-Hotel", cidade).build();
    Hotel c5 = hotel("E-Hotel", cidade).build();
    saveall(cidade, c1, c2, c3, c4, c5, bruno);

    signIn(bruno);

    Page<HotelDTO> page =
        get("/hotel")
            .queryParam("page", "0")
            .queryParam("size", "2")
            .expectedStatus(HttpStatus.OK)
            .getPage(HotelDTO.class);

    assertThat(page.getNumber(), equalTo(0));
    assertThat(page.getSize(), equalTo(2));
    assertThat(page.getTotalElements(), equalTo(5l));
    assertThat(page.getTotalPages(), equalTo(3));
    assertThat(page.getContent(), hasSize(2));
    assertThat(page.getContent(), contains(convert.toDTO(c1), convert.toDTO(c2)));

    page =
        get("/hotel")
            .queryParam("size", "2")
            .queryParam("page", "2")
            .expectedStatus(HttpStatus.OK)
            .getPage(HotelDTO.class);

    assertThat(page.getNumber(), equalTo(2));
    assertThat(page.getSize(), equalTo(2));
    assertThat(page.getTotalElements(), equalTo(5l));
    assertThat(page.getTotalPages(), equalTo(3));
    assertThat(page.getContent(), hasSize(1));
    assertThat(page.getContent(), contains(convert.toDTO(c5)));
  }
  /**
   * Adds elements for steps.
   *
   * @param page the pagination object
   * @param contextPath current context path of servlet
   * @param container the container node of the element
   */
  private void addStepElements(
      final Page<?> page, final String contextPath, final Element container) {
    for (int step = 0; step < DEFAULT_STEPS; step++) {
      // beyond total pages is not allowed
      if (page.getTotalPages() < step + 1) {
        continue;
      }

      String url;
      int stepValue;
      if ((page.getNumber() + DEFAULT_STEPS) <= page.getTotalPages()) {
        // calculate by page number
        url = getPagedParams(contextPath, page.getNumber() + step, page.getSize());
        stepValue = page.getNumber() + step + 1;
      } else if (page.getTotalPages() < DEFAULT_STEPS && page.getTotalPages() >= step + 1) {
        // between step and DEFAULT_STEPS
        url = getPagedParams(contextPath, step, page.getSize());
        stepValue = step + 1;
      } else {
        // calculate by totalPages
        url =
            getPagedParams(
                contextPath, page.getTotalPages() - DEFAULT_STEPS + step, page.getSize());
        stepValue = page.getTotalPages() - DEFAULT_STEPS + step + 1;
      }

      final Element a = new Element("a");
      a.setAttribute("href", url);
      a.addChild(new Text(Integer.toString(stepValue)));

      final Element li = new Element("li");
      li.addChild(a);
      // set active
      if (page.getNumber() + 1 == stepValue) {
        li.setAttribute("class", "active");
      }

      container.addChild(li);
    }
  }
示例#11
0
 private WebPage(Page<T> page) {
   number = page.getNumber();
   size = page.getSize();
   totalPages = page.getTotalPages();
   numberOfElements = page.getNumberOfElements();
   totalElements = page.getTotalElements();
   hasPreviousPage = page.hasPreviousPage();
   isFirstPage = page.isFirstPage();
   hasNextPage = page.hasNextPage();
   isLastPage = page.isLastPage();
   content = page.getContent();
   hasContent = page.hasContent();
   sort = new WebSort(page.getSort());
 }
  public static <S, T> Page<T> mapPage(Page<S> source, Class<T> targetClass) {
    List<S> sourceList = source.getContent();

    List<T> list = new ArrayList<>();
    for (int i = 0; i < sourceList.size(); i++) {
      T target = INSTANCE.map(sourceList.get(i), targetClass);
      list.add(target);
    }

    return new PageImpl<>(
        list,
        new PageRequest(source.getNumber(), source.getSize(), source.getSort()),
        source.getTotalElements());
  }
  @Test
  public void testList() {
    User bruno = admin("bruno").build();
    City cidade = city("São Paulo").build();
    Hotel c1 = hotel("Macbook", cidade).build();
    Hotel c2 = hotel("IPhone", cidade).build();
    saveall(cidade, c1, c2, bruno);

    signIn(bruno);

    Page<HotelDTO> page = get("/hotel").expectedStatus(HttpStatus.OK).getPage(HotelDTO.class);

    assertThat(page.getNumber(), equalTo(0));
    assertThat(page.getSize(), equalTo(50));
    assertThat(page.getContent(), contains(convert.toDTO(c2), convert.toDTO(c1)));
  }
  /**
   * Gets the link node of the first page.
   *
   * @param page the pagination object
   * @param contextPath current context path of servlet
   * @return the node
   */
  private Element getFirstElement(final Page<?> page, final String contextPath) {
    final Element result = new Element("li");

    final Element a = new Element("a");
    a.setAttribute("href", getPagedParams(contextPath, 0, page.getSize()));
    result.addChild(a);

    final Element icon = new Element("i");
    icon.setAttribute("class", "glyphicon glyphicon-step-backward");
    a.addChild(icon);

    if (page.isFirst()) {
      result.setAttribute("class", "disabled");
    }

    return result;
  }
示例#15
0
  @Test
  public void testListPost() {
    Page<Post> postPage = postService.listPost(10, 0);

    Assert.assertTrue(postPage.getSize() != 0);

    for (Post post : postPage.getContent()) {
      System.out.println(post.getId() + "\t" + post.getTitle() + "\t" + post.getContent());
      //            post.setPostTerms(null);
      post.getPostTerms().clear();
      Long expectedId = post.getId();

      Post acutal = postService.save(post);
      Assert.assertNotNull(acutal);
      Assert.assertEquals(expectedId, acutal.getId());
    }
  }
  public PageResource(Page<T> page, String pageParam, String sizeParam) {
    super();
    number = page.getNumber();
    size = page.getSize();
    numberOfElements = page.getNumberOfElements();
    content = page.getContent();
    sort = page.getSort();
    firstPage = page.isFirst();
    lastPage = page.isLast();
    totalPages = page.getTotalPages();
    totalElements = page.getTotalElements();

    addPreviousLink(page, pageParam, sizeParam);
    addNextLink(page, pageParam, sizeParam);
    addFirstLink(page, pageParam, sizeParam);
    addLastLink(page, pageParam, sizeParam);
    addSelfLink(page, pageParam, sizeParam);
  }
示例#17
0
 /** 设置 Spring data JPA 分页对象,转换为本系统分页对象 */
 public void setSpringPage(org.springframework.data.domain.Page<T> page) {
   this.pageNo = page.getNumber();
   this.pageSize = page.getSize();
   this.count = page.getTotalElements();
   this.list = page.getContent();
 }
 private void addFirstLink(Page<T> page, String pageParam, String sizeParam) {
   if (page.isFirst()) {
     Link link = buildPageLink(pageParam, 0, sizeParam, page.getSize(), Link.REL_FIRST);
     add(link);
   }
 }
 private void addSelfLink(Page<T> page, String pageParam, String sizeParam) {
   Link link =
       buildPageLink(pageParam, page.getNumber(), sizeParam, page.getSize(), Link.REL_SELF);
   add(link);
 }