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 java.util.stream.Collectors.*; |
37 |
|
|
38 |
|
import info.magnolia.jcr.RuntimeRepositoryException; |
39 |
|
import info.magnolia.jcr.util.NodeTypes; |
40 |
|
|
41 |
|
import java.nio.file.Paths; |
42 |
|
import java.util.ArrayList; |
43 |
|
import java.util.LinkedList; |
44 |
|
import java.util.List; |
45 |
|
import java.util.Map; |
46 |
|
import java.util.function.Function; |
47 |
|
import java.util.regex.Matcher; |
48 |
|
import java.util.regex.Pattern; |
49 |
|
import java.util.stream.Stream; |
50 |
|
|
51 |
|
import javax.jcr.RepositoryException; |
52 |
|
import javax.jcr.Workspace; |
53 |
|
import javax.jcr.nodetype.NodeType; |
54 |
|
import javax.jcr.nodetype.NodeTypeManager; |
55 |
|
import javax.jcr.query.Query; |
56 |
|
import javax.jcr.query.QueryManager; |
57 |
|
|
58 |
|
import org.apache.commons.collections4.CollectionUtils; |
59 |
|
import org.apache.commons.lang3.StringUtils; |
60 |
|
import org.apache.jackrabbit.util.Text; |
61 |
|
import org.slf4j.Logger; |
62 |
|
import org.slf4j.LoggerFactory; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
|
|
| 95.7% |
Uncovered Elements: 6 (141) |
Complexity: 46 |
Complexity Density: 0.51 |
|
67 |
|
public class QueryBuilder { |
68 |
|
|
69 |
|
private static final Logger log = LoggerFactory.getLogger(QueryBuilder.class); |
70 |
|
|
71 |
|
private static final String SELECTOR_NAME = "t"; |
72 |
|
|
73 |
|
private static final String SELECT_TEMPLATE = "SELECT * FROM [nt:base] AS " + SELECTOR_NAME; |
74 |
|
|
75 |
|
private static final String WHERE_TEMPLATE_FOR_PATH = " ISDESCENDANTNODE('%s')"; |
76 |
|
|
77 |
|
private static final String ORDER_BY = " ORDER BY "; |
78 |
|
|
79 |
|
private static final String ASCENDING_KEYWORD = " ASC"; |
80 |
|
|
81 |
|
private static final String JCR_NAME_FUNCTION = "LOWER(NAME(" + SELECTOR_NAME + "))"; |
82 |
|
|
83 |
|
private static final String JCR_NAME = "@name"; |
84 |
|
|
85 |
|
private static final String WHERE_TEMPLATE_FOR_SEARCH = "LOWER(LOCALNAME()) LIKE '%1$s%%'"; |
86 |
|
|
87 |
|
private static final String CONTAINS_TEMPLATE_FOR_SEARCH = "CONTAINS(" + SELECTOR_NAME + ".*, '%1$s')"; |
88 |
|
|
89 |
|
private static final String JCR_IS_SAME_NODE_FUNCTION = "ISSAMENODE(" + SELECTOR_NAME + ", '%1$s')"; |
90 |
|
|
91 |
|
private final Pattern simpleTermsRegexPattern = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); |
92 |
|
|
93 |
|
private final Workspace workspace; |
94 |
|
private String rootPath; |
95 |
|
private List<String> nodeTypes = new ArrayList<>(); |
96 |
|
private String keyword; |
97 |
|
private List<FilteringCondition> filteringConditions = new ArrayList<>(); |
98 |
|
private List<String> propertiesToOrder; |
99 |
|
private long offset; |
100 |
|
private long limit; |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
44 |
private QueryBuilder(Workspace workspace) {... |
103 |
44 |
this.workspace = workspace; |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
44 |
public static QueryBuilder inWorkspace(Workspace workspace) {... |
107 |
44 |
return new QueryBuilder(workspace); |
108 |
|
} |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
110 |
13 |
public QueryBuilder rootPath(String rootPath) {... |
111 |
13 |
this.rootPath = rootPath; |
112 |
13 |
return this; |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
115 |
35 |
public QueryBuilder nodeTypes(List<String> nodeTypes) {... |
116 |
35 |
this.nodeTypes.addAll(nodeTypes); |
117 |
35 |
return this; |
118 |
|
} |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
120 |
19 |
public QueryBuilder keyword(String keyword) {... |
121 |
19 |
this.keyword = keyword; |
122 |
19 |
return this; |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
125 |
23 |
public QueryBuilder conditions(Map<String, List<String>> conditions) {... |
126 |
23 |
List<FilteringCondition> conditionList = conditions.entrySet().stream() |
127 |
|
.map(this::toFilteringConditions) |
128 |
|
.flatMap(Function.identity()) |
129 |
|
.collect(toList()); |
130 |
23 |
this.filteringConditions.addAll(conditionList); |
131 |
23 |
return this; |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
134 |
12 |
private Stream<FilteringCondition> toFilteringConditions(Map.Entry<String, List<String>> entry) {... |
135 |
12 |
String key = entry.getKey(); |
136 |
12 |
return entry.getValue().stream() |
137 |
|
.map(value -> new FilteringCondition(sanitize(key), sanitize(value))); |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
140 |
42 |
private static String sanitize(String text) {... |
141 |
42 |
String trimmedText = text.trim(); |
142 |
42 |
return trimmedText.replaceAll("'", "''"); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
145 |
18 |
public QueryBuilder orderBy(List<String> propertiesToOrder) {... |
146 |
18 |
this.propertiesToOrder = propertiesToOrder; |
147 |
18 |
return this; |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
150 |
14 |
public QueryBuilder offset(long offset) {... |
151 |
14 |
this.offset = offset; |
152 |
14 |
return this; |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
155 |
14 |
public QueryBuilder limit(long limit) {... |
156 |
14 |
this.limit = limit; |
157 |
14 |
return this; |
158 |
|
} |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 5 |
Complexity Density: 0.25 |
|
160 |
44 |
public Query build() {... |
161 |
44 |
StringBuilder statement = new StringBuilder(SELECT_TEMPLATE); |
162 |
44 |
try { |
163 |
|
|
164 |
44 |
List<String> conditionClauses = new ArrayList<>(); |
165 |
44 |
conditionClauses.add(getWhereClauseForNodeTypes()); |
166 |
44 |
conditionClauses.add(getWhereClauseWorkspacePath()); |
167 |
44 |
conditionClauses.add(getWhereClauseForSearch()); |
168 |
44 |
conditionClauses.addAll(getWhereClausesForFiltering()); |
169 |
|
|
170 |
|
|
171 |
44 |
List<String> clauses = conditionClauses.stream() |
172 |
|
.filter(clause -> !clause.isEmpty()) |
173 |
|
.collect(toList()); |
174 |
|
|
175 |
|
|
176 |
44 |
statement.append(clauses.isEmpty() ? "" : " WHERE "); |
177 |
44 |
statement.append(clauses.stream() |
178 |
|
.map(clause -> "(" + clause + ")") |
179 |
|
.collect(joining(" AND "))); |
180 |
44 |
statement.append(getOrderByClause()); |
181 |
|
|
182 |
|
|
183 |
44 |
QueryManager jcrQueryManager = workspace.getQueryManager(); |
184 |
44 |
Query query = jcrQueryManager.createQuery(statement.toString(), Query.JCR_SQL2); |
185 |
|
|
186 |
41 |
if (offset > 0) { |
187 |
2 |
query.setOffset(offset); |
188 |
|
} |
189 |
41 |
if (limit > 0) { |
190 |
14 |
query.setLimit(limit); |
191 |
|
} |
192 |
|
|
193 |
41 |
log.debug("SQL statement is {}", query.getStatement()); |
194 |
41 |
return query; |
195 |
|
|
196 |
|
} catch (RepositoryException e) { |
197 |
3 |
throw new RuntimeRepositoryException(e); |
198 |
|
} |
199 |
|
} |
200 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
201 |
44 |
private String getOrderByClause() {... |
202 |
44 |
if (CollectionUtils.isEmpty(propertiesToOrder)) { |
203 |
26 |
return String.join("", ORDER_BY, JCR_NAME_FUNCTION, ASCENDING_KEYWORD); |
204 |
|
} |
205 |
|
|
206 |
18 |
StringBuilder orderByBuilder = new StringBuilder(ORDER_BY); |
207 |
18 |
for (String propertyToOrder : propertiesToOrder) { |
208 |
19 |
if (propertyToOrder.startsWith(JCR_NAME)) { |
209 |
14 |
orderByBuilder.append(JCR_NAME_FUNCTION).append(StringUtils.substringAfter(propertyToOrder, JCR_NAME)).append(", "); |
210 |
14 |
continue; |
211 |
|
} |
212 |
5 |
orderByBuilder.append(SELECTOR_NAME).append(".[").append(StringUtils.replace(propertyToOrder, " ", "] ")).append(", "); |
213 |
|
} |
214 |
|
|
215 |
18 |
return StringUtils.removeEnd(orderByBuilder.toString(), ", "); |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
218 |
44 |
private String getWhereClauseForNodeTypes() {... |
219 |
44 |
return nodeTypes.stream() |
220 |
|
.map(this::getNodeType) |
221 |
|
.filter(nodeType -> !nodeType.isNodeType(NodeTypes.Folder.NAME)) |
222 |
|
.map(this::getConditionBasedOnNodeType) |
223 |
|
.collect(joining(" OR ")); |
224 |
|
} |
225 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
226 |
38 |
private NodeType getNodeType(String nodeTypeStr) {... |
227 |
38 |
try { |
228 |
38 |
NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager(); |
229 |
38 |
return nodeTypeManager.getNodeType(nodeTypeStr); |
230 |
|
} catch (RepositoryException e) { |
231 |
0 |
throw new RuntimeRepositoryException(e); |
232 |
|
} |
233 |
|
} |
234 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
235 |
37 |
private String getConditionBasedOnNodeType(NodeType nodeType) {... |
236 |
37 |
if (nodeType.isMixin()) { |
237 |
0 |
return String.format("[jcr:mixinTypes] = '%s'", nodeType.getName()); |
238 |
|
} |
239 |
37 |
return String.format("[jcr:primaryType] = '%s'", nodeType.getName()); |
240 |
|
} |
241 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 3 |
Complexity Density: 3 |
|
242 |
44 |
private String getWhereClauseWorkspacePath() {... |
243 |
44 |
return StringUtils.isNotBlank(rootPath) && !"/".equals(rootPath) ? String.format(WHERE_TEMPLATE_FOR_PATH, rootPath) : ""; |
244 |
|
} |
245 |
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 6 |
Complexity Density: 0.5 |
|
246 |
44 |
private String getWhereClauseForSearch() {... |
247 |
44 |
if (StringUtils.isBlank(keyword)) { |
248 |
36 |
return ""; |
249 |
|
} |
250 |
|
|
251 |
8 |
String lowercaseText = keyword.toLowerCase(); |
252 |
8 |
String jcrCharsEscapedText = Text.escapeIllegalJcrChars(lowercaseText); |
253 |
8 |
String singleQuoteEscapedText = sanitize(jcrCharsEscapedText); |
254 |
8 |
String escapedFullTextExpression = escapeFullTextExpression(lowercaseText); |
255 |
|
|
256 |
|
|
257 |
8 |
if (Paths.get(escapedFullTextExpression).isAbsolute()) { |
258 |
1 |
String rootPath = this.rootPath; |
259 |
|
|
260 |
1 |
if (StringUtils.isEmpty(rootPath) || "/".equals(rootPath) || escapedFullTextExpression.startsWith(rootPath)) { |
261 |
1 |
rootPath = ""; |
262 |
|
} |
263 |
|
|
264 |
1 |
return String.format(JCR_IS_SAME_NODE_FUNCTION, rootPath + escapedFullTextExpression); |
265 |
|
} |
266 |
7 |
return String.format(WHERE_TEMPLATE_FOR_SEARCH, singleQuoteEscapedText) + String.format(" OR " + CONTAINS_TEMPLATE_FOR_SEARCH, escapedFullTextExpression); |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
@link |
272 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
273 |
8 |
private String escapeFullTextExpression(String fulltextExpression) {... |
274 |
8 |
List<String> matchList = findSimpleTerms(fulltextExpression); |
275 |
|
|
276 |
8 |
List<String> simpleTerms = new ArrayList<>(); |
277 |
8 |
for (String token : matchList) { |
278 |
8 |
simpleTerms.add(escapeIllegalFullTextSearchChars(token)); |
279 |
|
} |
280 |
|
|
281 |
8 |
if ("\"".equals(fulltextExpression)) { |
282 |
0 |
simpleTerms.add("\\\""); |
283 |
|
} |
284 |
|
|
285 |
8 |
return sanitize(simpleTerms.stream().collect(joining(" "))); |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
@return |
290 |
|
|
291 |
|
@link |
292 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
293 |
8 |
private List<String> findSimpleTerms(String unescapedFullTextExpression) {... |
294 |
8 |
List<String> matchList = new LinkedList<>(); |
295 |
8 |
Matcher regexMatcher = simpleTermsRegexPattern.matcher(unescapedFullTextExpression); |
296 |
16 |
while (regexMatcher.find()) { |
297 |
8 |
matchList.add(regexMatcher.group()); |
298 |
|
} |
299 |
8 |
return matchList; |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
@link |
309 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 8 |
Complexity Density: 1.14 |
|
310 |
8 |
private String escapeIllegalFullTextSearchChars(String simpleTerm) {... |
311 |
8 |
StringBuilder sb = new StringBuilder(simpleTerm.length()); |
312 |
|
|
313 |
95 |
for (int i = 0; i < simpleTerm.length(); i++) { |
314 |
87 |
char ch = simpleTerm.charAt(i); |
315 |
87 |
if (("\\+-".contains(String.valueOf(ch)) && simpleTerm.length() == 1) |
316 |
|
|| ("()[]{}".contains(String.valueOf(ch))) |
317 |
|
|| ("\"".contains(String.valueOf(ch)) && (i != 0 && i != simpleTerm.length() - 1))) { |
318 |
2 |
sb.append('\\'); |
319 |
|
} |
320 |
87 |
sb.append(ch); |
321 |
|
} |
322 |
8 |
return sb.toString(); |
323 |
|
} |
324 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
325 |
44 |
private List<String> getWhereClausesForFiltering() {... |
326 |
44 |
return filteringConditions.stream() |
327 |
|
.map(FilteringCondition::asSqlString) |
328 |
|
.collect(toList()); |
329 |
|
} |
330 |
|
} |