forked from plantbreeding/brapi-Java-ProdServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchQueryBuilder.java
More file actions
324 lines (280 loc) · 11.3 KB
/
SearchQueryBuilder.java
File metadata and controls
324 lines (280 loc) · 11.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
package org.brapi.test.BrAPITestServer.service;
import java.math.BigDecimal;
import java.util.*;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import io.swagger.model.GeoJSONSearchArea;
import io.swagger.model.core.SortOrder;
public class SearchQueryBuilder<T> {
private String selectClause;
private String selectOnlyIds;
private String whereClause;
private String defaultSort;
private String sortClause;
private Map<String, Object> params;
private Class<T> clazz;
public SearchQueryBuilder(Class<T> clazz) {
this.selectClause = "SELECT distinct entity FROM " + clazz.getSimpleName() + " entity ";
this.selectOnlyIds = "SELECT distinct entity.id FROM " + clazz.getSimpleName() + " entity ";
this.whereClause = "WHERE 1=1 ";
this.defaultSort = " ORDER BY entity.id ASC ";
this.sortClause = "";
this.params = new HashMap<>();
this.clazz = clazz;
}
public String getQuery() {
if (sortClause.isEmpty()) {
// By default, sort on entity id to have query result remain idempotent
sortClause = defaultSort;
}
return selectClause + whereClause + sortClause;
}
public String getIdQuery() {
if (sortClause.isEmpty()) {
// By default, sort on entity id to have query result remain idempotent
sortClause = defaultSort;
}
return selectOnlyIds + whereClause + sortClause;
}
public Map<String, Object> getParams() {
return params;
}
public Class<T> getClazz() {
return clazz;
}
public SearchQueryBuilder<T> appendList(List<String> list, String columnName) {
String paramName = paramFilter(columnName);
if (list != null && !list.isEmpty()) {
this.whereClause += "AND " + entityPrefix(columnName) + " in :" + paramName + " ";
this.params.put(paramName, list);
}
return this;
}
public SearchQueryBuilder<T> appendIds(List<UUID> ids) {
return appendIds(ids, null);
}
public SearchQueryBuilder<T> appendIds(List<UUID> ids, String inputColumnName) {
String columnName = inputColumnName == null ? "id" : inputColumnName;
String paramName = paramFilter(columnName);
if (ids != null && !ids.isEmpty()) {
this.whereClause += "AND " + entityPrefix(columnName) + " in :" + paramName + " ";
this.params.put(paramName, ids);
}
return this;
}
public SearchQueryBuilder<T> appendIntList(List<Integer> list, String columnName) {
String paramName = paramFilter(columnName);
if (list != null && !list.isEmpty()) {
this.whereClause += "AND " + entityPrefix(columnName) + " in :" + paramName + " ";
this.params.put(paramName, list);
}
return this;
}
public <E extends Enum<E>> SearchQueryBuilder<T> appendEnumList(List<E> list, String columnName) {
String paramName = paramFilter(columnName);
if (list != null && !list.isEmpty()) {
this.whereClause += "AND " + entityPrefix(columnName) + " in :" + paramName + " ";
this.params.put(paramName, list);
}
return this;
}
public SearchQueryBuilder<T> appendSingle(Boolean single, String columnName) {
String paramName = paramFilter(columnName);
if (single != null) {
this.whereClause += "AND " + entityPrefix(columnName) + " = :" + paramName + " ";
this.params.put(paramName, single);
}
return this;
}
public SearchQueryBuilder<T> appendSingle(Integer single, String columnName) {
String paramName = paramFilter(columnName);
if (single != null) {
this.whereClause += "AND " + entityPrefix(columnName) + " = :" + paramName + " ";
this.params.put(paramName, single);
}
return this;
}
public SearchQueryBuilder<T> appendSingle(String single, String columnName) {
String paramName = paramFilter(columnName);
if (single != null && !single.isEmpty()) {
this.whereClause += "AND " + entityPrefix(columnName) + " = :" + paramName + " ";
this.params.put(paramName, single);
}
return this;
}
public SearchQueryBuilder<T> appendSingle(UUID single, String columnName) {
String paramName = paramFilter(columnName);
if (single != null) {
this.whereClause += "AND " + entityPrefix(columnName) + " = :" + paramName + " ";
this.params.put(paramName, single);
}
return this;
}
public <E extends Enum<E>> SearchQueryBuilder<T> appendEnum(E enumVal, String columnName) {
String paramName = paramFilter(columnName);
if (enumVal != null) {
this.whereClause += "AND " + entityPrefix(columnName) + " = :" + paramName + " ";
this.params.put(paramName, enumVal);
}
return this;
}
public SearchQueryBuilder<T> appendDateRange(LocalDate start, LocalDate end, String columnName) {
return appendDateRange(DateUtility.toDate(start), DateUtility.toDate(end), columnName);
}
public SearchQueryBuilder<T> appendDateRange(OffsetDateTime start, OffsetDateTime end, String columnName) {
return appendDateRange(DateUtility.toDate(start), DateUtility.toDate(end), columnName);
}
public SearchQueryBuilder<T> appendDateRange(Date start, Date end, String columnName) {
String paramNameStart = paramFilter(columnName) + "Start";
String paramNameEnd = paramFilter(columnName) + "End";
if (start != null && end != null) {
whereClause += "AND " + entityPrefix(columnName) + " BETWEEN :" + paramNameStart + " AND :" + paramNameEnd
+ " ";
params.put(paramNameStart, start);
params.put(paramNameEnd, end);
} else if (start != null) {
whereClause += "AND " + entityPrefix(columnName) + " >= :" + paramNameStart + " ";
params.put(paramNameStart, start);
} else if (end != null) {
whereClause += "AND " + entityPrefix(columnName) + " <= :" + paramNameEnd + " ";
params.put(paramNameEnd, end);
}
return this;
}
public SearchQueryBuilder<T> appendNumberRange(Integer min, Integer max, String columnName) {
String paramNameMin = paramFilter(columnName) + "Min";
String paramNameMax = paramFilter(columnName) + "Max";
if (min != null && max != null) {
whereClause += "AND " + entityPrefix(columnName) + " BETWEEN :" + paramNameMin + " AND :" + paramNameMax
+ " ";
params.put(paramNameMin, min);
params.put(paramNameMax, max);
} else if (min != null) {
whereClause += "AND " + entityPrefix(columnName) + " >= :" + paramNameMin + " ";
params.put(paramNameMin, min);
} else if (max != null) {
whereClause += "AND " + entityPrefix(columnName) + " <= :" + paramNameMax + " ";
params.put(paramNameMax, max);
}
return this;
}
public SearchQueryBuilder<T> appendNumberRange(BigDecimal min, BigDecimal max, String columnName) {
String paramNameMin = paramFilter(columnName) + "Min";
String paramNameMax = paramFilter(columnName) + "Max";
if (min != null && max != null) {
whereClause += "AND " + entityPrefix(columnName) + " BETWEEN :" + paramNameMin + " AND :" + paramNameMax
+ " ";
params.put(paramNameMin, min);
params.put(paramNameMax, max);
} else if (min != null) {
whereClause += "AND " + entityPrefix(columnName) + " >= :" + paramNameMin + " ";
params.put(paramNameMin, min);
} else if (max != null) {
whereClause += "AND " + entityPrefix(columnName) + " <= :" + paramNameMax + " ";
params.put(paramNameMax, max);
}
return this;
}
public SearchQueryBuilder<T> appendNamesList(List<String> list, String columnFirst, String columnMiddle,
String columnLast) {
if (list != null && !list.isEmpty()) {
this.params.put("namesList", list);
this.whereClause += "AND (" + entityPrefix(columnFirst) + " in :namesList ";
this.whereClause += "OR " + entityPrefix(columnMiddle) + " in :namesList ";
this.whereClause += "OR " + entityPrefix(columnLast) + " in :namesList ";
this.whereClause += "OR concat(" + entityPrefix(columnFirst) + ", ' ', " + entityPrefix(columnMiddle)
+ ") in :namesList ";
this.whereClause += "OR concat(" + entityPrefix(columnFirst) + ", ' ', " + entityPrefix(columnLast)
+ ") in :namesList ";
this.whereClause += "OR concat(" + entityPrefix(columnMiddle) + ", ' ', " + entityPrefix(columnLast)
+ ") in :namesList ";
this.whereClause += "OR concat(" + entityPrefix(columnFirst) + ", ' ', " + entityPrefix(columnMiddle)
+ ", ' ', " + entityPrefix(columnLast) + ") in :namesList ";
this.whereClause += ") ";
}
return this;
}
public SearchQueryBuilder<T> appendGeoJSONArea(GeoJSONSearchArea area) {
// if (single != null && !single.isEmpty()) {
// this.query += "AND " + entityPrefix(columnName) + " = :" + columnName + " ";
// this.params.put(columnName, single);
// }
return this;
}
public SearchQueryBuilder<T> withExRefs(String externalReferenceID, String externalReferenceSource) {
List<String> exRefIds = new ArrayList<>();
List<String> exRefSources = new ArrayList<>();
if (externalReferenceID != null)
exRefIds.add(externalReferenceID);
if (externalReferenceSource != null)
exRefSources.add(externalReferenceSource);
return withExRefs(exRefIds, exRefSources);
}
public SearchQueryBuilder<T> withExRefs(List<String> exRefIds, List<String> exRefSources) {
if ((exRefIds != null && !exRefIds.isEmpty()) || (exRefSources != null && !exRefSources.isEmpty())) {
this.join("externalReferences", "externalReference");
}
if (exRefIds != null && !exRefIds.isEmpty()) {
this.whereClause += "AND externalReference.externalReferenceId in :externalReferenceId ";
this.params.put("externalReferenceId", exRefIds);
}
if (exRefSources != null && !exRefSources.isEmpty()) {
this.whereClause += "AND externalReference.externalReferenceSource in :externalReferenceSource ";
this.params.put("externalReferenceSource", exRefSources);
}
return this;
}
public SearchQueryBuilder<T> join(String join, String name) {
this.selectClause += "JOIN " + entityPrefix(join) + " " + paramFilter(name) + " ";
this.selectOnlyIds += "JOIN " + entityPrefix(join) + " " + paramFilter(name) + " ";
return this;
}
public SearchQueryBuilder<T> leftJoinFetch(String join, String name) {
this.selectClause += generateLeftJoinFetch(join, name);
return this;
}
/**
* Use this method to remove left join fetches from specific collection attributes so you can leverage the same query to
* iterate through other lazily loaded collections on an entity you need to fetch.
*/
public SearchQueryBuilder<T> removeAndReplaceLeftJoinFetch(String join,
String name,
String existingJoin,
String existingName) {
this.selectClause =
this.selectClause.replace(generateLeftJoinFetch(existingJoin, existingName), generateLeftJoinFetch(join, name));
return this;
}
/**
* Use this method to remove left join fetches from specific collection attributes so you can leverage the same
* base query criteria to add another join fetch with leftJoinFetch()
*/
public SearchQueryBuilder<T> removeLeftJoinFetch(String join, String name) {
this.selectClause =
this.selectClause.replace(generateLeftJoinFetch(join, name), "");
return this;
}
private String generateLeftJoinFetch(String join, String paramName) {
return "LEFT JOIN FETCH " + entityPrefix(join) + " " + paramFilter(paramName) + " ";
}
private String entityPrefix(String field) {
if (field.startsWith("*")) {
return field.substring(1);
} else {
return "entity." + field;
}
}
private String paramFilter(String param) {
if (param == null)
return "";
return param.replace('.', '_').replace('*', '_');
}
public SearchQueryBuilder<T> withSort(String sortByStr, SortOrder sortOrder) {
String sortOrderStr = "ASC";
if (sortOrder != null) {
sortOrderStr = sortOrder.toString();
}
this.sortClause += " ORDER BY " + entityPrefix(sortByStr) + " " + sortOrderStr;
return this;
}
}