-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfps.patch
More file actions
473 lines (473 loc) · 14.3 KB
/
fps.patch
File metadata and controls
473 lines (473 loc) · 14.3 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
Index: src/main/java/com/handson/basic/util/Strings.java
===================================================================
diff --git a/src/main/java/com/handson/basic/util/Strings.java b/src/main/java/com/handson/basic/util/Strings.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/util/Strings.java
@@ -0,0 +1,8 @@
+package com.handson.basic.util;
+
+public class Strings {
+
+ public static String likeLowerOrNull(String str) {
+ return str != null ? "%" + str.toLowerCase() + "%" : null;
+ }
+}
Index: src/main/java/com/handson/basic/util/FPS.java
===================================================================
diff --git a/src/main/java/com/handson/basic/util/FPS.java b/src/main/java/com/handson/basic/util/FPS.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/util/FPS.java
@@ -0,0 +1,211 @@
+package com.handson.basic.util;
+
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.handson.basic.model.Pagination;
+import com.handson.basic.model.PaginationAndList;
+import com.handson.basic.model.SortDirection;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class FPS {
+ private List<FPSField> select = new ArrayList<>();
+ private List<String> from = new ArrayList<>();
+ private List<String> joins = new ArrayList<>();
+ private List<FPSCondition> conditions = new ArrayList<>();
+ private String sortField;
+ private SortDirection sortDirection;
+ private Integer page;
+ private Integer count;
+ private Class itemClass;
+
+
+ TimeZone utcTz = TimeZone.getTimeZone("UTC");
+ SimpleDateFormat isoDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
+ public FPS() {
+ super();
+ isoDf.setTimeZone(utcTz);
+ }
+
+ public PaginationAndList exec(EntityManager em, ObjectMapper om) throws JsonProcessingException {
+ Query qrySelect = em.createNativeQuery(getSelectSql(), itemClass);
+ Query qryCount = em.createNativeQuery(getCountSql());
+ conditions.forEach(x-> {
+ if (x.getValue() != null) {
+ qrySelect.setParameter(x.getParameterName(), x.getValue());
+ qryCount.setParameter(x.getParameterName(), x.getValue());
+ }
+ });
+ List rows = qrySelect.getResultList();
+ BigInteger total = (BigInteger) qryCount.getSingleResult();
+ return PaginationAndList.of(Pagination.of(page, (total.intValue() / count) + 1, total.intValue()) ,rows);
+ }
+
+ private String getSelectSql() {
+ String fieldsSql = " select " + select.stream().map(f -> f.getField() + " " + f.getAlias()) .collect(Collectors.joining(","));
+ String fromSql = getFromSql();
+ String whereSql = getWhereSql();
+ String orderSql = "";
+ if (sortField != null) {
+ orderSql = " order by " + sortField + (SortDirection.desc.equals(sortDirection) ? " desc " : "");
+ }
+ String limitSql = "";
+ if (page != null || count != null) {
+ if (count != null) limitSql += " limit " + count;
+ if (page != null) limitSql += " offset " + (page - 1) * count;
+ }
+ return fieldsSql + fromSql + whereSql + orderSql + limitSql;
+ }
+
+ private String getWhereSql() {
+ String whereSql = " where " + joins.stream().collect(Collectors.joining(" and "));
+ if (joins.size() > 0) whereSql += " and ";
+ whereSql += conditions.stream().filter(x -> x.getValue() != null).map(y -> y.getCondition()).collect(Collectors.joining(" and "));
+ if ("where".equals(whereSql.trim())) whereSql = "";
+ return whereSql;
+ }
+
+ private String getFromSql() {
+ return " from " + from.stream().collect(Collectors.joining(","));
+ }
+
+ private String getCountSql() {
+ String fieldsSql = " select count(*)";
+ String fromSql = getFromSql();
+ String whereSql = getWhereSql();
+ return fieldsSql + fromSql + whereSql ;
+ }
+
+ private boolean isaBoolean(Object o) {
+ return o instanceof Boolean;
+ }
+
+ private boolean isDate(Object o) {
+ return o instanceof Date || o instanceof java.sql.Date || o instanceof Timestamp;
+ }
+
+ private boolean isNumeric(Object o) {
+ return o instanceof Integer || o instanceof Long || o instanceof Double || o instanceof BigInteger
+ || o instanceof BigDecimal || o instanceof Float;
+ }
+
+ public List<FPSField> getSelect() {
+ return select;
+ }
+
+ public List<String> getFrom() {
+ return from;
+ }
+
+ public List<String> getJoins() {
+ return joins;
+ }
+
+ public List<FPSCondition> getConditions() {
+ return conditions;
+ }
+
+ public String getSortField() {
+ return sortField;
+ }
+
+ public SortDirection getSortDirection() {
+ return sortDirection;
+ }
+
+ public Integer getPage() {
+ return page;
+ }
+
+ public Integer getCount() {
+ return count;
+ }
+
+
+ public static final class FPSBuilder {
+ private List<FPSField> select = new ArrayList<>();
+ private List<String> from = new ArrayList<>();
+ private List<String> joins = new ArrayList<>();
+ private List<FPSCondition> conditions = new ArrayList<>();
+ private String sortField;
+ private SortDirection sortDirection;
+ private Integer page;
+ private Integer count;
+ private Class itemClass;
+
+ private FPSBuilder() {
+ }
+
+ public static FPSBuilder aFPS() {
+ return new FPSBuilder();
+ }
+
+ public FPSBuilder select(List<FPSField> select) {
+ this.select = select;
+ return this;
+ }
+
+ public FPSBuilder from(List<String> from) {
+ this.from = from;
+ return this;
+ }
+
+ public FPSBuilder joins(List<String> joins) {
+ this.joins = joins;
+ return this;
+ }
+
+ public FPSBuilder conditions(List<FPSCondition> conditions) {
+ this.conditions = conditions;
+ return this;
+ }
+
+ public FPSBuilder sortField(String sortField) {
+ this.sortField = sortField;
+ return this;
+ }
+
+ public FPSBuilder sortDirection(SortDirection sortDirection) {
+ this.sortDirection = sortDirection;
+ return this;
+ }
+
+ public FPSBuilder page(Integer page) {
+ this.page = page;
+ return this;
+ }
+
+ public FPSBuilder count(Integer count) {
+ this.count = count;
+ return this;
+ }
+
+ public FPSBuilder itemClass(Class itemClass) {
+ this.itemClass = itemClass;
+ return this;
+ }
+
+
+ public FPS build() {
+ FPS fPS = new FPS();
+ fPS.conditions = this.conditions;
+ fPS.page = this.page;
+ fPS.itemClass = this.itemClass;
+ fPS.count = this.count;
+ fPS.sortDirection = this.sortDirection;
+ fPS.select = this.select;
+ fPS.from = this.from;
+ fPS.joins = this.joins;
+ fPS.sortField = this.sortField;
+ return fPS;
+ }
+ }
+}
Index: src/main/java/com/handson/basic/model/SortDirection.java
===================================================================
diff --git a/src/main/java/com/handson/basic/model/SortDirection.java b/src/main/java/com/handson/basic/model/SortDirection.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/model/SortDirection.java
@@ -0,0 +1,5 @@
+package com.handson.basic.model;
+
+public enum SortDirection {
+ asc,desc
+}
Index: src/main/java/com/handson/basic/util/FPSField.java
===================================================================
diff --git a/src/main/java/com/handson/basic/util/FPSField.java b/src/main/java/com/handson/basic/util/FPSField.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/util/FPSField.java
@@ -0,0 +1,44 @@
+package com.handson.basic.util;
+
+
+public class FPSField {
+ String field;
+ String alias;
+
+ public String getField() {
+ return field;
+ }
+
+ public String getAlias() {
+ return alias;
+ }
+
+ public static final class FPSFieldBuilder {
+ String field;
+ String alias;
+
+ private FPSFieldBuilder() {
+ }
+
+ public static FPSFieldBuilder aFPSField() {
+ return new FPSFieldBuilder();
+ }
+
+ public FPSFieldBuilder field(String field) {
+ this.field = field;
+ return this;
+ }
+
+ public FPSFieldBuilder alias(String alias) {
+ this.alias = alias;
+ return this;
+ }
+
+ public FPSField build() {
+ FPSField fPSField = new FPSField();
+ fPSField.field = this.field;
+ fPSField.alias = this.alias;
+ return fPSField;
+ }
+ }
+}
Index: src/main/java/com/handson/basic/util/FPSCondition.java
===================================================================
diff --git a/src/main/java/com/handson/basic/util/FPSCondition.java b/src/main/java/com/handson/basic/util/FPSCondition.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/util/FPSCondition.java
@@ -0,0 +1,57 @@
+package com.handson.basic.util;
+
+
+public class FPSCondition {
+ private String condition;
+ private String parameterName;
+ private Object value;
+
+ public String getCondition() {
+ return condition;
+ }
+
+ public String getParameterName() {
+ return parameterName;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+
+ public static final class FPSConditionBuilder {
+ private String condition;
+ private String parameterName;
+ private Object value;
+
+ private FPSConditionBuilder() {
+ }
+
+ public static FPSConditionBuilder aFPSCondition() {
+ return new FPSConditionBuilder();
+ }
+
+ public FPSConditionBuilder condition(String condition) {
+ this.condition = condition;
+ return this;
+ }
+
+ public FPSConditionBuilder parameterName(String parameterName) {
+ this.parameterName = parameterName;
+ return this;
+ }
+
+ public FPSConditionBuilder value(Object value) {
+ this.value = value;
+ return this;
+ }
+
+ public FPSCondition build() {
+ FPSCondition fPSCondition = new FPSCondition();
+ fPSCondition.value = this.value;
+ fPSCondition.condition = this.condition;
+ fPSCondition.parameterName = this.parameterName;
+ return fPSCondition;
+ }
+ }
+}
Index: src/main/java/com/handson/basic/model/PaginationAndList.java
===================================================================
diff --git a/src/main/java/com/handson/basic/model/PaginationAndList.java b/src/main/java/com/handson/basic/model/PaginationAndList.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/model/PaginationAndList.java
@@ -0,0 +1,47 @@
+package com.handson.basic.model;
+
+
+import java.util.List;
+import java.util.Objects;
+
+public class PaginationAndList {
+ private Pagination pagination;
+ private List data;
+
+ public PaginationAndList() {
+ }
+
+ public Pagination getPagination() {
+ return this.pagination;
+ }
+
+ public List getData() {
+ return this.data;
+ }
+
+ public static PaginationAndList of(Pagination pagination, List data) {
+ PaginationAndList res = new PaginationAndList();
+ res.pagination = pagination;
+ res.data = data;
+ return res;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else if (o != null && this.getClass() == o.getClass()) {
+ PaginationAndList that = (PaginationAndList)o;
+ return Objects.equals(this.pagination, that.pagination) && Objects.equals(this.data, that.data);
+ } else {
+ return false;
+ }
+ }
+
+ public int hashCode() {
+ return Objects.hash(new Object[]{this.pagination, this.data});
+ }
+
+ public String toString() {
+ return "PaginationAndList{pagination=" + this.pagination + ", data=" + this.data + "}";
+ }
+}
Index: src/main/java/com/handson/basic/model/Pagination.java
===================================================================
diff --git a/src/main/java/com/handson/basic/model/Pagination.java b/src/main/java/com/handson/basic/model/Pagination.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/com/handson/basic/model/Pagination.java
@@ -0,0 +1,52 @@
+package com.handson.basic.model;
+
+
+import java.util.Objects;
+
+public class Pagination {
+ private Integer page;
+ private Integer ofPage;
+ private Integer count;
+
+ public Pagination() {
+ }
+
+ public Integer getPage() {
+ return this.page;
+ }
+
+ public Integer getOfPage() {
+ return this.ofPage;
+ }
+
+ public Integer getCount() {
+ return this.count;
+ }
+
+ public static Pagination of(Integer page, Integer ofPage, Integer count) {
+ Pagination res = new Pagination();
+ res.page = page;
+ res.ofPage = ofPage;
+ res.count = count;
+ return res;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else if (o != null && this.getClass() == o.getClass()) {
+ Pagination that = (Pagination)o;
+ return Objects.equals(this.page, that.page) && Objects.equals(this.ofPage, that.ofPage) && Objects.equals(this.count, that.count);
+ } else {
+ return false;
+ }
+ }
+
+ public int hashCode() {
+ return Objects.hash(new Object[]{this.page, this.ofPage, this.count});
+ }
+
+ public String toString() {
+ return "Pagination{page=" + this.page + ", ofPage=" + this.ofPage + ", count=" + this.count + "}";
+ }
+}