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 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.util.GSXML;
|
---|
23 | import org.greenstone.gsdl3.util.GSPath;
|
---|
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.ArrayList;
|
---|
32 | import java.util.HashMap;
|
---|
33 |
|
---|
34 | import org.apache.log4j.*;
|
---|
35 |
|
---|
36 | /** Partially implements a generic search service
|
---|
37 | *
|
---|
38 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
39 | */
|
---|
40 |
|
---|
41 | public abstract class AbstractSearch
|
---|
42 | extends ServiceRack
|
---|
43 | {
|
---|
44 |
|
---|
45 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractSearch.class.getName());
|
---|
46 |
|
---|
47 |
|
---|
48 | // the search service
|
---|
49 | protected static final String TEXT_QUERY_SERVICE = "TextQuery";
|
---|
50 |
|
---|
51 | // compulsory params
|
---|
52 | protected static final String INDEX_PARAM = "index";
|
---|
53 | protected static final String QUERY_PARAM = "query";
|
---|
54 |
|
---|
55 | // optional standard params - some of these have to be implemented
|
---|
56 | protected static final String MAXDOCS_PARAM = "maxDocs";
|
---|
57 | protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage";
|
---|
58 | protected static final String START_PAGE_PARAM = "startPage";
|
---|
59 |
|
---|
60 | // some other common params that may be used
|
---|
61 | protected static final String CASE_PARAM = "case";
|
---|
62 | protected static final String STEM_PARAM = "stem";
|
---|
63 | protected static final String ACCENT_PARAM="accent";
|
---|
64 |
|
---|
65 | protected static final String BOOLEAN_PARAM_ON = "1";
|
---|
66 | protected static final String BOOLEAN_PARAM_OFF = "0";
|
---|
67 | protected static final String MATCH_PARAM = "matchMode";
|
---|
68 | protected static final String MATCH_PARAM_ALL = "all";
|
---|
69 | protected static final String MATCH_PARAM_SOME = "some";
|
---|
70 |
|
---|
71 | /** can more than one index be searched at the smae time? */
|
---|
72 | protected boolean does_multi_index_search = false;
|
---|
73 | /** does this service support paging of results? */
|
---|
74 | protected boolean does_paging = false;
|
---|
75 | /** does this service support asking for a subset of results? */
|
---|
76 | protected boolean does_chunking = false;
|
---|
77 | /** the default document type - use if all documents are the same type
|
---|
78 | */
|
---|
79 | protected String default_document_type = null;
|
---|
80 | /** the default index, or comma separated list if more than one is
|
---|
81 | * the default (with start and end commas, eg ,TI,SU,).
|
---|
82 | * Should be set by configure()
|
---|
83 | */
|
---|
84 | protected String default_index = "";
|
---|
85 |
|
---|
86 | public AbstractSearch()
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** sets up the short service info for TextQuery. If other services
|
---|
91 | * will be provided, should be added in the subclass configure
|
---|
92 | * also looks for search format info, and document format info
|
---|
93 | */
|
---|
94 | public boolean configure(Element info, Element extra_info)
|
---|
95 | {
|
---|
96 | if (!super.configure(info, extra_info)){
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | logger.info("Configuring AbstractSearch...");
|
---|
101 |
|
---|
102 | this.config_info = info;
|
---|
103 |
|
---|
104 | // set up short_service_info_ - for now just has id and type. the name (lang dependent) will be added in if the list is requested.
|
---|
105 | Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
106 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
107 | tq_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
108 | this.short_service_info.appendChild(tq_service);
|
---|
109 |
|
---|
110 | // add some format info to service map if there is any - look in extra info
|
---|
111 | // first look in buildConfig
|
---|
112 | Element format = (Element)GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
|
---|
113 |
|
---|
114 | if (format==null) {
|
---|
115 | String path = GSPath.appendLink(GSXML.SEARCH_ELEM, GSXML.FORMAT_ELEM);
|
---|
116 | format = (Element) GSXML.getNodeByPath(extra_info, path);
|
---|
117 | }
|
---|
118 | //Element format = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
|
---|
119 | if (format != null) {
|
---|
120 | this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(format, true));
|
---|
121 | }
|
---|
122 |
|
---|
123 | // look for document display format - for documentType
|
---|
124 | String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
|
---|
125 | Element display_format = (Element)GSXML.getNodeByPath(extra_info, path);
|
---|
126 | if (display_format != null) {
|
---|
127 | // check for docType option.
|
---|
128 | Element doc_type_opt = GSXML.getNamedElement(display_format, "gsf:option", GSXML.NAME_ATT, "documentType");
|
---|
129 | if (doc_type_opt != null) {
|
---|
130 | String value = doc_type_opt.getAttribute(GSXML.VALUE_ATT);
|
---|
131 | if (!value.equals("")) {
|
---|
132 | this.default_document_type = value;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** returns the description of the TextQuery service. If a subclass
|
---|
141 | * provides other services they need to provides their own descriptions */
|
---|
142 | protected Element getServiceDescription(String service, String lang, String subset)
|
---|
143 | {
|
---|
144 | if (!service.equals(TEXT_QUERY_SERVICE)) {
|
---|
145 | return null;
|
---|
146 | }
|
---|
147 |
|
---|
148 | Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
149 | tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
150 | tq_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
151 | if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
152 | tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getServiceName(TEXT_QUERY_SERVICE, lang) ));
|
---|
153 | tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getServiceSubmit(TEXT_QUERY_SERVICE, lang) ));
|
---|
154 | tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getServiceDescription(TEXT_QUERY_SERVICE, lang)));
|
---|
155 | }
|
---|
156 | if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
157 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
158 | addCustomQueryParams(param_list, lang);
|
---|
159 | addStandardQueryParams(param_list, lang);
|
---|
160 | tq_service.appendChild(param_list);
|
---|
161 | }
|
---|
162 | return tq_service;
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | // perhaps these should be changed to search down the class hierarchy for
|
---|
167 | // values - do can just put the info in the resource bundle to use it
|
---|
168 | /** returns the default name for the TextQuery service */
|
---|
169 | protected String getServiceName(String service_id, String lang) {
|
---|
170 | return getTextString(service_id+".name", lang);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** returns the default description for the TextQuery service */
|
---|
174 | protected String getServiceDescription(String service_id, String lang) {
|
---|
175 | return getTextString(service_id+".description", lang);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** returns the default submit button text for the TextQuery service */
|
---|
179 | protected String getServiceSubmit(String service_id, String lang) {
|
---|
180 | return getTextString(service_id+".submit", lang);
|
---|
181 |
|
---|
182 | }
|
---|
183 | /** adds the standard query params into the service description */
|
---|
184 | protected void addStandardQueryParams(Element param_list, String lang)
|
---|
185 | {
|
---|
186 | createParameter(INDEX_PARAM, param_list, lang);
|
---|
187 | if (does_chunking) {
|
---|
188 | createParameter(MAXDOCS_PARAM, param_list, lang);
|
---|
189 | }
|
---|
190 | if (does_paging) {
|
---|
191 | createParameter(HITS_PER_PAGE_PARAM, param_list, lang);
|
---|
192 | createParameter(START_PAGE_PARAM, param_list, lang);
|
---|
193 | }
|
---|
194 | createParameter(QUERY_PARAM, param_list, lang);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** adds any service specific query params into the service
|
---|
198 | * default implementation: add nothing. subclasses may need to
|
---|
199 | * override this to add in their specific parameters
|
---|
200 | */
|
---|
201 | protected void addCustomQueryParams(Element param_list, String lang)
|
---|
202 | {
|
---|
203 | // default behaviour, do nothing
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** default implementations for the standard parameters plus some
|
---|
207 | * other common ones
|
---|
208 | * index, maxDocs, hitsPerPage, startPage, query, case, stem,
|
---|
209 | */
|
---|
210 | protected void createParameter(String name, Element param_list, String lang) {
|
---|
211 | Element param = null;
|
---|
212 | if (name.equals(QUERY_PARAM)) {
|
---|
213 | param = GSXML.createParameterDescription(this.doc, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
214 | param_list.appendChild(param);
|
---|
215 | } else if (name.equals(INDEX_PARAM)) {
|
---|
216 |
|
---|
217 | // should we make these class fields?
|
---|
218 | ArrayList index_ids = new ArrayList();
|
---|
219 | ArrayList index_names = new ArrayList();
|
---|
220 | getIndexData(index_ids, index_names, lang);
|
---|
221 | String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE;
|
---|
222 | if (does_multi_index_search) {
|
---|
223 | param_type = GSXML.PARAM_TYPE_ENUM_MULTI;
|
---|
224 | }
|
---|
225 | param = GSXML.createParameterDescription2(this.doc, INDEX_PARAM, getTextString("param."+INDEX_PARAM, lang), param_type, this.default_index, index_ids, index_names);
|
---|
226 | param_list.appendChild(param);
|
---|
227 | } else if (name.equals(MAXDOCS_PARAM) || name.equals(HITS_PER_PAGE_PARAM)) {
|
---|
228 | String default_val = "100";
|
---|
229 | if (name.equals(HITS_PER_PAGE_PARAM)) {
|
---|
230 | default_val = "10";
|
---|
231 | }
|
---|
232 | param = GSXML.createParameterDescription(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_INTEGER, default_val, null, null);
|
---|
233 | param_list.appendChild(param);
|
---|
234 | } else if (name.equals(CASE_PARAM) || name.equals(STEM_PARAM) || name.equals(ACCENT_PARAM)) {
|
---|
235 | String[] bool_ops = {"0", "1"};
|
---|
236 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
237 | param = GSXML.createParameterDescription(this.doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, bool_ops, bool_texts);
|
---|
238 | param_list.appendChild(param);
|
---|
239 | } else if (name.equals(MATCH_PARAM)) {
|
---|
240 | String[] vals = {MATCH_PARAM_SOME, MATCH_PARAM_ALL };
|
---|
241 | String[] val_texts = {getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_SOME, lang), getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_ALL, lang)};
|
---|
242 | param = GSXML.createParameterDescription(this.doc, MATCH_PARAM, getTextString("param."+MATCH_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, MATCH_PARAM_SOME, vals, val_texts);
|
---|
243 | param_list.appendChild(param);
|
---|
244 | } else if (name.equals(START_PAGE_PARAM)) {
|
---|
245 | // start page - set to 1 for the search page
|
---|
246 | param = GSXML.createParameterDescription(this.doc, START_PAGE_PARAM, "", GSXML.PARAM_TYPE_INVISIBLE, "1", null, null);
|
---|
247 | param_list.appendChild(param);
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | }
|
---|
252 | /** create an element to go into the search results list. A node element
|
---|
253 | * has the form
|
---|
254 | * <docNode nodeId='xxx' nodeType='leaf' docType='hierarchy' rank='0.23'/>
|
---|
255 | */
|
---|
256 | protected Element createDocNode(String node_id, String rank) {
|
---|
257 | Element node = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
258 | node.setAttribute(GSXML.NODE_ID_ATT, node_id);
|
---|
259 | node.setAttribute(GSXML.NODE_RANK_ATT, rank);
|
---|
260 | String doc_type = null;
|
---|
261 | if (default_document_type != null) {
|
---|
262 | doc_type = default_document_type;
|
---|
263 | } else {
|
---|
264 | doc_type = getDocType(node_id);
|
---|
265 | }
|
---|
266 | node.setAttribute(GSXML.DOC_TYPE_ATT, doc_type);
|
---|
267 | String node_type = getNodeType(node_id, doc_type);
|
---|
268 | node.setAttribute(GSXML.NODE_TYPE_ATT, node_type);
|
---|
269 | return node;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** returns the node type of the specified node.
|
---|
273 | should be one of
|
---|
274 | GSXML.NODE_TYPE_LEAF,
|
---|
275 | GSXML.NODE_TYPE_INTERNAL,
|
---|
276 | GSXML.NODE_TYPE_ROOT
|
---|
277 | */
|
---|
278 | protected String getNodeType(String node_id, String doc_type) {
|
---|
279 | if (doc_type.equals(GSXML.DOC_TYPE_SIMPLE)) {
|
---|
280 | return GSXML.NODE_TYPE_LEAF;
|
---|
281 | }
|
---|
282 |
|
---|
283 | if (!hasParent(node_id)) {
|
---|
284 | return GSXML.NODE_TYPE_ROOT;
|
---|
285 | }
|
---|
286 | if (doc_type.equals(GSXML.DOC_TYPE_PAGED)) {
|
---|
287 | return GSXML.NODE_TYPE_LEAF;
|
---|
288 | }
|
---|
289 | if (!hasChildren(node_id)) {
|
---|
290 | return GSXML.NODE_TYPE_LEAF;
|
---|
291 | }
|
---|
292 | return GSXML.NODE_TYPE_INTERNAL;
|
---|
293 |
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | /** returns the document type of the doc that the specified node
|
---|
298 | belongs to. should be one of
|
---|
299 | GSXML.DOC_TYPE_SIMPLE,
|
---|
300 | GSXML.DOC_TYPE_PAGED,
|
---|
301 | GSXML.DOC_TYPE_HIERARCHY
|
---|
302 | default implementation returns GSXML.DOC_TYPE_SIMPLE, over ride
|
---|
303 | if documents can be hierarchical
|
---|
304 | */
|
---|
305 | protected String getDocType(String node_id) {
|
---|
306 | return GSXML.DOC_TYPE_SIMPLE;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /** returns true if the node has child nodes
|
---|
310 | * default implementation returns false, over ride if documents can be
|
---|
311 | * hierarchical
|
---|
312 | */
|
---|
313 | protected boolean hasChildren(String node_id) {
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 | /** returns true if the node has a parent
|
---|
317 | * default implementation returns false, over ride if documents can be
|
---|
318 | * hierarchical*/
|
---|
319 | protected boolean hasParent(String node_id) {
|
---|
320 | return false;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /** do the actual query
|
---|
324 | * must be implemented by subclass */
|
---|
325 | abstract protected Element processTextQuery(Element request);
|
---|
326 |
|
---|
327 | /** get the details about the indexes available
|
---|
328 | * must be implemented by subclass
|
---|
329 | * there must be at least one index */
|
---|
330 | abstract protected void getIndexData(ArrayList index_ids, ArrayList index_names, String lang);
|
---|
331 |
|
---|
332 | }
|
---|
333 |
|
---|