Repository navigation
Expand file tree
/
Copy pathSpringPagingModelRequest.java
More file actions
112 lines (103 loc) · 4.34 KB
/
Copy pathSpringPagingModelRequest.java
File metadata and controls
112 lines (103 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import be.chillworld.model.SortDirection;
import be.chillworld.request.CurrentPageExceedException;
import be.chillworld.request.PagingModelRequest;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.zkoss.zk.ui.WrongValueException;
/**
* Abstract implementation for Spring paging. Required to implement the method
* "public Page getPage(PageRequest request);"
*
* If you want to have multiple sortfields, you have to seperate them in zul
* page with ; or , or a space example for ZUL : <listheader sort="client(id;name,birthdate)/>
*
* Sortdirection can be forced with ASC, DESC or INVERSE.
* Example : <listheader sort="client(id;inverse(name))/>
* If id sortage is ASC name will be sorted secondly DESC.
* Example : <listheader sort="client(name,desc(birthdate))/>
* Birthdate will ALWAYS be sorted DESC but the sortage of name changes when pressing sort.
*
* Ignorecase can be asked with LOWER or UPPER.
* Example : <listheader sort="client(lower(name))/>
*
* @author cossaer.f
*/
public abstract class SpringPagingModelRequest implements PagingModelRequest {
private long totalSize;
@Override
public List getContent(int activePage, int pageSize, String sortField, SortDirection sortDirection) throws CurrentPageExceedException {
PageRequest request;
Sort.Direction direction;
if (sortDirection != null) {
direction = Sort.Direction.fromString(sortDirection.getShortName());
} else {
direction = Sort.Direction.ASC;
}
if (!StringUtils.isEmpty(sortField)) {
List<Order> sortOrders = new ArrayList<>();
for (String field : sortField.split("(\\s*,\\s*)|(\\s*;\\s*)|(\\s+)")) {// split on (',' or ';' or space) with leading or ending spaces.
sortOrders.add(createSortOrder(field, direction));
}
request = new PageRequest(activePage, pageSize, new Sort(sortOrders));
} else {
request = new PageRequest(activePage, pageSize);
}
// bug fix when usage with filters and currentPage is larger then totalpages.
Page page = getPage(request);
if (page.getTotalPages()<=activePage) {
throw new CurrentPageExceedException();
}
totalSize = page.getTotalElements();
return page.getContent();
}
@Override
public long getTotalSize() {
return totalSize;
}
public abstract Page getPage(PageRequest request);
private Order createSortOrder(String sortField, Sort.Direction direction) {
String[] splitted = sortField.split("(\\()");
Sort.Direction sortDirection = direction;
boolean ignoreCase = false;
String field = "";
for (int counter = 0; counter < splitted.length; counter++) {
if (counter != splitted.length - 1) {
try {
FieldBehavior behavior = FieldBehavior.valueOf(splitted[counter].toUpperCase());
switch (behavior) {
case LOWER:
case UPPER:
ignoreCase = true;
break;
case ASC:
sortDirection = Sort.Direction.ASC;
break;
case DESC:
sortDirection = Sort.Direction.DESC;
break;
case INVERSE:
sortDirection = direction == Sort.Direction.ASC ? Sort.Direction.DESC : Sort.Direction.ASC;
}
} catch (IllegalArgumentException ex) {
throw new WrongValueException("Value : " + splitted[counter] + " is not a predifined value.\nAllowed values are : ASC, DESC, INVERSE, UPPER, LOWER");
}
} else {
field = splitted[counter].replace(")", "");
}
}
Order order = new Order(sortDirection, field);
return ignoreCase?order.ignoreCase():order;
}
private enum FieldBehavior {
ASC,
DESC,
INVERSE,
LOWER,
UPPER;
}
}