1 /*
2 * Copyright 2018-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3 * Department of Computer Science and Engineering, University of West Bohemia.
4 * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5 *
6 * Author Petr Vcelak (vcelak@kiv.zcu.cz).
7 *
8 * This file is part of MRECore project.
9 *
10 * MRECore is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License.
13 *
14 * MRECore is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MRECore. If not, see <http://www.gnu.org/licenses/>.
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 * The Class DataTableQueryCriteria.
30 *
31 * @author Petr Vcelak (vcelak@kiv.zcu.cz)
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 * The page number.
40 */
41 private Long pageNumber = null;
42
43 /**
44 * The page size.
45 */
46 private Long pageSize = null;
47
48 /**
49 * The total records.
50 */
51 private Long totalRecords = null;
52
53 /**
54 * The sort by.
55 */
56 private DataTableQuerySortBy sortBy;
57
58 /**
59 * The filter by.
60 */
61 private DataTableQueryFilterBy filterBy;
62
63 /**
64 * Gets the page number.
65 *
66 * @return the pageNumber
67 */
68 public Long getPageNumber() {
69 return (pageNumber == null) ? DEFAULT_PAGE_NUMBER : pageNumber;
70 }
71
72 /**
73 * Sets the page number.
74 *
75 * @param pageNumber the pageNumber to set
76 */
77 public void setPageNumber(Long pageNumber) {
78 this.pageNumber = pageNumber;
79 }
80
81 /**
82 * Gets the page size.
83 *
84 * @return the pageSize
85 */
86 public Long getPageSize() {
87 return (pageSize == null) ? DEFAULT_PAGE_SIZE : pageSize;
88 }
89
90 /**
91 * Sets the page size.
92 *
93 * @param pageSize the pageSize to set
94 */
95 public void setPageSize(Long pageSize) {
96 this.pageSize = pageSize;
97 }
98
99 /**
100 * Gets the total records.
101 *
102 * @return the totalRecords
103 */
104 public Long getTotalRecords() {
105 return totalRecords;
106 }
107
108 /**
109 * Sets the total records.
110 *
111 * @param totalRecords the totalRecords to set
112 */
113 public void setTotalRecords(Long totalRecords) {
114 this.totalRecords = totalRecords;
115 }
116
117 /**
118 * Gets the sort by.
119 *
120 * @return the sortBy
121 */
122 public DataTableQuerySortBy getSortBy() {
123 return sortBy;
124 }
125
126 /**
127 * Sets the sort by.
128 *
129 * @param sortBy the sortBy to set
130 */
131 public void setSortBy(DataTableQuerySortBy sortBy) {
132 this.sortBy = sortBy;
133 }
134
135 /**
136 * Gets the filter by.
137 *
138 * @return the filterBy
139 */
140 public DataTableQueryFilterBy getFilterBy() {
141 return filterBy;
142 }
143
144 /**
145 * Sets the filter by.
146 *
147 * @param filterBy the filterBy to set
148 */
149 public void setFilterBy(DataTableQueryFilterBy filterBy) {
150 this.filterBy = filterBy;
151 }
152
153 /**
154 * Checks if is filter by empty.
155 *
156 * @return true, if is filter by empty
157 */
158 public boolean isFilterByEmpty() {
159
160 return filterBy == null || filterBy.getMapOfFilters() == null || filterBy.getMapOfFilters().isEmpty();
161 }
162
163 /**
164 * Checks if is sort by empty.
165 *
166 * @return true, if is sort by empty
167 */
168 public boolean isSortByEmpty() {
169
170 return sortBy == null || sortBy.getSortBys() == null || sortBy.getSortBys().isEmpty();
171 }
172
173 /**
174 * Gets the filter by clause.
175 *
176 * @return the filter by clause
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 * Gets the order by clause.
224 *
225 * @return the order by clause
226 */
227 public String getOrderByClause() {
228
229 // StringBuilder sbsb = null;
230 //
231 // if (!isSortByEmpty()) {
232 // Iterator<Entry<String, DataTableQuerySortOrder>> sbit = sortBy.getSortBys().entrySet().iterator();
233 //
234 //// while (sbit.hasNext()) {
235 //// Map.Entry<String, DataTableQuerySortOrder> pair = sbit.next();
236 ////
237 //// if (sbsb == null) {
238 //// sbsb = new StringBuilder();
239 //// sbsb.append(ORDER_BY).append(pair.getKey()).append(SPACE).append(pair.getValue());
240 //// } else {
241 //// sbsb.append(COMMA).append(pair.getKey()).append(SPACE).append(pair.getValue());
242 //// }
243 //// }
244 // }
245 //
246 // return (sbsb == null) ? BLANK : sbsb.toString();
247 return BLANK;
248 }
249
250 /**
251 * The Constant BLANK.
252 */
253 private static final String BLANK = "";
254
255 /**
256 * The Constant SPACE.
257 */
258 private static final String SPACE = " ";
259
260 /**
261 * The Constant LIKE_PREFIX.
262 */
263 private static final String LIKE_PREFIX = " LIKE '%";
264
265 /**
266 * The Constant LIKE_SUFFIX.
267 */
268 private static final String LIKE_SUFFIX = "%' ";
269
270 /**
271 * The Constant AND.
272 */
273 private static final String AND = " AND ";
274
275 /**
276 * The Constant OR.
277 */
278 private static final String OR = " OR ";
279
280 /**
281 * The Constant ORDER_BY.
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 * The Constant COMMA.
291 */
292 private static final String COMMA = " , ";
293
294 /**
295 * The Constant PAGE_NUMBER.
296 */
297 public static final String PAGE_NUMBER = "start";
298
299 /**
300 * The Constant PAGE_SIZE.
301 */
302 public static final String PAGE_SIZE = "length";
303
304 /**
305 * The Constant DRAW.
306 */
307 public static final String DRAW = "draw";
308
309 /**
310 * The constant for unique ID.
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 }