Project

General

Profile

1
package eu.dnetlib.data.search.utils.cql;
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 ParameterQueryEnhancer {
17

    
18
    private static final Logger logger = Logger.getLogger(ParameterQueryEnhancer.class);
19

    
20
    /**
21
     * Enhance the given CQL query with FP7 specific index fields
22
     */
23
    public static void enhanceQueryWithFundingLevelParams(StringBuilder queryBuilder, HttpServletRequest request, VocabularyManager vocabularyManager, boolean isModelSygma) {
24
        logger.debug("enhance funding " + isModelSygma);
25
        
26
        String funder = request.getParameter("funder");
27
        String fundingStream = request.getParameter("fundingStream");
28
        String FP7scientificArea = request.getParameter("FP7scientificArea");
29

    
30
        if(isModelSygma) {
31
            if (funder != null) {
32
                if(!funder.equalsIgnoreCase("WT")) {
33
                    Vocabulary sygmaFundersVocabulary = vocabularyManager.getVocabulary("sygma_funders", Locale.ROOT);
34
                    addExactQueryTerm("relfundinglevel0_id", devocabularizedTerm(funder.toUpperCase(), sygmaFundersVocabulary), queryBuilder);
35
                } else {
36
                    Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT);
37
                    addExactQueryTerm("relfunderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder);
38
                }
39
            }
40

    
41
            if (fundingStream != null && !fundingStream.trim().isEmpty()) {
42
                Vocabulary relfundinglevel1Vocabulary = vocabularyManager.getVocabulary("programmes_simple", Locale.ROOT);
43
                addVocabularizedQueryTerm("relfundinglevel1_id", fundingStream.toUpperCase(), relfundinglevel1Vocabulary, queryBuilder);
44
            }
45

    
46
            if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) {
47
                Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT);
48
                addVocabularizedQueryTerm("relfundinglevel2_id", FP7scientificArea, relfundinglevel2Vocabulary, queryBuilder);
49
            }
50

    
51
        } else {
52

    
53
            if (funder != null) {
54
                //Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT);
55
                addExactQueryTerm("relfundershortname", funder.toUpperCase(), queryBuilder);
56
            }
57

    
58
            if (fundingStream != null && !fundingStream.trim().isEmpty()) {
59
                if(fundingStream.equalsIgnoreCase("FP7") || fundingStream.equalsIgnoreCase("H2020")) {
60
                    addORQueryTermExact(new String[]{"relfundinglevel0_name", "relfundinglevel1_name", "relfundinglevel2_name"}, "\"" + fundingStream.toUpperCase() + "\"", queryBuilder);
61
                } else {
62
                    addORQueryTermExact(new String[]{"relfundinglevel0_name", "relfundinglevel1_name", "relfundinglevel2_name"}, "\"" + fundingStream + "\"", queryBuilder);
63
                }
64
            }
65

    
66
            if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) {
67
                //Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT);
68
                queryBuilder.append(" and (relfundershortname exact \"EC\")").append(" and (relfundinglevel2_name exact \"").append(FP7scientificArea).append("\")");
69
            }
70
        }
71

    
72
    }
