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

Last change on this file since 24394 was 24394, checked in by davidb, 13 years ago

Through the audioDB extension we now support a form of content-based audio/music searching. These commited changes reflect this generalization in our Service inheritance hierarchy for searching. Basically, what used to be thought of as a search service implied a *text* search service.

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