source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/CrossCollectionSearch.java@ 13978

Last change on this file since 13978 was 13978, checked in by kjdon, 17 years ago

cross coll search now works across remote servers

  • Property svn:keywords set to Author Date Id Revision
File size: 16.6 KB
Line 
1/*
2 * CrossCollectionSearch.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 */
19package org.greenstone.gsdl3.service;
20
21import org.greenstone.gsdl3.util.GSXML;
22import org.greenstone.gsdl3.util.GSPath;
23
24import org.w3c.dom.Element;
25import org.w3c.dom.NodeList;
26
27import java.util.ArrayList;
28import java.util.Map;
29import java.util.Iterator;
30import java.util.Set;
31import java.util.HashMap;
32
33import org.apache.log4j.*;
34
35/**
36 *
37 * @author <a href="mailto:[email protected]">Katherine Don</a>
38 */
39
40public class CrossCollectionSearch
41 extends ServiceRack {
42
43 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.CrossCollectionSearch.class.getName());
44 protected static final String QUERY_PARAM = "query";
45 protected static final String COLLECTION_PARAM = "collection";
46
47 // the services on offer - these proxy the actual collection ones
48 protected static final String TEXT_QUERY_SERVICE = "TextQuery";
49 protected static final String ADV_QUERY_SERVICE = "AdvTextQuery";
50 protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
51
52 protected String [] coll_ids_list = null;
53 protected String [] coll_ids_list_no_all = null;
54 protected String [] coll_names_list = null;
55
56
57 /** constructor */
58 public CrossCollectionSearch()
59 {
60 }
61
62 public boolean configure(Element info, Element extra_info)
63 {
64 // any parameters? colls to include??
65 logger.info("Configuring CrossCollectionSearch...");
66 // query service
67 Element ccs_service = this.doc.createElement(GSXML.SERVICE_ELEM);
68 ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
69 ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
70 this.short_service_info.appendChild(ccs_service);
71
72 // metadata service
73 Element dmr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
74 dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
75 dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
76 this.short_service_info.appendChild(dmr_service);
77
78 // get any format info
79 Element format_info = (Element)GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
80 if (format_info != null) {
81 this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(format_info, true));
82 } else {
83 // add in a default format statement
84 String format_string = "<format xmlns:gsf='http://www.greenstone.org/greenstone3/schema/ConfigFormat' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><gsf:template match='documentNode'><td><a><xsl:attribute name='href'>?a=d&amp;c=<xsl:value-of select='@collection'/>&amp;d=<xsl:value-of select='@nodeID'/><xsl:if test=\"@nodeType='leaf'\">&amp;sib=1</xsl:if>&amp;dt=<xsl:value-of select='@docType'/>&amp;p.a=q&amp;&amp;p.s="+TEXT_QUERY_SERVICE+"&amp;p.c=";
85 if (this.cluster_name!=null) {
86 format_string += this.cluster_name;
87 }
88 format_string += "</xsl:attribute><gsf:icon/></a></td><td><gsf:metadata name='Title'/> (<xsl:value-of select='@collection'/>) </td></gsf:template></format>";
89 this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(this.converter.getDOM(format_string).getDocumentElement(), true));
90 }
91 return true;
92 }
93
94 protected Element getServiceDescription(String service, String lang, String subset)
95 {
96 if (service.equals(TEXT_QUERY_SERVICE)) {
97
98 Element ccs_service = this.doc.createElement(GSXML.SERVICE_ELEM);
99 ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
100 ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
101
102 // display info
103 if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
104 ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(TEXT_QUERY_SERVICE+".name", lang)));
105 ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(TEXT_QUERY_SERVICE+".submit", lang)));
106 ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(TEXT_QUERY_SERVICE+".description", lang)));
107 }
108 // param info
109 if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
110 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
111 // collection list
112 if (coll_ids_list ==null ) {
113 initCollectionList();
114 }
115 Element param = GSXML.createParameterDescription(this.doc, COLLECTION_PARAM, getTextString("param."+COLLECTION_PARAM, lang), GSXML.PARAM_TYPE_ENUM_MULTI, "all", coll_ids_list, coll_names_list);
116 param_list.appendChild(param);
117 // query param
118 param = GSXML.createParameterDescription(this.doc, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
119 param_list.appendChild(param);
120 ccs_service.appendChild(param_list);
121 }
122
123 logger.debug("service description="+this.converter.getPrettyString(ccs_service));
124 return ccs_service;
125 }
126 // these ones are probably never called, but put them here just in case
127 Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
128 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
129 service_elem.setAttribute(GSXML.NAME_ATT, service);
130 return service_elem;
131
132
133 }
134
135
136 protected Element processTextQuery(Element request) {
137 // Create a new (empty) result message
138 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
139 result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
140 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
141
142 String lang = request.getAttribute(GSXML.LANG_ATT);
143 // Get the parameters of the request
144 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
145 if (param_list == null) {
146 logger.error("TextQuery request had no paramList.");
147 return result; // Return the empty result
148 }
149
150 // get the collection list
151 String [] colls_list = coll_ids_list_no_all;
152 Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM);
153 if (coll_param != null) {
154 String coll_list = GSXML.getValue(coll_param);
155 if (!coll_list.equals("all") && !coll_list.equals("")) {
156 colls_list = coll_list.split(",");
157 }
158 }
159
160 Element query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
161 // we are sending the same request to each collection - build up the to
162 // attribute for the request
163 StringBuffer to_att = new StringBuffer();
164 for (int i=0; i<colls_list.length; i++) {
165 if (i>0) {
166 to_att.append(",");
167 }
168 to_att.append(GSPath.appendLink(colls_list[i], "TextQuery"));
169
170 }
171 // send the query to all colls
172 Element query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to_att.toString(), lang, "");
173 query_message.appendChild(query_request);
174 // should we add params individually?
175 Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
176 query_request.appendChild(new_param_list);
177 new_param_list.appendChild(this.doc.importNode(GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, QUERY_PARAM), true));
178 Element query_result = this.router.process(query_message);
179
180 // gather up the data from each response
181 int numDocsMatched = 0;
182 int numDocsReturned = 0;
183
184 //term info??
185
186 NodeList metadata = query_result.getElementsByTagName(GSXML.METADATA_ELEM);
187 for (int j=0; j<metadata.getLength(); j++) {
188 Element meta = (Element)metadata.item(j);
189 if (meta.getAttribute(GSXML.NAME_ATT).equals("numDocsReturned")) {
190 numDocsReturned += Integer.parseInt(GSXML.getValue(meta));
191 } else if (meta.getAttribute(GSXML.NAME_ATT).equals("numDocsMatched")) {
192 numDocsMatched += Integer.parseInt(GSXML.getValue(meta));
193 }
194 }
195
196 Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
197 result.appendChild(metadata_list);
198 GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+numDocsReturned);
199 //GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+numDocsMatched);
200
201 Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
202 result.appendChild(doc_node_list);
203
204 NodeList responses = query_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
205
206 for (int k=0; k<responses.getLength(); k++) {
207 String coll_name = GSPath.removeLastLink(((Element)responses.item(k)).getAttribute(GSXML.FROM_ATT));
208 NodeList nodes = ((Element)responses.item(k)).getElementsByTagName(GSXML.DOC_NODE_ELEM);
209 if (nodes==null || nodes.getLength()==0) continue;
210 Element last_node = null;
211 Element this_node = null;
212 for (int n=0; n<nodes.getLength(); n++) {
213 this_node = (Element)nodes.item(n);
214 this_node.setAttribute("collection", coll_name);
215 if (k==0) {
216
217 doc_node_list.appendChild(this.doc.importNode(this_node, true));
218 } else {
219 if (last_node==null) {
220 last_node = (Element)GSXML.getChildByTagName(doc_node_list, GSXML.DOC_NODE_ELEM);
221 }
222 last_node = GSXML.insertIntoOrderedList(doc_node_list, GSXML.DOC_NODE_ELEM, last_node, this_node, "rank", true);
223 }
224
225 }
226 }
227 return result;
228 }
229
230// protected Element processAdvTextQuery(Element request)
231// {
232
233// }
234 protected boolean initCollectionList() {
235 String lang="en";
236 String uid = "";
237 // first, get the message router info
238 Element coll_list_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
239 Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, ""); // uid
240 coll_list_message.appendChild(coll_list_request);
241 logger.debug("coll list request = "+this.converter.getPrettyString(coll_list_request));
242 Element coll_list_response = (Element)this.router.process(coll_list_message);
243 if (coll_list_response==null) {
244 logger.error("couldn't query the message router!");
245 return false;
246 }
247 logger.debug("coll list response = "+this.converter.getPrettyString(coll_list_response));
248 // second, get some info from each collection. we want the coll name
249 // and whether its got a text query service
250
251 NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
252 // we will send all the requests in a single message
253 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
254 for (int i=0; i<colls.getLength(); i++) {
255 Element c = (Element)colls.item(i);
256 String name = c.getAttribute(GSXML.NAME_ATT);
257 Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, lang, uid);
258 metadata_message.appendChild(metadata_request);
259 }
260 logger.debug("metadata request = "+this.converter.getPrettyString(metadata_message));
261 Element metadata_response = (Element)this.router.process(metadata_message);
262 logger.debug("metadata response = "+this.converter.getPrettyString(metadata_response));
263 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
264 ArrayList valid_colls = new ArrayList();
265 ArrayList valid_coll_names = new ArrayList();
266 for (int i=0; i<coll_responses.getLength(); i++) {
267 Element response = (Element)coll_responses.item(i);
268 Element coll = (Element)GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM);
269 Element service_list = (Element)GSXML.getChildByTagName(coll, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
270 if (service_list==null) continue;
271 Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE); // should be AbstractSearch.TEXT_QUERY_SERVICE
272 if (query_service == null) continue;
273 // use the name of the response in case we are talking to a remote collection, not the name of the collection.
274 String coll_id = response.getAttribute(GSXML.FROM_ATT);
275 String coll_name = coll_id+": "+GSXML.getDisplayText(coll, GSXML.DISPLAY_TEXT_NAME, "en", "en"); // just use english for now until we do some caching or something
276 valid_colls.add(coll_id);
277 valid_coll_names.add(coll_name);
278 }
279
280 if (valid_colls.size()==0) {
281 return false;
282 }
283 // ids no all has the list without 'all' option.
284 this.coll_ids_list_no_all = new String[1];
285 this.coll_ids_list_no_all = (String []) valid_colls.toArray(coll_ids_list_no_all);
286
287 valid_colls.add(0, "all");
288 valid_coll_names.add(0, getTextString("param."+COLLECTION_PARAM+".all", "en" ));
289 this.coll_ids_list = new String[1];
290 this.coll_names_list = new String[1];
291 this.coll_ids_list = (String []) valid_colls.toArray(coll_ids_list);
292 this.coll_names_list = (String []) valid_coll_names.toArray(coll_names_list);
293 return true;
294 }
295
296 protected Element processDocumentMetadataRetrieve(Element request) {
297 // Create a new (empty) result message
298 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
299 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
300 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
301
302 String lang = request.getAttribute(GSXML.LANG_ATT);
303 // Get the parameters of the request
304 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
305 if (param_list == null) {
306 logger.error("DocumentMetadataRetrieve request had no paramList.");
307 return result; // Return the empty result
308 }
309
310 NodeList query_doc_list = request.getElementsByTagName(GSXML.DOC_NODE_ELEM);
311 if (query_doc_list.getLength()==0) {
312 logger.error("DocumentMetadataRetrieve request had no documentNodes.");
313 return result; // Return the empty result
314 }
315
316 // the resulting doc node list
317 Element result_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
318 result.appendChild(result_node_list);
319
320 // get all the metadata params
321 Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
322 Element param = GSXML.createParameter(this.doc, "metadata", "Title");
323 new_param_list.appendChild(param);
324
325 // organise the nodes into collection lists
326 HashMap coll_map = new HashMap();
327
328 for (int i=0; i<query_doc_list.getLength(); i++) {
329 Element doc_node = (Element)query_doc_list.item(i);
330 String coll_name = doc_node.getAttribute("collection");
331 Element coll_items = (Element)coll_map.get(coll_name);
332 if (coll_items==null) {
333 coll_items=this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
334 coll_map.put(coll_name, coll_items);
335 }
336 coll_items.appendChild(this.doc.importNode(doc_node, true));
337 }
338
339 // create teh individual requests
340 Element meta_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
341 Set mapping_set = coll_map.entrySet();
342 Iterator iter = mapping_set.iterator();
343
344 while (iter.hasNext()) {
345 Map.Entry e = (Map.Entry)iter.next();
346 String cname = (String)e.getKey();
347 Element doc_nodes = (Element)e.getValue();
348 Element meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(cname, DOCUMENT_METADATA_RETRIEVE_SERVICE), lang, "");
349 meta_request.appendChild(doc_nodes);
350 meta_request.appendChild(new_param_list.cloneNode(true));
351 meta_request_message.appendChild(meta_request);
352
353 }
354
355 Element meta_result = this.router.process(meta_request_message);
356
357 // now need to put the doc nodes back in the right order
358 // go through the original list again. keep an element pointer to
359 // the next element in each collections list
360 NodeList meta_responses = meta_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
361 for (int i=0; i<meta_responses.getLength(); i++) {
362 String collname = GSPath.removeLastLink(((Element)meta_responses.item(i)).getAttribute(GSXML.FROM_ATT));
363 Element first_elem = (Element)GSXML.getNodeByPath(meta_responses.item(i), "documentNodeList/documentNode");
364 coll_map.put(collname, first_elem);
365 }
366
367 for (int i=0; i<query_doc_list.getLength(); i++) {
368 Element doc_node = (Element)query_doc_list.item(i);
369 Element new_node = (Element)this.doc.importNode(doc_node, false);
370 result_node_list.appendChild(new_node);
371 String coll_name = doc_node.getAttribute("collection");
372
373 Element meta_elem = (Element)coll_map.get(coll_name);
374 GSXML.mergeMetadataLists(new_node, meta_elem);
375 coll_map.put(coll_name, meta_elem.getNextSibling());
376 }
377 return result;
378 }
379}
380
Note: See TracBrowser for help on using the repository browser.