source: greenstone3/trunk/src/java/org/greenstone/gsdl3/service/AbstractGS2FieldSearch.java@ 14521

Last change on this file since 14521 was 14521, checked in by shaoqun, 17 years ago

correct a mistake in the previous commit

  • Property svn:keywords set to Author Date Id Revision
File size: 24.0 KB
Line 
1/*
2 * AbstractGS2FieldSearch.java
3 * Copyright (C) 2006 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18package org.greenstone.gsdl3.service;
19
20
21// Greenstone classes
22import org.greenstone.mgpp.*;
23import org.greenstone.gsdl3.util.*;
24
25// XML classes
26import org.w3c.dom.Document;
27import org.w3c.dom.Element;
28import org.w3c.dom.NodeList;
29
30// java classes
31import java.util.Iterator;
32import java.util.Set;
33import java.util.HashMap;
34import java.util.Map;
35import java.util.ArrayList;
36import java.util.Vector;
37import java.io.File;
38
39import org.apache.log4j.*;
40
41
42abstract public class AbstractGS2FieldSearch
43 extends AbstractGS2Search
44{
45
46 // extra services offered by mgpp collections
47 protected static final String FIELD_QUERY_SERVICE = "FieldQuery";
48 protected static final String ADVANCED_FIELD_QUERY_SERVICE = "AdvancedFieldQuery";
49
50 // extra parameters used
51 protected static final String LEVEL_PARAM = "level";
52 protected static final String RANK_PARAM = "sortBy";
53 protected static final String RANK_PARAM_RANK = "1";
54 protected static final String RANK_PARAM_NONE = "0";
55 protected static final String SIMPLE_FIELD_PARAM = "simpleField";
56 protected static final String ADVANCED_FIELD_PARAM = "complexField";
57
58 // more params for field query
59 protected static final String FIELD_QUERY_PARAM = "fqv";
60 protected static final String FIELD_STEM_PARAM = "fqs";
61 protected static final String FIELD_CASE_PARAM = "fqc";
62 protected static final String FIELD_ACCENT_PARAM="fqa";
63 protected static final String FIELD_FIELD_PARAM = "fqf";
64 protected static final String FIELD_COMBINE_PARAM = "fqk";
65 protected static final String FIELD_COMBINE_PARAM_AND = "0";
66 protected static final String FIELD_COMBINE_PARAM_OR = "1";
67 protected static final String FIELD_COMBINE_PARAM_NOT = "2";
68
69 // some stuff for config files
70 protected static final String SEARCH_TYPE_ELEM = "searchType";
71 protected static final String SEARCH_TYPE_PLAIN = "plain";
72 protected static final String SEARCH_TYPE_FORM = "form";
73 protected static final String SEARCH_TYPE_FORM_SIMPLE = "simple";
74 protected static final String SEARCH_TYPE_FORM_ADVANCED = "advanced";
75
76 protected static final String DEFAULT_LEVEL_ELEM = "defaultLevel";
77 protected static final String DEFAULT_GDBM_LEVEL_ELEM = "defaultGDBMLevel";
78 protected static final String LEVEL_ELEM = "level";
79 protected static final String FIELD_ATT = "field";
80
81
82 protected static final int TEXT_QUERY = 0;
83 protected static final int SIMPLE_QUERY = 1;
84 protected static final int ADVANCED_QUERY = 2;
85
86 protected String AND_OPERATOR = "&";
87 protected String OR_OPERATOR = "|";
88 protected String NOT_OPERATOR = "!";
89
90 // the default level for searching
91 protected String default_level=null;
92 // default level for gdbm db
93 protected String default_gdbm_level = null;
94 // which search services will we offer??
95 protected boolean plain_search = false;
96 protected boolean simple_form_search = false;
97 protected boolean advanced_form_search = false;
98
99
100 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2FieldSearch.class.getName());
101
102 /** constructor */
103 public AbstractGS2FieldSearch()
104 {
105 }
106
107 public void cleanUp() {
108 super.cleanUp();
109 }
110
111 /** configure this service */
112 public boolean configure(Element info, Element extra_info)
113 {
114 if (!super.configure(info, extra_info)){
115 return false;
116 }
117 // the generic config has set up the text query service, but we may not want it
118 Element search_type_list = (Element) GSXML.getChildByTagName(info, SEARCH_TYPE_ELEM + GSXML.LIST_MODIFIER);
119 if (search_type_list == null) {
120 // assume form and plain
121 this.plain_search = true;
122 this.simple_form_search = true;
123 this.advanced_form_search = true;
124 } else {
125 NodeList types = search_type_list.getElementsByTagName(SEARCH_TYPE_ELEM);
126 for (int i=0; i<types.getLength(); i++) {
127 Element t = (Element)types.item(i);
128 String type_name = t.getAttribute(GSXML.NAME_ATT);
129 if (type_name.equals(SEARCH_TYPE_PLAIN)) {
130 this.plain_search = true;
131 } else if (type_name.equals(SEARCH_TYPE_FORM)) {
132 String type_type = t.getAttribute(GSXML.TYPE_ATT);
133 if (type_type.equals("")) {
134 this.simple_form_search = true;
135 this.advanced_form_search = true;
136 } else if (type_type.equals(SEARCH_TYPE_FORM_SIMPLE)) {
137 this.simple_form_search = true;
138 } else if (type_type.equals(SEARCH_TYPE_FORM_ADVANCED)) {
139 this.advanced_form_search = true;
140
141 }
142 }
143 }
144 }
145
146 if (!this.plain_search) {
147 // need to remove the TextQuery service
148 Element tq_service = GSXML.getNamedElement(short_service_info, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
149 short_service_info.removeChild(tq_service);
150
151 }
152
153
154 // Get the default level out of <defaultLevel> (buildConfig.xml)
155 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
156 if (def != null) {
157 this.default_level = def.getAttribute(GSXML.SHORTNAME_ATT);
158 }
159 if (this.default_level == null || this.default_level.equals("")) {
160 logger.error("default level not specified!, assuming Doc");
161 this.default_level = "Doc";
162 }
163
164 // Get the default GDBM level
165 def = (Element) GSXML.getChildByTagName(info, DEFAULT_GDBM_LEVEL_ELEM);
166 if (def != null) {
167 this.default_gdbm_level = def.getAttribute(GSXML.SHORTNAME_ATT);
168 }
169 if (this.default_gdbm_level == null || this.default_gdbm_level.equals("")) {
170 logger.error("default gdbm level not specified!, assuming Sec");
171 this.default_gdbm_level = "Sec";
172 }
173
174 // get display info from extra info - for levels
175 if (extra_info !=null) {
176 Document owner = info.getOwnerDocument();
177
178 NodeList levels = info.getElementsByTagName(GSXML.LEVEL_ELEM);
179 Element config_search = (Element)GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
180
181 for (int i=0; i<levels.getLength();i++) {
182 Element lev = (Element)levels.item(i);
183 String name = lev.getAttribute(GSXML.NAME_ATT);
184 Element node_extra = GSXML.getNamedElement(config_search,
185 GSXML.LEVEL_ELEM,
186 GSXML.NAME_ATT,
187 name);
188 if (node_extra == null) {
189 logger.error("haven't found extra info for level named "+name);
190 continue;
191 }
192
193 // get the display elements if any - displayName
194 NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
195 if (display_names !=null) {
196 for (int j=0; j<display_names.getLength(); j++) {
197 Element e = (Element)display_names.item(j);
198 lev.appendChild(owner.importNode(e, true));
199 }
200 }
201 } // for each level
202 }
203
204 // the format info is the same for all services
205 Element format_info = (Element)format_info_map.get(TEXT_QUERY_SERVICE);
206
207 // set up the extra services which are available for this collection
208 if (this.simple_form_search) {
209 // set up short_service_info_ - for now just has id and type - name will be added in on the fly
210 Element fq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
211 fq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
212 fq_service.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
213 this.short_service_info.appendChild(fq_service);
214
215 if (format_info != null) {
216 this.format_info_map.put(FIELD_QUERY_SERVICE, format_info);
217 }
218 }
219
220 if (this.advanced_form_search) {
221 Element afq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
222 afq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
223 afq_service.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
224 this.short_service_info.appendChild(afq_service);
225
226 if (format_info != null) {
227 this.format_info_map.put(ADVANCED_FIELD_QUERY_SERVICE, format_info);
228 }
229 }
230
231 return true;
232 }
233
234
235 protected Element getServiceDescription(String service_id, String lang, String subset) {
236 // should we check that the service is actually on offer? presumably we wont get asked for services that we haven't advertised previously.
237
238 if (!service_id.equals(FIELD_QUERY_SERVICE) && !service_id.equals(ADVANCED_FIELD_QUERY_SERVICE)) {
239 return super.getServiceDescription(service_id, lang, subset);
240 }
241
242
243 Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
244 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
245 service.setAttribute(GSXML.NAME_ATT, service_id);
246 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
247 service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(service_id+".name", lang)));
248 service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(service_id+".submit", lang)));
249 service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(service_id+".description", lang)));
250
251 }
252 if (subset == null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
253 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
254 service.appendChild(param_list);
255 if (service_id.equals(FIELD_QUERY_SERVICE)) {
256
257 addCustomQueryParams(param_list, lang);
258 createParameter(MAXDOCS_PARAM, param_list, lang);
259 if (!default_index_subcollection.equals("")){
260 createParameter(INDEX_SUBCOLLECTION_PARAM,param_list, lang);
261 }
262 if (!default_index_language.equals("")){
263 createParameter(INDEX_LANGUAGE_PARAM,param_list, lang);
264 }
265 // create a multi param for the fields etc
266 // text box, field
267 Element multiparam = null;
268 Element param=null;
269 multiparam = GSXML.createParameterDescription(this.doc, SIMPLE_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
270 multiparam.setAttribute("occurs", "4");
271 param_list.appendChild(multiparam);
272
273 // the components
274
275 createParameter(FIELD_QUERY_PARAM, multiparam, lang);
276 createParameter(FIELD_FIELD_PARAM, multiparam, lang);
277
278 } else {
279 createParameter(LEVEL_PARAM, param_list, lang);
280 createParameter(RANK_PARAM, param_list, lang);
281 createParameter(MAXDOCS_PARAM, param_list, lang);
282 if (!default_index_subcollection.equals("")){
283 createParameter(INDEX_SUBCOLLECTION_PARAM,param_list, lang);
284 }
285 if (!default_index_language.equals("")){
286 createParameter(INDEX_LANGUAGE_PARAM,param_list, lang);
287 }
288
289 // create a multi param for the fields etc
290 // text box, stem, case, field
291
292 Element multiparam = null;
293 Element param=null;
294
295 multiparam = GSXML.createParameterDescription(this.doc, ADVANCED_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
296 multiparam.setAttribute("occurs", "4");
297 param_list.appendChild(multiparam);
298
299 createParameter(FIELD_COMBINE_PARAM, multiparam, lang);
300 createParameter(FIELD_QUERY_PARAM, multiparam, lang);
301 if (this.does_case) {
302 createParameter(FIELD_CASE_PARAM, multiparam, lang);
303 }
304 if (this.does_stem) {
305 createParameter(FIELD_STEM_PARAM, multiparam, lang);
306 }
307 if (this.does_accent) {
308 createParameter(FIELD_ACCENT_PARAM, multiparam, lang);
309 }
310 createParameter(FIELD_FIELD_PARAM, multiparam, lang);
311
312 }
313 }
314 return service;
315
316 }
317
318 /** add in the level params to TextQuery */
319 protected void addCustomQueryParams(Element param_list, String lang)
320 {
321 createParameter(LEVEL_PARAM, param_list, lang);
322 super.addCustomQueryParams(param_list, lang);
323 }
324
325 /** create a param and add to the list */
326 protected void createParameter(String name, Element param_list, String lang)
327 {
328 Element param = null;
329 if (name.equals(LEVEL_PARAM)) {
330 ArrayList level_ids = new ArrayList();
331 ArrayList level_names = new ArrayList();
332 getLevelData(level_ids, level_names, lang);
333 if (level_ids.size()>1) {
334 // the first one is the default
335 param = GSXML.createParameterDescription2(this.doc, LEVEL_PARAM, getTextString("param."+LEVEL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)level_ids.get(0), level_ids, level_names);
336 } else {
337 // we need to set the level, but hidden, in case there is an invalid level saved
338 param = GSXML.createParameterDescription(this.doc, LEVEL_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, (String)level_ids.get(0), null, null);
339 }
340 } else if (name.equals(RANK_PARAM)) {
341 String [] vals1 = {RANK_PARAM_RANK, RANK_PARAM_NONE };
342 String [] vals1_texts = { getTextString("param."+RANK_PARAM+"."+RANK_PARAM_RANK, lang), getTextString("param."+RANK_PARAM+"."+RANK_PARAM_NONE, lang)};
343
344 param = GSXML.createParameterDescription(this.doc, RANK_PARAM, getTextString("param."+RANK_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, RANK_PARAM_RANK, vals1, vals1_texts );
345
346 } else if (name.equals(FIELD_QUERY_PARAM)) {
347 param = GSXML.createParameterDescription(this.doc, FIELD_QUERY_PARAM, getTextString("param."+FIELD_QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
348
349
350 } else if (name.equals(FIELD_CASE_PARAM) || name.equals(FIELD_STEM_PARAM) || name.equals(FIELD_ACCENT_PARAM)) {
351 String[] bool_ops = {"0", "1"};
352 String[] bool_texts = {getTextString("param.boolean.off", lang, "AbstractSearch"),getTextString("param.boolean.on", lang, "AbstractSearch")};
353 param = GSXML.createParameterDescription(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, bool_ops, bool_texts);
354
355 } else if (name.equals(FIELD_FIELD_PARAM)) {
356 ArrayList fields = new ArrayList();
357 ArrayList field_names = new ArrayList();
358 getIndexData(fields, field_names, lang);
359 // the field list - read from config file
360 param = GSXML.createParameterDescription2(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)fields.get(0), fields, field_names );
361
362 } else if (name.equals(FIELD_COMBINE_PARAM)) {
363
364 String []vals = {FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT};
365 String []val_texts = {getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_AND, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_OR, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_NOT, lang)};
366
367
368 param = GSXML.createParameterDescription(this.doc, FIELD_COMBINE_PARAM, "", GSXML.PARAM_TYPE_ENUM_SINGLE, FIELD_COMBINE_PARAM_AND, vals, val_texts);
369 param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
370 }
371
372 if (param != null) {
373 param_list.appendChild(param);
374 } else {
375 super.createParameter(name, param_list, lang);
376 }
377 }
378
379 // should cache some of this
380 protected void getLevelData(ArrayList level_ids, ArrayList level_names, String lang)
381 {
382 Element level_list = (Element)GSXML.getChildByTagName(this.config_info, LEVEL_ELEM+GSXML.LIST_MODIFIER);
383 NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
384 for (int i=0; i<levels.getLength(); i++) {
385 Element level = (Element)levels.item(i);
386 String shortname = level.getAttribute(GSXML.SHORTNAME_ATT);
387 if (shortname.equals("")) {
388 continue;
389 }
390 level_ids.add(shortname);
391 String display_name = GSXML.getDisplayText(level, GSXML.DISPLAY_TEXT_NAME, lang, "en");
392 if (display_name.equals("")) {
393 // we'll use the name, and the dictionary
394 display_name = level.getAttribute(GSXML.NAME_ATT);
395 if (display_name.equals("")) {
396 display_name = shortname;
397 } else {
398 display_name = getTextString("level."+display_name, lang);
399 }
400 }
401 level_names.add(display_name);
402 }
403 }
404
405 // the following three functions are needed so the base class can
406 // call the process+SERVICE_NAME methods
407 /** process a text query */
408 protected Element processTextQuery(Element request) {
409 return processAnyQuery(request, TEXT_QUERY);
410 }
411
412 /** process a field query */
413 protected Element processFieldQuery(Element request) {
414 return processAnyQuery(request, SIMPLE_QUERY);
415 }
416
417 /** process an advanced field query */
418 protected Element processAdvancedFieldQuery(Element request) {
419 return processAnyQuery(request, ADVANCED_QUERY);
420 }
421
422 /** process a query */
423 protected Element processAnyQuery(Element request, int query_type)
424 {
425
426 String service_name=null;
427 String empty_query_test_param=null;
428 // set up the type specific bits
429 switch (query_type) {
430 case TEXT_QUERY:
431 service_name = TEXT_QUERY_SERVICE;
432 empty_query_test_param = QUERY_PARAM;
433 break;
434 case SIMPLE_QUERY:
435 service_name = FIELD_QUERY_SERVICE;
436 empty_query_test_param = FIELD_QUERY_PARAM;
437 break;
438 case ADVANCED_QUERY:
439 service_name = ADVANCED_FIELD_QUERY_SERVICE;
440 empty_query_test_param = FIELD_QUERY_PARAM;
441 break;
442 default:
443 // should never get here
444 logger.error("wrong query type!!");
445 return null;
446 }
447
448 // Create a new (empty) result message
449 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
450 result.setAttribute(GSXML.FROM_ATT, service_name);
451 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
452
453 // Get the parameters of the request
454 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
455 if (param_list == null) {
456 logger.error("TextQuery request had no paramList.");
457 return result; // Return the empty result
458 }
459
460 // Process the request parameters
461 HashMap params = GSXML.extractParams(param_list, false);
462
463 // Make sure a query has been specified
464 String query = (String) params.get(empty_query_test_param);
465 if (query == null || query.equals("")) {
466 return result; // Return the empty result
467 }
468
469 // If a field hasn't been specified, use the default - for textQuery
470 String field = (String) params.get(INDEX_PARAM);
471 if (field == null) {
472 field = default_index;
473 }
474
475 // set up the appropriate query system
476 if (!setUpQueryer(params)) {
477 return result;
478 }
479
480 // if field search, create the query string
481 switch (query_type) {
482 case TEXT_QUERY:
483 query = addFieldInfo(query, field);
484 break;
485 case SIMPLE_QUERY:
486 query = parseFieldQueryParams(params);
487 break;
488 case ADVANCED_QUERY:
489 query = parseAdvancedFieldQueryParams(params);
490 break;
491 }
492
493 // run the query
494 Object query_result = runQuery(query);
495
496 // build up the response
497
498 // Create a metadata list to store information about the query results
499 // should we be using metadataList? or something else?
500 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
501 result.appendChild(metadata_list);
502
503 // Add a metadata element specifying the number of matching documents
504 long totalDocs = numDocsMatched(query_result);
505
506 GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+totalDocs);
507
508 // Create a document list to store the matching documents, and add them
509 String [] docs = getDocIDs(query_result);
510 String [] doc_ranks = getDocRanks(query_result);
511
512 // add a metadata item to specify docs returned
513 int docs_returned = docs.length;
514 if (does_paging) {
515 String maxdocs_str = (String)params.get(MAXDOCS_PARAM);
516 if (maxdocs_str != null) {
517 int maxdocs = Integer.parseInt(maxdocs_str);
518 docs_returned = (maxdocs < (int)totalDocs ? maxdocs : (int)totalDocs);
519 }
520 }
521 GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+docs_returned);
522
523 // add a metadata item to specify what actual query was done - eg if stuff was stripped out etc. and then we can use the query later, cos we don't know which parameter was the query
524 GSXML.addMetadata(this.doc, metadata_list, "query", query);
525 if (docs.length>0) {
526 Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
527 result.appendChild(document_list);
528 for (int d = 0; d < docs.length; d++) {
529 String doc_id = internalNum2OID(docs[d]);
530 Element doc_node = createDocNode(doc_id, doc_ranks[d]);
531 document_list.appendChild(doc_node);
532 }
533 }
534
535 // Create a term list to store the term information, and add it
536 Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
537 result.appendChild(term_list);
538 addTermInfo(term_list, params, query_result);
539
540 return result;
541
542 }
543
544 /** methods to handle actually doing the query */
545 /** do any initialisation of the query object */
546 abstract protected boolean setUpQueryer(HashMap params);
547 /** do the query */
548 abstract protected Object runQuery(String query);
549 /** get the total number of docs that match */
550 abstract protected long numDocsMatched(Object query_result);
551 /** get the list of doc ids */
552 abstract protected String [] getDocIDs(Object query_result);
553 /** get the list of doc ranks */
554 abstract protected String [] getDocRanks(Object query_result);
555 /** add in term info if available */
556 abstract protected boolean addTermInfo(Element term_list, HashMap params,
557 Object query_result);
558
559 /** combines all the field params into a single query
560 * - for simple field query */
561 /** We assume the combination (AND/OR) is done by the match param */
562 protected String parseFieldQueryParams(HashMap params) {
563
564 StringBuffer final_query = new StringBuffer(256);
565 String text_line = (String)params.get(FIELD_QUERY_PARAM);
566 String[] texts = text_line.split(",", -1);
567 String field_line = (String)params.get(FIELD_FIELD_PARAM);
568 String[] fields = field_line.split(",", -1);
569
570 for (int i=0; i<texts.length; i++) {
571 String q = texts[i].trim();
572 if (!q.equals("")) {
573 final_query.append(" "+addFieldInfo(q, fields[i]));
574 }
575 }
576
577 return final_query.toString();
578 }
579
580 abstract protected String addFieldInfo(String query, String field);
581
582 /** combines all the field params into a single query
583 * - for advanced field query */
584 protected String parseAdvancedFieldQueryParams(HashMap params) {
585
586 StringBuffer final_query = new StringBuffer(256);
587 String text_line = (String)params.get(FIELD_QUERY_PARAM);
588 String[] texts = text_line.split(",", -1);
589 String field_line = (String)params.get(FIELD_FIELD_PARAM);
590 String[] fields = field_line.split(",", -1);
591 String [] cases = null;
592 String [] stems = null;
593 String [] accents = null;
594 if (does_case) {
595 String case_line = (String)params.get(FIELD_CASE_PARAM);
596 if (case_line !=null)
597 cases = case_line.split(",", -1);
598 }
599 if (does_stem) {
600 String stem_line = (String)params.get(FIELD_STEM_PARAM);
601 if (stem_line !=null)
602 stems = stem_line.split(",", -1);
603 }
604 if (does_accent) {
605 String accent_line = (String)params.get(FIELD_ACCENT_PARAM);
606 if (accent_line !=null)
607 accents = accent_line.split(",", -1);
608 }
609 String combine_line = (String)params.get(FIELD_COMBINE_PARAM);
610 String [] combines = combine_line.split(",", -1);
611 String combine = "";
612 for (int i=0; i<texts.length; i++) {
613 if (i==0) {// assume first one is blank
614 combine = "";
615 } else {
616 String x = combines[i];
617 if (x.equals(FIELD_COMBINE_PARAM_AND)) {
618 combine = AND_OPERATOR;
619 } else if (x.equals(FIELD_COMBINE_PARAM_OR)) {
620 combine = OR_OPERATOR;
621 } else if (x.equals(FIELD_COMBINE_PARAM_NOT)) {
622 combine = NOT_OPERATOR;
623 }
624
625 }
626
627 String q = texts[i].trim();
628 boolean modified = false;
629 if (!q.equals("")) {
630 String c = null;
631 String s = null;
632 String a = null;
633 if (does_case) {
634 modified = true;
635 c = cases[i];
636 }
637 if (does_stem) {
638 modified = true;
639 s = stems[i];
640 }
641 if (does_accent) {
642 modified = true;
643 a = accents[i];
644 }
645 if (modified) {
646 q = addStemOptions(q, s, c, a);
647 }
648 addQueryElem(final_query, q, fields[i], combine);
649 }
650 }
651 return final_query.toString();
652 }
653
654 abstract protected void addQueryElem(StringBuffer final_query,
655 String query, String field,
656 String combine);
657 abstract protected String addStemOptions(String query, String stem,
658 String casef, String accent);
659
660}
661
662
Note: See TracBrowser for help on using the repository browser.