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

Last change on this file since 24858 was 24858, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

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