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 |
|
package info.magnolia.rest.delivery.jcr; |
35 |
|
|
36 |
|
import static info.magnolia.rest.delivery.jcr.Operator.*; |
37 |
|
import static java.util.stream.Collectors.joining; |
38 |
|
|
39 |
|
import java.time.LocalDate; |
40 |
|
import java.time.LocalTime; |
41 |
|
import java.time.ZoneId; |
42 |
|
import java.time.ZonedDateTime; |
43 |
|
import java.time.format.DateTimeFormatter; |
44 |
|
import java.time.format.DateTimeParseException; |
45 |
|
import java.util.ArrayList; |
46 |
|
import java.util.Arrays; |
47 |
|
import java.util.List; |
48 |
|
import java.util.stream.Collectors; |
49 |
|
|
50 |
|
import javax.ws.rs.BadRequestException; |
51 |
|
|
52 |
|
import org.apache.commons.lang3.StringUtils; |
53 |
|
import org.apache.commons.lang3.math.NumberUtils; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
|
|
| 97.4% |
Uncovered Elements: 4 (156) |
Complexity: 50 |
Complexity Density: 0.52 |
|
58 |
|
public class FilteringCondition { |
59 |
|
|
60 |
|
private static final String NAME_PROPERTY = "@name"; |
61 |
|
private static final String PARENT_PROPERTY = "@ancestor"; |
62 |
|
|
63 |
|
static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); |
64 |
|
static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
65 |
|
|
66 |
|
private static final String VALUES_SPLITTER = "\\|"; |
67 |
|
|
68 |
|
private static final String OPERATOR_OPEN = "["; |
69 |
|
private static final String OPERATOR_CLOSE = "]"; |
70 |
|
|
71 |
|
private static final String RANGE_SYMBOL = "~"; |
72 |
|
|
73 |
|
private final String INVALID_VALUE_STRING = "Value string is invalid."; |
74 |
|
|
75 |
|
private final String property; |
76 |
|
private final Operator operator; |
77 |
|
private final String[] values; |
78 |
|
|
79 |
|
private boolean isDateQuery = false; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private final List<String[]> rangeValues; |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
87 |
54 |
public FilteringCondition(String propertyString, String valueString) {... |
88 |
54 |
if (StringUtils.isEmpty(propertyString)) { |
89 |
1 |
throw new IllegalArgumentException("Property string is invalid."); |
90 |
|
} |
91 |
|
|
92 |
53 |
operator = extractOperator(propertyString); |
93 |
52 |
property = StringUtils.substringBeforeLast(propertyString, OPERATOR_OPEN); |
94 |
52 |
values = valueString.split(VALUES_SPLITTER); |
95 |
|
|
96 |
52 |
validateValueString(); |
97 |
|
|
98 |
50 |
isDateQuery = isDateQuery(); |
99 |
|
|
100 |
50 |
if (isDateQuery) { |
101 |
16 |
rangeValues = extractDateRangeValues(values); |
102 |
|
} else { |
103 |
34 |
rangeValues = extractRangeValues(values); |
104 |
|
} |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 5 |
Complexity Density: 1.25 |
|
107 |
52 |
private void validateValueString() {... |
108 |
|
|
109 |
52 |
if (Arrays.stream(values) |
110 |
|
.anyMatch(value -> value.contains(RANGE_SYMBOL) |
111 |
|
&& value.split(RANGE_SYMBOL).length != 2)) { |
112 |
1 |
throw new BadRequestException(INVALID_VALUE_STRING); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
51 |
if (Arrays.stream(values) |
117 |
|
.anyMatch(value -> dateMatches(DATE_FORMATTER, value)) |
118 |
|
&& Arrays.stream(values) |
119 |
|
.anyMatch(value -> value.contains(RANGE_SYMBOL))) { |
120 |
1 |
throw new BadRequestException(INVALID_VALUE_STRING); |
121 |
|
} |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
124 |
16 |
private List<String[]> extractDateRangeValues(String[] values) {... |
125 |
16 |
List<String[]> rangeValues = new ArrayList<>(); |
126 |
16 |
for (String value : values) { |
127 |
17 |
if (value.contains(RANGE_SYMBOL)) { |
128 |
10 |
String[] bounds = value.split(RANGE_SYMBOL); |
129 |
10 |
rangeValues.add(bounds); |
130 |
|
} else { |
131 |
7 |
String[] bounds = new String[] { value, value }; |
132 |
7 |
rangeValues.add(bounds); |
133 |
|
} |
134 |
|
} |
135 |
16 |
return rangeValues; |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
138 |
50 |
private boolean isDateQuery() {... |
139 |
|
|
140 |
50 |
if (Arrays.stream(values) |
141 |
|
.allMatch(value -> dateMatches(DATE_FORMATTER, value))) { |
142 |
7 |
return true; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
43 |
for (String value : values) { |
147 |
44 |
if (Arrays.stream(value.split(RANGE_SYMBOL)) |
148 |
|
.anyMatch(bound -> !dateMatches(DATE_FORMATTER, bound))) { |
149 |
34 |
return false; |
150 |
|
} |
151 |
|
} |
152 |
|
|
153 |
9 |
return true; |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
156 |
53 |
private Operator extractOperator(String propertyString) {... |
157 |
53 |
if (!propertyString.contains(OPERATOR_OPEN) && !propertyString.contains(OPERATOR_CLOSE)) { |
158 |
22 |
return Operator.EQ; |
159 |
|
} |
160 |
31 |
return Operator.from(StringUtils.substringBetween(propertyString, OPERATOR_OPEN, OPERATOR_CLOSE)); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
163 |
34 |
private List<String[]> extractRangeValues(String[] values) {... |
164 |
34 |
return Arrays.stream(values) |
165 |
|
.map(value -> value.split(RANGE_SYMBOL)) |
166 |
|
.filter(bounds -> bounds.length == 2) |
167 |
|
.collect(Collectors.toList()); |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
170 |
50 |
public String asSqlString() {... |
171 |
50 |
if (isDateQuery) { |
172 |
16 |
return rangeValues.stream() |
173 |
|
.map(this::getDateConditionClause) |
174 |
|
.collect(joining(" OR ")); |
175 |
|
} |
176 |
34 |
if (operator == IN) { |
177 |
5 |
return rangeValues.stream() |
178 |
|
.map(this::getInConditionClause) |
179 |
|
.collect(joining(" OR ")); |
180 |
|
} |
181 |
29 |
if (operator == NOT_IN) { |
182 |
2 |
return rangeValues.stream() |
183 |
|
.map(this::getNotInConditionClause) |
184 |
|
.collect(joining(" OR ")); |
185 |
|
} |
186 |
27 |
return Arrays.stream(values) |
187 |
|
.map(this::getConditionClause) |
188 |
|
.collect(joining(" OR ")); |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
191 |
6 |
private String getInConditionClause(String[] bounds) {... |
192 |
6 |
return String.format("[%s] >= %s AND [%s] <= %s", property, resolveValue(bounds[0]), property, resolveValue(bounds[1])); |
193 |
|
} |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
195 |
2 |
private String getNotInConditionClause(String[] bounds) {... |
196 |
2 |
return String.format("[%s] < %s OR [%s] > %s", property, resolveValue(bounds[0]), property, resolveValue(bounds[1])); |
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 8 |
Complexity Density: 0.32 |
|
199 |
17 |
private String getDateConditionClause(String[] bounds) {... |
200 |
17 |
switch (operator) { |
201 |
2 |
case GT: |
202 |
2 |
rangeIsNotSupported(bounds); |
203 |
1 |
return String.format("[%s] > %s", property, resolveDateValue(bounds[0], true)); |
204 |
2 |
case LT: |
205 |
2 |
rangeIsNotSupported(bounds); |
206 |
1 |
return String.format("[%s] < %s", property, resolveDateValue(bounds[0], false)); |
207 |
2 |
case GTE: |
208 |
2 |
rangeIsNotSupported(bounds); |
209 |
1 |
return String.format("[%s] >= %s", property, resolveDateValue(bounds[0], false)); |
210 |
2 |
case LTE: |
211 |
2 |
rangeIsNotSupported(bounds); |
212 |
1 |
return String.format("[%s] <= %s", property, resolveDateValue(bounds[0], true)); |
213 |
2 |
case NE: |
214 |
2 |
rangeIsNotSupported(bounds); |
215 |
1 |
return String.format("[%s] < %s OR [%s] > %s", property, resolveDateValue(bounds[0], false), |
216 |
|
property, resolveDateValue(bounds[0], true)); |
217 |
7 |
default: |
218 |
7 |
String lowerBound = resolveDateValue(bounds[0], false); |
219 |
7 |
String upperBound = resolveDateValue(bounds[1], true); |
220 |
|
|
221 |
7 |
if (operator == IN) { |
222 |
3 |
return String.format("[%s] >= %s AND [%s] <= %s", property, lowerBound, property, upperBound); |
223 |
4 |
} else if (operator == NOT_IN) { |
224 |
1 |
return String.format("[%s] < %s OR [%s] > %s", property, lowerBound, property, upperBound); |
225 |
|
} |
226 |
|
|
227 |
3 |
rangeIsNotSupported(bounds); |
228 |
2 |
return String.format("[%s] >= %s AND [%s] <= %s", property, lowerBound, property, upperBound); |
229 |
|
} |
230 |
|
} |
231 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
232 |
13 |
private void rangeIsNotSupported(String[] bounds) {... |
233 |
13 |
if (bounds.length == 2 && !bounds[0].equals(bounds[1])) { |
234 |
6 |
throw new BadRequestException("Range is not supported."); |
235 |
|
} |
236 |
|
} |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
238 |
33 |
private String getConditionClause(String value) {... |
239 |
33 |
if (PARENT_PROPERTY.equalsIgnoreCase(property)) { |
240 |
1 |
return String.format("ISDESCENDANTNODE('%s')", value); |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
32 |
if (NAME_PROPERTY.equalsIgnoreCase(property)) { |
248 |
4 |
return String.format("LOWER(LOCALNAME(t)) LIKE '%s'", value); |
249 |
|
} |
250 |
|
|
251 |
28 |
return String.format("[%s] %s %s", property, operator.asSqlString(), resolveValue(value)); |
252 |
|
} |
253 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
254 |
44 |
private String resolveValue(String value) {... |
255 |
44 |
if (NumberUtils.isNumber(value)) { |
256 |
16 |
return value; |
257 |
|
} |
258 |
|
|
259 |
28 |
if (dateMatches(DATE_TIME_FORMATTER, value)) { |
260 |
10 |
return String.format("CAST('%s' AS DATE)", value); |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
18 |
if (value.startsWith("\"") && value.endsWith("\"")) { |
265 |
6 |
value = value.substring(1, value.length() - 1); |
266 |
|
} |
267 |
|
|
268 |
18 |
return String.format("'%s'", value); |
269 |
|
} |
270 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
271 |
20 |
private String resolveDateValue(String value, boolean upperBound) {... |
272 |
20 |
if (dateMatches(DATE_FORMATTER, value)) { |
273 |
20 |
if (upperBound) { |
274 |
10 |
return String.format("CAST('%s' AS DATE)", getEndOfDay(value)); |
275 |
|
} else { |
276 |
10 |
return String.format("CAST('%s' AS DATE)", getBeginningOfDay(value)); |
277 |
|
} |
278 |
|
} |
279 |
0 |
return String.format("CAST('%s' AS DATE)", value); |
280 |
|
} |
281 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
282 |
213 |
private boolean dateMatches(DateTimeFormatter dateTimeFormatter, String value) {... |
283 |
213 |
try { |
284 |
213 |
dateTimeFormatter.parse(value); |
285 |
65 |
return true; |
286 |
|
} catch (DateTimeParseException e) { |
287 |
148 |
return false; |
288 |
|
} |
289 |
|
} |
290 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
291 |
10 |
private static String getBeginningOfDay(String date) {... |
292 |
10 |
LocalDate localDate = LocalDate.parse(date, DATE_FORMATTER); |
293 |
10 |
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); |
294 |
10 |
return zonedDateTime.format(DATE_TIME_FORMATTER); |
295 |
|
} |
296 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
297 |
10 |
private static String getEndOfDay(String date) {... |
298 |
10 |
LocalDate localDate = LocalDate.parse(date, DATE_FORMATTER); |
299 |
10 |
ZonedDateTime zonedDateTime = localDate.atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()); |
300 |
10 |
return zonedDateTime.format(DATE_TIME_FORMATTER); |
301 |
|
} |
302 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
303 |
0 |
@Override... |
304 |
|
public String toString() { |
305 |
0 |
return asSqlString(); |
306 |
|
} |
307 |
|
} |