73

    
74
    public static void enhanceProjectQueryWithFundingLevelParams(StringBuilder queryBuilder, HttpServletRequest request, VocabularyManager vocabularyManager) {
75
        String funder = request.getParameter("funder");
76
        String fundingStream = request.getParameter("fundingStream");
77
        String FP7scientificArea = request.getParameter("FP7scientificArea");
78

    
79
        if (funder != null) {
80
            //addExactQueryTerm("fundinglevel0_name", funder.toUpperCase(), queryBuilder);
81
            //Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT);
82
            addExactQueryTerm("fundershortname", funder.toUpperCase(), queryBuilder);
83
        }
84

    
85
        if (fundingStream != null && !fundingStream.trim().isEmpty()) {
86
            //addExactQueryTerm("fundinglevel1_name", fundingStream.toUpperCase(), queryBuilder);
87
            if(fundingStream.equalsIgnoreCase("FP7") || fundingStream.equalsIgnoreCase("H2020")) {
88
                addORQueryTermExact(new String[]{"fundinglevel0_name", "fundinglevel1_name", "fundinglevel2_name"}, "\"" + fundingStream.toUpperCase() + "\"", queryBuilder);
89
            } else {
90
                addORQueryTermExact(new String[]{"fundinglevel0_name", "fundinglevel1_name", "fundinglevel2_name"}, "\"" + fundingStream + "\"", queryBuilder);
91
            }
92
        }
93

    
94
        if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) {
95
            //Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT);
96
            queryBuilder.append("and (fundershortname exact \"EC\")").append(" and (fundinglevel2_name exact \"").append(FP7scientificArea).append("\")");
97
        }
98
    }
99

    
100
    public static void enhanceQueryWithFundingParams(StringBuilder queryBuilder, HttpServletRequest request) {
101
        String hasECFunding = request.getParameter("hasECFunding");
102
        String hasWTFunding = request.getParameter("hasWTFunding");
103

    
104
        addBooleanQueryTerm("contextid", hasECFunding, "EC", queryBuilder);
105
        addBooleanQueryTerm("contextid", hasWTFunding, "WT", queryBuilder);
106
    }
107

    
108
    public static void enhanceQueryWithCommunityParams(StringBuilder queryBuilder, HttpServletRequest request) {
109
        String community = request.getParameter("community");
110

    
111
        if (community!= null) {
112
            addExactQueryTerm("communityId", community, queryBuilder);
113
        }
114

    
115
    }
116

    
117
    public static void enhanceQueryWithProjectFundingParams(StringBuilder queryBuilder, HttpServletRequest request) {
118
        String hasECFunding = request.getParameter("hasECFunding");
119
        String hasWTFunding = request.getParameter("hasWTFunding");
120

    
121
        addBooleanQueryTerm("funderid", hasECFunding, "ec__________::EC", queryBuilder);
122
        addBooleanQueryTerm("funderid", hasWTFunding, "wt__________::WT", queryBuilder);
123

    
124
    }
125

    
126
    public static void enhanceQueryWithRelProjectParams(StringBuilder queryBuilder, HttpServletRequest request) {
127
        String hasProject = request.getParameter("hasProject");
128
        String projectID = request.getParameter("projectID");
129
        String FP7ProjectID = request.getParameter("FP7ProjectID");
130

    
131
        if (hasProject != null && !hasProject.isEmpty()) {
132
            if (hasProject.equals("true")) {
133
                addEqualQueryTerm("relprojectid", "*", queryBuilder);
134
            } else {
135
                addNotEqualQueryTerm("relprojectid", "*", queryBuilder);
136
            }
137
        }
138

    
139
        if (FP7ProjectID != null && !FP7ProjectID.trim().isEmpty()) {
140
            addExactQueryTerm("relprojectcode", FP7ProjectID, queryBuilder);
141
            addExactQueryTerm(" relfundinglevel0_id", "ec__________::EC::FP7", queryBuilder);
142
        }
143

    
144
        if (projectID != null && !projectID.trim().isEmpty()) {
145
            queryBuilder.append(" and (relprojectcode exact \"").append(projectID).append("\")");
146
        }
147
    }
148

    
149
    public static void enhanceQueryWithAccessRights(StringBuilder queryBuilder, HttpServletRequest request) {
150
        String oa = request.getParameter("OA");
151
        addBooleanQueryTerm("resultbestaccessright", oa, "Open Access", queryBuilder);
152
    }
