1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.services.fieldquery;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.jdom2.Document;
29 import org.jdom2.Element;
30 import org.mycore.parsers.bool.MCRCondition;
31
32
33 public class MCRQuery {
34
35 private static final Logger LOGGER = LogManager.getLogger(MCRQuery.class);
36
37
38 private MCRCondition<Void> cond;
39
40
41 private int maxResults = 0;
42
43
44 private int numPerPage = 10;
45
46
47 private List<MCRSortBy> sortBy = new ArrayList<>();
48
49
50 private List<String> returnFields = new ArrayList<>();
51
52
53 private Document doc = null;
54
55
56
57
58
59
60
61 public MCRQuery(MCRCondition<Void> cond) {
62 this.cond = MCRQueryParser.normalizeCondition(cond);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public MCRQuery(MCRCondition<Void> cond, List<MCRSortBy> sortBy, int maxResults, List<String> returnFields) {
79 this.cond = MCRQueryParser.normalizeCondition(cond);
80 this.setSortBy(sortBy);
81 setMaxResults(maxResults);
82 this.setReturnFields(returnFields);
83 }
84
85
86
87
88
89
90 public MCRCondition<Void> getCondition() {
91 return cond;
92 }
93
94
95
96
97
98
99 public int getMaxResults() {
100 return maxResults;
101 }
102
103
104
105
106
107
108
109
110 public void setMaxResults(int maxResults) {
111 if (maxResults < 0) {
112 this.maxResults = 0;
113 } else {
114 this.maxResults = maxResults;
115 }
116 doc = null;
117 }
118
119
120
121
122
123
124
125
126 public void setMaxResults(String maxResultsString) {
127 doc = null;
128 if (maxResultsString == null || maxResultsString.length() == 0) {
129 this.maxResults = 0;
130 return;
131 }
132 try {
133 this.maxResults = Integer.parseInt(maxResultsString);
134 } catch (NumberFormatException e) {
135 LOGGER.warn("The Results maxstring " + maxResultsString + " contains not an integer, 0 as default is set");
136 this.maxResults = 0;
137 }
138 }
139
140
141
142
143
144
145 public int getNumPerPage() {
146 return numPerPage;
147 }
148
149
150
151
152
153
154
155 public void setNumPerPage(int numPerPage) {
156 if (numPerPage < 0) {
157 this.numPerPage = 10;
158 } else {
159 this.numPerPage = numPerPage;
160 }
161 doc = null;
162 }
163
164
165
166
167
168
169
170 public void setNumPerPage(String numPerPageString) {
171 doc = null;
172 if (numPerPageString == null || numPerPageString.length() == 0) {
173 this.numPerPage = 10;
174 return;
175 }
176 try {
177 this.numPerPage = Integer.parseInt(numPerPageString);
178 } catch (NumberFormatException e) {
179 LOGGER.warn("The numPerPage string " + numPerPageString + " contains not an integer, 10 as default is set");
180 this.numPerPage = 10;
181 }
182 }
183
184
185
186
187
188
189 public List<MCRSortBy> getSortBy() {
190 return sortBy;
191 }
192
193
194
195
196
197
198
199 public void setSortBy(List<MCRSortBy> sortBy) {
200 if (sortBy == null) {
201 sortBy = new ArrayList<>();
202 }
203 this.sortBy = sortBy;
204 doc = null;
205 }
206
207
208
209
210
211
212
213 public void setSortBy(MCRSortBy sortBy) {
214 this.sortBy = new ArrayList<>();
215 if (sortBy != null) {
216 this.sortBy.add(sortBy);
217 }
218 doc = null;
219 }
220
221
222
223
224
225
226
227 public List<String> getReturnFields() {
228 return returnFields;
229 }
230
231
232
233
234
235
236
237 public String getReturnFieldsAsString() {
238 return returnFields.stream().collect(Collectors.joining(","));
239 }
240
241
242
243
244
245
246
247 public void setReturnFields(List<String> returnFields) {
248 this.returnFields = returnFields == null ? new ArrayList<>() : returnFields;
249 }
250
251
252
253
254
255
256
257 public void setReturnFields(String returnFields) {
258 if (returnFields == null || returnFields.length() == 0) {
259 this.returnFields = new ArrayList<>();
260 return;
261 }
262 this.returnFields = Arrays.asList(returnFields.split(","));
263 }
264
265
266
267
268
269
270 public synchronized Document buildXML() {
271 if (doc == null) {
272 Element query = new Element("query");
273 query.setAttribute("maxResults", String.valueOf(maxResults));
274 query.setAttribute("numPerPage", String.valueOf(numPerPage));
275
276 if (sortBy != null && sortBy.size() > 0) {
277 Element sortByElem = new Element("sortBy");
278 query.addContent(sortByElem);
279 for (MCRSortBy sb : sortBy) {
280 Element ref = new Element("field");
281 ref.setAttribute("name", sb.getFieldName());
282 ref.setAttribute("order", sb.getSortOrder() ? "ascending" : "descending");
283 sortByElem.addContent(ref);
284 }
285 }
286
287 if (returnFields != null && returnFields.size() > 0) {
288 Element returns = new Element("returnFields");
289 returns.setText(returnFields.stream().collect(Collectors.joining(",")));
290 query.addContent(returns);
291 }
292
293 Element conditions = new Element("conditions");
294 query.addContent(conditions);
295 conditions.setAttribute("format", "xml");
296 conditions.addContent(cond.toXML());
297 doc = new Document(query);
298 }
299 return doc;
300 }
301
302
303
304
305
306
307
308
309 public static MCRQuery parseXML(Document doc) {
310 Element xml = doc.getRootElement();
311 Element conditions = xml.getChild("conditions");
312 MCRQuery query = null;
313 if (conditions.getAttributeValue("format", "xml").equals("xml")) {
314 Element condElem = conditions.getChildren().get(0);
315 query = new MCRQuery(new MCRQueryParser().parse(condElem));
316 } else {
317 String queryString = conditions.getTextTrim();
318 query = new MCRQuery(new MCRQueryParser().parse(queryString));
319 }
320
321 String max = xml.getAttributeValue("maxResults", "");
322 query.setMaxResults(max);
323 String num = xml.getAttributeValue("numPerPage", "");
324 query.setNumPerPage(num);
325
326 List<MCRSortBy> sortBy = null;
327 Element sortByElem = xml.getChild("sortBy");
328
329 if (sortByElem != null) {
330 List<Element> children = sortByElem.getChildren();
331 sortBy = new ArrayList<>(children.size());
332
333 for (Element sortByChild : children) {
334 String name = sortByChild.getAttributeValue("name");
335 String ad = sortByChild.getAttributeValue("order");
336
337 boolean direction = "ascending".equals(ad) ? MCRSortBy.ASCENDING : MCRSortBy.DESCENDING;
338 sortBy.add(new MCRSortBy(name, direction));
339 }
340 }
341
342 if (sortBy != null) {
343 query.setSortBy(sortBy);
344 }
345
346 Element returns = xml.getChild("returnFields");
347 if (returns != null) {
348 query.setReturnFields(returns.getText());
349 }
350
351 return query;
352 }
353 }