1
|
package eu.dnetlib.data.search.web.api;
|
2
|
|
3
|
import eu.dnetlib.data.search.utils.vocabulary.VocabularyManager;
|
4
|
import eu.dnetlib.domain.enabling.Vocabulary;
|
5
|
import org.apache.log4j.Logger;
|
6
|
|
7
|
import javax.servlet.http.HttpServletRequest;
|
8
|
import java.text.ParseException;
|
9
|
import java.text.SimpleDateFormat;
|
10
|
import java.util.Date;
|
11
|
import java.util.Locale;
|
12
|
|
13
|
/**
|
14
|
* Created by kiatrop on 10/7/2014.
|
15
|
*/
|
16
|
public class QueryEnhancer {
|
17
|
|
18
|
private static final Logger logger = Logger.getLogger(QueryEnhancer.class);
|
19
|
|
20
|
/**
|
21
|
* Enhance the given CQL query with FP7 specific index fields
|
22
|
* @param queryBuilder
|
23
|
* @param request
|
24
|
*/
|
25
|
public static void enhanceQueryWithFundingLevelParams(StringBuilder queryBuilder, HttpServletRequest request, VocabularyManager vocabularyManager, boolean isModelSygma) {
|
26
|
logger.debug("enhance funding " + isModelSygma);
|
27
|
|
28
|
String funder = request.getParameter("funder");
|
29
|
String fundingStream = request.getParameter("fundingStream");
|
30
|
String FP7scientificArea = request.getParameter("FP7scientificArea");
|
31
|
|
32
|
if(isModelSygma) {
|
33
|
if (funder != null) {
|
34
|
if(!funder.toUpperCase().equals("WT")) {
|
35
|
Vocabulary sygmaFundersVocabulary = vocabularyManager.getVocabulary("sygma_funders", Locale.ROOT);
|
36
|
addExactQueryTerm("relfundinglevel0_id", devocabularizedTerm(funder.toUpperCase(), sygmaFundersVocabulary), queryBuilder);
|
37
|
} else {
|
38
|
Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT);
|
39
|
addExactQueryTerm("relfunderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder);
|
40
|
}
|
41
|
}
|
42
|
|
43
|
if (fundingStream != null && !fundingStream.trim().isEmpty()) {
|
44
|
Vocabulary relfundinglevel1Vocabulary = vocabularyManager.getVocabulary("programmes_simple", Locale.ROOT);
|
45
|
queryBuilder.append(" and (relfundinglevel1_id exact \"").append(devocabularizedTerm(fundingStream.toUpperCase(), relfundinglevel1Vocabulary)).append("\")");
|
46
|
}
|
47
|
|
48
|
if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) {
|
49
|
Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT);
|
50
|
queryBuilder.append(" and (relfundinglevel2_id exact \"").append(devocabularizedTerm(FP7scientificArea, relfundinglevel2Vocabulary)).append("\")");
|
51
|
}
|
52
|
|
53
|
} else {
|
54
|
|
55
|
if (funder != null) {
|
56
|
Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT);
|
57
|
addExactQueryTerm("relfunderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder);
|
58
|
}
|
59
|
|
60
|
if (fundingStream != null && !fundingStream.trim().isEmpty()) {
|
61
|
addORQueryTerm(new String[]{"relfundinglevel0_name", "relfundinglevel1_name", "relfundinglevel2_name"}, fundingStream.toUpperCase(), queryBuilder);
|
62
|
}
|
63
|
|
64
|
if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) {
|
65
|
Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT);
|
66
|
queryBuilder.append(" and (relfundinglevel2_id exact \"").append(devocabularizedTerm(FP7scientificArea, relfundinglevel2Vocabulary)).append("\")");
|
67
|
}
|
68
|
}
|
69
|
|
70
|
}
|
71
|
|
72
|
public static void enhanceProjectQueryWithFundingLevelParams(StringBuilder queryBuilder, HttpServletRequest request, VocabularyManager vocabularyManager) {
|
73
|
String funder = request.getParameter("funder");
|
74
|
String fundingStream = request.getParameter("fundingStream");
|
75
|
String FP7scientificArea = request.getParameter("FP7scientificArea");
|
76
|
|
77
|
if (funder != null) {
|
78
|
//addExactQueryTerm("fundinglevel0_name", funder.toUpperCase(), queryBuilder);
|
79
|
Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT);
|
80
|
addExactQueryTerm("funderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder);
|
81
|
}
|
82
|
|
83
|
if (fundingStream != null && !fundingStream.trim().isEmpty()) {
|
84
|
//addExactQueryTerm("fundinglevel1_name", fundingStream.toUpperCase(), queryBuilder);
|
85
|
addORQueryTerm(new String[]{"fundinglevel0_name", "fundinglevel1_name", "fundinglevel2_name"}, fundingStream.toUpperCase(), queryBuilder);
|
86
|
}
|
87
|
|
88
|
if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) {
|
89
|
Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT);
|
90
|
queryBuilder.append(" and (fundinglevel2_id exact \"").append(devocabularizedTerm(FP7scientificArea, relfundinglevel2Vocabulary)).append("\")");
|
91
|
}
|
92
|
}
|
93
|
|
94
|
public static void enhanceQueryWithFundingParams(StringBuilder queryBuilder, HttpServletRequest request) {
|
95
|
String hasECFunding = request.getParameter("hasECFunding");
|
96
|
String hasWTFunding = request.getParameter("hasWTFunding");
|
97
|
|
98
|
addBooleanQueryTerm("contextid", hasECFunding, "EC", queryBuilder);
|
99
|
addBooleanQueryTerm("contextid", hasWTFunding, "WT", queryBuilder);
|
100
|
}
|
101
|
|
102
|
public static void enhanceQueryWithProjectFundingParams(StringBuilder queryBuilder, HttpServletRequest request) {
|
103
|
String hasECFunding = request.getParameter("hasECFunding");
|
104
|
String hasWTFunding = request.getParameter("hasWTFunding");
|
105
|
|
106
|
addBooleanQueryTerm("funderid", hasECFunding, "ec__________::EC", queryBuilder);
|
107
|
addBooleanQueryTerm("funderid", hasWTFunding, "wt__________::WT", queryBuilder);
|
108
|
|
109
|
}
|
110
|
|
111
|
public static void enhanceQueryWithRelProjectParams(StringBuilder queryBuilder, HttpServletRequest request) {
|
112
|
String hasProject = request.getParameter("hasProject");
|
113
|
String projectID = request.getParameter("projectID");
|
114
|
String FP7ProjectID = request.getParameter("FP7ProjectID");
|
115
|
|
116
|
if (hasProject != null && !hasProject.isEmpty()) {
|
117
|
if (hasProject.equals("true")) {
|
118
|
addEqualQueryTerm("relprojectid", "*", queryBuilder);
|
119
|
} else {
|
120
|
addNotEqualQueryTerm("relprojectid", "*", queryBuilder);
|
121
|
}
|
122
|
}
|
123
|
|
124
|
if (FP7ProjectID != null && !FP7ProjectID.trim().isEmpty()) {
|
125
|
addExactQueryTerm("relprojectcode", FP7ProjectID, queryBuilder);
|
126
|
addExactQueryTerm(" relfundinglevel0_id", "ec__________::EC::FP7", queryBuilder);
|
127
|
}
|
128
|
|
129
|
if (projectID != null && !projectID.trim().isEmpty()) {
|
130
|
queryBuilder.append(" and (relprojectcode exact \"").append(projectID).append("\")");
|
131
|
}
|
132
|
}
|
133
|
|
134
|
public static void enhanceQueryWithAccessRights(StringBuilder queryBuilder, HttpServletRequest request) {
|
135
|
String oa = request.getParameter("OA");
|
136
|
addBooleanQueryTerm("resultbestlicense", oa, "Open Access", queryBuilder);
|
137
|
}
|
138
|
|
139
|
public static void enhanceQueryWithDate(StringBuilder queryBuilder, HttpServletRequest request) throws IllegalArgumentException{
|
140
|
String fromDateAccepted = request.getParameter("fromDateAccepted");
|
141
|
String toDateAccepted = request.getParameter("toDateAccepted");
|
142
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
143
|
simpleDateFormat.setLenient(false);
|
144
|
|
145
|
if (toDateAccepted != null && !toDateAccepted.isEmpty() && (fromDateAccepted == null || fromDateAccepted.isEmpty()) ) {
|
146
|
if(!checkDate(toDateAccepted, simpleDateFormat)){
|
147
|
//logger.debug("Format is " + !checkDate(toDateAccepted, simpleDateFormat));
|
148
|
throw new IllegalArgumentException("toDateAccepted date must be formatted as YYYY-MM-DD");
|
149
|
}
|
150
|
fromDateAccepted = "*";
|
151
|
}
|
152
|
|
153
|
if (fromDateAccepted != null && !fromDateAccepted.isEmpty() && (toDateAccepted == null || toDateAccepted.isEmpty()) ) {
|
154
|
if (!checkDate(fromDateAccepted, simpleDateFormat)){
|
155
|
//logger.debug("Format from is " + fromDateAccepted != null && !fromDateAccepted.isEmpty());
|
156
|
throw new IllegalArgumentException("fromDateAccepted date must be formatted as YYYY-MM-DD");
|
157
|
}
|
158
|
toDateAccepted = simpleDateFormat.format(new Date());
|
159
|
}
|
160
|
|
161
|
if (toDateAccepted != null && !toDateAccepted.isEmpty() && fromDateAccepted != null && !fromDateAccepted.isEmpty()) {
|
162
|
queryBuilder.append(" and (resultdateofacceptance within \"").append(fromDateAccepted).append(" ").append(toDateAccepted).append("\")");
|
163
|
}
|
164
|
}
|
165
|
|
166
|
|
167
|
public static void enhanceQueryWithYearParams(StringBuilder queryBuilder, HttpServletRequest request) {
|
168
|
String startYear = request.getParameter("startYear");
|
169
|
String endYear = request.getParameter("endYear");
|
170
|
|
171
|
int sYear = -1;
|
172
|
if (startYear != null && !startYear.isEmpty()) {
|
173
|
try {
|
174
|
sYear = Integer.parseInt(startYear);
|
175
|
|
176
|
} catch(NumberFormatException e) {
|
177
|
throw new IllegalArgumentException("startYear parameter must be a numeric value.");
|
178
|
}
|
179
|
}
|
180
|
|
181
|
int eYear = -1;
|
182
|
if (endYear !=null && !endYear.isEmpty()) {
|
183
|
try {
|
184
|
eYear = Integer.parseInt(endYear);
|
185
|
|
186
|
} catch(NumberFormatException e) {
|
187
|
throw new IllegalArgumentException("endYear parameter must be a numeric value.");
|
188
|
}
|
189
|
}
|
190
|
|
191
|
if (eYear != -1 && sYear != -1) {
|
192
|
if (eYear < sYear) {
|
193
|
throw new IllegalArgumentException("endYear must be greater than startYear.");
|
194
|
}
|
195
|
}
|
196
|
|
197
|
addExactQueryTerm("projectstartyear", startYear, queryBuilder);
|
198
|
addExactQueryTerm("projectendyear", endYear, queryBuilder);
|
199
|
}
|
200
|
|
201
|
public static void enhanceQueryWithResultsSortParameters(StringBuilder queryBuilder, HttpServletRequest request) {
|
202
|
String sortByParameter = request.getParameter("sortBy");
|
203
|
|
204
|
if (sortByParameter != null) {
|
205
|
String[] sortParams = sortByParameter.split(",");
|
206
|
|
207
|
if (sortParams.length != 2) {
|
208
|
throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,ascending|,descending].");
|
209
|
}
|
210
|
|
211
|
String sortByField = sortParams[0];
|
212
|
String order = sortParams[1];
|
213
|
|
214
|
if (!checkPublicationSortParameterFields(sortByField)){
|
215
|
throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field.");
|
216
|
}
|
217
|
|
218
|
if (!checkOrder(order)) {
|
219
|
throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}");
|
220
|
}
|
221
|
|
222
|
addSortParameter(sortByField, order, queryBuilder);
|
223
|
}
|
224
|
}
|
225
|
|
226
|
public static void enhanceQueryWithProjectSortParameters(StringBuilder queryBuilder, HttpServletRequest request) {
|
227
|
String sortByParameter = request.getParameter("sortBy");
|
228
|
|
229
|
if (sortByParameter != null) {
|
230
|
String[] sortParams = sortByParameter.split(",");
|
231
|
|
232
|
if (sortParams.length != 2) {
|
233
|
throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,ascending|,descending].");
|
234
|
}
|
235
|
|
236
|
String sortByField = sortParams[0];
|
237
|
String order = sortParams[1];
|
238
|
|
239
|
if (!checkProjectSortParameterFields(sortByField)){
|
240
|
throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field.");
|
241
|
}
|
242
|
|
243
|
if (!checkOrder(order)) {
|
244
|
throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}");
|
245
|
}
|
246
|
|
247
|
addSortParameter(sortByField, order, queryBuilder);
|
248
|
}
|
249
|
}
|
250
|
|
251
|
private static boolean checkPublicationSortParameterFields(String sortField) {
|
252
|
if ((sortField != null) && (!sortField.isEmpty()) &&
|
253
|
sortField.matches("dateofcollection|resultstoragedate|resultembargoenddate|resultembargoendyear|" +
|
254
|
"resulttypeid|resulttypename|resultlanguageid|resultlanguagename|resultbestlicense|" +
|
255
|
"resultbestlicenseid|resultdateofacceptance|resultacceptanceyear")) {
|
256
|
return true;
|
257
|
}
|
258
|
return false;
|
259
|
}
|
260
|
|
261
|
private static boolean checkProjectSortParameterFields(String sortField) {
|
262
|
if ((sortField != null) && (!sortField.isEmpty()) &&
|
263
|
sortField.matches("dateofcollection|projectstartdate|projectstartyear|" +
|
264
|
"projectenddate|projectendyear|projectcallidentifier|projectduration|" +
|
265
|
"projectecsc39|projectcontracttypeid|projectcontracttypename")) {
|
266
|
return true;
|
267
|
}
|
268
|
return false;
|
269
|
}
|
270
|
|
271
|
private static boolean checkOrder(String order) {
|
272
|
if (order.matches("ascending|descending")) {
|
273
|
return true;
|
274
|
}
|
275
|
return false;
|
276
|
}
|
277
|
|
278
|
public static boolean checkDate(String date, SimpleDateFormat simpleDateFormat) {
|
279
|
try {
|
280
|
simpleDateFormat.parse(date);
|
281
|
|
282
|
} catch (ParseException pe) {
|
283
|
logger.warn("Wrong date format.", pe);
|
284
|
return false;
|
285
|
}
|
286
|
|
287
|
return true;
|
288
|
}
|
289
|
|
290
|
/**
|
291
|
* Enhance the given CQL query with OpenAIRE specific ids
|
292
|
* @param queryBuilder
|
293
|
* @param request
|
294
|
* @return
|
295
|
*/
|
296
|
public static void enhanceQueryWithOpenAIREIds(StringBuilder queryBuilder, HttpServletRequest request) {
|
297
|
String[] openairePublicationIDs = request.getParameterValues("openairePublicationID");
|
298
|
String[] openaireDatasetIDs = request.getParameterValues("openaireDatasetID");
|
299
|
String[] openaireAuthorIDs = request.getParameterValues("openaireAuthorID");
|
300
|
String[] openaireProviderIDs = request.getParameterValues("openaireProviderID");
|
301
|
String[] openaireProjectIDs = request.getParameterValues("openaireProjectID");
|
302
|
|
303
|
enhanceQueryWithIds("objidentifier", openairePublicationIDs, queryBuilder);
|
304
|
enhanceQueryWithIds("objidentifier", openaireDatasetIDs, queryBuilder);
|
305
|
enhanceQueryWithIds("relpersonid", openaireAuthorIDs, queryBuilder);
|
306
|
enhanceQueryWithIds("resulthostingdatasourceid", openaireProviderIDs, queryBuilder);
|
307
|
enhanceQueryWithIds("relprojectid", openaireProjectIDs, queryBuilder);
|
308
|
}
|
309
|
|
310
|
public static void enhanceQueryWithMetadataKeywords(StringBuilder queryBuilder, HttpServletRequest request) {
|
311
|
String keywords = request.getParameter("keywords");
|
312
|
String title = request.getParameter("title");
|
313
|
String author = request.getParameter("author");
|
314
|
|
315
|
addMetadataQueryTerm(null, keywords, queryBuilder);
|
316
|
addMetadataQueryTerm("resulttitle", title, queryBuilder);
|
317
|
addMetadataQueryTerm("relperson", author, queryBuilder);
|
318
|
}
|
319
|
|
320
|
public static void enhanceQueryWithProjectMetadataKeywords(StringBuilder queryBuilder, HttpServletRequest request) {
|
321
|
String keywords = request.getParameter("keywords");
|
322
|
String acronym = request.getParameter("acronym");
|
323
|
String name = request.getParameter("name");
|
324
|
String grantID = request.getParameter("grantID");
|
325
|
String callID = request.getParameter("callID");
|
326
|
|
327
|
addMetadataQueryTerm(null, keywords, queryBuilder);
|
328
|
addMetadataQueryTerm("projectacronym", acronym, queryBuilder);
|
329
|
addEqualQueryTerm("projecttitle", name, queryBuilder);
|
330
|
addExactQueryTerm("projectcode", grantID, queryBuilder);
|
331
|
addExactQueryTerm("projectcallidentifier", callID, queryBuilder);
|
332
|
}
|
333
|
|
334
|
public static void enhanceQueryWithParticipantsInfoParams(StringBuilder queryBuilder, HttpServletRequest request) {
|
335
|
String[] participantCountries = request.getParameterValues("participantCountries");
|
336
|
String participantAcronyms = request.getParameter("participantAcronyms");
|
337
|
String openaireParticipantID = request.getParameter("openaireParticipantID");
|
338
|
|
339
|
|
340
|
if(participantCountries != null) {
|
341
|
enhanceQueryWithCommaSeparatedValues("relorganizationcountryid", participantCountries, queryBuilder);
|
342
|
}
|
343
|
|
344
|
addORQueryTerm("relorganizationname", "relorganizationshortname", participantAcronyms, queryBuilder);
|
345
|
|
346
|
if(openaireParticipantID != null) {
|
347
|
addEqualQueryTerm("relorganizationid", openaireParticipantID, queryBuilder);
|
348
|
}
|
349
|
|
350
|
}
|
351
|
|
352
|
public static void enhanceQueryWithSC39Params(StringBuilder queryBuilder, HttpServletRequest request) {
|
353
|
String sc39 = request.getParameter("sc39");
|
354
|
addBooleanQueryTerm("projectecsc39", sc39, queryBuilder);
|
355
|
}
|
356
|
|
357
|
|
358
|
|
359
|
|
360
|
public static void enhanceQueryWithDoi(StringBuilder queryBuilder, HttpServletRequest request) {
|
361
|
String[] dois = request.getParameterValues("doi");
|
362
|
|
363
|
if (dois != null && !(dois.length==0)) {
|
364
|
queryBuilder.append(" and ");
|
365
|
for (int i = 0; i < dois.length; i++) {
|
366
|
String[] commaSeparated = dois[i].split(",");
|
367
|
for (int j = 0; j < commaSeparated.length; j++) {
|
368
|
queryBuilder.append("(pidclassid exact \"doi\" and pid exact \"").append(commaSeparated[j]).append("\")");
|
369
|
if (i < dois.length-1 || j < commaSeparated.length-1 ) {
|
370
|
queryBuilder.append(" or ");
|
371
|
}
|
372
|
}
|
373
|
}
|
374
|
}
|
375
|
}
|
376
|
|
377
|
public static void enhanceQueryWithIds(String idIndexFieldName, String[] ids, StringBuilder queryBuilder) {
|
378
|
if (ids != null && !(ids.length==0)) {
|
379
|
queryBuilder.append(" and ");
|
380
|
for (int i = 0; i < ids.length; i++) {
|
381
|
String[] commaSeparated = ids[i].split(",");
|
382
|
for (int j = 0; j < commaSeparated.length; j++) {
|
383
|
queryBuilder.append("(" + idIndexFieldName + " exact \"").append(commaSeparated[j]).append("\")");
|
384
|
if (i < ids.length-1 || j < commaSeparated.length-1 ) {
|
385
|
queryBuilder.append(" or ");
|
386
|
}
|
387
|
}
|
388
|
}
|
389
|
}
|
390
|
}
|
391
|
|
392
|
public static void enhanceQueryWithCommaSeparatedValues(String indexFieldName, String[] fieldValues, StringBuilder queryBuilder) {
|
393
|
if (fieldValues != null && !(fieldValues.length==0)) {
|
394
|
queryBuilder.append(" and ");
|
395
|
for (int i = 0; i < fieldValues.length; i++) {
|
396
|
String[] commaSeparated = fieldValues[i].split(",");
|
397
|
for (int j = 0; j < commaSeparated.length; j++) {
|
398
|
queryBuilder.append("(").append(indexFieldName).append(" exact \"").append(commaSeparated[j].toUpperCase()).append("\")");
|
399
|
if (i < fieldValues.length-1 || j < commaSeparated.length-1 ) {
|
400
|
queryBuilder.append(" and ");
|
401
|
}
|
402
|
}
|
403
|
}
|
404
|
}
|
405
|
}
|
406
|
|
407
|
public static void addMetadataQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder) {
|
408
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
409
|
if(indexFieldName != null) {
|
410
|
for (String term: fieldValue.trim().split(" ")){
|
411
|
queryBuilder.append(" and (").append(indexFieldName).append(" = ").append(term).append(")");
|
412
|
}
|
413
|
} else {
|
414
|
queryBuilder.append(" and ( " );
|
415
|
String[] keywords = fieldValue.trim().split(" ");
|
416
|
for (int i = 0; i < keywords.length; i++) {
|
417
|
if (i == keywords.length -1) {
|
418
|
queryBuilder.append(keywords[i]);
|
419
|
} else {
|
420
|
queryBuilder.append(keywords[i]).append(" and ");
|
421
|
}
|
422
|
}
|
423
|
queryBuilder.append(")" );
|
424
|
}
|
425
|
}
|
426
|
}
|
427
|
|
428
|
public static void addORQueryTerm(String indexFieldName1, String indexFieldName2, String fieldValue, StringBuilder queryBuilder) {
|
429
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
430
|
for (String term: fieldValue.trim().split(" ")) {
|
431
|
queryBuilder.append(" and (" + indexFieldName1 + " = " + term + " or " + indexFieldName2 + " = " + term + ")");
|
432
|
}
|
433
|
}
|
434
|
}
|
435
|
|
436
|
public static void addORQueryTerm(String[] indexFields, String fieldValue, StringBuilder queryBuilder) {
|
437
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
438
|
queryBuilder.append(" and ( ");
|
439
|
for (int i = 0; i < indexFields.length; i++) {
|
440
|
for (String term : fieldValue.trim().split(" ")) {
|
441
|
queryBuilder.append(" (" + indexFields[i] + " = " + term + ")");
|
442
|
}
|
443
|
|
444
|
if (i == indexFields.length-1) {
|
445
|
queryBuilder.append(")");
|
446
|
|
447
|
} else {
|
448
|
queryBuilder.append(" or ");
|
449
|
}
|
450
|
|
451
|
}
|
452
|
}
|
453
|
}
|
454
|
|
455
|
private static void addBooleanQueryTerm(String indexFieldName, String requestValue, StringBuilder queryBuilder) {
|
456
|
if (requestValue != null && !requestValue.trim().isEmpty()) {
|
457
|
addExactQueryTerm(indexFieldName, requestValue, queryBuilder);
|
458
|
}
|
459
|
}
|
460
|
|
461
|
public static void addBooleanQueryTerm(String indexFieldName, String requestValue, String fieldValue, StringBuilder queryBuilder){
|
462
|
if (requestValue != null && !requestValue.trim().isEmpty()) {
|
463
|
if (requestValue.trim().equals("true")) {
|
464
|
addExactQueryTerm(indexFieldName, fieldValue, queryBuilder);
|
465
|
} else {
|
466
|
addDifferentEqualQueryTerm(indexFieldName, fieldValue, queryBuilder);
|
467
|
}
|
468
|
}
|
469
|
}
|
470
|
|
471
|
public static void addExactQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
|
472
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
473
|
queryBuilder.append(" and (" + indexFieldName + " exact \""+ fieldValue +"\")");
|
474
|
}
|
475
|
}
|
476
|
|
477
|
public static void addEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
|
478
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
479
|
queryBuilder.append(" and (" + indexFieldName + " = \""+ fieldValue.replaceAll("\"","") +"\")");
|
480
|
}
|
481
|
}
|
482
|
|
483
|
public static void addNotEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
|
484
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
485
|
queryBuilder.append(" not ").append(indexFieldName).append(" = \"").append(fieldValue).append("\"");
|
486
|
}
|
487
|
}
|
488
|
|
489
|
public static void addDifferentEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
|
490
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
491
|
queryBuilder.append(" and (").append(indexFieldName).append(" <> \"").append(fieldValue).append("\")");
|
492
|
}
|
493
|
}
|
494
|
|
495
|
public static void addVocabularizedQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder, Vocabulary vocabulary){
|
496
|
if (fieldValue != null && !fieldValue.trim().isEmpty()) {
|
497
|
queryBuilder.append(" and (").append(indexFieldName).append(" exact \"").append(devocabularizedTerm(fieldValue, vocabulary)).append("\")");
|
498
|
}
|
499
|
}
|
500
|
|
501
|
private static void addSortParameter(String indexField, String order, StringBuilder queryBuilder) {
|
502
|
queryBuilder.append(" sortBy " + indexField + "/sort." + order);
|
503
|
}
|
504
|
|
505
|
/**
|
506
|
* Returns the encoding of the given value. If the encoding does not exist
|
507
|
* in the given vocabulary the method returns the original value.|
|
508
|
* @param value the value to be encoded
|
509
|
* @param vocabulary the vocabulary containing the encoding - value mapping
|
510
|
* @return
|
511
|
*/
|
512
|
public static String devocabularizedTerm(String value, Vocabulary vocabulary) {
|
513
|
logger.debug("Vocabulary name " + vocabulary.getName() );
|
514
|
logger.debug("Vocabulary English names " + vocabulary.getEnglishNames());
|
515
|
|
516
|
if (vocabulary != null) {
|
517
|
String term = vocabulary.getEncoding(value);
|
518
|
if (term == null) {
|
519
|
return value;
|
520
|
}
|
521
|
return term;
|
522
|
}
|
523
|
return value;
|
524
|
}
|
525
|
|
526
|
|
527
|
}
|