153

    
154
    public static void enhanceQueryWithDate(StringBuilder queryBuilder, HttpServletRequest request) throws IllegalArgumentException{
155
        String fromDateAccepted = request.getParameter("fromDateAccepted");
156
        String toDateAccepted = request.getParameter("toDateAccepted");
157
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
158
        simpleDateFormat.setLenient(false);
159

    
160
        if (toDateAccepted != null && !toDateAccepted.isEmpty() && (fromDateAccepted == null || fromDateAccepted.isEmpty()) ) {
161
            if(!checkDate(toDateAccepted, simpleDateFormat)){
162
                //logger.debug("Format is " + !checkDate(toDateAccepted, simpleDateFormat));
163
                throw new IllegalArgumentException("toDateAccepted date must be formatted as YYYY-MM-DD");
164
            }
165
            fromDateAccepted = "*";
166
        }
167

    
168
        if (fromDateAccepted != null && !fromDateAccepted.isEmpty() && (toDateAccepted == null || toDateAccepted.isEmpty()) ) {
169
            if (!checkDate(fromDateAccepted, simpleDateFormat)){
170
                //logger.debug("Format from is " + fromDateAccepted != null && !fromDateAccepted.isEmpty());
171
                throw new IllegalArgumentException("fromDateAccepted date must be formatted as YYYY-MM-DD");
172
            }
173
            toDateAccepted = simpleDateFormat.format(new Date());
174
        }
175

    
176
        if (toDateAccepted != null && !toDateAccepted.isEmpty() && fromDateAccepted != null && !fromDateAccepted.isEmpty()) {
177
            queryBuilder.append(" and (resultdateofacceptance within \"").append(fromDateAccepted).append(" ").append(toDateAccepted).append("\")");
178
        }
179
    }
180

    
181

    
182
    public static void enhanceQueryWithYearParams(StringBuilder queryBuilder, HttpServletRequest request) {
183
        String startYear = request.getParameter("startYear");
184
        String endYear = request.getParameter("endYear");
185

    
186
        int sYear = -1;
187
        if (startYear != null && !startYear.isEmpty()) {
188
            try {
189
                sYear = Integer.parseInt(startYear);
190

    
191
            } catch(NumberFormatException e) {
192
                throw new IllegalArgumentException("startYear parameter must be a numeric value.", e);
193
            }
194
        }
195

    
196
        int eYear = -1;
197
        if (endYear !=null && !endYear.isEmpty()) {
198
            try {
199
                eYear = Integer.parseInt(endYear);
200

    
201
            } catch(NumberFormatException e) {
202
                throw new IllegalArgumentException("endYear parameter must be a numeric value.",e);
203
            }
204
        }
205

    
206
        if (eYear != -1 && sYear != -1 && eYear < sYear) {
207
            throw new IllegalArgumentException("endYear must be greater than startYear.");
208
        }
209

    
210
        addExactQueryTerm("projectstartyear", startYear, queryBuilder);
211
        addExactQueryTerm("projectendyear", endYear, queryBuilder);
212
    }
213

    
214
    public static void enhanceQueryWithResultsSortParameters(StringBuilder queryBuilder, HttpServletRequest request) {
215
        String sortByParameter = request.getParameter("sortBy");
216

    
217
        if (sortByParameter != null) {
218
            String[] sortParams = sortByParameter.split(",");
219

    
220
            if (sortParams.length != 2) {
221
                throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,ascending|,descending].");
222
            }
223

    
224
            String sortByField = sortParams[0];
225
            String order = sortParams[1];
226

    
227
            if (!checkPublicationSortParameterFields(sortByField)){
228
                throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field.");
229
            }
230

    
231
            if (!checkOrder(order)) {
232
                throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}");
233
            }
234

    
235
            addSortParameter(sortByField, order, queryBuilder);
236
        }
237
    }
