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