Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
cc9f376e
authored
Mar 23, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add paging
parent
cc2e6d5f
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
262 additions
and
0 deletions
src/main/java/microservice/common/paging/Href.java
src/main/java/microservice/common/paging/Links.java
src/main/java/microservice/common/paging/Page.java
src/main/java/microservice/common/paging/PageBuilder.java
src/main/java/microservice/common/paging/PageBuilderJsonNodeImpl.java
src/main/java/microservice/common/paging/Sort.java
src/main/java/microservice/common/paging/Href.java
0 → 100644
View file @
cc9f376e
package
microservice
.
common
.
paging
;
/**
* Created by amir on 23/03/17.
*/
public
class
Href
{
public
static
final
int
CAPACITY
=
64
;
String
href
;
public
Href
(
String
url
,
int
number
,
int
size
)
{
this
.
href
=
new
StringBuilder
(
CAPACITY
)
.
append
(
url
)
.
append
(
'?'
)
.
append
(
"page="
)
.
append
(
number
)
.
append
(
"&size="
)
.
append
(
size
)
.
toString
();
}
public
String
getHref
()
{
return
href
;
}
}
src/main/java/microservice/common/paging/Links.java
0 → 100644
View file @
cc9f376e
package
microservice
.
common
.
paging
;
/**
* Created by amir on 23/03/17.
*/
public
class
Links
{
Href
self
=
null
;
Href
next
=
null
;
Href
first
=
null
;
Href
last
=
null
;
public
Links
(
String
url
,
Page
page
){
/**
* calculate links
*/
self
=
new
Href
(
url
,
page
.
number
,
page
.
size
);
first
=
new
Href
(
url
,
0
,
page
.
size
);
last
=
new
Href
(
url
,
page
.
totalPages
-
1
,
page
.
size
);
if
(
page
.
number
<
(
page
.
totalPages
-
1
))
next
=
new
Href
(
url
,
page
.
number
+
1
,
page
.
size
);
}
public
Href
getSelf
()
{
return
self
;
}
public
Href
getNext
()
{
return
next
;
}
public
Href
getFirst
()
{
return
first
;
}
public
Href
getLast
()
{
return
last
;
}
}
src/main/java/microservice/common/paging/Page.java
0 → 100644
View file @
cc9f376e
package
microservice
.
common
.
paging
;
/**
* Created by amir on 23/03/17.
*/
public
class
Page
{
boolean
last
;
long
totalElements
;
int
totalPages
;
int
numberOfElements
;
Sort
sort
;
boolean
first
;
int
size
;
int
number
;
public
Page
(
long
totalElements
,
int
numberOfElements
,
Sort
sort
,
int
number
,
int
pageMaxSize
)
{
this
.
totalElements
=
totalElements
;
this
.
numberOfElements
=
numberOfElements
;
this
.
sort
=
sort
;
this
.
number
=
number
;
this
.
size
=
pageMaxSize
;
if
(
totalElements
<=
pageMaxSize
)
this
.
totalPages
=
1
;
else
{
this
.
totalPages
=
(
int
)
(
totalElements
/
pageMaxSize
);
if
((
totalElements
%
pageMaxSize
)
!=
0
)
this
.
totalPages
++;
}
if
(
number
==
0
)
first
=
true
;
if
(
totalPages
==
0
||
number
==
(
totalPages
-
1
))
last
=
true
;
}
public
boolean
isLast
()
{
return
last
;
}
public
long
getTotalElements
()
{
return
totalElements
;
}
public
int
getTotalPages
()
{
return
totalPages
;
}
public
int
getNumberOfElements
()
{
return
numberOfElements
;
}
public
Sort
getSort
()
{
return
sort
;
}
public
boolean
isFirst
()
{
return
first
;
}
public
int
getSize
()
{
return
size
;
}
public
int
getNumber
()
{
return
number
;
}
}
src/main/java/microservice/common/paging/PageBuilder.java
0 → 100644
View file @
cc9f376e
package
microservice
.
common
.
paging
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
/**
* Created by amir on 23/03/17.
*/
public
interface
PageBuilder
<
T
>
{
T
buidPage
(
String
url
,
ArrayNode
content
,
int
pageNumber
,
long
totalElements
,
int
pageMaxSize
);
T
buidPage
(
String
url
,
ArrayNode
content
,
int
pageNumber
,
long
totalElements
,
int
pageMaxSize
,
String
sortProperty
,
boolean
sortAscending
);
}
src/main/java/microservice/common/paging/PageBuilderJsonNodeImpl.java
0 → 100644
View file @
cc9f376e
package
microservice
.
common
.
paging
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.JsonNodeFactory
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
common.JsonHandler
;
/**
* Created by amir on 22/03/17.
* {
* "content":[
{ ...},
{ ...},
....
],
"_links": {
"self": {
"href": "http://localhost:8012/ds/entities/MCX/entities?page=0&size=50"
},
"next": {
"href": "http://localhost:8012/ds/entities/MCX/entities?page=1&size=50""
},
"first": {
"href": "http://localhost:8012/ds/entities/MCX/entities?page=0&size=50""
},
"last": {
"href": "http://localhost:8012/ds/entities/MCX/entities?page=3&size=50"
}
},
"page":{
"last": false,
"totalElements": 174,
"totalPages": 4,
"numberOfElements": 50,
"sort": null,
"first": true,
"size": 50,
"number": 0
}
}
*/
public
class
PageBuilderJsonNodeImpl
implements
PageBuilder
<
JsonNode
>
{
static
final
private
JsonNodeFactory
nodeFactory
=
JsonNodeFactory
.
instance
;
public
JsonNode
buidPage
(
String
url
,
ArrayNode
content
,
int
pageNumber
,
long
totalElements
,
int
pageMaxSize
,
String
sortProperty
,
boolean
sortAscending
){
ObjectNode
pageNode
=
nodeFactory
.
objectNode
();
/**
* preparing
*/
Sort
sort
=
null
;
if
(
sortProperty
!=
null
)
sort
=
new
Sort
(
sortProperty
,
sortAscending
);
Page
pageInfo
=
new
Page
(
totalElements
,
content
.
size
(),
sort
,
pageNumber
,
pageMaxSize
);
Links
links
=
null
;
if
(
totalElements
>
0
&&
content
.
size
()
>
0
)
links
=
new
Links
(
url
,
pageInfo
);
/**
* bulding
*/
pageNode
.
set
(
"content"
,
content
);
pageNode
.
set
(
"_links"
,
JsonHandler
.
getJsonNodeFromObject
(
links
));
pageNode
.
set
(
"page"
,
JsonHandler
.
getJsonNodeFromObject
(
pageInfo
));
return
pageNode
;
}
public
JsonNode
buidPage
(
String
url
,
ArrayNode
content
,
int
pageNumber
,
long
totalElements
,
int
pageMaxSize
){
return
buidPage
(
url
,
content
,
pageNumber
,
totalElements
,
pageMaxSize
,
null
,
false
);
}
}
\ No newline at end of file
src/main/java/microservice/common/paging/Sort.java
0 → 100644
View file @
cc9f376e
package
microservice
.
common
.
paging
;
/**
* Created by amir on 23/03/17.
*/
public
class
Sort
{
String
property
;
boolean
ascending
;
public
Sort
(
String
property
,
boolean
ascending
)
{
this
.
property
=
property
;
this
.
ascending
=
ascending
;
}
public
String
getProperty
()
{
return
property
;
}
public
boolean
isAscending
()
{
return
ascending
;
}
}
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