1 | /*
|
---|
2 | * AbstractTextSearch.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 java.util.ArrayList;
|
---|
23 |
|
---|
24 | import org.apache.log4j.Logger;
|
---|
25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
26 |
|
---|
27 | import org.w3c.dom.Document;
|
---|
28 | import org.w3c.dom.Element;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Partially implements a generic search service
|
---|
32 | *
|
---|
33 | */
|
---|
34 |
|
---|
35 | public abstract class AbstractTextSearch extends AbstractSearch
|
---|
36 | {
|
---|
37 |
|
---|
38 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.AbstractTextSearch.class.getName());
|
---|
39 |
|
---|
40 | // optional standard params - some of these have to be implemented
|
---|
41 | protected static final String INDEX_SUBCOLLECTION_PARAM = "indexSubcollection";
|
---|
42 | protected static final String INDEX_LANGUAGE_PARAM = "indexLanguage";
|
---|
43 |
|
---|
44 | protected static final String INDEX_SUBCOLLECTION_ELEM = "indexSubcollection";
|
---|
45 | protected static final String INDEX_LANGUAGE_ELEM = "indexLanguage";
|
---|
46 |
|
---|
47 | // some other common params that may be used
|
---|
48 | protected static final String CASE_PARAM = "case";
|
---|
49 | protected static final String STEM_PARAM = "stem";
|
---|
50 | protected static final String ACCENT_PARAM = "accent";
|
---|
51 |
|
---|
52 | protected static final String BOOLEAN_PARAM_ON = "1";
|
---|
53 | protected static final String BOOLEAN_PARAM_OFF = "0";
|
---|
54 | protected static final String MATCH_PARAM = "matchMode";
|
---|
55 | protected static final String MATCH_PARAM_ALL = "all";
|
---|
56 | protected static final String MATCH_PARAM_SOME = "some";
|
---|
57 |
|
---|
58 | protected String default_index_subcollection = "";
|
---|
59 |
|
---|
60 | protected String default_index_language = "";
|
---|
61 |
|
---|
62 | public AbstractTextSearch()
|
---|
63 | {
|
---|
64 | super();
|
---|
65 | // the search service
|
---|
66 | QUERY_SERVICE = "TextQuery";
|
---|
67 | paramDefaults.put(CASE_PARAM, BOOLEAN_PARAM_ON);
|
---|
68 | paramDefaults.put(STEM_PARAM, BOOLEAN_PARAM_OFF);
|
---|
69 | paramDefaults.put(ACCENT_PARAM, BOOLEAN_PARAM_ON);
|
---|
70 | paramDefaults.put(MATCH_PARAM, MATCH_PARAM_SOME);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public boolean configure(Element info, Element extra_info)
|
---|
74 | {
|
---|
75 | if (!super.configure(info, extra_info))
|
---|
76 | {
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | logger.info("Configuring AbstractTextSearch...");
|
---|
81 | // we need to set up the save_params - we want all our search settings saved by default
|
---|
82 | this.save_params.add(INDEX_SUBCOLLECTION_PARAM);
|
---|
83 | this.save_params.add(INDEX_LANGUAGE_PARAM);
|
---|
84 | this.save_params.add(CASE_PARAM);
|
---|
85 | this.save_params.add(STEM_PARAM);
|
---|
86 | this.save_params.add(ACCENT_PARAM);
|
---|
87 | this.save_params.add(MATCH_PARAM);
|
---|
88 |
|
---|
89 | return true;
|
---|
90 |
|
---|
91 | }
|
---|
92 | /** adds the standard query params into the service description */
|
---|
93 | protected void addStandardQueryParams(Element param_list, String lang)
|
---|
94 | {
|
---|
95 | if (!default_index_subcollection.equals(""))
|
---|
96 | {
|
---|
97 | createParameter(INDEX_SUBCOLLECTION_PARAM, param_list, lang);
|
---|
98 | }
|
---|
99 | if (!default_index_language.equals(""))
|
---|
100 | {
|
---|
101 | createParameter(INDEX_LANGUAGE_PARAM, param_list, lang);
|
---|
102 | }
|
---|
103 |
|
---|
104 | super.addStandardQueryParams(param_list, lang);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Top up createParameterChain with TextQuery specific params: case, stem
|
---|
109 | * ...
|
---|
110 | */
|
---|
111 | protected boolean createParameterChain(String name, Element param_list, String lang, String default_value)
|
---|
112 | {
|
---|
113 | Document doc = param_list.getOwnerDocument();
|
---|
114 | Element param = null;
|
---|
115 | String param_default = default_value;
|
---|
116 | if (default_value == null) {
|
---|
117 | // have we got a stored up default? will be null if not there
|
---|
118 | param_default = paramDefaults.get(name);
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (super.createParameterChain(name, param_list, lang, default_value))
|
---|
122 | {
|
---|
123 | // found a match, so can stop here
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 | // otherwise look to see if it is a text specific parameter
|
---|
127 | if (name.equals(INDEX_SUBCOLLECTION_PARAM))
|
---|
128 | {
|
---|
129 | Element index_sub_list = (Element) GSXML.getChildByTagName(this.config_info, INDEX_SUBCOLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
130 | if (index_sub_list == null)
|
---|
131 | return true; // processed, just not a very interesting result
|
---|
132 | ArrayList<String> index_sub_ids = new ArrayList<String>();
|
---|
133 | ArrayList<String> index_sub_names = new ArrayList<String>();
|
---|
134 | getIndexSubcollectionData(index_sub_ids, index_sub_names, lang);
|
---|
135 | String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE;
|
---|
136 | if (does_multi_index_search)
|
---|
137 | {
|
---|
138 | param_type = GSXML.PARAM_TYPE_ENUM_MULTI;
|
---|
139 | }
|
---|
140 | if (param_default == null)
|
---|
141 | {
|
---|
142 | param_default = this.default_index_subcollection;
|
---|
143 | }
|
---|
144 | param = GSXML.createParameterDescription2(doc, INDEX_SUBCOLLECTION_PARAM, getTextString("param." + INDEX_SUBCOLLECTION_PARAM, lang), param_type, param_default, index_sub_ids, index_sub_names);
|
---|
145 | param_list.appendChild(param);
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 | else if (name.equals(INDEX_LANGUAGE_PARAM))
|
---|
149 | {
|
---|
150 | Element index_lang_list = (Element) GSXML.getChildByTagName(this.config_info, INDEX_LANGUAGE_ELEM + GSXML.LIST_MODIFIER);
|
---|
151 | if (index_lang_list == null)
|
---|
152 | return true; // processed, just not a very interesting result
|
---|
153 | ArrayList<String> index_lang_ids = new ArrayList<String>();
|
---|
154 | ArrayList<String> index_lang_names = new ArrayList<String>();
|
---|
155 | getIndexLanguageData(index_lang_ids, index_lang_names, lang);
|
---|
156 | String param_type = GSXML.PARAM_TYPE_ENUM_SINGLE;
|
---|
157 | if (does_multi_index_search)
|
---|
158 | {
|
---|
159 | param_type = GSXML.PARAM_TYPE_ENUM_MULTI;
|
---|
160 | }
|
---|
161 | if (param_default == null)
|
---|
162 | {
|
---|
163 | param_default = this.default_index_language;
|
---|
164 | }
|
---|
165 | param = GSXML.createParameterDescription2(doc, INDEX_LANGUAGE_PARAM, getTextString("param." + INDEX_LANGUAGE_PARAM, lang), param_type, param_default, index_lang_ids, index_lang_names);
|
---|
166 | param_list.appendChild(param);
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 | else if (name.equals(CASE_PARAM) || name.equals(STEM_PARAM) || name.equals(ACCENT_PARAM)) {
|
---|
170 | String[] bool_ops = {"0", "1"};
|
---|
171 | String[] bool_texts = {getTextString("param.boolean.off", lang),getTextString("param.boolean.on", lang)};
|
---|
172 | param = GSXML.createParameterDescription(doc, name, getTextString("param."+name, lang), GSXML.PARAM_TYPE_BOOLEAN, param_default, bool_ops, bool_texts);
|
---|
173 | param_list.appendChild(param);
|
---|
174 | return true;
|
---|
175 | } else if (name.equals(MATCH_PARAM)) {
|
---|
176 |
|
---|
177 | String[] vals = {MATCH_PARAM_SOME, MATCH_PARAM_ALL };
|
---|
178 | String[] val_texts = {getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_SOME, lang), getTextString("param."+MATCH_PARAM+"."+MATCH_PARAM_ALL, lang)};
|
---|
179 | param = GSXML.createParameterDescription(doc, MATCH_PARAM, getTextString("param."+MATCH_PARAM, lang), GSXML.PARAM_TYPE_ENUM_SINGLE, param_default, vals, val_texts);
|
---|
180 | param_list.appendChild(param);
|
---|
181 | return true;
|
---|
182 | }
|
---|
183 | // Get to here then none of the above params matched
|
---|
184 | // => return false so the chain can continue
|
---|
185 | return false;
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * do the actual query must be implemented by subclass
|
---|
191 | */
|
---|
192 | abstract protected Element processTextQuery(Element request);
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * get the details about the indexexSubcollections available might be
|
---|
196 | * implemented by subclass
|
---|
197 | */
|
---|
198 | protected void getIndexSubcollectionData(ArrayList<String> index_ids, ArrayList<String> index_names, String lang)
|
---|
199 | {
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * get the details about the indexes available might be implemented by
|
---|
204 | * subclass
|
---|
205 | */
|
---|
206 | protected void getIndexLanguageData(ArrayList<String> index_ids, ArrayList<String> index_names, String lang)
|
---|
207 | {
|
---|
208 | }
|
---|
209 |
|
---|
210 | }
|
---|