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.cms.util;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.jcr.util.NodeUtil;
40
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Calendar;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashSet;
47 import java.util.Iterator;
48 import java.util.Set;
49 import java.util.TimeZone;
50
51 import javax.jcr.LoginException;
52 import javax.jcr.NodeIterator;
53 import javax.jcr.RepositoryException;
54 import javax.jcr.Session;
55 import javax.jcr.query.InvalidQueryException;
56 import javax.jcr.query.Query;
57 import javax.jcr.query.QueryManager;
58 import javax.jcr.query.QueryResult;
59 import javax.jcr.query.qom.QueryObjectModel;
60
61 import org.apache.commons.lang3.StringUtils;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65
66
67
68 public class QueryUtil {
69
70 private static Logger log = LoggerFactory.getLogger(QueryUtil.class);
71
72
73
74
75
76
77 public static Collection<Content> query(String repository, String statement) {
78 return query(repository, statement, "sql");
79 }
80
81
82
83
84
85
86 public static Collection<Content> query(String repository, String statement, String language) {
87 return query(repository, statement, language, ItemType.NT_BASE);
88 }
89
90
91
92
93 public static Collection<Content> exceptionThrowingQuery(String repository, String statement, String language, String returnItemType) throws RepositoryException {
94 return exceptionThrowingQuery(repository, statement, language, returnItemType, Long.MAX_VALUE);
95 }
96
97
98
99
100
101
102 public static Collection<Content> exceptionThrowingQuery(String repository, String statement, String language, String returnItemType,
103 long maxResultSize) throws RepositoryException {
104 Collection<Content> results = new ArrayList<Content>();
105 if (maxResultSize <= 0) {
106 maxResultSize = Long.MAX_VALUE;
107 }
108 NodeIterator iterator = search(repository, statement, language, returnItemType);
109
110 long count = 1;
111 while (iterator.hasNext() && count <= maxResultSize) {
112 results.add(ContentUtil.getContent(repository, iterator.nextNode().getPath()));
113 count++;
114 }
115 return results;
116 }
117
118
119
120
121 public static Collection<Content> query(String repository, String statement, String language, String returnItemType) {
122 return query(repository, statement, language, returnItemType, Long.MAX_VALUE);
123 }
124
125
126
127
128
129
130
131 @SuppressWarnings("unchecked")
132
133 public static Collection<Content> query(String repository, String statement, String language, String returnItemType, long maxResultSize) {
134 try {
135 return exceptionThrowingQuery(repository, statement, language, returnItemType, maxResultSize);
136 } catch (Exception e) {
137 log.error("can't execute query [{}], will return empty collection", statement, e);
138 }
139 return Collections.EMPTY_LIST;
140 }
141
142
143
144
145
146 public static String createDateExpression(int year, int month, int day) {
147 Calendar cal = Calendar.getInstance();
148 cal.set(year, month - 1, day);
149 return createDateExpression(cal);
150 }
151
152
153
154
155
156
157 public static String createDateExpression(Calendar calendar) {
158 return DateUtil.createDateExpression(calendar);
159 }
160
161
162
163
164
165 public static String createDateTimeExpression(int year, int month, int day, int hour, int minutes, int seconds) {
166 Calendar cal = Calendar.getInstance();
167 cal.set(year, month - 1, day, hour, minutes, seconds);
168 return createDateTimeExpression(cal);
169 }
170
171
172
173
174
175
176 public static String createDateTimeExpression(Calendar calendar) {
177 return DateUtil.createDateTimeExpression(calendar);
178 }
179
180
181
182
183
184 public static String createDateTimeExpressionIgnoreTimeZone(int year, int month, int day, int hour, int minutes, int seconds) {
185 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
186 cal.set(year, month - 1, day, hour, minutes, seconds);
187 return createDateTimeExpression(cal);
188 }
189
190
191
192
193
194
195 public static String createDateTimeExpressionIgnoreTimeZone(Calendar calendar) {
196 return DateUtil.createDateTimeExpressionIgnoreTimeZone(calendar);
197 }
198
199
200
201
202
203
204
205
206
207 public static NodeIterator search(QueryObjectModel model, String returnItemType) throws InvalidQueryException, RepositoryException {
208 return NodeUtil.filterDuplicates(NodeUtil.filterParentNodeType(model.execute().getNodes(), returnItemType));
209 }
210
211
212
213
214
215
216
217
218
219 public static NodeIterator search(String workspace, String statement, String language) throws InvalidQueryException, RepositoryException {
220 Session session = MgnlContext.getJCRSession(workspace);
221 QueryManager manager = session.getWorkspace().getQueryManager();
222 Query query = manager.createQuery(statement, language);
223
224 return NodeUtil.filterDuplicates(query.execute().getNodes());
225 }
226
227
228
229
230
231
232
233
234
235
236 public static NodeIterator search(String workspace, String statement) throws InvalidQueryException, RepositoryException {
237 return search(workspace, statement, javax.jcr.query.Query.JCR_SQL2);
238 }
239
240
241
242
243
244
245
246
247
248 public static NodeIterator search(String workspace, String statement, String language, String returnItemType) throws LoginException, RepositoryException {
249 return search(workspace, statement, language, returnItemType, false);
250 }
251
252
253
254
255
256
257
258
259
260
261
262 public static NodeIterator search(String workspace, String statement, String language, String returnItemType, boolean isSelector) throws RepositoryException {
263 Session session = MgnlContext.getJCRSession(workspace);
264 QueryManager manager = session.getWorkspace().getQueryManager();
265 Query query = manager.createQuery(statement, language);
266 QueryResult result = query.execute();
267
268 if (isSelector) {
269 return NodeUtil.filterDuplicates(NodeUtil.filterParentNodeType(result.getRows(), returnItemType));
270 }
271 return NodeUtil.filterDuplicates(NodeUtil.filterParentNodeType(result.getNodes(), returnItemType));
272 }
273
274
275
276
277 public static String buildQuery(String statement, String startPath) {
278 Set<String> arguments = new HashSet<String>(Arrays.asList(StringUtils.splitByWholeSeparator(statement, ",")));
279
280 Iterator<String> argIt = arguments.iterator();
281 String queryString = "select * from [nt:base] as t where ISDESCENDANTNODE([" + startPath + "])";
282 while (argIt.hasNext()) {
283 queryString = queryString + " AND contains(t.*, '" + argIt.next() + "')";
284 }
285 log.debug("query string: {}", queryString);
286 return queryString;
287 }
288 }