238

    
239
    public static void enhanceQueryWithProjectSortParameters(StringBuilder queryBuilder, HttpServletRequest request) {
240
        String sortByParameter = request.getParameter("sortBy");
241

    
242
        if (sortByParameter != null) {
243
            String[] sortParams = sortByParameter.split(",");
244

    
245
            if (sortParams.length != 2) {
246
                throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,ascending|,descending].");
247
            }
248

    
249
            String sortByField = sortParams[0];
250
            String order = sortParams[1];
251

    
252
            if (!checkProjectSortParameterFields(sortByField)){
253
                throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field.");
254
            }
255

    
256
            if (!checkOrder(order)) {
257
                throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}");
258
            }
259

    
260
            addSortParameter(sortByField, order, queryBuilder);
261
        }
262
    }
263

    
264
    private static boolean checkPublicationSortParameterFields(String sortField) {
265
        if ((sortField != null) && (!sortField.isEmpty()) &&
266
                sortField.matches("dateofcollection|resultstoragedate|resultembargoenddate|resultembargoendyear|" +
267
                "resulttypeid|resulttypename|resultlanguageid|resultlanguagename|resultbestaccessright|" +
268
                "resultbestlicenseid|resultdateofacceptance|resultacceptanceyear")) {
269
            return true;
270
        }
271
        return false;
272
    }
273

    
274
    private static boolean checkProjectSortParameterFields(String sortField) {
275
        if ((sortField != null) && (!sortField.isEmpty()) &&
276
                sortField.matches("dateofcollection|projectstartdate|projectstartyear|" +
277
                        "projectenddate|projectendyear|projectcallidentifier|projectduration|" +
278
                        "projectecsc39|projectcontracttypeid|projectcontracttypename")) {
279
            return true;
280
        }
281
        return false;
282
    }
283

    
284
    private static boolean checkOrder(String order) {
285
        if (order.matches("ascending|descending")) {
286
            return true;
287
        }
288
        return false;
289
    }
290

    
291
    public static boolean checkDate(String date, SimpleDateFormat simpleDateFormat) {
292
        try {
293
            simpleDateFormat.parse(date);
294

    
295
        } catch (ParseException pe) {
296
            logger.warn("Wrong date format.", pe);
297
            return false;
298
        }
299

    
300
        return true;
301
    }
302

    
303
    /**
304
     * Enhance the given CQL query with OpenAIRE specific ids
305
     * @param queryBuilder The string builder of the query
306
     * @param request the HTTP request
307
     */
308
    public static void enhanceQueryWithOpenAIREIds(StringBuilder queryBuilder, HttpServletRequest request) {
309
        String[] openairePublicationIDs = request.getParameterValues("openairePublicationID");
310
        String[] openaireDatasetIDs = request.getParameterValues("openaireDatasetID");
311
        String[] openaireSoftwareIDs = request.getParameterValues("openaireSoftwareID");
312
        String[] openaireOtherIDs = request.getParameterValues("openaireOtherID");
313
        String[] openaireProviderIDs  = request.getParameterValues("openaireProviderID");
314
        String[] openaireProjectIDs  = request.getParameterValues("openaireProjectID");
315

    
316
        enhanceQueryWithIds("objidentifier", openairePublicationIDs, queryBuilder);
317
        enhanceQueryWithIds("objidentifier", openaireDatasetIDs, queryBuilder);
318
        enhanceQueryWithIds("objidentifier", openaireSoftwareIDs, queryBuilder);
319
        enhanceQueryWithIds("objidentifier", openaireOtherIDs, queryBuilder);
320
        enhanceQueryWithIds("resulthostingdatasourceid", openaireProviderIDs, queryBuilder);
321
        enhanceQueryWithIds("relprojectid", openaireProjectIDs, queryBuilder);
322
    }
323

    
324
    public static void enhanceQueryWithMetadataKeywords(StringBuilder queryBuilder, HttpServletRequest request) {
325
        String keywords = request.getParameter("keywords");
326
        String title = request.getParameter("title");
327
        String author = request.getParameter("author");
328
        String country = request.getParameter("country");
329

    
330
        addMetadataQueryTerm(null, keywords, queryBuilder);
331
        addMetadataQueryTerm("resulttitle", title, queryBuilder);
332
        addMetadataQueryTerm("resultauthor", author, queryBuilder);
333
        addMetadataQueryTerm("country", country, queryBuilder);
334
    }
