Commit cc9f376e by amir

add paging

parent cc2e6d5f
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;
}
}
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;
}
}
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;
}
}
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);
}
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
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;
}
}
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 sign in to comment