1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package cz.zcu.mre.data.datatable;
23
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28
29
30
31
32
33 public class DataTableQueryCriteria {
34
35 private final Long DEFAULT_PAGE_SIZE = 25L;
36 private final Long DEFAULT_PAGE_NUMBER = 0L;
37
38
39
40
41 private Long pageNumber = null;
42
43
44
45
46 private Long pageSize = null;
47
48
49
50
51 private Long totalRecords = null;
52
53
54
55
56 private DataTableQuerySortBy sortBy;
57
58
59
60
61 private DataTableQueryFilterBy filterBy;
62
63
64
65
66
67
68 public Long getPageNumber() {
69 return (pageNumber == null) ? DEFAULT_PAGE_NUMBER : pageNumber;
70 }
71
72
73
74
75
76
77 public void setPageNumber(Long pageNumber) {
78 this.pageNumber = pageNumber;
79 }
80
81
82
83
84
85
86 public Long getPageSize() {
87 return (pageSize == null) ? DEFAULT_PAGE_SIZE : pageSize;
88 }
89
90
91
92
93
94
95 public void setPageSize(Long pageSize) {
96 this.pageSize = pageSize;
97 }
98
99
100
101
102
103
104 public Long getTotalRecords() {
105 return totalRecords;
106 }
107
108
109
110
111
112
113 public void setTotalRecords(Long totalRecords) {
114 this.totalRecords = totalRecords;
115 }
116
117
118
119
120
121
122 public DataTableQuerySortBy getSortBy() {
123 return sortBy;
124 }
125
126
127
128
129
130
131 public void setSortBy(DataTableQuerySortBy sortBy) {
132 this.sortBy = sortBy;
133 }
134
135
136
137
138
139
140 public DataTableQueryFilterBy getFilterBy() {
141 return filterBy;
142 }
143
144
145
146
147
148
149 public void setFilterBy(DataTableQueryFilterBy filterBy) {
150 this.filterBy = filterBy;
151 }
152
153
154
155
156
157
158 public boolean isFilterByEmpty() {
159
160 return filterBy == null || filterBy.getMapOfFilters() == null || filterBy.getMapOfFilters().isEmpty();
161 }
162
163
164
165
166
167
168 public boolean isSortByEmpty() {
169
170 return sortBy == null || sortBy.getSortBys() == null || sortBy.getSortBys().isEmpty();
171 }
172
173
174
175
176
177
178 public String getFilterByClause() {
179
180 StringBuilder fbsb = null;
181
182 if (!isFilterByEmpty()) {
183 Iterator<Entry<String, String>> fbit = filterBy.getMapOfFilters().entrySet().iterator();
184
185 while (fbit.hasNext()) {
186
187 Map.Entry<String, String> pair = fbit.next();
188
189 if (fbsb == null) {
190 fbsb = new StringBuilder();
191 fbsb.append(BRKT_OPN);
192
193 fbsb.append(SPACE)
194 .append(BRKT_OPN)
195 .append(pair.getKey())
196 .append(LIKE_PREFIX)
197 .append(pair.getValue())
198 .append(LIKE_SUFFIX)
199 .append(BRKT_CLS);
200
201 } else {
202
203 fbsb.append(filterBy.isGlobalSearch() ? OR : AND)
204 .append(BRKT_OPN)
205 .append(pair.getKey())
206 .append(LIKE_PREFIX)
207 .append(pair.getValue())
208 .append(LIKE_SUFFIX)
209 .append(BRKT_CLS);
210
211 }
212 }
213
214 if (fbsb != null) {
215 fbsb.append(BRKT_CLS);
216 }
217 }
218
219 return (fbsb == null) ? BLANK : fbsb.toString();
220 }
221
222
223
224
225
226
227 public String getOrderByClause() {
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 return BLANK;
248 }
249
250
251
252
253 private static final String BLANK = "";
254
255
256
257
258 private static final String SPACE = " ";
259
260
261
262
263 private static final String LIKE_PREFIX = " LIKE '%";
264
265
266
267
268 private static final String LIKE_SUFFIX = "%' ";
269
270
271
272
273 private static final String AND = " AND ";
274
275
276
277
278 private static final String OR = " OR ";
279
280
281
282
283 private static final String ORDER_BY = " ORDER BY ";
284
285 private static final String BRKT_OPN = " ( ";
286
287 private static final String BRKT_CLS = " ) ";
288
289
290
291
292 private static final String COMMA = " , ";
293
294
295
296
297 public static final String PAGE_NUMBER = "start";
298
299
300
301
302 public static final String PAGE_SIZE = "length";
303
304
305
306
307 public static final String DRAW = "draw";
308
309
310
311
312 static String UNIQUE_ID = "_";
313
314 @Override
315 public String toString() {
316 return "PaginationCriteria{" + "pageNumber=" + pageNumber + ", pageSize=" + pageSize + ", totalRecords=" + totalRecords + ", sortBy=" + sortBy + ", filterBy=" + filterBy + '}';
317 }
318 }