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