335

    
336
    public static void enhanceQueryWithProjectMetadataKeywords(StringBuilder queryBuilder, HttpServletRequest request) {
337
        String keywords = request.getParameter("keywords");
338
        String acronym = request.getParameter("acronym");
339
        String name = request.getParameter("name");
340
        String grantID = request.getParameter("grantID");
341
        String callID = request.getParameter("callID");
342

    
343
        addMetadataQueryTerm(null, keywords, queryBuilder);
344
        addMetadataQueryTerm("projectacronym", acronym, queryBuilder);
345
        addEqualQueryTerm("projecttitle", name, queryBuilder);
346
        addExactQueryTerm("projectcode_nt", grantID, queryBuilder);
347
        addExactQueryTerm("projectcallidentifier", callID, queryBuilder);
348
    }
349

    
350
    public static void enhanceQueryWithParticipantsInfoParams(StringBuilder queryBuilder, HttpServletRequest request) {
351
        String[] participantCountries = request.getParameterValues("participantCountries");
352
        String participantAcronyms = request.getParameter("participantAcronyms");
353
        String openaireParticipantID = request.getParameter("openaireParticipantID");
354

    
355

    
356
        if(participantCountries != null) {
357
            enhanceQueryWithCommaSeparatedValues("relorganizationcountryid", participantCountries, queryBuilder);
358
        }
359

    
360
        addORQueryTerm("relorganizationname", "relorganizationshortname", participantAcronyms, queryBuilder);
361

    
362
        if(openaireParticipantID != null) {
363
            addEqualQueryTerm("relorganizationid", openaireParticipantID, queryBuilder);
364
        }
365

    
366
    }
367

    
368
    public static void enhanceQueryWithSC39Params(StringBuilder queryBuilder, HttpServletRequest request) {
369
        String sc39 = request.getParameter("sc39");
370
        addBooleanQueryTerm("projectecsc39", sc39, queryBuilder);
371
    }
372

    
373

    
374

    
375

    
376
    public static void enhanceQueryWithDoi(StringBuilder queryBuilder, HttpServletRequest request) {
377
        String[] dois = request.getParameterValues("doi");
378

    
379
        if (dois != null && !(dois.length==0)) {
380
            queryBuilder.append(" and ");
381
            for (int i = 0; i < dois.length; i++) {
382
                String[] commaSeparated = dois[i].split(",");
383
                for (int j = 0; j < commaSeparated.length; j++) {
384
                    queryBuilder.append("(pidclassid exact \"doi\" and pid exact \"").append(commaSeparated[j]).append("\")");
385
                    if (i < dois.length-1 || j < commaSeparated.length-1 ) {
386
                        queryBuilder.append(" or ");
387
                    }
388
                }
389
            }
390
        }
391
    }
392

    
393
    public static void enhanceQueryWithOriginalId(StringBuilder queryBuilder, HttpServletRequest request) {
394
        String[] originalIds = request.getParameterValues("originalId");
395

    
396
        if (originalIds != null && !(originalIds.length==0)) {
397
            queryBuilder.append(" and ");
398
            for (int i = 0; i < originalIds.length; i++) {
399
                String[] commaSeparated = originalIds[i].split(",");
400
                for (int j = 0; j < commaSeparated.length; j++) {
401
                    queryBuilder.append("(originalId exact \"").append(commaSeparated[j]).append("\")");
402
                    if (i < originalIds.length-1 || j < commaSeparated.length-1 ) {
403
                        queryBuilder.append(" or ");
404
                    }
405
                }
406
            }
407
        }
408
    }
