Commit 1949ff93 by Jan Hrabal

paging

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