source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CrossCollectionSearch.java@ 24979

Last change on this file since 24979 was 24979, checked in by sjm84, 12 years ago

Reformatting this file ahead of some changes

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