409

    
410
    public static void enhanceProjectQueryWithOpenAIREIds(StringBuilder queryBuilder, HttpServletRequest request) {
411
        String openaireProjectID  = request.getParameter("openaireProjectID");
412

    
413
        if (openaireProjectID != null) {
414
            addExactQueryTerm("objIdentifier", openaireProjectID, queryBuilder);
415
        }
416
    }
417

    
418
    public static void enhanceQueryWithIds(String idIndexFieldName, String[] ids, StringBuilder queryBuilder) {
419
        if (ids != null && !(ids.length==0)) {
420
            queryBuilder.append(" and ");
421
            for (int i = 0; i < ids.length; i++) {
422
                String[] commaSeparated = ids[i].split(",");
423
                for (int j = 0; j < commaSeparated.length; j++) {
424
                    queryBuilder.append("(" + idIndexFieldName + " exact \"").append(commaSeparated[j]).append("\")");
425
                    if (i < ids.length-1 || j < commaSeparated.length-1 ) {
426
                        queryBuilder.append(" or ");
427
                    }
428
                }
429
            }
430
        }
431
    }
432

    
433
    public static void enhanceQueryWithCommaSeparatedValues(String indexFieldName, String[] fieldValues, StringBuilder queryBuilder) {
434
        if (fieldValues != null && !(fieldValues.length==0)) {
435
            queryBuilder.append(" and ");
436
            for (int i = 0; i < fieldValues.length; i++) {
437
                String[] commaSeparated = fieldValues[i].split(",");
438
                for (int j = 0; j < commaSeparated.length; j++) {
439
                    queryBuilder.append("(").append(indexFieldName).append(" exact \"").append(commaSeparated[j].toUpperCase()).append("\")");
440
                    if (i < fieldValues.length-1 || j < commaSeparated.length-1 ) {
441
                        queryBuilder.append(" and ");
442
                    }
443
                }
444
            }
445
        }
446
    }
447

    
448
    public static void addMetadataQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder) {
449
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
450
            if(indexFieldName != null) {
451
                for (String term: fieldValue.trim().split(" ")){
452
                    addEqualQueryTerm(indexFieldName, term, queryBuilder);
453
                }
454
            } else {
455
                queryBuilder.append(" and ( " );
456
                String[] keywords = fieldValue.trim().split(" ");
457
                for (int i = 0; i < keywords.length; i++) {
458
                    if (i == keywords.length -1) {
459
                        queryBuilder.append(keywords[i]);
460
                    } else {
461
                        queryBuilder.append(keywords[i]).append(" and ");
462
                    }
463
                }
464
                queryBuilder.append(")" );
465
            }
466
        }
467
    }
468

    
469
    public static void addORQueryTerm(String indexFieldName1, String indexFieldName2, String fieldValue, StringBuilder queryBuilder) {
470
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
471
            for (String term: fieldValue.trim().split(" ")) {
472
                queryBuilder.append(" and (" + indexFieldName1 + " = " + term + " or  " + indexFieldName2 + " = " + term + ")");
473
            }
474
        }
475
    }
476

    
477
    public static void addORQueryTerm(String[] indexFields, String fieldValue, StringBuilder queryBuilder) {
478
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
479
            queryBuilder.append(" and ( ");
480
            for (int i = 0; i < indexFields.length; i++) {
481
                for (String term : fieldValue.trim().split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1)) {
482
                    //logger.info("term: " + term);
483
                    queryBuilder.append(" (" + indexFields[i] + " = " + term + ")");
484
                }
485

    
486
                if (i == indexFields.length-1) {
487
                    queryBuilder.append(")");
488

    
489
                } else {
490
                    queryBuilder.append(" or ");
491
                }
492

    
493
            }
494
        }
495
    }
