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