Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
1949ff93
Commit
1949ff93
authored
Sep 21, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
paging
parent
f02baf51
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
83 deletions
+90
-83
AbstractHibernateRepository.java
...ain/java/com/jh/boot/jpa/AbstractHibernateRepository.java
+1
-1
ListHelper.java
src/main/java/com/jh/boot/web/list/ListHelper.java
+75
-77
Page.java
src/main/java/com/jh/boot/web/list/Page.java
+14
-5
No files found.
src/main/java/com/jh/boot/jpa/AbstractHibernateRepository.java
View file @
1949ff93
...
...
@@ -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
(),
pag
ingInfo
.
getPageSize
(),
pag
esCount
,
count
,
c
.
list
(),
pagingInfo
.
getField
(),
pagingInfo
.
getTrend
());
}
...
...
src/main/java/com/jh/boot/web/list/ListHelper.java
View file @
1949ff93
...
...
@@ -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
=
2
0
;
public
static
final
int
DEFAULT_PAGE_SIZE
=
1
0
;
/**
* 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
.
get
Header
(
"X-Sorting-Field
"
);
trend
=
request
.
get
Header
(
"X-Sorting-Trend
"
);
field
=
request
.
get
Parameter
(
"sortBy
"
);
trend
=
request
.
get
Parameter
(
"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);
//
}
}
...
...
src/main/java/com/jh/boot/web/list/Page.java
View file @
1949ff93
...
...
@@ -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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment