Commit 1949ff93 by Jan Hrabal

paging

parent f02baf51
......@@ -149,7 +149,7 @@ public abstract class AbstractHibernateRepository {
sortedCriteria(c, pagingInfo);
return new Page<>(pagingInfo.getPage(), pagesCount, count, c.list(), pagingInfo.getField(), pagingInfo.getTrend());
return new Page<>(pagingInfo.getPage(), pagingInfo.getPageSize(), pagesCount, count, c.list(), pagingInfo.getField(), pagingInfo.getTrend());
}
......
......@@ -4,12 +4,6 @@
*/
package com.jh.boot.web.list;
import java.util.List;
import java.util.Objects;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.NativeWebRequest;
......@@ -22,7 +16,7 @@ import com.jh.boot.utils.Utils;
public class ListHelper {
/** The Constant DEFAULT_PAGE_SIZE. */
public static final int DEFAULT_PAGE_SIZE = 20;
public static final int DEFAULT_PAGE_SIZE = 10;
/**
* Read paging info.
......@@ -39,13 +33,17 @@ public class ListHelper {
Integer page = 0;
Integer pageSize = 10;
/*
if (Utils.isTrue(request.getHeader("X-Paging"))) {
String s = request.getHeader("X-Paging-Page");
page = Utils.parseInt(s, 0);
s = request.getHeader("X-Paging-PageSize");
pageSize = Utils.parseInt(s, 10);
pageSize = Utils.parseInt(s, DEFAULT_PAGE_SIZE);
}
*/
page = Utils.parseInt(request.getParameter("page"), 0);
pageSize = Utils.parseInt(request.getParameter("pageSize"), DEFAULT_PAGE_SIZE);
SortingInfo si = readSortingInfo(request, defaultField, defaultTrend);
......@@ -81,7 +79,7 @@ public class ListHelper {
return null;
}
if (!(Utils.isTrue(request.getHeader("X-Sorting")) || defaultField != null)) {
if (!(StringUtils.hasText(request.getParameter("sortBy")) || defaultField != null)) {
return null;
}
......@@ -89,8 +87,8 @@ public class ListHelper {
String trend = null;
SortTrend st = null;
field = request.getHeader("X-Sorting-Field");
trend = request.getHeader("X-Sorting-Trend");
field = request.getParameter("sortBy");
trend = request.getParameter("sort");
if (!StringUtils.hasText(field) && StringUtils.hasText(defaultField)) {
field = defaultField;
......@@ -112,71 +110,71 @@ public class ListHelper {
}
/**
* Write paging info.
*
* @param page the page
* @return the http headers
*/
public static HttpHeaders writePagingInfo(Page<?> page) {
return writePagingInfo(new HttpHeaders(), page);
}
/**
* Writes complete headers for provided page .
*
* @param headers the headers
* @param page the page
* @return the http headers
*/
public static HttpHeaders writePagingInfo(HttpHeaders headers, Page<?> page) {
if (page == null) {
return headers;
}
headers.add("X-Paging", "true");
headers.add("X-Paging-Page", String.valueOf(page.getPage()));
headers.add("X-Paging-PagesCount", String.valueOf(page.getPagesCount()));
writeSortingInfo(headers, page);
return headers;
}
/**
* Writes only sorting information for provided page.
*
* @param headers the headers
* @param page the page
* @return the http headers
*/
public static HttpHeaders writeSortingInfo(HttpHeaders headers, Page<?> page) {
if (page == null || !page.isSorted()) {
return headers;
}
headers.add("X-Sorting", Objects.toString(page.isSorted()));
if (page.isSorted()) {
headers.add("X-Sorting-Field", page.getSortBy());
headers.add("X-Sorting-Trend", page.getTrend() == SortTrend.DESCENDING ? "desc" : "asc");
}
return headers;
}
/**
* Creates paged response entity.
*
* @param <T> the generic type
* @param page the page
* @return the response entity
*/
public static <T> ResponseEntity<List<T>> pagedResponse(Page<T> page) {
return new ResponseEntity<>(page.getContent(), ListHelper.writePagingInfo(page), HttpStatus.OK);
}
// /**
// * Write paging info.
// *
// * @param page the page
// * @return the http headers
// */
// public static HttpHeaders writePagingInfo(Page<?> page) {
// return writePagingInfo(new HttpHeaders(), page);
// }
//
//
// /**
// * Writes complete headers for provided page .
// *
// * @param headers the headers
// * @param page the page
// * @return the http headers
// */
// public static HttpHeaders writePagingInfo(HttpHeaders headers, Page<?> page) {
// if (page == null) {
// return headers;
// }
//
// headers.add("X-Paging", "true");
// headers.add("X-Paging-Page", String.valueOf(page.getPage()));
// headers.add("X-Paging-PagesCount", String.valueOf(page.getPagesCount()));
//
// writeSortingInfo(headers, page);
//
// return headers;
// }
// /**
// * Writes only sorting information for provided page.
// *
// * @param headers the headers
// * @param page the page
// * @return the http headers
// */
// public static HttpHeaders writeSortingInfo(HttpHeaders headers, Page<?> page) {
// if (page == null || !page.isSorted()) {
// return headers;
// }
//
// headers.add("X-Sorting", Objects.toString(page.isSorted()));
// if (page.isSorted()) {
// headers.add("X-Sorting-Field", page.getSortBy());
// headers.add("X-Sorting-Trend", page.getTrend() == SortTrend.DESCENDING ? "desc" : "asc");
// }
//
// return headers;
// }
// /**
// * Creates paged response entity.
// *
// * @param <T> the generic type
// * @param page the page
// * @return the response entity
// */
// public static <T> ResponseEntity<List<T>> pagedResponse(Page<T> page) {
// return new ResponseEntity<>(page.getContent(), ListHelper.writePagingInfo(page), HttpStatus.OK);
// }
}
......
......@@ -24,6 +24,8 @@ public class Page<T> {
/** The page. */
private int page;
private int pageSize;
/** The sort by. */
private String sortBy;
......@@ -38,7 +40,7 @@ public class Page<T> {
* @param content the content
*/
public Page(List<T> content) {
this(0, 1, content);
this(0, 1, content.size(), content);
}
/**
......@@ -48,10 +50,11 @@ public class Page<T> {
* @param pagesCount the pages count
* @param content the content
*/
public Page(int page, int pagesCount, List<T> content) {
public Page(int page, int pagesCount, int pageSize, List<T> content) {
this.content = content;
this.page = page;
this.pagesCount = pagesCount;
this.pageSize = pageSize;
}
/**
......@@ -63,16 +66,17 @@ public class Page<T> {
* @param sortBy the sort by
* @param trend the trend
*/
public Page(int page, int pagesCount, List<T> content, String sortBy, SortTrend trend) {
public Page(int page, int pagesCount, int pageSize, List<T> content, String sortBy, SortTrend trend) {
super();
this.content = content;
this.pagesCount = pagesCount;
this.page = page;
this.sortBy = sortBy;
this.trend = trend;
this.pageSize = pageSize;
}
public Page(int page, int pagesCount, int totalElementsCount, List<T> content, String sortBy, SortTrend trend) {
public Page(int page, int pagesCount, int pageSize, int totalElementsCount, List<T> content, String sortBy, SortTrend trend) {
super();
this.content = content;
this.pagesCount = pagesCount;
......@@ -80,6 +84,7 @@ public class Page<T> {
this.page = page;
this.sortBy = sortBy;
this.trend = trend;
this.pageSize = pageSize;
}
/**
......@@ -141,7 +146,11 @@ public class Page<T> {
}
public static <T> Page<T> empty() {
return new Page<T>(0, 0, Collections.emptyList());
return new Page<T>(0, 0, ListHelper.DEFAULT_PAGE_SIZE, Collections.emptyList());
}
public int getPageSize() {
return pageSize;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment