source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/AbstractSearch.java@ 28181

Last change on this file since 28181 was 28181, checked in by kjdon, 11 years ago

making search param defaults able to be set in config file. uses <paramDefault name=xx value=yy> element. Now all defaults are set in paramDefaults HashMap instead of individual variables. have left index etc ones for now as they are more complicated.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/*
2 * AbstractSearch.java
3 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.service;
20
21// Greenstone classes
22import java.util.ArrayList;
23import java.util.HashMap;
24
25import org.apache.log4j.Logger;
26import org.greenstone.gsdl3.util.AbstractBasicDocument;
27import org.greenstone.gsdl3.util.BasicDocument;
28import org.greenstone.gsdl3.util.GSPath;
29import org.greenstone.gsdl3.util.GSXML;
30import org.w3c.dom.Element;
31import org.w3c.dom.NodeList;
32
33/**
34 * Partially implements a generic search service
35 *
36 */
37
38public abstract class AbstractSearch extends ServiceRack
39{
40
41 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractSearch.class.getName());
42
43 // the search service
44 protected String QUERY_SERVICE = null; // set by subclass
45
46 // compulsory params
47 protected static final String INDEX_PARAM = "index";
48 protected static final String QUERY_PARAM = "query";
49 protected static final String RAW_PARAM = "rawquery";
50
51 // optional standard params - some of these have to be implemented
52 protected static final String MAXDOCS_PARAM = "maxDocs";
53 protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage";
54 protected static final String START_PAGE_PARAM = "startPage";
55
56 protected AbstractBasicDocument gs_doc = null;
57
58 /** can more than one index be searched at the same time? */
59 protected boolean does_multi_index_search = false;
60 /** does this service support paging of results? */
61 protected boolean does_paging = false;
62 /** does this service support asking for a subset of results? */
63 protected boolean does_chunking = false;
64 /** does this service support faceting search results */
65 protected boolean does_faceting = false;
66 /**
67 * the default document type - use if all documents are the same type
68 */
69 protected String default_document_type = null;
70 /**
71 * the default index, or comma separated list if more than one is the
72 * default (with start and end commas, eg ,TI,SU,). Should be set by
73 * configure()
74 */
75 protected String default_index = "";
76
77 protected HashMap<String, String> paramDefaults = null;
78
79 public AbstractSearch()
80 {
81 paramDefaults = new HashMap<String, String>();
82 paramDefaults.put(MAXDOCS_PARAM, "100");
83 paramDefaults.put(HITS_PER_PAGE_PARAM, "10");
84 paramDefaults.put(START_PAGE_PARAM, "1");
85 }
86
87 /**
88 * Sets up the short service info for service by QUERY_SERVICE (e.g.
89 * TextQuery or AudioQuery) If other services will be provided, should be
90 * added in the subclass configure also looks for search format info, and
91 * document format info
92 */
93 public boolean configure(Element info, Element extra_info)
94 {
95 if (!super.configure(info, extra_info))
96 {
97 return false;
98 }
99
100 logger.info("Configuring AbstractSearch...");
101
102 this.config_info = info;
103
104 // set up short_service_info_
105 // => for now just has id and type. the name (lang dependent)
106 // will be added in if the list is requested.
107
108 Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
109 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
110 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
111 this.short_service_info.appendChild(tq_service);
112
113 // load up any search param defaults
114 Element search_elem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
115 if (search_elem != null) {
116 getSearchParamDefaults(search_elem);
117 }
118 // add some format info to service map if there is any
119 // => lookin extra info first look in buildConfig
120
121 Element format = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
122
123 if (format == null)
124 {
125 // try to find a format element inside <search> that contains a gsf:template. Note what if we have only xsl:templates??
126
127 NodeList format_elems = null;
128 if (search_elem != null)
129 {
130 format_elems = search_elem.getElementsByTagName(GSXML.FORMAT_ELEM);
131 }
132 for (int i = 0; i < format_elems.getLength(); i++)
133 {
134 format = (Element) format_elems.item(i);
135 if (format.getElementsByTagName("gsf:template").getLength() != 0)
136 {
137 break;
138 }
139 }
140 }//end of if(format==null)
141 //
142 if (format != null)
143 {
144 this.format_info_map.put(QUERY_SERVICE, this.doc.importNode(format, true));
145 }
146
147 // look for document display format - for documentType
148 String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
149 Element display_format = (Element) GSXML.getNodeByPath(extra_info, path);
150 if (display_format != null)
151 {
152 // check for docType option.
153 Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType");
154 if (doc_type_opt != null)
155 {
156 String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT);
157 if (!value.equals(""))
158 {
159 this.default_document_type = value;
160 }
161 }
162 }
163
164 // Base line for document (might be overriden by sub-classes)
165 gs_doc = new BasicDocument(this.doc, this.default_document_type);
166
167 return true;
168 }
169
170 protected void getSearchParamDefaults(Element search_elem) {
171
172 NodeList param_defaults_list = GSXML.getChildrenByTagName(search_elem, GSXML.PARAM_DEFAULT_ELEM);
173 for (int i=0; i<param_defaults_list.getLength(); i++) {
174 Element paramdef = (Element)param_defaults_list.item(i);
175 String name = paramdef.getAttribute(GSXML.NAME_ATT);
176 String val = paramdef.getAttribute(GSXML.VALUE_ATT);
177 if (!name.equals("") && !val.equals("")) {
178 paramDefaults.put(name, val);
179 }
180 }
181 }
182 /**
183 * returns a basic description for QUERY_SERVICE. If a subclass provides
184 * other services they need to provide their own descriptions
185 */
186 protected Element getServiceDescription(String service, String lang, String subset)
187 {
188 if (!service.equals(QUERY_SERVICE))
189 {
190 return null;
191 }
192
193 Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
194 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
195 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
196 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
197 {
198 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(QUERY_SERVICE, lang)));
199 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getServiceSubmit(QUERY_SERVICE, lang)));
200 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(QUERY_SERVICE, lang)));
201 }
202 if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
203 {
204 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
205 addCustomQueryParams(param_list, lang);
206 addStandardQueryParams(param_list, lang);
207 tq_service.appendChild(param_list);
208 }
209 return tq_service;
210
211 }
212
213 // perhaps these should be changed to search down the class hierarchy for
214 // values - do can just put the info in the resource bundle to use it
215 /** returns the default name for the TextQuery service */
216 protected String getServiceName(String service_id, String lang)
217 {
218 return getTextString(service_id + ".name", lang);
219 }
220
221 /** returns the default description for the TextQuery service */
222 protected String getServiceDescription(String service_id, String lang)
223 {
224 return getTextString(service_id + ".description", lang);
225 }
226
227 /** returns the default submit button text for the TextQuery service */
228 protected String getServiceSubmit(String service_id, String lang)
229 {
230 return getTextString(service_id + ".submit", lang);
231
232 }
233
234 /** adds the standard query params into the service description */
235 protected void addStandardQueryParams(Element param_list, String lang)
236 {
237 // this test is not so good. here we are using absence of default index
238 // to determine whether we have indexes or not. But in other places,
239 // absence of default index just means to use the first one as default.
240 if (!default_index.equals(""))
241 {
242 createParameter(INDEX_PARAM, param_list, lang);
243 }
244 if (does_chunking)
245 {
246 createParameter(MAXDOCS_PARAM, param_list, lang);
247 }
248 if (does_paging)
249 {
250 createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
251 createParameter(START_PAGE_PARAM, param_list, lang);
252 }
253 createParameter(QUERY_PARAM, param_list, lang);
254 }
255
256 /**
257 * adds any service specific query params into the service default
258 * implementation: add nothing. subclasses may need to override this to add
259 * in their specific parameters
260 */
261 protected void addCustomQueryParams(Element param_list, String lang)
262 {
263 // default behaviour, do nothing
264 }
265
266 protected void createParameter(String name, Element param_list, String lang)
267 {
268 createParameter(name, param_list, lang, null);
269 }
270
271 protected void createParameter(String name, Element param_list, String lang, String default_value)
272 {
273 // at this level, not interested in boolean return type
274 createParameterChain(name, param_list, lang, default_value);
275 }
276
277 /**
278 * default implementations for the standard parameters plus some other
279 * common ones index, maxDocs, hitsPerPage, startPage
280 */
281
282 protected boolean createParameterChain(String name, Element param_list, String lang, String default_value)
283 {
284 Element param = null;
285 String param_default = default_value;
286 if (default_value == null) {
287 // have we got a stored up default? will be null if not there
288 param_default = paramDefaults.get(name);
289 }
290 if (name.equals(QUERY_PARAM) || name.equals(RAW_PARAM))
291 {
292 param = GSXML.createParameterDescription(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_STRING, param_default, null, null);
293 param_list.appendChild(param);
294 return true;
295 }
296 else if (name.equals(INDEX_PARAM))
297 {
298 // should we make these class fields?
299 ArrayList<String> index_ids = new ArrayList<String>();
300 ArrayList<String> index_names = new ArrayList<String>();
301 getIndexData(index_ids, index_names, lang);
302 String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE;
303 if (does_multi_index_search)
304 {
305 param_type = GSXML.PARAM_TYPE_ENUM_MULTI;
306 }
307 if (param_default == null)
308 {
309 param_default = this.default_index;
310 }
311 param = GSXML.createParameterDescription2(this.doc, INDEX_PARAM, getTextString("param." + INDEX_PARAM, lang), param_type, param_default, index_ids, index_names);
312 param_list.appendChild(param);
313 return true;
314 }
315 else if (name.equals(MAXDOCS_PARAM))
316 {
317 param = GSXML.createParameterDescription(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null);
318 param_list.appendChild(param);
319 return true;
320 }
321 else if (name.equals(HITS_PER_PAGE_PARAM))
322 {
323 param = GSXML.createParameterDescription(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null);
324 param_list.appendChild(param);
325 return true;
326 }
327 else if (name.equals(START_PAGE_PARAM))
328 {
329 // start page - set to 1 for the search page
330 param = GSXML.createParameterDescription(this.doc, START_PAGE_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, param_default, null, null);
331 param_list.appendChild(param);
332 return true;
333 }
334
335 // Get to there then none of the above params matched
336 // => return false so the chain can continue
337 return false;
338 }
339
340 /**
341 * create an element to go into the search results list. A node element has
342 * the form <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy'
343 * rank='0.23'/>
344 */
345 protected Element createDocNode(String node_id, String rank)
346 {
347 return this.gs_doc.createDocNode(node_id, rank);
348 }
349
350 /**
351 * returns the document type of the doc that the specified node belongs to.
352 * should be one of GSXML.DOC_TYPE_SIMPLE, GSXML.DOC_TYPE_PAGED,
353 * GSXML.DOC_TYPE_HIERARCHY
354 */
355 protected String getDocType(String node_id)
356 {
357 return this.gs_doc.getDocType(node_id);
358 }
359
360 /**
361 * returns the node type of the specified node. should be one of
362 * GSXML.NODE_TYPE_LEAF, GSXML.NODE_TYPE_INTERNAL, GSXML.NODE_TYPE_ROOT
363 */
364 protected String getNodeType(String node_id, String doc_type)
365 {
366 return this.gs_doc.getNodeType(node_id, doc_type);
367 }
368
369 /** returns true if the node has child nodes */
370 protected boolean hasChildren(String node_id)
371 {
372 return this.gs_doc.hasChildren(node_id);
373 }
374
375 /** returns true if the node has a parent */
376 protected boolean hasParent(String node_id)
377 {
378 return this.gs_doc.hasParent(node_id);
379 }
380
381 /**
382 * get the details about the indexes available must be implemented by
383 * subclass there must be at least one index
384 */
385 abstract protected void getIndexData(ArrayList<String> index_ids, ArrayList<String> index_names, String lang);
386
387}
Note: See TracBrowser for help on using the repository browser.