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

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

Cleaning up imports

  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 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;
23
24import org.apache.log4j.Logger;
25import org.greenstone.gsdl3.util.AbstractSimpleDocument;
26import org.greenstone.gsdl3.util.GSPath;
27import org.greenstone.gsdl3.util.GSXML;
28import org.greenstone.gsdl3.util.SimpleDocument;
29import org.w3c.dom.Element;
30import org.w3c.dom.NodeList;
31
32/**
33 * Partially implements a generic search service
34 *
35 * @author Katherine Don
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 AbstractSimpleDocument 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 /**
65 * the default document type - use if all documents are the same type
66 */
67 protected String default_document_type = null;
68 /**
69 * the default index, or comma separated list if more than one is the
70 * default (with start and end commas, eg ,TI,SU,). Should be set by
71 * configure()
72 */
73 protected String default_index = "";
74
75 protected String default_max_docs = "100";
76
77 protected String default_hits_per_page = "10";
78
79 public AbstractSearch()
80 {
81 }
82
83 /**
84 * Sets up the short service info for service by QUERY_SERVICE (e.g.
85 * TextQuery or AudioQuery) If other services will be provided, should be
86 * added in the subclass configure also looks for search format info, and
87 * document format info
88 */
89 public boolean configure(Element info, Element extra_info)
90 {
91 if (!super.configure(info, extra_info))
92 {
93 return false;
94 }
95
96 logger.info("Configuring AbstractSearch...");
97
98 this.config_info = info;
99
100 // set up short_service_info_
101 // => for now just has id and type. the name (lang dependent)
102 // will be added in if the list is requested.
103
104 Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
105 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
106 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
107 this.short_service_info.appendChild(tq_service);
108
109 // add some format info to service map if there is any
110 // => lookin extra info first look in buildConfig
111
112 Element format = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
113
114 if (format == null)
115 {
116 String path = GSPath.appendLink(GSXML.SEARCH_ELEM, GSXML.FORMAT_ELEM);
117
118 // Note by xiao: instead of retrieving the first 'format'
119 // element inside the 'search' element, we are trying to
120 // find the real format element which has at least one
121 // 'gsf:template' child element. (extra_info is
122 // collectionConfig.xml)
123
124 //format = (Element) GSXML.getNodeByPath(extra_info, path);
125
126 Element search_elem = (Element) GSXML.getChildByTagName(extra_info, GSXML.SEARCH_ELEM);
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 SimpleDocument(this.doc, this.default_document_type);
166
167 return true;
168 }
169
170 /**
171 * returns a basic description for QUERY_SERVICE. If a subclass provides
172 * other services they need to provide their own descriptions
173 */
174 protected Element getServiceDescription(String service, String lang, String subset)
175 {
176 if (!service.equals(QUERY_SERVICE))
177 {
178 return null;
179 }
180
181 Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
182 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
183 tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
184 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
185 {
186 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(QUERY_SERVICE, lang)));
187 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getServiceSubmit(QUERY_SERVICE, lang)));
188 tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(QUERY_SERVICE, lang)));
189 }
190 if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
191 {
192 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
193 addCustomQueryParams(param_list, lang);
194 addStandardQueryParams(param_list, lang);
195 tq_service.appendChild(param_list);
196 }
197 return tq_service;
198
199 }
200
201 // perhaps these should be changed to search down the class hierarchy for
202 // values - do can just put the info in the resource bundle to use it
203 /** returns the default name for the TextQuery service */
204 protected String getServiceName(String service_id, String lang)
205 {
206 return getTextString(service_id + ".name", lang);
207 }
208
209 /** returns the default description for the TextQuery service */
210 protected String getServiceDescription(String service_id, String lang)
211 {
212 return getTextString(service_id + ".description", lang);
213 }
214
215 /** returns the default submit button text for the TextQuery service */
216 protected String getServiceSubmit(String service_id, String lang)
217 {
218 return getTextString(service_id + ".submit", lang);
219
220 }
221
222 /** adds the standard query params into the service description */
223 protected void addStandardQueryParams(Element param_list, String lang)
224 {
225 // this test is not so good. here we are using absence of default index
226 // to determine whether we have indexes or not. But in other places,
227 // absence of default index just means to use the first one as default.
228 if (!default_index.equals(""))
229 {
230 createParameter(INDEX_PARAM, param_list, lang);
231 }
232 if (does_chunking)
233 {
234 createParameter(MAXDOCS_PARAM, param_list, lang);
235 }
236 if (does_paging)
237 {
238 createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
239 createParameter(START_PAGE_PARAM, param_list, lang);
240 }
241 createParameter(QUERY_PARAM, param_list, lang);
242 }
243
244 /**
245 * adds any service specific query params into the service default
246 * implementation: add nothing. subclasses may need to override this to add
247 * in their specific parameters
248 */
249 protected void addCustomQueryParams(Element param_list, String lang)
250 {
251 // default behaviour, do nothing
252 }
253
254 protected void createParameter(String name, Element param_list, String lang)
255 {
256 createParameter(name, param_list, lang, null);
257 }
258
259 protected void createParameter(String name, Element param_list, String lang, String default_value)
260 {
261 // at this level, not interested in boolean return type
262 createParameterChain(name, param_list, lang, default_value);
263 }
264
265 /**
266 * default implementations for the standard parameters plus some other
267 * common ones index, maxDocs, hitsPerPage, startPage
268 */
269
270 protected boolean createParameterChain(String name, Element param_list, String lang, String default_value)
271 {
272 Element param = null;
273 String param_default = default_value;
274
275 if (name.equals(QUERY_PARAM) || name.equals(RAW_PARAM))
276 {
277 param = GSXML.createParameterDescription(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_STRING, param_default, null, null);
278 param_list.appendChild(param);
279 return true;
280 }
281 else if (name.equals(INDEX_PARAM))
282 {
283 // should we make these class fields?
284 ArrayList<String> index_ids = new ArrayList<String>();
285 ArrayList<String> index_names = new ArrayList<String>();
286 getIndexData(index_ids, index_names, lang);
287 String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE;
288 if (does_multi_index_search)
289 {
290 param_type = GSXML.PARAM_TYPE_ENUM_MULTI;
291 }
292 if (param_default == null)
293 {
294 param_default = this.default_index;
295 }
296 param = GSXML.createParameterDescription2(this.doc, INDEX_PARAM, getTextString("param." + INDEX_PARAM, lang), param_type, param_default, index_ids, index_names);
297 param_list.appendChild(param);
298 return true;
299 }
300 else if (name.equals(MAXDOCS_PARAM))
301 {
302 if (param_default == null)
303 {
304 param_default = this.default_max_docs;
305 }
306
307 param = GSXML.createParameterDescription(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null);
308 param_list.appendChild(param);
309 return true;
310 }
311 else if (name.equals(HITS_PER_PAGE_PARAM))
312 {
313 if (param_default == null)
314 {
315 param_default = this.default_hits_per_page;
316 }
317
318 param = GSXML.createParameterDescription(this.doc, name, getTextString("param." + name, lang), GSXML.PARAM_TYPE_INTEGER, param_default, null, null);
319 param_list.appendChild(param);
320 return true;
321 }
322 else if (name.equals(START_PAGE_PARAM))
323 {
324 if (param_default == null)
325 {
326 param_default = "1";
327 }
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.