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 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.mgpp.*;
|
---|
23 | import org.greenstone.gsdl3.util.*;
|
---|
24 |
|
---|
25 | // XML classes
|
---|
26 | import org.w3c.dom.Document;
|
---|
27 | import org.w3c.dom.Element;
|
---|
28 | import org.w3c.dom.NodeList;
|
---|
29 |
|
---|
30 | // java classes
|
---|
31 | import java.util.Iterator;
|
---|
32 | import java.util.Set;
|
---|
33 | import java.util.HashMap;
|
---|
34 | import java.util.Map;
|
---|
35 | import java.util.ArrayList;
|
---|
36 | import java.util.Vector;
|
---|
37 | import java.io.File;
|
---|
38 |
|
---|
39 | import org.apache.log4j.*;
|
---|
40 |
|
---|
41 |
|
---|
42 | abstract 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 | param = GSXML.createParameterDescription2(this.doc, LEVEL_PARAM, getTextString("param."+LEVEL_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, this.default_level, level_ids, level_names);
|
---|
337 | } else {
|
---|
338 | // we need to set the level, but hidden, in case there is an invalid level saved
|
---|
339 | //param = GSXML.createParameterDescription(this.doc, LEVEL_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, (String)level_ids.get(0), null, null);
|
---|
340 | param = GSXML.createParameterDescription(this.doc, LEVEL_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, this.default_level, null, null);
|
---|
341 | }
|
---|
342 | } else if (name.equals(RANK_PARAM)) {
|
---|
343 | String [] vals1 = {RANK_PARAM_RANK, RANK_PARAM_NONE };
|
---|
344 | String [] vals1_texts = { getTextString("param."+RANK_PARAM+"."+RANK_PARAM_RANK, lang), getTextString("param."+RANK_PARAM+"."+RANK_PARAM_NONE, lang)};
|
---|
345 |
|
---|
346 | param = GSXML.createParameterDescription(this.doc, RANK_PARAM, getTextString("param."+RANK_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, RANK_PARAM_RANK, vals1, vals1_texts );
|
---|
347 |
|
---|
348 | } else if (name.equals(FIELD_QUERY_PARAM)) {
|
---|
349 | param = GSXML.createParameterDescription(this.doc, FIELD_QUERY_PARAM, getTextString("param."+FIELD_QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
350 |
|
---|
351 |
|
---|
352 | } else if (name.equals(FIELD_CASE_PARAM) || name.equals(FIELD_STEM_PARAM) || name.equals(FIELD_ACCENT_PARAM)) {
|
---|
353 | String[] bool_ops = {"0", "1"};
|
---|
354 | String[] bool_texts = {getTextString("param.boolean.off", lang, "AbstractSearch"),getTextString("param.boolean.on", lang, "AbstractSearch")};
|
---|
355 | param = GSXML.createParameterDescription(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, bool_ops, bool_texts);
|
---|
356 |
|
---|
357 | } else if (name.equals(FIELD_FIELD_PARAM)) {
|
---|
358 | ArrayList fields = new ArrayList();
|
---|
359 | ArrayList field_names = new ArrayList();
|
---|
360 | getIndexData(fields, field_names, lang);
|
---|
361 | // the field list - read from config file
|
---|
362 | param = GSXML.createParameterDescription2(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, (String)fields.get(0), fields, field_names );
|
---|
363 |
|
---|
364 | } else if (name.equals(FIELD_COMBINE_PARAM)) {
|
---|
365 |
|
---|
366 | String []vals = {FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT};
|
---|
367 | 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)};
|
---|
368 |
|
---|
369 |
|
---|
370 | param = GSXML.createParameterDescription(this.doc, FIELD_COMBINE_PARAM, "", GSXML.PARAM_TYPE_ENUM_SINGLE, FIELD_COMBINE_PARAM_AND, vals, val_texts);
|
---|
371 | param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (param != null) {
|
---|
375 | param_list.appendChild(param);
|
---|
376 | } else {
|
---|
377 | super.createParameter(name, param_list, lang);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | // should cache some of this
|
---|
382 | protected void getLevelData(ArrayList level_ids, ArrayList level_names, String lang)
|
---|
383 | {
|
---|
384 | Element level_list = (Element)GSXML.getChildByTagName(this.config_info, LEVEL_ELEM+GSXML.LIST_MODIFIER);
|
---|
385 | NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
|
---|
386 | for (int i=0; i<levels.getLength(); i++) {
|
---|
387 | Element level = (Element)levels.item(i);
|
---|
388 | String shortname = level.getAttribute(GSXML.SHORTNAME_ATT);
|
---|
389 | if (shortname.equals("")) {
|
---|
390 | continue;
|
---|
391 | }
|
---|
392 | level_ids.add(shortname);
|
---|
393 | String display_name = GSXML.getDisplayText(level, GSXML.DISPLAY_TEXT_NAME, lang, "en");
|
---|
394 | if (display_name.equals("")) {
|
---|
395 | // we'll use the name, and the dictionary
|
---|
396 | display_name = level.getAttribute(GSXML.NAME_ATT);
|
---|
397 | if (display_name.equals("")) {
|
---|
398 | display_name = shortname;
|
---|
399 | } else {
|
---|
400 | display_name = getTextString("level."+display_name, lang);
|
---|
401 | }
|
---|
402 | }
|
---|
403 | level_names.add(display_name);
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | // the following three functions are needed so the base class can
|
---|
408 | // call the process+SERVICE_NAME methods
|
---|
409 | /** process a text query */
|
---|
410 | protected Element processTextQuery(Element request) {
|
---|
411 | return processAnyQuery(request, TEXT_QUERY);
|
---|
412 | }
|
---|
413 |
|
---|
414 | /** process a field query */
|
---|
415 | protected Element processFieldQuery(Element request) {
|
---|
416 | return processAnyQuery(request, SIMPLE_QUERY);
|
---|
417 | }
|
---|
418 |
|
---|
419 | /** process an advanced field query */
|
---|
420 | protected Element processAdvancedFieldQuery(Element request) {
|
---|
421 | return processAnyQuery(request, ADVANCED_QUERY);
|
---|
422 | }
|
---|
423 |
|
---|
424 | /** process a query */
|
---|
425 | protected Element processAnyQuery(Element request, int query_type)
|
---|
426 | {
|
---|
427 |
|
---|
428 | String service_name=null;
|
---|
429 | String empty_query_test_param=null;
|
---|
430 | // set up the type specific bits
|
---|
431 | switch (query_type) {
|
---|
432 | case TEXT_QUERY:
|
---|
433 | service_name = TEXT_QUERY_SERVICE;
|
---|
434 | empty_query_test_param = QUERY_PARAM;
|
---|
435 | break;
|
---|
436 | case SIMPLE_QUERY:
|
---|
437 | service_name = FIELD_QUERY_SERVICE;
|
---|
438 | empty_query_test_param = FIELD_QUERY_PARAM;
|
---|
439 | break;
|
---|
440 | case ADVANCED_QUERY:
|
---|
441 | service_name = ADVANCED_FIELD_QUERY_SERVICE;
|
---|
442 | empty_query_test_param = FIELD_QUERY_PARAM;
|
---|
443 | break;
|
---|
444 | default:
|
---|
445 | // should never get here
|
---|
446 | logger.error("wrong query type!!");
|
---|
447 | return null;
|
---|
448 | }
|
---|
449 |
|
---|
450 | // Create a new (empty) result message
|
---|
451 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
452 | result.setAttribute(GSXML.FROM_ATT, service_name);
|
---|
453 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
454 |
|
---|
455 | // Get the parameters of the request
|
---|
456 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
457 | if (param_list == null) {
|
---|
458 | logger.error("TextQuery request had no paramList.");
|
---|
459 | return result; // Return the empty result
|
---|
460 | }
|
---|
461 |
|
---|
462 | // Process the request parameters
|
---|
463 | HashMap params = GSXML.extractParams(param_list, false);
|
---|
464 |
|
---|
465 | // Make sure a query has been specified
|
---|
466 | String query = (String) params.get(empty_query_test_param);
|
---|
467 | if (query == null || query.equals("")) {
|
---|
468 | return result; // Return the empty result
|
---|
469 | }
|
---|
470 |
|
---|
471 | // If a field hasn't been specified, use the default - for textQuery
|
---|
472 | String field = (String) params.get(INDEX_PARAM);
|
---|
473 | if (field == null) {
|
---|
474 | field = default_index;
|
---|
475 | }
|
---|
476 |
|
---|
477 | // set up the appropriate query system
|
---|
478 | if (!setUpQueryer(params)) {
|
---|
479 | return result;
|
---|
480 | }
|
---|
481 |
|
---|
482 | // if field search, create the query string
|
---|
483 | switch (query_type) {
|
---|
484 | case TEXT_QUERY:
|
---|
485 | query = addFieldInfo(query, field);
|
---|
486 | break;
|
---|
487 | case SIMPLE_QUERY:
|
---|
488 | query = parseFieldQueryParams(params);
|
---|
489 | break;
|
---|
490 | case ADVANCED_QUERY:
|
---|
491 | query = parseAdvancedFieldQueryParams(params);
|
---|
492 | break;
|
---|
493 | }
|
---|
494 |
|
---|
495 | // run the query
|
---|
496 | Object query_result = runQuery(query);
|
---|
497 |
|
---|
498 | // build up the response
|
---|
499 |
|
---|
500 | // Create a metadata list to store information about the query results
|
---|
501 | // should we be using metadataList? or something else?
|
---|
502 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
503 | result.appendChild(metadata_list);
|
---|
504 |
|
---|
505 | // Add a metadata element specifying the number of matching documents
|
---|
506 | long totalDocs = numDocsMatched(query_result);
|
---|
507 |
|
---|
508 | GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+totalDocs);
|
---|
509 |
|
---|
510 | // Create a document list to store the matching documents, and add them
|
---|
511 | String [] docs = getDocIDs(query_result);
|
---|
512 | String [] doc_ranks = getDocRanks(query_result);
|
---|
513 |
|
---|
514 | // add a metadata item to specify docs returned
|
---|
515 | int docs_returned = docs.length;
|
---|
516 | if (does_paging) {
|
---|
517 | String maxdocs_str = (String)params.get(MAXDOCS_PARAM);
|
---|
518 | if (maxdocs_str != null) {
|
---|
519 | int maxdocs = Integer.parseInt(maxdocs_str);
|
---|
520 | docs_returned = (maxdocs < (int)totalDocs ? maxdocs : (int)totalDocs);
|
---|
521 | }
|
---|
522 | }
|
---|
523 | GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+docs_returned);
|
---|
524 |
|
---|
525 | // 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
|
---|
526 | GSXML.addMetadata(this.doc, metadata_list, "query", query);
|
---|
527 | if (docs.length>0) {
|
---|
528 | Element document_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
529 | result.appendChild(document_list);
|
---|
530 | for (int d = 0; d < docs.length; d++) {
|
---|
531 | String doc_id = internalNum2OID(docs[d]);
|
---|
532 | Element doc_node = createDocNode(doc_id, doc_ranks[d]);
|
---|
533 | document_list.appendChild(doc_node);
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | // Create a term list to store the term information, and add it
|
---|
538 | Element term_list = this.doc.createElement(GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
539 | result.appendChild(term_list);
|
---|
540 | addTermInfo(term_list, params, query_result);
|
---|
541 |
|
---|
542 | return result;
|
---|
543 |
|
---|
544 | }
|
---|
545 |
|
---|
546 | /** methods to handle actually doing the query */
|
---|
547 | /** do any initialisation of the query object */
|
---|
548 | abstract protected boolean setUpQueryer(HashMap params);
|
---|
549 | /** do the query */
|
---|
550 | abstract protected Object runQuery(String query);
|
---|
551 | /** get the total number of docs that match */
|
---|
552 | abstract protected long numDocsMatched(Object query_result);
|
---|
553 | /** get the list of doc ids */
|
---|
554 | abstract protected String [] getDocIDs(Object query_result);
|
---|
555 | /** get the list of doc ranks */
|
---|
556 | abstract protected String [] getDocRanks(Object query_result);
|
---|
557 | /** add in term info if available */
|
---|
558 | abstract protected boolean addTermInfo(Element term_list, HashMap params,
|
---|
559 | Object query_result);
|
---|
560 |
|
---|
561 | /** combines all the field params into a single query
|
---|
562 | * - for simple field query */
|
---|
563 | /** We assume the combination (AND/OR) is done by the match param */
|
---|
564 | protected String parseFieldQueryParams(HashMap params) {
|
---|
565 |
|
---|
566 | StringBuffer final_query = new StringBuffer(256);
|
---|
567 | String text_line = (String)params.get(FIELD_QUERY_PARAM);
|
---|
568 | String[] texts = text_line.split(",", -1);
|
---|
569 | String field_line = (String)params.get(FIELD_FIELD_PARAM);
|
---|
570 | String[] fields = field_line.split(",", -1);
|
---|
571 |
|
---|
572 | for (int i=0; i<texts.length; i++) {
|
---|
573 | String q = texts[i].trim();
|
---|
574 | if (!q.equals("")) {
|
---|
575 | final_query.append(" "+addFieldInfo(q, fields[i]));
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | return final_query.toString();
|
---|
580 | }
|
---|
581 |
|
---|
582 | abstract protected String addFieldInfo(String query, String field);
|
---|
583 |
|
---|
584 | /** combines all the field params into a single query
|
---|
585 | * - for advanced field query */
|
---|
586 | protected String parseAdvancedFieldQueryParams(HashMap params) {
|
---|
587 |
|
---|
588 | StringBuffer final_query = new StringBuffer(256);
|
---|
589 | String text_line = (String)params.get(FIELD_QUERY_PARAM);
|
---|
590 | String[] texts = text_line.split(",", -1);
|
---|
591 | String field_line = (String)params.get(FIELD_FIELD_PARAM);
|
---|
592 | String[] fields = field_line.split(",", -1);
|
---|
593 | String [] cases = null;
|
---|
594 | String [] stems = null;
|
---|
595 | String [] accents = null;
|
---|
596 | if (does_case) {
|
---|
597 | String case_line = (String)params.get(FIELD_CASE_PARAM);
|
---|
598 | if (case_line !=null)
|
---|
599 | cases = case_line.split(",", -1);
|
---|
600 | }
|
---|
601 | if (does_stem) {
|
---|
602 | String stem_line = (String)params.get(FIELD_STEM_PARAM);
|
---|
603 | if (stem_line !=null)
|
---|
604 | stems = stem_line.split(",", -1);
|
---|
605 | }
|
---|
606 | if (does_accent) {
|
---|
607 | String accent_line = (String)params.get(FIELD_ACCENT_PARAM);
|
---|
608 | if (accent_line !=null)
|
---|
609 | accents = accent_line.split(",", -1);
|
---|
610 | }
|
---|
611 | String combine_line = (String)params.get(FIELD_COMBINE_PARAM);
|
---|
612 | String [] combines = combine_line.split(",", -1);
|
---|
613 | String combine = "";
|
---|
614 | for (int i=0; i<texts.length; i++) {
|
---|
615 | if (i==0) {// assume first one is blank
|
---|
616 | combine = "";
|
---|
617 | } else {
|
---|
618 | String x = combines[i];
|
---|
619 | if (x.equals(FIELD_COMBINE_PARAM_AND)) {
|
---|
620 | combine = AND_OPERATOR;
|
---|
621 | } else if (x.equals(FIELD_COMBINE_PARAM_OR)) {
|
---|
622 | combine = OR_OPERATOR;
|
---|
623 | } else if (x.equals(FIELD_COMBINE_PARAM_NOT)) {
|
---|
624 | combine = NOT_OPERATOR;
|
---|
625 | }
|
---|
626 |
|
---|
627 | }
|
---|
628 |
|
---|
629 | String q = texts[i].trim();
|
---|
630 | boolean modified = false;
|
---|
631 | if (!q.equals("")) {
|
---|
632 | String c = null;
|
---|
633 | String s = null;
|
---|
634 | String a = null;
|
---|
635 | if (does_case) {
|
---|
636 | modified = true;
|
---|
637 | c = cases[i];
|
---|
638 | }
|
---|
639 | if (does_stem) {
|
---|
640 | modified = true;
|
---|
641 | s = stems[i];
|
---|
642 | }
|
---|
643 | if (does_accent) {
|
---|
644 | modified = true;
|
---|
645 | a = accents[i];
|
---|
646 | }
|
---|
647 | if (modified) {
|
---|
648 | q = addStemOptions(q, s, c, a);
|
---|
649 | }
|
---|
650 | addQueryElem(final_query, q, fields[i], combine);
|
---|
651 | }
|
---|
652 | }
|
---|
653 | return final_query.toString();
|
---|
654 | }
|
---|
655 |
|
---|
656 | abstract protected void addQueryElem(StringBuffer final_query,
|
---|
657 | String query, String field,
|
---|
658 | String combine);
|
---|
659 | abstract protected String addStemOptions(String query, String stem,
|
---|
660 | String casef, String accent);
|
---|
661 |
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|