496

    
497
    public static void addORQueryTermExact(String[] indexFields, String fieldValue, StringBuilder queryBuilder) {
498
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
499
            queryBuilder.append(" and ( ");
500
            for (int i = 0; i < indexFields.length; i++) {
501
                for (String term : fieldValue.trim().split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1)) {
502
                    //logger.info("term: " + term);
503
                    queryBuilder.append(" (" + indexFields[i] + " exact " + term + ")");
504
                }
505

    
506
                if (i == indexFields.length-1) {
507
                    queryBuilder.append(")");
508

    
509
                } else {
510
                    queryBuilder.append(" or ");
511
                }
512

    
513
            }
514
        }
515
    }
516

    
517
    private static void addBooleanQueryTerm(String indexFieldName, String requestValue, StringBuilder queryBuilder) {
518
        if (requestValue != null && !requestValue.trim().isEmpty()) {
519
            addExactQueryTerm(indexFieldName, requestValue, queryBuilder);
520
        }
521
    }
522

    
523
    public static void addBooleanQueryTerm(String indexFieldName, String requestValue, String fieldValue, StringBuilder queryBuilder){
524
        if (requestValue != null && !requestValue.trim().isEmpty()) {
525
            if (requestValue.trim().equals("true")) {
526
                addExactQueryTerm(indexFieldName, fieldValue, queryBuilder);
527
            } else {
528
                addDifferentQueryTerm(indexFieldName, fieldValue, queryBuilder);
529
            }
530
        }
531
    }
532

    
533
    public static void addExactQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
534
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
535
            CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder, CQLQueryBuilder.Operator.AND, indexFieldName, CQLQueryBuilder.Operator.EXACT, fieldValue);
536
        }
537
    }
538

    
539
    public static void addEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
540
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
541
            if (fieldValue.startsWith("\"") && fieldValue.endsWith("\"")) {
542
                CQLQueryBuilder.appendFieldTerm(queryBuilder, CQLQueryBuilder.Operator.AND, indexFieldName, CQLQueryBuilder.Operator.EQUAL, fieldValue);
543
            } else {
544
                CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder, CQLQueryBuilder.Operator.AND, indexFieldName, CQLQueryBuilder.Operator.EQUAL, fieldValue);
545
            }
546
        }
547

    
548

    
549

    
550

    
551
    }
552

    
553
    public static void addNotEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
554
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
555
            CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder, CQLQueryBuilder.Operator.NOT, indexFieldName, CQLQueryBuilder.Operator.EQUAL, fieldValue);
556
        }
557
    }
558

    
559
    public static void addDifferentQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){
560
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
561
            CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder, CQLQueryBuilder.Operator.AND, indexFieldName, CQLQueryBuilder.Operator.NOTEQUAL, fieldValue);
562
        }
563
    }
564

    
565
    public static void addVocabularizedQueryTerm(String indexFieldName, String fieldValue, Vocabulary vocabulary, StringBuilder queryBuilder){
566
        if (fieldValue != null && !fieldValue.trim().isEmpty()) {
567
            addExactQueryTerm(indexFieldName, devocabularizedTerm(fieldValue, vocabulary), queryBuilder);
568
        }
569
    }
570

    
571
    private static void addSortParameter(String indexField, String order, StringBuilder queryBuilder) {
572
        queryBuilder.append(" sortBy " + indexField + "/sort." + order);
573
    }
574

    
575
    /**
576
     * Returns the encoding of the given value. If the encoding does not exist
577
     * in the given vocabulary the method returns the original value.|
578
     * @param value the value to be encoded
579
     * @param vocabulary the vocabulary containing the encoding - value mapping
580
     * @return the encoding of the given value taken from the given vocabulary
581
     */
582
    public static String devocabularizedTerm(String value, Vocabulary vocabulary) {
583
        if (vocabulary != null) {
584
            String term = vocabulary.getEncoding(value);
585
            if (term == null) {
586
                return value;
587
            }
588
            return term;
589
        }
590
        return value;
591
    }
592
}
(2-2/2)