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

Last change on this file since 29731 was 29731, checked in by kjdon, 9 years ago

need to add the metadata for the form searches so that the action knows if it does paging or not. I think this will currently add it for raw search too. Don't know what raw search is. may need to remove it for that??

  • Property svn:keywords set to Author Date Id Revision
File size: 30.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// Greenstone classes
21import java.io.Serializable;
22import java.util.ArrayList;
23import java.util.HashMap;
24
25import org.apache.log4j.Logger;
26import org.greenstone.gsdl3.util.FacetWrapper;
27import org.greenstone.gsdl3.util.GSXML;
28import org.greenstone.gsdl3.util.XMLConverter;
29
30import org.w3c.dom.Document;
31import org.w3c.dom.Element;
32import org.w3c.dom.NodeList;
33
34abstract public class AbstractGS2FieldSearch extends AbstractGS2TextSearch
35{
36 // extra services offered by mgpp collections
37 protected static final String FIELD_QUERY_SERVICE = "FieldQuery";
38 protected static final String ADVANCED_FIELD_QUERY_SERVICE = "AdvancedFieldQuery";
39 protected static final String RAW_QUERY_SERVICE = "RawQuery";
40
41 // extra parameters used
42 protected static final String LEVEL_PARAM = "level";
43 protected static final String RANK_PARAM = "sortBy";
44 protected static final String RANK_PARAM_RANK = "1";
45 protected static final String RANK_PARAM_NONE = "0";
46 protected static final String SIMPLE_FIELD_PARAM = "simpleField";
47 protected static final String ADVANCED_FIELD_PARAM = "complexField";
48
49 protected static final String RAW_PARAM = "rawquery";
50
51 // more params for field query
52 protected static final String FIELD_QUERY_PARAM = "fqv";
53 protected static final String FIELD_STEM_PARAM = "fqs";
54 protected static final String FIELD_CASE_PARAM = "fqc";
55 protected static final String FIELD_ACCENT_PARAM = "fqa";
56 protected static final String FIELD_FIELD_PARAM = "fqf";
57 protected static final String FIELD_COMBINE_PARAM = "fqk";
58 protected static final String FIELD_COMBINE_PARAM_AND = "0";
59 protected static final String FIELD_COMBINE_PARAM_OR = "1";
60 protected static final String FIELD_COMBINE_PARAM_NOT = "2";
61
62 // some stuff for config files
63 protected static final String SEARCH_TYPE_ELEM = "searchType";
64 protected static final String SEARCH_TYPE_PLAIN = "plain";
65 protected static final String SEARCH_TYPE_SIMPLE_FORM = "simpleform";
66 protected static final String SEARCH_TYPE_ADVANCED_FORM = "advancedform";
67 protected static final String SEARCH_TYPE_RAW = "raw";
68
69 protected static final String DEFAULT_LEVEL_ELEM = "defaultLevel";
70 protected static final String DEFAULT_DB_LEVEL_ELEM = "defaultDBLevel";
71 protected static final String LEVEL_ELEM = "level";
72 protected static final String FIELD_ATT = "field";
73
74 protected static final int TEXT_QUERY = 0;
75 protected static final int SIMPLE_QUERY = 1;
76 protected static final int ADVANCED_QUERY = 2;
77 protected static final int RAW_QUERY = 3;
78
79 protected String AND_OPERATOR = "&";
80 protected String OR_OPERATOR = "|";
81 protected String NOT_OPERATOR = "!";
82
83 // the default level for searching
84 protected String default_level = null;
85 // default level for collection db
86 protected String default_db_level = null;
87 // metadata for service, such as does_paging = true
88 protected Element service_metadata_list = null;
89 // which search services will we offer??
90 protected boolean plain_search = false;
91 protected boolean simple_form_search = false;
92 protected boolean advanced_form_search = false;
93 protected boolean raw_search = false;
94
95 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractGS2FieldSearch.class.getName());
96
97 /** constructor */
98 public AbstractGS2FieldSearch()
99 {
100 super();
101 paramDefaults.put(RANK_PARAM, RANK_PARAM_RANK);
102 paramDefaults.put(FIELD_COMBINE_PARAM, FIELD_COMBINE_PARAM_AND);
103 // paramDefaults.put(FIELD_CASE_PARAM, BOOLEAN_PARAM_ON);
104 // paramDefaults.put(FIELD_STEM_PARAM, BOOLEAN_PARAM_OFF);
105 // paramDefaults.put(FIELD_ACCENT_PARAM, BOOLEAN_PARAM_ON);
106 }
107
108 public void cleanUp()
109 {
110 super.cleanUp();
111 }
112
113 /** configure this service */
114 public boolean configure(Element info, Element extra_info)
115 {
116 if (!super.configure(info, extra_info))
117 {
118 return false;
119 }
120
121 // Get the default level out of <defaultLevel> (buildConfig.xml)
122 Element def = (Element) GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
123 if (def != null)
124 {
125 this.default_level = def.getAttribute(GSXML.SHORTNAME_ATT);
126 }
127 if (this.default_level == null || this.default_level.equals(""))
128 {
129 logger.error("default level not specified!, assuming Doc");
130 this.default_level = "Doc";
131 }
132
133 // Get the default DB level
134 def = (Element) GSXML.getChildByTagName(info, DEFAULT_DB_LEVEL_ELEM);
135 if (def != null)
136 {
137 this.default_db_level = def.getAttribute(GSXML.SHORTNAME_ATT);
138 }
139 if (this.default_db_level == null || this.default_db_level.equals(""))
140 {
141 logger.error("default database level (defaultDBLevel) not specified!, assuming Sec");
142 this.default_db_level = "Sec";
143 }
144
145 // get stuff from from extra info (which is collectionConfig.xml)
146 if (extra_info != null)
147 {
148
149 // the search element
150 Element config_search = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
151
152 NodeList search_types = config_search.getElementsByTagName(SEARCH_TYPE_ELEM);
153 if (search_types == null)
154 {
155 // none specified, assume plain only
156 this.plain_search = true;
157 }
158 else
159 {
160 for (int i = 0; i < search_types.getLength(); i++)
161 {
162 Element t = (Element) search_types.item(i);
163
164 String type_name = t.getAttribute(GSXML.NAME_ATT);
165 if (type_name.equals(SEARCH_TYPE_PLAIN))
166 {
167 this.plain_search = true;
168 }
169 else if (type_name.equals(SEARCH_TYPE_SIMPLE_FORM))
170 {
171 this.simple_form_search = true;
172 }
173 else if (type_name.equals(SEARCH_TYPE_ADVANCED_FORM))
174 {
175 this.advanced_form_search = true;
176 }
177 else if (type_name.equals(SEARCH_TYPE_RAW))
178 {
179 this.raw_search = true;
180 }
181 }
182 }
183
184 // AbstractGS2TextSearch has set up the TextQuery service, but we may not want it
185 if (!this.plain_search)
186 {
187 // need to remove the TextQuery service
188 Element tq_service = GSXML.getNamedElement(short_service_info, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, QUERY_SERVICE);
189 short_service_info.removeChild(tq_service);
190 }
191
192 Document owner = info.getOwnerDocument();
193
194 NodeList levels = info.getElementsByTagName(GSXML.LEVEL_ELEM);
195
196 for (int i = 0; i < levels.getLength(); i++)
197 {
198 Element lev = (Element) levels.item(i);
199 String name = lev.getAttribute(GSXML.NAME_ATT);
200 Element node_extra = GSXML.getNamedElement(config_search, GSXML.LEVEL_ELEM, GSXML.NAME_ATT, name);
201 if (node_extra == null)
202 {
203 logger.error("haven't found extra info for level named " + name);
204 continue;
205 }
206
207 // get the display elements if any - displayName
208 NodeList display_names = node_extra.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
209 if (display_names != null)
210 {
211 for (int j = 0; j < display_names.getLength(); j++)
212 {
213 Element e = (Element) display_names.item(j);
214 lev.appendChild(owner.importNode(e, true));
215 }
216 }
217 } // for each level
218 }
219 else
220 {
221 // for soem reason we don't have the collectionConfig file. assume plain only
222 this.plain_search = true;
223 }
224
225 // the format info is the same for all services
226 Element format_info = (Element) format_info_map.get(QUERY_SERVICE);
227
228 // set up the extra services which are available for this collection
229 if (this.simple_form_search)
230 {
231 // set up short_service_info_ - for now just has id and type - name will be added in on the fly
232 Element fq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
233 fq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
234 fq_service.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
235 this.short_service_info.appendChild(fq_service);
236
237 if (format_info != null)
238 {
239 this.format_info_map.put(FIELD_QUERY_SERVICE, format_info);
240 }
241 }
242
243 if (this.advanced_form_search)
244 {
245 Element afq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
246 afq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
247 afq_service.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
248 this.short_service_info.appendChild(afq_service);
249
250 if (format_info != null)
251 {
252 this.format_info_map.put(ADVANCED_FIELD_QUERY_SERVICE, format_info);
253 }
254 }
255
256 if (this.raw_search)
257 {
258 Element rq_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
259 rq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
260 rq_service.setAttribute(GSXML.NAME_ATT, RAW_QUERY_SERVICE);
261 this.short_service_info.appendChild(rq_service);
262
263 if (format_info != null)
264 {
265 this.format_info_map.put(RAW_QUERY_SERVICE, format_info);
266 }
267 }
268
269 return true;
270 }
271
272 // if field case/stem/accent defaults are not specified explicitly, we should use case/stem/accent defaults
273 protected void getSearchParamDefaults(Element search_elem) {
274
275 boolean found_f_stem = false;
276 boolean found_f_case = false;
277 boolean found_f_accent = false;
278 NodeList param_defaults_list = GSXML.getChildrenByTagName(search_elem, GSXML.PARAM_DEFAULT_ELEM);
279 for (int i=0; i<param_defaults_list.getLength(); i++) {
280 Element paramdef = (Element)param_defaults_list.item(i);
281 String name = paramdef.getAttribute(GSXML.NAME_ATT);
282 String val = paramdef.getAttribute(GSXML.VALUE_ATT);
283 if (!name.equals("") && !val.equals("")) {
284 paramDefaults.put(name, val);
285 if (name.equals(FIELD_STEM_PARAM)) {
286 found_f_stem = true;
287 } else if (name.equals(FIELD_CASE_PARAM)) {
288 found_f_case = true;
289 } else if (name.equals(FIELD_ACCENT_PARAM)) {
290 found_f_accent = true;
291 }
292 if (!found_f_stem) {
293 paramDefaults.put (FIELD_STEM_PARAM, paramDefaults.get(STEM_PARAM));
294 }
295 if (!found_f_case) {
296 paramDefaults.put (FIELD_CASE_PARAM, paramDefaults.get(CASE_PARAM));
297 }
298 if (!found_f_accent) {
299 paramDefaults.put (FIELD_ACCENT_PARAM, paramDefaults.get(ACCENT_PARAM));
300 }
301
302 }
303 }
304 }
305 protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
306 {
307 // should we check that the service is actually on offer? presumably we wont get asked for services that we haven't advertised previously.
308
309 if (!service_id.equals(FIELD_QUERY_SERVICE) && !service_id.equals(ADVANCED_FIELD_QUERY_SERVICE) && !service_id.equals(RAW_QUERY_SERVICE))
310 {
311 return super.getServiceDescription(doc, service_id, lang, subset);
312 }
313
314 Element service = doc.createElement(GSXML.SERVICE_ELEM);
315 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
316 service.setAttribute(GSXML.NAME_ATT, service_id);
317 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
318 {
319 service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_NAME, getTextString(service_id + ".name", lang)));
320 service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(service_id + ".submit", lang)));
321 service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(service_id + ".description", lang)));
322
323 }
324 if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
325 {
326 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
327 service.appendChild(param_list);
328 if (service_id.equals(FIELD_QUERY_SERVICE))
329 {
330
331 addCustomQueryParams(param_list, lang);
332 if (!default_index_subcollection.equals(""))
333 {
334 createParameter(INDEX_SUBCOLLECTION_PARAM, param_list, lang);
335 }
336 if (!default_index_language.equals(""))
337 {
338 createParameter(INDEX_LANGUAGE_PARAM, param_list, lang);
339 }
340
341 if (does_chunking) {
342 createParameter(MAXDOCS_PARAM, param_list, lang);
343 }
344 if (does_paging)
345 {
346 createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
347 createParameter(START_PAGE_PARAM, param_list, lang);
348 }
349
350 // create a multi param for the fields etc
351 // text box, field
352 Element multiparam = null;
353 Element param = null;
354 multiparam = GSXML.createParameterDescription(doc, SIMPLE_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
355 multiparam.setAttribute("occurs", "4");
356 param_list.appendChild(multiparam);
357
358 // the components
359
360 createParameter(FIELD_QUERY_PARAM, multiparam, lang);
361 createParameter(FIELD_FIELD_PARAM, multiparam, lang);
362
363 }
364 else if (service_id.equals(ADVANCED_FIELD_QUERY_SERVICE))
365 {
366 addCustomQueryParamsAdvField(param_list, lang);
367 if (!default_index_subcollection.equals(""))
368 {
369 createParameter(INDEX_SUBCOLLECTION_PARAM, param_list, lang);
370 }
371 if (!default_index_language.equals(""))
372 {
373 createParameter(INDEX_LANGUAGE_PARAM, param_list, lang);
374 }
375
376 // create a multi param for the fields etc
377 // text box, stem, case, field
378
379 Element multiparam = null;
380 Element param = null;
381
382 multiparam = GSXML.createParameterDescription(doc, ADVANCED_FIELD_PARAM, "", GSXML.PARAM_TYPE_MULTI, null, null, null);
383 multiparam.setAttribute("occurs", "4");
384 param_list.appendChild(multiparam);
385
386 createParameter(FIELD_COMBINE_PARAM, multiparam, lang);
387 createParameter(FIELD_QUERY_PARAM, multiparam, lang);
388 if (this.does_case)
389 {
390 createParameter(FIELD_CASE_PARAM, multiparam, lang);
391 }
392 if (this.does_stem)
393 {
394 createParameter(FIELD_STEM_PARAM, multiparam, lang);
395 }
396 if (this.does_accent)
397 {
398 createParameter(FIELD_ACCENT_PARAM, multiparam, lang);
399 }
400 if (does_chunking) {
401 createParameter(MAXDOCS_PARAM, param_list, lang);
402 }
403 if (does_paging)
404 {
405 createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
406 createParameter(START_PAGE_PARAM, param_list, lang);
407 }
408 createParameter(FIELD_FIELD_PARAM, multiparam, lang);
409
410 }
411 else if (service_id.equals(RAW_QUERY_SERVICE))
412 {
413 if (does_chunking)
414 {
415 createParameter(MAXDOCS_PARAM, param_list, lang);
416 }
417 if (does_paging)
418 {
419 createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
420 createParameter(START_PAGE_PARAM, param_list, lang);
421 }
422 createParameter(RAW_PARAM, param_list, lang);
423 service.appendChild(param_list);
424 }
425 }
426 if (subset == null || subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER)) {
427 if (service_metadata_list == null) {
428 Document ml_doc = XMLConverter.newDOM();
429 service_metadata_list = ml_doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
430 if (does_paging) {
431 service_metadata_list.appendChild(GSXML.createMetadataElement(ml_doc, "does_paging", "true"));
432 }
433 }
434 service.appendChild(doc.importNode(service_metadata_list, true));
435 }
436 return service;
437
438 }
439
440 /** add in the level params to TextQuery */
441 protected void addCustomQueryParams(Element param_list, String lang)
442 {
443 createParameter(LEVEL_PARAM, param_list, lang);
444 super.addCustomQueryParams(param_list, lang);
445 }
446 /** add in the level params to TextQuery */
447 protected void addCustomQueryParamsAdvField(Element param_list, String lang)
448 {
449 createParameter(LEVEL_PARAM, param_list, lang);
450 }
451
452 /** create a param and add to the list */
453 protected void createParameter(String name, Element param_list, String lang)
454 {
455 Document doc = param_list.getOwnerDocument();
456 Element param = null;
457 String param_default = paramDefaults.get(name);
458 if (name.equals(LEVEL_PARAM))
459 {
460 ArrayList<String> level_ids = new ArrayList<String>();
461 ArrayList<String> level_names = new ArrayList<String>();
462 getLevelData(level_ids, level_names, lang);
463 if (level_ids.size() > 1)
464 {
465 // the first one is the default
466 //param = GSXML.createParameterDescription2(doc, LEVEL_PARAM, getTextString("param."+LEVEL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)level_ids.get(0), level_ids, level_names);
467 param = GSXML.createParameterDescription2(doc, LEVEL_PARAM, getTextString("param." + LEVEL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, this.default_level, level_ids, level_names);
468 }
469 else
470 {
471 // we need to set the level, but hidden, in case there is an invalid level saved
472 //param = GSXML.createParameterDescription(doc, LEVEL_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, (String)level_ids.get(0), null, null);
473 param = GSXML.createParameterDescription(doc, LEVEL_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, this.default_level, null, null);
474 }
475 }
476 else if (name.equals(RANK_PARAM))
477 {
478 String[] vals1 = { RANK_PARAM_RANK, RANK_PARAM_NONE };
479 String[] vals1_texts = { getTextString("param." + RANK_PARAM + "." + RANK_PARAM_RANK, lang), getTextString("param." + RANK_PARAM + "." + RANK_PARAM_NONE, lang) };
480
481 param = GSXML.createParameterDescription(doc, RANK_PARAM, getTextString("param." + RANK_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, vals1, vals1_texts);
482
483 }
484 else if (name.equals(FIELD_QUERY_PARAM))
485 {
486 param = GSXML.createParameterDescription(doc, FIELD_QUERY_PARAM, getTextString("param." + FIELD_QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
487
488 }
489 else if (name.equals(FIELD_CASE_PARAM) || name.equals(FIELD_STEM_PARAM) || name.equals(FIELD_ACCENT_PARAM))
490 {
491 String[] bool_ops = { "0", "1" };
492 String[] bool_texts = { getTextString("param.boolean.off", lang, "AbstractTextSearch"), getTextString("param.boolean.on", lang, "AbstractTextSearch") };
493 param = GSXML.createParameterDescription(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_BOOLEAN, param_default, bool_ops, bool_texts);
494
495 }
496 else if (name.equals(FIELD_FIELD_PARAM))
497 {
498 ArrayList<String> fields = new ArrayList<String>();
499 ArrayList<String> field_names = new ArrayList<String>();
500 getIndexData(fields, field_names, lang);
501 // the field list - read from config file
502
503 // Fix for http://trac.greenstone.org/ticket/245 "java crash, index out of bounds"
504 // org.greenstone.gsdl3.service.AbstractGS2FieldSearch.createParameter(AbstractGS2FieldSearch.java:362)
505 // Changed from:
506 // param = GSXML.createParameterDescription2(doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)fields.get(0), fields, field_names );
507 String default_value = (fields.size() > 0) ? fields.get(0) : null;
508 // don't want to access element 0 if fields.size()==0, and
509 // GSXML.createParameterDescription2 checks for default_value==null condition
510 param = GSXML.createParameterDescription2(doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, default_value, fields, field_names);
511
512 }
513 else if (name.equals(FIELD_COMBINE_PARAM))
514 {
515
516 String[] vals = { FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT };
517 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) };
518
519 param = GSXML.createParameterDescription(doc, FIELD_COMBINE_PARAM, "", GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, vals, val_texts);
520 param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
521 }
522
523 if (param != null)
524 {
525 param_list.appendChild(param);
526 }
527 else
528 {
529 super.createParameter(name, param_list, lang);
530 }
531 }
532
533 // should cache some of this
534 protected void getLevelData(ArrayList<String> level_ids, ArrayList<String> level_names, String lang)
535 {
536 Element level_list = (Element) GSXML.getChildByTagName(this.config_info, LEVEL_ELEM + GSXML.LIST_MODIFIER);
537 NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
538 for (int i = 0; i < levels.getLength(); i++)
539 {
540 Element level = (Element) levels.item(i);
541 String shortname = level.getAttribute(GSXML.SHORTNAME_ATT);
542 if (shortname.equals(""))
543 {
544 continue;
545 }
546 level_ids.add(shortname);
547 String display_name = GSXML.getDisplayText(level, GSXML.DISPLAY_TEXT_NAME, lang, "en");
548 if (display_name.equals(""))
549 {
550 // we'll use the name, and the dictionary
551 display_name = level.getAttribute(GSXML.NAME_ATT);
552 if (display_name.equals(""))
553 {
554 display_name = shortname;
555 }
556 else
557 {
558 display_name = getTextString("level." + display_name, lang);
559 }
560 }
561 level_names.add(display_name);
562 }
563 }
564
565 // the following three functions are needed so the base class can
566 // call the process+SERVICE_NAME methods
567 /** process a text query */
568 protected Element processTextQuery(Element request)
569 {
570 return processAnyQuery(request, TEXT_QUERY);
571 }
572
573 /** process a field query */
574 protected Element processFieldQuery(Element request)
575 {
576 return processAnyQuery(request, SIMPLE_QUERY);
577 }
578
579 /** process an advanced field query */
580 protected Element processAdvancedFieldQuery(Element request)
581 {
582 return processAnyQuery(request, ADVANCED_QUERY);
583 }
584
585 protected Element processRawQuery(Element request)
586 {
587 return processAnyQuery(request, RAW_QUERY);
588 }
589
590 /** process a query */
591 protected Element processAnyQuery(Element request, int query_type)
592 {
593 String service_name = null;
594 String empty_query_test_param = null;
595 // set up the type specific bits
596 switch (query_type)
597 {
598 case TEXT_QUERY:
599 service_name = QUERY_SERVICE;
600 empty_query_test_param = QUERY_PARAM;
601 break;
602 case SIMPLE_QUERY:
603 service_name = FIELD_QUERY_SERVICE;
604 empty_query_test_param = FIELD_QUERY_PARAM;
605 break;
606 case ADVANCED_QUERY:
607 service_name = ADVANCED_FIELD_QUERY_SERVICE;
608 empty_query_test_param = FIELD_QUERY_PARAM;
609 break;
610 case RAW_QUERY:
611 service_name = RAW_QUERY_SERVICE;
612 empty_query_test_param = RAW_PARAM;
613 break;
614 default:
615 // should never get here
616 logger.error("wrong query type!!");
617 return null;
618 }
619
620 // Create a new (empty) result message
621 Document result_doc = XMLConverter.newDOM();
622 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
623 result.setAttribute(GSXML.FROM_ATT, service_name);
624 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
625
626 // Get the parameters of the request
627 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
628 if (param_list == null)
629 {
630 logger.error("TextQuery request had no paramList.");
631 return result; // Return the empty result
632 }
633
634 // Process the request parameters
635 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
636
637 // Make sure a query has been specified
638 String query = (String) params.get(empty_query_test_param);
639 if (query == null || query.equals(""))
640 {
641 return result; // Return the empty result
642 }
643
644 // If a field hasn't been specified, use the default - for textQuery
645 String field = (String) params.get(INDEX_PARAM);
646 if (field == null)
647 {
648 field = default_index;
649 }
650
651 // set up the appropriate query system
652 if (!setUpQueryer(params))
653 {
654 return result;
655 }
656
657 // if field search, create the query string
658 switch (query_type)
659 {
660 case TEXT_QUERY:
661 query = addFieldInfo(query, field);
662 break;
663 case SIMPLE_QUERY:
664 query = parseFieldQueryParams(params);
665 break;
666 case ADVANCED_QUERY:
667 query = parseAdvancedFieldQueryParams(params);
668 break;
669 }
670
671 // run the query
672 Object query_result = runQuery(query);
673
674 // build up the response
675
676 // Create a metadata list to store information about the query results
677 // should we be using metadataList? or something else?
678 Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
679 result.appendChild(metadata_list);
680
681 // Add a metadata element specifying the number of matching documents
682 long totalDocs = numDocsMatched(query_result);
683
684 GSXML.addMetadata(metadata_list, "numDocsMatched", "" + totalDocs);
685
686 // Create a document list to store the matching documents, and add them
687 String[] docs = getDocIDs(query_result);
688 String[] doc_ranks = getDocRanks(query_result);
689
690 // add a metadata item to specify docs returned
691 if (does_chunking) // this means we have a max docs param, and might ask for only a subset of results
692 {
693 logger.error("does_chunking = true");
694 int docs_returned = docs.length;
695 String maxdocs_str = (String) params.get(MAXDOCS_PARAM);
696 if (maxdocs_str != null)
697 {
698 int maxdocs = Integer.parseInt(maxdocs_str);
699 if (maxdocs > 0) { // maxdocs==-1 means return all
700 docs_returned = (maxdocs < (int) totalDocs ? maxdocs : (int) totalDocs);
701 }
702 }
703 GSXML.addMetadata(metadata_list, "numDocsReturned", "" + docs_returned);
704 }
705
706
707 // 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
708 GSXML.addMetadata(metadata_list, "query", query);
709 if (docs.length > 0)
710 {
711 Element document_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
712 result.appendChild(document_list);
713 for (int d = 0; d < docs.length; d++)
714 {
715 String doc_id = internalNum2OID(docs[d]);
716 Element doc_node = createDocNode(result_doc, doc_id, doc_ranks[d]);
717 document_list.appendChild(doc_node);
718 }
719 }
720
721 // Create a term list to store the term information, and add it
722 Element term_list = result_doc.createElement(GSXML.TERM_ELEM + GSXML.LIST_MODIFIER);
723 result.appendChild(term_list);
724 addTermInfo(term_list, params, query_result);
725
726 if(does_faceting)
727 {
728 ArrayList<FacetWrapper> facets = getFacets(query_result);
729 if(facets != null)
730 {
731 Element facet_list = result_doc.createElement(GSXML.FACET_ELEM + GSXML.LIST_MODIFIER);
732 result.appendChild(facet_list);
733
734 for(FacetWrapper currentFacet : facets)
735 {
736 Element facet_elem = result_doc.createElement(GSXML.FACET_ELEM);
737 facet_elem.setAttribute(GSXML.NAME_ATT, currentFacet.getName());
738 facet_list.appendChild(facet_elem);
739
740 HashMap<String, Long> countMap = currentFacet.getCounts();
741
742 for(String countName : countMap.keySet())
743 {
744 long countValue = countMap.get(countName);
745 if(countValue > 0)
746 {
747 Element count_elem = result_doc.createElement(GSXML.COUNT_ELEM);
748 count_elem.setAttribute(GSXML.NAME_ATT, countName);
749 count_elem.setTextContent("" + countValue);
750
751 facet_elem.appendChild(count_elem);
752 }
753 }
754 }
755 }
756 }
757
758 return result;
759
760 }
761
762 /** methods to handle actually doing the query */
763 /** do any initialisation of the query object */
764 abstract protected boolean setUpQueryer(HashMap<String, Serializable> params);
765
766 /** do the query */
767 abstract protected Object runQuery(String query);
768
769 /** get the total number of docs that match */
770 abstract protected long numDocsMatched(Object query_result);
771
772 /** get the list of doc ids */
773 abstract protected String[] getDocIDs(Object query_result);
774
775 /** get the list of doc ranks */
776 abstract protected String[] getDocRanks(Object query_result);
777
778 /** get the list of facets */
779 abstract protected ArrayList<FacetWrapper> getFacets(Object query_result);
780
781 /** add in term info if available */
782 abstract protected boolean addTermInfo(Element term_list, HashMap<String, Serializable> params, Object query_result);
783
784 /**
785 * combines all the field params into a single query - for simple field
786 * query
787 */
788 /** We assume the combination (AND/OR) is done by the match param */
789 protected String parseFieldQueryParams(HashMap<String, Serializable> params)
790 {
791
792 StringBuffer final_query = new StringBuffer(256);
793 String text_line = (String) params.get(FIELD_QUERY_PARAM);
794 String[] texts = text_line.split(",", -1);
795 String field_line = (String) params.get(FIELD_FIELD_PARAM);
796 String[] fields = field_line.split(",", -1);
797
798 for (int i = 0; i < texts.length; i++)
799 {
800 String q = texts[i].trim();
801 if (!q.equals(""))
802 {
803 final_query.append(" " + addFieldInfo(q, fields[i]));
804 }
805 }
806
807 return final_query.toString();
808 }
809
810 abstract protected String addFieldInfo(String query, String field);
811
812 /**
813 * combines all the field params into a single query - for advanced field
814 * query
815 */
816 protected String parseAdvancedFieldQueryParams(HashMap<String, Serializable> params)
817 {
818
819 StringBuffer final_query = new StringBuffer(256);
820 String text_line = (String) params.get(FIELD_QUERY_PARAM);
821 String[] texts = text_line.split(",", -1);
822 String field_line = (String) params.get(FIELD_FIELD_PARAM);
823 String[] fields = field_line.split(",", -1);
824 String[] cases = null;
825 String[] stems = null;
826 String[] accents = null;
827 if (does_case)
828 {
829 String case_line = (String) params.get(FIELD_CASE_PARAM);
830 if (case_line != null)
831 cases = case_line.split(",", -1);
832 }
833 if (does_stem)
834 {
835 String stem_line = (String) params.get(FIELD_STEM_PARAM);
836 if (stem_line != null)
837 stems = stem_line.split(",", -1);
838 }
839 if (does_accent)
840 {
841 String accent_line = (String) params.get(FIELD_ACCENT_PARAM);
842 if (accent_line != null)
843 accents = accent_line.split(",", -1);
844 }
845 String combine_line = (String) params.get(FIELD_COMBINE_PARAM);
846 String[] combines = combine_line.split(",", -1);
847 String combine = "";
848 for (int i = 0; i < texts.length; i++)
849 {
850 if (i == 0)
851 {// assume first one is blank
852 combine = "";
853 }
854 else
855 {
856 String x = combines[i];
857 if (x.equals(FIELD_COMBINE_PARAM_AND))
858 {
859 combine = AND_OPERATOR;
860 }
861 else if (x.equals(FIELD_COMBINE_PARAM_OR))
862 {
863 combine = OR_OPERATOR;
864 }
865 else if (x.equals(FIELD_COMBINE_PARAM_NOT))
866 {
867 combine = NOT_OPERATOR;
868 }
869
870 }
871
872 String q = texts[i].trim();
873 boolean modified = false;
874 if (!q.equals(""))
875 {
876 String c = null;
877 String s = null;
878 String a = null;
879 if (does_case)
880 {
881 modified = true;
882 c = cases[i];
883 }
884 if (does_stem)
885 {
886 modified = true;
887 s = stems[i];
888 }
889 if (does_accent)
890 {
891 modified = true;
892 a = accents[i];
893 }
894 if (modified)
895 {
896 q = addStemOptions(q, s, c, a);
897 }
898 addQueryElem(final_query, q, fields[i], combine);
899 }
900 }
901 return final_query.toString();
902 }
903
904 abstract protected void addQueryElem(StringBuffer final_query, String query, String field, String combine);
905
906 abstract protected String addStemOptions(String query, String stem, String casef, String accent);
907
908}
Note: See TracBrowser for help on using the repository browser.