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