1 | /*
|
---|
2 | * GS2MGPPSearch.java
|
---|
3 | * Copyright (C) 2002 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.mgpp.*;
|
---|
23 | import org.greenstone.gdbm.*;
|
---|
24 | import org.greenstone.gsdl3.util.*;
|
---|
25 | // xml classes
|
---|
26 | import org.w3c.dom.Document;
|
---|
27 | import org.w3c.dom.Node;
|
---|
28 | import org.w3c.dom.Text;
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 | import org.w3c.dom.NodeList;
|
---|
31 | // general java classes
|
---|
32 | import java.util.HashMap;
|
---|
33 | import java.util.Vector;
|
---|
34 | import java.util.Set;
|
---|
35 | import java.util.Map;
|
---|
36 | import java.util.Iterator;
|
---|
37 | import java.util.Locale;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A ServiceRack class for searching in greenstone 2 MGPP collections
|
---|
41 | *
|
---|
42 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
43 | * @version $Revision: 3649 $
|
---|
44 | * @see ServiceRack
|
---|
45 | */
|
---|
46 | public class GS2MGPPSearch
|
---|
47 | extends ServiceRack {
|
---|
48 |
|
---|
49 | // these strings must match what is found in the properties file
|
---|
50 | // the services on offer
|
---|
51 | private static final String TEXT_QUERY_SERVICE = "TextQuery";
|
---|
52 | private static final String FIELD_QUERY_SERVICE = "FieldQuery";
|
---|
53 | private static final String ADVANCED_FIELD_QUERY_SERVICE = "AdvancedFieldQuery";
|
---|
54 | // params used
|
---|
55 | private static final String CASE_PARAM = "case";
|
---|
56 | private static final String STEM_PARAM = "stem";
|
---|
57 | private static final String MATCH_PARAM = "matchMode";
|
---|
58 | private static final String MATCH_PARAM_ALL = "all";
|
---|
59 | private static final String MATCH_PARAM_SOME = "some";
|
---|
60 | private static final String INDEX_PARAM = "index";
|
---|
61 | private static final String LEVEL_PARAM = "level";
|
---|
62 | private static final String RANK_PARAM = "sortBy";
|
---|
63 | private static final String RANK_PARAM_RANK = "rank";
|
---|
64 | private static final String RANK_PARAM_NONE = "natural";
|
---|
65 | private static final String MAXDOCS_PARAM = "maxDocs";
|
---|
66 | private static final String BOOLEAN_PARAM_ON = "1";
|
---|
67 | private static final String BOOLEAN_PARAM_OFF = "0";
|
---|
68 | private static final String QUERY_PARAM = "query";
|
---|
69 | private static final String SIMPLE_FIELD_PARAM = "simpleField";
|
---|
70 | private static final String ADVANCED_FIELD_PARAM = "complexField";
|
---|
71 | // more params for field query
|
---|
72 | private static final String FIELD_QUERY_PARAM = "fqv";
|
---|
73 | private static final String FIELD_STEM_PARAM = "fqs";
|
---|
74 | private static final String FIELD_CASE_PARAM = "fqc";
|
---|
75 | private static final String FIELD_FIELD_PARAM = "fqf";
|
---|
76 | private static final String FIELD_COMBINE_PARAM = "fqk";
|
---|
77 | private static final String FIELD_COMBINE_PARAM_AND = "0";
|
---|
78 | private static final String FIELD_COMBINE_PARAM_OR = "1";
|
---|
79 | private static final String FIELD_COMBINE_PARAM_NOT = "2";
|
---|
80 |
|
---|
81 | // elements used in the config file that are specific to this class
|
---|
82 | private static final String DEFAULT_INDEX_ELEM = "defaultIndex";
|
---|
83 | private static final String DEFAULT_LEVEL_ELEM = "defaultLevel";
|
---|
84 | private static final String INDEX_ELEM = "index";
|
---|
85 | private static final String LEVEL_ELEM = "level";
|
---|
86 |
|
---|
87 | private MGPPWrapper mgpp_src_=null;
|
---|
88 | private GDBMWrapper gdbm_src_=null;
|
---|
89 |
|
---|
90 | private String default_index_=null;
|
---|
91 | private String default_level_=null;
|
---|
92 |
|
---|
93 | private Element config_info_ = null;
|
---|
94 |
|
---|
95 | /** constructor */
|
---|
96 | public GS2MGPPSearch() {
|
---|
97 | mgpp_src_ = new MGPPWrapper();
|
---|
98 | gdbm_src_ = new GDBMWrapper();
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** configure this service */
|
---|
103 | public boolean configure(Element info) {
|
---|
104 |
|
---|
105 | System.out.println("configuring GS2MGPPSearch");
|
---|
106 | config_info_ = info;
|
---|
107 |
|
---|
108 | // get the default index from <defaultIndex>
|
---|
109 | Element def = (Element)GSXML.getChildByTagName(info, DEFAULT_INDEX_ELEM );
|
---|
110 | if (def !=null) {
|
---|
111 | default_index_ = def.getAttribute(GSXML.NAME_ATT);
|
---|
112 | }
|
---|
113 | if (default_index_==null||default_index_.equals("")) {
|
---|
114 | System.err.println("Error: default index not specified!");
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // get the default level out of <defaultLevel>
|
---|
119 | def = (Element)GSXML.getChildByTagName(info, DEFAULT_LEVEL_ELEM);
|
---|
120 | if (def !=null) {
|
---|
121 | default_level_ = def.getAttribute(GSXML.NAME_ATT);
|
---|
122 | }
|
---|
123 | if (default_level_==null||default_level_.equals("")) {
|
---|
124 | System.err.println("Error: default level not specified!");
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 | // the default level is also the level which gdbm is expecting
|
---|
128 | mgpp_src_.setReturnLevel(default_level_);
|
---|
129 |
|
---|
130 | // set up which services are available for this collection
|
---|
131 | Element e = null;
|
---|
132 | // these entries should reflect the build config file - some services may not be available depending on how the collection was built.
|
---|
133 |
|
---|
134 | // set up short_service_info_ - for now just has name and type
|
---|
135 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
136 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
137 | e.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
138 | short_service_info_.appendChild(e);
|
---|
139 |
|
---|
140 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
141 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
142 | e.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
|
---|
143 | short_service_info_.appendChild(e);
|
---|
144 |
|
---|
145 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
146 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
147 | e.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
|
---|
148 | short_service_info_.appendChild(e);
|
---|
149 |
|
---|
150 | // set up service_info_map_ - for now, just has the same elements as above
|
---|
151 | // should have full details about each service incl params lists etc.
|
---|
152 | // do the text query one - for now a static list. later use buildcfg.xml values to dynamically change this
|
---|
153 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
154 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
155 | e.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
156 | Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
157 | createTextQueryParamList(param_list, false, null);
|
---|
158 | e.appendChild(param_list);
|
---|
159 | service_info_map_.put(TEXT_QUERY_SERVICE, e);
|
---|
160 |
|
---|
161 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
162 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
163 | e.setAttribute(GSXML.NAME_ATT, FIELD_QUERY_SERVICE);
|
---|
164 | Element param_list1 = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
165 | createFieldQueryParamList(param_list1, false, null);
|
---|
166 | e.appendChild(param_list1);
|
---|
167 | service_info_map_.put(FIELD_QUERY_SERVICE, e);
|
---|
168 |
|
---|
169 | e = doc_.createElement(GSXML.SERVICE_ELEM);
|
---|
170 | e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
171 | e.setAttribute(GSXML.NAME_ATT, ADVANCED_FIELD_QUERY_SERVICE);
|
---|
172 | Element param_list2 = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
173 | createAdvancedFieldQueryParamList(param_list2, false, null);
|
---|
174 | e.appendChild(param_list2);
|
---|
175 | service_info_map_.put(ADVANCED_FIELD_QUERY_SERVICE, e);
|
---|
176 |
|
---|
177 | if (gdbm_src_.openDatabase(GSFile.GDBMDatabaseFile(site_home_, cluster_name_), GDBMWrapper.READER)) {
|
---|
178 | return true;
|
---|
179 | }
|
---|
180 | else {
|
---|
181 | System.err.println("couldn't open gdbm database!");
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** creates a new param element and adds it to the param list */
|
---|
187 | protected void createParameter(String name, Element param_list, boolean display, String lang) {
|
---|
188 | Element param=null;
|
---|
189 |
|
---|
190 | if (name.equals(INDEX_PARAM)) {
|
---|
191 | // the index info - read from config file
|
---|
192 | Element index_list = (Element)GSXML.getChildByTagName(config_info_, INDEX_ELEM+GSXML.LIST_MODIFIER);
|
---|
193 | NodeList indexes = index_list.getElementsByTagName(INDEX_ELEM);
|
---|
194 | int len = indexes.getLength();
|
---|
195 | if (len > 1) { // add index param to list only if more than one index specified
|
---|
196 | String [] inds = new String[len];
|
---|
197 | for (int i=0; i<len; i++) {
|
---|
198 | inds[i] = ((Element)indexes.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
199 | }
|
---|
200 | if (display) {
|
---|
201 | // use the same index names for now - should get these out of the config info
|
---|
202 | param = GSXML.createParameterDisplay(doc_, INDEX_PARAM, getTextString("param."+INDEX_PARAM, lang), inds, inds);
|
---|
203 | } else {
|
---|
204 | param = GSXML.createParameter(doc_, INDEX_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, default_index_, inds);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | } else if (name.equals(LEVEL_PARAM)) {
|
---|
208 | // the level info - read from config file
|
---|
209 | Element level_list = (Element)GSXML.getChildByTagName(config_info_, LEVEL_ELEM+GSXML.LIST_MODIFIER);
|
---|
210 | NodeList levels = level_list.getElementsByTagName(LEVEL_ELEM);
|
---|
211 | int len = levels.getLength();
|
---|
212 | if (len > 1) { // add level param to list only if more than one index specified
|
---|
213 | String [] levs = new String[len];
|
---|
214 | for (int i=0; i<len; i++) {
|
---|
215 | levs[i] = ((Element)levels.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
216 | }
|
---|
217 | if (display) {
|
---|
218 | param = GSXML.createParameterDisplay(doc_, LEVEL_PARAM, getTextString("param."+LEVEL_PARAM, lang), levs, levs);
|
---|
219 | } else {
|
---|
220 | param = GSXML.createParameter(doc_, LEVEL_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, default_level_, levs);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | } else if (name.equals(STEM_PARAM)) {
|
---|
224 | if (display) {
|
---|
225 | String[] bool_ops = {"0", "1"};
|
---|
226 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
227 | param = GSXML.createParameterDisplay(doc_, STEM_PARAM, getTextString("param."+STEM_PARAM, lang), bool_ops, bool_texts);
|
---|
228 | } else {
|
---|
229 | param = GSXML.createParameter(doc_, STEM_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
|
---|
230 | }
|
---|
231 | } else if (name.equals(CASE_PARAM)) {
|
---|
232 | if (display) {
|
---|
233 | String[] bool_ops = {"0", "1"};
|
---|
234 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
235 | param = GSXML.createParameterDisplay(doc_, CASE_PARAM, getTextString("param."+CASE_PARAM, lang), bool_ops, bool_texts);
|
---|
236 | } else {
|
---|
237 | param = GSXML.createParameter(doc_, CASE_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
|
---|
238 | }
|
---|
239 | } else if (name.equals(MATCH_PARAM)) {
|
---|
240 | String []vals = {MATCH_PARAM_ALL, MATCH_PARAM_SOME};
|
---|
241 | if (display) {
|
---|
242 | String []val_texts = {getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_ALL, lang),getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_SOME, lang)};
|
---|
243 |
|
---|
244 | param = GSXML.createParameterDisplay(doc_, MATCH_PARAM, getTextString("param."+MATCH_PARAM, lang), vals, val_texts);
|
---|
245 | } else {
|
---|
246 | param = GSXML.createParameter(doc_, MATCH_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, MATCH_PARAM_ALL, vals);
|
---|
247 | }
|
---|
248 | } else if (name.equals(RANK_PARAM)) {
|
---|
249 | String [] vals1 = {RANK_PARAM_RANK, RANK_PARAM_NONE };
|
---|
250 | if (display) {
|
---|
251 | String [] vals1_texts = { getTextString("param."+RANK_PARAM+"."+RANK_PARAM_RANK, lang),
|
---|
252 | getTextString("param."+RANK_PARAM+"."+RANK_PARAM_NONE, lang)};
|
---|
253 |
|
---|
254 | param = GSXML.createParameterDisplay(doc_, RANK_PARAM, getTextString("param."+RANK_PARAM, lang), vals1, vals1_texts);
|
---|
255 | } else {
|
---|
256 | param = GSXML.createParameter(doc_, RANK_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, RANK_PARAM_RANK, vals1 );
|
---|
257 | }
|
---|
258 | } else if (name.equals(MAXDOCS_PARAM)) {
|
---|
259 | if (display) {
|
---|
260 | param = GSXML.createParameterDisplay(doc_, MAXDOCS_PARAM, getTextString("param."+MAXDOCS_PARAM, lang), null, null);
|
---|
261 | } else {
|
---|
262 | param = GSXML.createParameter(doc_, MAXDOCS_PARAM, GSXML.PARAM_TYPE_INTEGER, "10", null);
|
---|
263 | }
|
---|
264 | } else if (name.equals(QUERY_PARAM)) {
|
---|
265 | if (display) {
|
---|
266 | param = GSXML.createParameterDisplay(doc_, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), null, null);
|
---|
267 | } else {
|
---|
268 | param = GSXML.createParameter(doc_, QUERY_PARAM, GSXML.PARAM_TYPE_STRING, null, null);
|
---|
269 | }
|
---|
270 | } else if (name.equals(FIELD_QUERY_PARAM)) {
|
---|
271 | if (display) {
|
---|
272 | param = GSXML.createParameterDisplay(doc_, FIELD_QUERY_PARAM, getTextString("param."+FIELD_QUERY_PARAM, lang), null, null);
|
---|
273 | } else {
|
---|
274 | param = GSXML.createParameter(doc_, FIELD_QUERY_PARAM, GSXML.PARAM_TYPE_STRING, null, null);
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | } else if (name.equals(FIELD_CASE_PARAM)) {
|
---|
279 | if (display) {
|
---|
280 | String[] bool_ops = {"0", "1"};
|
---|
281 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
282 | param = GSXML.createParameterDisplay(doc_, FIELD_CASE_PARAM, getTextString("param."+FIELD_CASE_PARAM, lang), bool_ops, bool_texts);
|
---|
283 |
|
---|
284 | } else {
|
---|
285 | param = GSXML.createParameter(doc_, FIELD_CASE_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
|
---|
286 |
|
---|
287 | }
|
---|
288 | } else if (name.equals(FIELD_STEM_PARAM)) {
|
---|
289 | if (display) {
|
---|
290 | String[] bool_ops = {"0", "1"};
|
---|
291 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
292 | param = GSXML.createParameterDisplay(doc_, FIELD_STEM_PARAM, getTextString("param."+FIELD_STEM_PARAM, lang), bool_ops, bool_texts);
|
---|
293 | } else {
|
---|
294 | param = GSXML.createParameter(doc_, FIELD_STEM_PARAM, GSXML.PARAM_TYPE_BOOLEAN, BOOLEAN_PARAM_ON, null);
|
---|
295 |
|
---|
296 | }
|
---|
297 | } else if (name.equals(FIELD_FIELD_PARAM)) {
|
---|
298 |
|
---|
299 | // the field list - should be read from config file
|
---|
300 | String [] vals2 = {"TX", "SU", "TI"};
|
---|
301 | String [] vals2_texts = {"TextOnly", "Subject", "Title"};
|
---|
302 |
|
---|
303 | if (display) {
|
---|
304 | param = GSXML.createParameterDisplay(doc_, FIELD_FIELD_PARAM, getTextString("param."+FIELD_FIELD_PARAM, lang), vals2, vals2_texts);
|
---|
305 |
|
---|
306 | } else {
|
---|
307 | param = GSXML.createParameter(doc_, FIELD_FIELD_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, "", vals2 );
|
---|
308 |
|
---|
309 | }
|
---|
310 | } else if (name.equals(FIELD_COMBINE_PARAM)) {
|
---|
311 |
|
---|
312 | String []vals = {FIELD_COMBINE_PARAM_AND, FIELD_COMBINE_PARAM_OR, FIELD_COMBINE_PARAM_NOT};
|
---|
313 | if (display) {
|
---|
314 | String []val_texts = {getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_AND, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_OR, lang), getTextString("param."+FIELD_COMBINE_PARAM+"."+FIELD_COMBINE_PARAM_NOT, lang)};
|
---|
315 |
|
---|
316 | param = GSXML.createParameterDisplay(doc_, FIELD_COMBINE_PARAM, "", vals, val_texts);
|
---|
317 |
|
---|
318 | } else {
|
---|
319 | param = GSXML.createParameter(doc_, FIELD_COMBINE_PARAM, GSXML.PARAM_TYPE_ENUM_SINGLE, FIELD_COMBINE_PARAM_AND, vals);
|
---|
320 | param.setAttribute(GSXML.PARAM_IGNORE_POS_ATT, "0");
|
---|
321 |
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | // add the param to the list
|
---|
326 | if (param !=null) {
|
---|
327 | param_list.appendChild(param);
|
---|
328 | }
|
---|
329 | }
|
---|
330 | /** this creates all teh params and appends them to param_list.
|
---|
331 | * if display=true it creates the text strings version
|
---|
332 | * otherwise it creates the description version
|
---|
333 | */
|
---|
334 | protected boolean createTextQueryParamList(Element param_list, boolean display, String lang) {
|
---|
335 |
|
---|
336 | // the order they are specified here is the order they appear on
|
---|
337 | // the query form
|
---|
338 | createParameter(INDEX_PARAM, param_list, display, lang);
|
---|
339 | createParameter(LEVEL_PARAM, param_list, display, lang);
|
---|
340 | createParameter(CASE_PARAM, param_list, display, lang);
|
---|
341 | createParameter(STEM_PARAM, param_list, display, lang);
|
---|
342 | createParameter(MATCH_PARAM, param_list, display, lang);
|
---|
343 | createParameter(RANK_PARAM, param_list, display, lang);
|
---|
344 | createParameter(MAXDOCS_PARAM, param_list, display, lang);
|
---|
345 | createParameter(QUERY_PARAM, param_list, display, lang);
|
---|
346 | return true;
|
---|
347 | }
|
---|
348 | /** this creates all teh params and appends them to param_list.
|
---|
349 | * if display=true it creates the text strings version
|
---|
350 | * otherwise it creates the description version
|
---|
351 | */
|
---|
352 | protected boolean createFieldQueryParamList(Element param_list, boolean display, String lang) {
|
---|
353 |
|
---|
354 | createParameter(MATCH_PARAM, param_list, display, lang);
|
---|
355 | createParameter(LEVEL_PARAM, param_list, display, lang);
|
---|
356 | createParameter(CASE_PARAM, param_list, display, lang);
|
---|
357 | createParameter(STEM_PARAM, param_list, display, lang);
|
---|
358 | createParameter(RANK_PARAM, param_list, display, lang);
|
---|
359 | createParameter(MAXDOCS_PARAM, param_list, display, lang);
|
---|
360 |
|
---|
361 | // create a multi param for the fields etc
|
---|
362 | // text box, field
|
---|
363 | Element multiparam = null;
|
---|
364 | Element param=null;
|
---|
365 | if (display) {
|
---|
366 | param = GSXML.createParameterDisplay(doc_, SIMPLE_FIELD_PARAM, "", null, null);
|
---|
367 | param_list.appendChild(param);
|
---|
368 | } else {
|
---|
369 | multiparam = GSXML.createParameter(doc_, SIMPLE_FIELD_PARAM, GSXML.PARAM_TYPE_MULTI, null, null);
|
---|
370 | multiparam.setAttribute("occurs", "4");
|
---|
371 | param_list.appendChild(multiparam);
|
---|
372 | }
|
---|
373 | // the components
|
---|
374 | if (display) {
|
---|
375 | createParameter(FIELD_QUERY_PARAM, param_list, display, lang);
|
---|
376 | createParameter(FIELD_FIELD_PARAM, param_list, display, lang);
|
---|
377 | } else {
|
---|
378 | createParameter(FIELD_QUERY_PARAM, multiparam, display, lang);
|
---|
379 | createParameter(FIELD_FIELD_PARAM, multiparam, display, lang);
|
---|
380 | }
|
---|
381 | return true;
|
---|
382 | }
|
---|
383 |
|
---|
384 | protected boolean createAdvancedFieldQueryParamList(Element param_list, boolean display, String lang) {
|
---|
385 |
|
---|
386 | // first do index and level params
|
---|
387 | createParameter(LEVEL_PARAM, param_list, display, lang);
|
---|
388 | createParameter(RANK_PARAM, param_list, display, lang);
|
---|
389 | createParameter(MAXDOCS_PARAM, param_list, display, lang);
|
---|
390 |
|
---|
391 |
|
---|
392 | // create a multi param for the fields etc
|
---|
393 | // text box, stem, case, field
|
---|
394 |
|
---|
395 | Element multiparam = null;
|
---|
396 | Element param=null;
|
---|
397 |
|
---|
398 | if (display) {
|
---|
399 | param = GSXML.createParameterDisplay(doc_, ADVANCED_FIELD_PARAM, "", null, null);
|
---|
400 | param_list.appendChild(param);
|
---|
401 | } else {
|
---|
402 | multiparam = GSXML.createParameter(doc_, ADVANCED_FIELD_PARAM, GSXML.PARAM_TYPE_MULTI, null, null);
|
---|
403 | multiparam.setAttribute("occurs", "4");
|
---|
404 | param_list.appendChild(multiparam);
|
---|
405 | }
|
---|
406 |
|
---|
407 | // the components
|
---|
408 |
|
---|
409 | //the combiners
|
---|
410 |
|
---|
411 |
|
---|
412 | if (display) {
|
---|
413 | createParameter(FIELD_COMBINE_PARAM, param_list, display, lang);
|
---|
414 | createParameter(FIELD_QUERY_PARAM, param_list, display, lang);
|
---|
415 | createParameter(FIELD_CASE_PARAM, param_list, display, lang);
|
---|
416 | createParameter(FIELD_STEM_PARAM, param_list, display, lang);
|
---|
417 | createParameter(FIELD_FIELD_PARAM, param_list, display, lang);
|
---|
418 | } else {
|
---|
419 | createParameter(FIELD_COMBINE_PARAM, multiparam, display, lang);
|
---|
420 | createParameter(FIELD_QUERY_PARAM, multiparam, display, lang);
|
---|
421 | createParameter(FIELD_CASE_PARAM, multiparam, display, lang);
|
---|
422 | createParameter(FIELD_STEM_PARAM, multiparam, display, lang);
|
---|
423 | createParameter(FIELD_FIELD_PARAM, multiparam, display, lang);
|
---|
424 | }
|
---|
425 |
|
---|
426 | return true;
|
---|
427 | }
|
---|
428 | /** creates a display element containing all the text strings needed to display the service page, in the language specified */
|
---|
429 | protected Element createServiceDisplay(String service, String lang) {
|
---|
430 | Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
|
---|
431 | display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_NAME_ELEM, getTextString(service+".name", lang)));
|
---|
432 | display.appendChild(GSXML.createTextElement(doc_, GSXML.DISPLAY_SUBMIT_ELEM, getTextString(service+".submit", lang)));
|
---|
433 |
|
---|
434 | // now need to add in the params
|
---|
435 | if (service.equals(TEXT_QUERY_SERVICE)) {
|
---|
436 |
|
---|
437 | createTextQueryParamList(display, true, lang);
|
---|
438 | }
|
---|
439 | // now need to add in the params
|
---|
440 | if (service.equals(FIELD_QUERY_SERVICE)) {
|
---|
441 |
|
---|
442 | createFieldQueryParamList(display, true, lang);
|
---|
443 | }
|
---|
444 | // now need to add in the params
|
---|
445 | if (service.equals(ADVANCED_FIELD_QUERY_SERVICE)) {
|
---|
446 |
|
---|
447 | createAdvancedFieldQueryParamList(display, true, lang);
|
---|
448 | }
|
---|
449 | return display;
|
---|
450 |
|
---|
451 | }
|
---|
452 |
|
---|
453 |
|
---|
454 | /** process a text query */
|
---|
455 | protected Element processTextQuery(Element request) {
|
---|
456 |
|
---|
457 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
458 | String from = GSPath.appendLink(cluster_name_, TEXT_QUERY_SERVICE);
|
---|
459 | result.setAttribute(GSXML.FROM_ATT, from);
|
---|
460 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
461 |
|
---|
462 | // get param list
|
---|
463 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
464 | if (param_list==null) {
|
---|
465 | System.err.println("GS2MGPPSearch, TextQuery Error: no param list in request!");
|
---|
466 | return result; // empty result
|
---|
467 | }
|
---|
468 | HashMap params = GSXML.extractParams(param_list);
|
---|
469 | String query = (String)params.get(QUERY_PARAM);
|
---|
470 | if (query == null) {
|
---|
471 | // nothing to query
|
---|
472 | return result;
|
---|
473 | }
|
---|
474 |
|
---|
475 | String index = (String)params.get(INDEX_PARAM);
|
---|
476 | if (index==null) { // if its not present, use the default index
|
---|
477 | index=default_index_;
|
---|
478 | }
|
---|
479 |
|
---|
480 | // now set up the mgpp stuff
|
---|
481 | String basedir = GSFile.collectionBaseDir(site_home_,
|
---|
482 | cluster_name_);
|
---|
483 | String indexdir = GSFile.collectionIndexPath(cluster_name_, index);
|
---|
484 |
|
---|
485 | mgpp_src_.loadIndexData(basedir, indexdir);
|
---|
486 | setStandardQueryParams(params);
|
---|
487 |
|
---|
488 | mgpp_src_.runQuery(query);
|
---|
489 | MGPPQueryResult mqr= mgpp_src_.getQueryResult();
|
---|
490 |
|
---|
491 | // get the docnums out, and convert to HASH ids
|
---|
492 | Vector docs = mqr.getDocs();
|
---|
493 | if (docs.size()==0) {
|
---|
494 | // no docs found
|
---|
495 | return result;
|
---|
496 | }
|
---|
497 | // create a documentList element
|
---|
498 | Element c = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
499 | result.appendChild(c);
|
---|
500 | Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
501 | c.appendChild(document_list);
|
---|
502 | // add each document
|
---|
503 | for (int d=0; d<docs.size(); d++) {
|
---|
504 | long docnum = ((MGPPDocInfo)docs.elementAt(d)).num_;
|
---|
505 | String id = gdbm_src_.docnum2Oid(docnum);
|
---|
506 | Node no = GSXML.createDocumentElement(doc_, id);
|
---|
507 | document_list.appendChild(no);
|
---|
508 | }
|
---|
509 | return result;
|
---|
510 | }//processTextQuery
|
---|
511 |
|
---|
512 | protected Element processFieldQuery(Element request) {
|
---|
513 | return processFieldQuery(request, "simple");
|
---|
514 | }
|
---|
515 |
|
---|
516 | protected Element processAdvancedFieldQuery(Element request) {
|
---|
517 | return processFieldQuery(request, "advanced");
|
---|
518 | }
|
---|
519 |
|
---|
520 | /** process a fielded query */
|
---|
521 | protected Element processFieldQuery(Element request, String type) {
|
---|
522 |
|
---|
523 | System.out.println("processing Field Query...");
|
---|
524 | Element result = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
525 | String from = GSPath.appendLink(cluster_name_, FIELD_QUERY_SERVICE);
|
---|
526 | result.setAttribute(GSXML.FROM_ATT, from);
|
---|
527 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
528 |
|
---|
529 | // get param list
|
---|
530 | Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
531 | if (param_list==null) {
|
---|
532 | System.err.println("GS2MGPPSearch, FieldQuery Error: no param list in request!");
|
---|
533 | return result; // empty result
|
---|
534 | }
|
---|
535 |
|
---|
536 | HashMap params = GSXML.extractAllParams(param_list);
|
---|
537 | printHash(params);
|
---|
538 |
|
---|
539 | //check one of the params to see if it is present
|
---|
540 | String test_param = (String)params.get(FIELD_QUERY_PARAM);
|
---|
541 | if (test_param == null) { // no query
|
---|
542 | return result;
|
---|
543 | }
|
---|
544 |
|
---|
545 | // for now we are using the default index
|
---|
546 | String index = default_index_;
|
---|
547 |
|
---|
548 | // now set up the mgpp stuff
|
---|
549 | String basedir = GSFile.collectionBaseDir(site_home_,
|
---|
550 | cluster_name_);
|
---|
551 | String indexdir = GSFile.collectionIndexPath(cluster_name_, index);
|
---|
552 |
|
---|
553 | mgpp_src_.loadIndexData(basedir, indexdir);
|
---|
554 | setStandardQueryParams(params);
|
---|
555 |
|
---|
556 | String query="";
|
---|
557 | if (type.equals("simple")) {
|
---|
558 | query = parseFieldQueryParams(params);
|
---|
559 | } else if (type.equals("advanced")) {
|
---|
560 | query = parseAdvancedFieldQueryParams(params);
|
---|
561 | }
|
---|
562 | System.out.println("query="+query);
|
---|
563 | mgpp_src_.runQuery(query);
|
---|
564 | MGPPQueryResult mqr= mgpp_src_.getQueryResult();
|
---|
565 |
|
---|
566 | // get the docnums out, and convert to HASH ids
|
---|
567 | Vector docs = mqr.getDocs();
|
---|
568 | if (docs.size()==0) {
|
---|
569 | // no docs found
|
---|
570 | return result;
|
---|
571 | }
|
---|
572 | // create a documentList element
|
---|
573 | Element c = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
574 | result.appendChild(c);
|
---|
575 | Element document_list = doc_.createElement(GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
576 | c.appendChild(document_list);
|
---|
577 | // add each document
|
---|
578 | for (int d=0; d<docs.size(); d++) {
|
---|
579 | long docnum = ((MGPPDocInfo)docs.elementAt(d)).num_;
|
---|
580 | String id = gdbm_src_.docnum2Oid(docnum);
|
---|
581 | Node no = GSXML.createDocumentElement(doc_, id);
|
---|
582 | document_list.appendChild(no);
|
---|
583 | }
|
---|
584 | return result;
|
---|
585 | }
|
---|
586 |
|
---|
587 |
|
---|
588 |
|
---|
589 | // should probably use a list rather than map
|
---|
590 | protected boolean setStandardQueryParams(HashMap params) {
|
---|
591 |
|
---|
592 | // set the default ones
|
---|
593 | mgpp_src_.setReturnTerms(true);
|
---|
594 | Set entries = params.entrySet();
|
---|
595 | Iterator i = entries.iterator();
|
---|
596 | while (i.hasNext()) {
|
---|
597 | Map.Entry m = (Map.Entry)i.next();
|
---|
598 | String name = (String)m.getKey();
|
---|
599 | String value = (String)m.getValue();
|
---|
600 |
|
---|
601 | if (name.equals(CASE_PARAM)) {
|
---|
602 | boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
|
---|
603 | mgpp_src_.setCase(val);
|
---|
604 | } else if (name.equals(STEM_PARAM)) {
|
---|
605 | boolean val = (value.equals(BOOLEAN_PARAM_ON)?true:false);
|
---|
606 | mgpp_src_.setStem(val);
|
---|
607 | } else if (name.equals(MAXDOCS_PARAM)) {
|
---|
608 | int docs = Integer.parseInt(value);
|
---|
609 | mgpp_src_.setMaxDocs(docs);
|
---|
610 | } else if (name.equals(LEVEL_PARAM)) {
|
---|
611 | mgpp_src_.setQueryLevel(value);
|
---|
612 | } else if (name.equals(MATCH_PARAM)) {
|
---|
613 | int mode;
|
---|
614 | if (value.equals(MATCH_PARAM_ALL)) mode=1;
|
---|
615 | else mode=0;
|
---|
616 | mgpp_src_.setMatchMode(mode);
|
---|
617 | } else if (name.equals(RANK_PARAM)) {
|
---|
618 | if (value.equals(RANK_PARAM_RANK)) {
|
---|
619 | mgpp_src_.setSortByRank(true);
|
---|
620 | } else if (value.equals(RANK_PARAM_NONE)) {
|
---|
621 | mgpp_src_.setSortByRank(false);
|
---|
622 | }
|
---|
623 | } // ignore any others
|
---|
624 | }
|
---|
625 | return true;
|
---|
626 | }
|
---|
627 |
|
---|
628 | protected String parseFieldQueryParams(HashMap params) {
|
---|
629 |
|
---|
630 | StringBuffer final_query = new StringBuffer(256);
|
---|
631 | String text_line = (String)params.get(FIELD_QUERY_PARAM);
|
---|
632 | String[] texts = text_line.split(",", -1);
|
---|
633 | String field_line = (String)params.get(FIELD_FIELD_PARAM);
|
---|
634 | String[] fields = field_line.split(",", -1);
|
---|
635 | String combine="&";
|
---|
636 | String match = (String)params.get(MATCH_PARAM);
|
---|
637 | if (match.equals(MATCH_PARAM_SOME)) {
|
---|
638 | combine = "|";
|
---|
639 | }
|
---|
640 |
|
---|
641 | for (int i=0; i<texts.length; i++) {
|
---|
642 |
|
---|
643 | String q = texts[i].trim();
|
---|
644 | if (!q.equals("")) {
|
---|
645 | addQueryElem(final_query, q, fields[i], combine);
|
---|
646 | }
|
---|
647 | }
|
---|
648 |
|
---|
649 | System.out.println("final query = "+final_query.toString());
|
---|
650 | return final_query.toString();
|
---|
651 | }
|
---|
652 |
|
---|
653 | protected String parseAdvancedFieldQueryParams(HashMap params) {
|
---|
654 |
|
---|
655 | StringBuffer final_query = new StringBuffer(256);
|
---|
656 | String text_line = (String)params.get(FIELD_QUERY_PARAM);
|
---|
657 | String[] texts = text_line.split(",", -1);
|
---|
658 | String field_line = (String)params.get(FIELD_FIELD_PARAM);
|
---|
659 | String[] fields = field_line.split(",", -1);
|
---|
660 | String case_line = (String)params.get(FIELD_CASE_PARAM);
|
---|
661 | String[] cases = case_line.split(",", -1);
|
---|
662 | String stem_line = (String)params.get(FIELD_STEM_PARAM);
|
---|
663 | String[] stems = stem_line.split(",", -1);
|
---|
664 | String combine_line = (String)params.get(FIELD_COMBINE_PARAM);
|
---|
665 | String [] combines = combine_line.split(",", -1);
|
---|
666 | String combine = "&";
|
---|
667 | for (int i=0; i<texts.length; i++) {
|
---|
668 | if (i==0) {
|
---|
669 | combine = "";
|
---|
670 | } else {
|
---|
671 | String x = combines[i-1]; // assume first one is missing
|
---|
672 | if (x.equals(FIELD_COMBINE_PARAM_AND)) {
|
---|
673 | combine = "&";
|
---|
674 | } else if (x.equals(FIELD_COMBINE_PARAM_OR)) {
|
---|
675 | combine = "|";
|
---|
676 | } else if (x.equals(FIELD_COMBINE_PARAM_NOT)) {
|
---|
677 | combine = "!";
|
---|
678 | }
|
---|
679 |
|
---|
680 | }
|
---|
681 |
|
---|
682 | String q = texts[i].trim();
|
---|
683 | if (!q.equals("")) {
|
---|
684 | q = addStemAndCase(q, stems[i], cases[i]);
|
---|
685 | addQueryElem(final_query, q, fields[i], combine);
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | System.out.println("final query = "+final_query.toString());
|
---|
690 | return final_query.toString();
|
---|
691 | }
|
---|
692 |
|
---|
693 | protected void addQueryElem(StringBuffer s, String q, String f, String c) {
|
---|
694 |
|
---|
695 | String combine="";
|
---|
696 | if (s.length()>0) {
|
---|
697 | combine = " "+c+" ";
|
---|
698 | }
|
---|
699 | if (f!="") {
|
---|
700 | s.append(combine+"["+q+"]:"+f);
|
---|
701 | } else {
|
---|
702 | s.append(combine+q);
|
---|
703 | }
|
---|
704 | }
|
---|
705 | protected String addStemAndCase(String q, String s, String c) {
|
---|
706 | String mods = "#";
|
---|
707 | if (c.equals("1")) {
|
---|
708 | mods += "i";
|
---|
709 | } else {
|
---|
710 | mods += "c";
|
---|
711 | }
|
---|
712 | if (s.equals("1")) {
|
---|
713 | mods += "s";
|
---|
714 | } else {
|
---|
715 | mods+= "u";
|
---|
716 | }
|
---|
717 | StringBuffer temp = new StringBuffer();
|
---|
718 | String [] terms = q.split(" ");
|
---|
719 | for (int i=0; i<terms.length; i++) {
|
---|
720 | String t = terms[i].trim();
|
---|
721 |
|
---|
722 | if (!t.equals("") && !t.equals("TX")) {
|
---|
723 | temp.append(" "+t+mods);
|
---|
724 | }
|
---|
725 | }
|
---|
726 | return temp.toString();
|
---|
727 | }
|
---|
728 |
|
---|
729 | protected void printHash(HashMap map) {
|
---|
730 | System.out.println("Map entries are :");
|
---|
731 | Set entries = map.entrySet();
|
---|
732 | Iterator i = entries.iterator();
|
---|
733 | while (i.hasNext()) {
|
---|
734 | Map.Entry m = (Map.Entry)i.next();
|
---|
735 | String name = (String)m.getKey();
|
---|
736 | String value = (String)m.getValue();
|
---|
737 | System.out.println(name+"="+value);
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 | }
|
---|
742 |
|
---|
743 |
|
---|