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

Last change on this file since 32453 was 32453, checked in by kjdon, 6 years ago

replacing hard coded param names with static string variables. set up save params for those params we need to save to the session.

  • Property svn:keywords set to Author Date Id Revision
File size: 25.5 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 java.util.ArrayList;
22import java.util.HashMap;
23import java.util.Iterator;
24import java.util.Map;
25import java.util.Set;
26
27import org.apache.log4j.Logger;
28import org.greenstone.gsdl3.util.GSPath;
29import org.greenstone.gsdl3.util.GSXML;
30import org.greenstone.gsdl3.util.UserContext;
31import org.greenstone.gsdl3.util.XMLConverter;
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.Node;
35import org.w3c.dom.NodeList;
36
37/**
38 * This ServiceRack gets specified in siteConfig.xml. So it is loaded by the MessaegRouter, and two services get activated: TextQuery, DocumentMetadataRetrieve.
39These are located at MR level, not inside a collection. QueryAction will send messages to "TextQuery", rather than eg "mgppdemo/TextQuery".
40These two services will requery the MR for search results/document metadata based on collections or documents listed.
41 */
42
43public class CrossCollectionSearch extends ServiceRack
44{
45
46 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.CrossCollectionSearch.class.getName());
47 protected static final String QUERY_PARAM = "query";
48 protected static final String COLLECTION_PARAM = "collection";
49 protected static final String GROUP_PARAM = "group";
50 protected static final String MAXDOCS_PARAM = "maxDocs"; // matches standard maxDocs, but in this case, means max docs per collection
51 protected static final String HITS_PER_PAGE_PARAM = "hitsPerPage";
52 protected static final String MAXDOCS_DEFAULT = "20";
53 // the services on offer - these proxy the actual collection ones
54 protected static final String TEXT_QUERY_SERVICE = "TextQuery";
55 protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
56
57 protected String[] coll_ids_list = null;
58 protected String[] coll_ids_list_no_all = null;
59 // maps lang to coll names list
60 protected HashMap<String, String[]> coll_names_map = null;
61
62 //protected String[] coll_names_list = null;
63
64 /** constructor */
65 public CrossCollectionSearch()
66 {
67 }
68
69 public boolean configure(Element info, Element extra_info)
70 {
71 // any parameters? colls to include??
72 logger.info("Configuring CrossCollectionSearch...");
73 // query service
74 Element ccs_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
75 ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
76 ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
77 this.short_service_info.appendChild(ccs_service);
78
79 // metadata service
80 Element dmr_service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
81 dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
82 dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
83 this.short_service_info.appendChild(dmr_service);
84
85
86 // what params do we want saved in the session?
87 this.save_params.add(QUERY_PARAM);
88 this.save_params.add(COLLECTION_PARAM);
89 this.save_params.add(GROUP_PARAM);
90 this.save_params.add(MAXDOCS_PARAM);
91 this.save_params.add(HITS_PER_PAGE_PARAM);
92
93 // get any format info
94 Element format_info = (Element) GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM);
95 if (format_info != null)
96 {
97 this.format_info_map.put(TEXT_QUERY_SERVICE, this.desc_doc.importNode(format_info, true));
98 }
99 else
100 {
101 // add in a default format statement
102 //"xmlns:gsf='" + GSXML.GSF_NAMESPACE + "' xmlns:xsl='" + GSXML.XSL_NAMESPACE + "
103 String format_string = "<format "+GSXML.STD_NAMESPACES_ATTS + "><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=";
104 if (this.cluster_name != null)
105 {
106 format_string += this.cluster_name;
107 }
108 format_string += "</xsl:attribute><gsf:icon/></a></td><td><gsf:metadata name='Title'/> (<xsl:value-of select='@collection'/>) </td></gsf:template></format>";
109 this.format_info_map.put(TEXT_QUERY_SERVICE, this.desc_doc.importNode(this.converter.getDOM(format_string).getDocumentElement(), true));
110 }
111 return true;
112 }
113
114 protected Element getServiceDescription(Document doc, String service, String lang, String subset)
115 {
116 if (service.equals(TEXT_QUERY_SERVICE))
117 {
118
119 Element ccs_service = doc.createElement(GSXML.SERVICE_ELEM);
120 ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
121 ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
122
123 // display info
124 if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
125 {
126 ccs_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_NAME, getTextString(TEXT_QUERY_SERVICE + ".name", lang)));
127 ccs_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(TEXT_QUERY_SERVICE + ".submit", lang)));
128 ccs_service.appendChild(GSXML.createDisplayTextElement(doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(TEXT_QUERY_SERVICE + ".description", lang)));
129 }
130 // param info
131 if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
132 {
133 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
134 // collection list
135 if (coll_ids_list == null)
136 {
137 initCollectionList(lang);
138 }
139 if (!coll_names_map.containsKey(lang))
140 {
141 addCollectionNames(lang);
142 }
143 Element param = GSXML.createParameterDescription(doc, COLLECTION_PARAM, getTextString("param." + COLLECTION_PARAM, lang), GSXML.PARAM_TYPE_ENUM_MULTI, "all", coll_ids_list, coll_names_map.get(lang));
144 param_list.appendChild(param);
145 // max docs param
146 param = GSXML.createParameterDescription(doc, MAXDOCS_PARAM, getTextString("param." + MAXDOCS_PARAM, lang), GSXML.PARAM_TYPE_INTEGER, MAXDOCS_DEFAULT, null, null);
147 param_list.appendChild(param);
148 // query param
149 param = GSXML.createParameterDescription(doc, QUERY_PARAM, getTextString("param." + QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
150 param_list.appendChild(param);
151 ccs_service.appendChild(param_list);
152 }
153
154 logger.debug("service description=" + this.converter.getPrettyString(ccs_service));
155 return ccs_service;
156 }
157 // these ones are probably never called, but put them here just in case
158 Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
159 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
160 service_elem.setAttribute(GSXML.NAME_ATT, service);
161 return service_elem;
162
163 }
164
165 protected Element processTextQuery(Element request)
166 {
167 // Create a new (empty) result message
168 Document result_doc = XMLConverter.newDOM();
169 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
170 result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
171 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
172
173 UserContext userContext = new UserContext(request);
174
175 // Get the parameters of the request
176 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
177 if (param_list == null)
178 {
179 logger.error("TextQuery request had no paramList.");
180 return result; // Return the empty result
181 }
182 // get the collection list
183 String[] colls_list = coll_ids_list_no_all;
184 Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM);
185 if (coll_param != null)
186 {
187 String coll_list = GSXML.getValue(coll_param);
188 if (!coll_list.equals("all") && !coll_list.equals(""))
189 {
190 colls_list = coll_list.split(",");
191 }
192 }
193
194 colls_list = mergeGroups(userContext, param_list, colls_list);
195
196 String maxdocs = MAXDOCS_DEFAULT;
197 Element maxdocs_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, MAXDOCS_PARAM);
198 if (maxdocs_param != null) {
199 maxdocs = GSXML.getValue(maxdocs_param);
200 }
201
202 Document msg_doc = XMLConverter.newDOM();
203 Element query_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
204 // we are sending the same request to each collection - build up the to
205 // attribute for the request
206 StringBuffer to_att = new StringBuffer();
207 for (int i = 0; i < colls_list.length; i++)
208 {
209 if (i > 0)
210 {
211 to_att.append(",");
212 }
213 to_att.append(GSPath.appendLink(colls_list[i], "TextQuery"));
214
215 }
216 // send the query to all colls
217 Element query_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, to_att.toString(), userContext);
218 query_message.appendChild(query_request);
219 // should we add params individually?
220 Element new_param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
221 query_request.appendChild(new_param_list);
222 new_param_list.appendChild(msg_doc.importNode(GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, QUERY_PARAM), true));
223
224 // for cross coll search, we only want maxdocs from each collection
225 // some colls use maxdocs, some use hits per page so lets send both
226 new_param_list.appendChild(GSXML.createParameter(msg_doc, MAXDOCS_PARAM, maxdocs));
227 new_param_list.appendChild(GSXML.createParameter(msg_doc, HITS_PER_PAGE_PARAM, maxdocs));
228 Element query_result = (Element) this.router.process(query_message);
229 // create the doc list for the response
230 Element doc_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
231 result.appendChild(doc_node_list);
232 Element result_snippet_list = result_doc.createElement(GSXML.HL_SNIPPET_ELEM + GSXML.LIST_MODIFIER);
233 result.appendChild(result_snippet_list);
234 NodeList hl_snippet_list = query_result.getElementsByTagName(GSXML.HL_SNIPPET_ELEM);
235 if (hl_snippet_list != null){
236 for (int hls = 0; hls < hl_snippet_list.getLength(); hls++){
237 result_snippet_list.appendChild(result_doc.importNode(hl_snippet_list.item(hls), true));
238 }
239 }
240
241 NodeList responses = query_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
242 int num_docs = 0;
243 for (int k = 0; k < responses.getLength(); k++)
244 {
245 String coll_name = GSPath.removeLastLink(((Element) responses.item(k)).getAttribute(GSXML.FROM_ATT));
246 NodeList nodes = ((Element) responses.item(k)).getElementsByTagName(GSXML.DOC_NODE_ELEM);
247 if (nodes == null || nodes.getLength() == 0)
248 continue;
249 num_docs += nodes.getLength();
250 Element last_node = null;
251 Element this_node = null;
252 for (int n = 0; n < nodes.getLength(); n++)
253 {
254 this_node = (Element) nodes.item(n);
255 this_node.setAttribute("collection", coll_name);
256
257 if (k == 0)
258 {
259
260 doc_node_list.appendChild(result_doc.importNode(this_node, true));
261 }
262 else
263 {
264 if (last_node == null)
265 {
266 last_node = (Element) GSXML.getChildByTagName(doc_node_list, GSXML.DOC_NODE_ELEM);
267 }
268 last_node = GSXML.insertIntoOrderedList(doc_node_list, GSXML.DOC_NODE_ELEM, last_node, this_node, "rank", true);
269 }
270
271 }
272 }
273 // just send back num docs returned. Too hard to work out number of matches etc as each index type
274 // does it differently
275 Element metadata_list = result_doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
276 result.appendChild(metadata_list);
277 GSXML.addMetadata(metadata_list, "numDocsReturned", "" + num_docs);
278 return result;
279 }
280
281 // protected Element processAdvTextQuery(Element request)
282 // {
283
284 // }
285
286
287
288 protected boolean initCollectionList(String lang)
289 {
290 UserContext userContext = new UserContext();
291 userContext.setLanguage(lang);
292 userContext.setUserID("");
293
294 // first, get the message router info
295 Document msg_doc = XMLConverter.newDOM();
296 Element coll_list_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
297 Element coll_list_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext); // uid
298 coll_list_message.appendChild(coll_list_request);
299 logger.debug("coll list request = " + this.converter.getPrettyString(coll_list_request));
300 Element coll_list_response = (Element) this.router.process(coll_list_message);
301 if (coll_list_response == null)
302 {
303 logger.error("couldn't query the message router!");
304 return false;
305 }
306 logger.debug("coll list response = " + this.converter.getPrettyString(coll_list_response));
307 // second, get some info from each collection. we want the coll name
308 // and whether its got a text query service
309
310 NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
311 // we can send the same request to multiple collections at once by using a comma separated list
312 Element metadata_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
313 StringBuffer colls_sb = new StringBuffer();
314 for (int i = 0; i < colls.getLength(); i++)
315 {
316 Element c = (Element) colls.item(i);
317 String name = c.getAttribute(GSXML.NAME_ATT);
318 if (i != 0)
319 {
320 colls_sb.append(",");
321 }
322 colls_sb.append(name);
323 //Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, userContext);
324 //metadata_message.appendChild(metadata_request);
325 }
326
327 Element metadata_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, colls_sb.toString(), userContext);
328 metadata_message.appendChild(metadata_request);
329 logger.debug("metadata request = " + this.converter.getPrettyString(metadata_message));
330 Element metadata_response = (Element) this.router.process(metadata_message);
331 logger.debug("metadata response = " + this.converter.getPrettyString(metadata_response));
332 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
333 ArrayList<String> valid_colls = new ArrayList<String>();
334 ArrayList<String> valid_coll_names = new ArrayList<String>();
335 for (int i = 0; i < coll_responses.getLength(); i++)
336 {
337 Element response = (Element) coll_responses.item(i);
338 Element coll = (Element) GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM);
339 Element service_list = (Element) GSXML.getChildByTagName(coll, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
340 if (service_list == null)
341 continue;
342 Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE); // should be AbstractTextSearch.TEXT_QUERY_SERVICE
343 if (query_service == null)
344 continue;
345 // use the name of the response in case we are talking to a remote collection, not the name of the collection.
346 String coll_id = response.getAttribute(GSXML.FROM_ATT);
347 String coll_name = getDisplayText(coll, GSXML.DISPLAY_TEXT_NAME, lang, "en");
348 valid_colls.add(coll_id);
349 valid_coll_names.add(coll_name);
350 }
351
352 this.coll_names_map = new HashMap<String, String[]>();
353
354 // ids no all has the list without 'all' option.
355 this.coll_ids_list_no_all = new String[1];
356 this.coll_ids_list_no_all = valid_colls.toArray(coll_ids_list_no_all);
357
358 valid_colls.add(0, "all");
359 valid_coll_names.add(0, getTextString("param." + COLLECTION_PARAM + ".all", lang));
360
361 this.coll_ids_list = new String[1];
362 this.coll_ids_list = valid_colls.toArray(coll_ids_list);
363
364 String[] coll_names_list = new String[1];
365 coll_names_list = valid_coll_names.toArray(coll_names_list);
366 this.coll_names_map.put(lang, coll_names_list);
367 return true;
368 }
369
370 protected void addCollectionNames(String lang)
371 {
372
373 UserContext userContext = new UserContext();
374 userContext.setLanguage(lang);
375 userContext.setUserID("");
376
377 ArrayList<String> coll_names = new ArrayList<String>();
378 coll_names.add(getTextString("param." + COLLECTION_PARAM + ".all", lang));
379
380 // need to request MR for collection descriptions
381 Document msg_doc = XMLConverter.newDOM();
382 Element metadata_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
383
384 // get a comma separated list of coll ids to send to MR
385 // the first item is the place holder for 'all'
386 StringBuffer colls_sb = new StringBuffer();
387 for (int i = 1; i < coll_ids_list.length; i++)
388 {
389 if (i != 1)
390 {
391 colls_sb.append(",");
392 }
393 colls_sb.append(coll_ids_list[i]);
394 }
395 Element metadata_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, colls_sb.toString(), userContext);
396 // param_list to request just displayTextList
397 Element param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
398 Element param = GSXML.createParameter(msg_doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
399 param_list.appendChild(param);
400 metadata_request.appendChild(param_list);
401 metadata_message.appendChild(metadata_request);
402 logger.debug("coll names metadata request = " + this.converter.getPrettyString(metadata_message));
403 Element metadata_response = (Element) this.router.process(metadata_message);
404 logger.debug("coll names metadata response = " + this.converter.getPrettyString(metadata_response));
405 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
406 for (int i = 0; i < coll_responses.getLength(); i++)
407 {
408 Element response = (Element) coll_responses.item(i);
409 Element coll = (Element) GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM);
410 String coll_name = getDisplayText(coll, GSXML.DISPLAY_TEXT_NAME, lang, "en");
411 coll_names.add(coll_name);
412 }
413
414 String[] coll_names_list = new String[1];
415 coll_names_list = coll_names.toArray(coll_names_list);
416 this.coll_names_map.put(lang, coll_names_list);
417
418 }
419
420 protected Element processDocumentMetadataRetrieve(Element request)
421 {
422 // Create a new (empty) result message
423 Document result_doc = XMLConverter.newDOM();
424 Element result = result_doc.createElement(GSXML.RESPONSE_ELEM);
425 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
426 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
427
428 UserContext userContext = new UserContext(request);
429 // Get the parameters of the request
430 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
431 if (param_list == null)
432 {
433 logger.error("DocumentMetadataRetrieve request had no paramList.");
434 return result; // Return the empty result
435 }
436
437 NodeList query_doc_list = request.getElementsByTagName(GSXML.DOC_NODE_ELEM);
438 if (query_doc_list.getLength() == 0)
439 {
440 logger.error("DocumentMetadataRetrieve request had no documentNodes.");
441 return result; // Return the empty result
442 }
443
444 // the resulting doc node list
445 Element result_node_list = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
446 result.appendChild(result_node_list);
447
448
449 // organise the nodes into collection lists
450 HashMap<String, Node> coll_map = new HashMap<String, Node>();
451
452 for (int i = 0; i < query_doc_list.getLength(); i++)
453 {
454 Element doc_node = (Element) query_doc_list.item(i);
455 String coll_name = doc_node.getAttribute("collection");
456 Element coll_items = (Element) coll_map.get(coll_name);
457 if (coll_items == null)
458 {
459 coll_items = result_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
460 coll_map.put(coll_name, coll_items);
461 }
462 coll_items.appendChild(result_doc.importNode(doc_node, true));
463 }
464
465 // create teh individual requests
466 Document msg_doc = XMLConverter.newDOM();
467 Element meta_request_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
468
469 Set mapping_set = coll_map.entrySet();
470 Iterator iter = mapping_set.iterator();
471
472 while (iter.hasNext())
473 {
474 Map.Entry e = (Map.Entry) iter.next();
475 String cname = (String) e.getKey();
476 Element doc_nodes = (Element) e.getValue();
477 Element meta_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(cname, DOCUMENT_METADATA_RETRIEVE_SERVICE), userContext);
478 meta_request.appendChild(msg_doc.importNode(doc_nodes, true));
479 meta_request.appendChild(msg_doc.importNode(param_list, true));
480 meta_request_message.appendChild(meta_request);
481
482 }
483
484 Node meta_result_node = this.router.process(meta_request_message);
485 Element meta_result = GSXML.nodeToElement(meta_result_node);
486
487 // now need to put the doc nodes back in the right order
488 // go through the original list again. keep an element pointer to
489 // the next element in each collections list
490 NodeList meta_responses = meta_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
491 for (int i = 0; i < meta_responses.getLength(); i++)
492 {
493 String collname = GSPath.removeLastLink(((Element) meta_responses.item(i)).getAttribute(GSXML.FROM_ATT));
494 Element first_elem = (Element) GSXML.getNodeByPath(meta_responses.item(i), "documentNodeList/documentNode");
495 coll_map.put(collname, first_elem);
496 }
497
498 for (int i = 0; i < query_doc_list.getLength(); i++)
499 {
500 Element doc_node = (Element) query_doc_list.item(i);
501 Element new_node = (Element) result_doc.importNode(doc_node, false);
502 result_node_list.appendChild(new_node);
503 String coll_name = doc_node.getAttribute("collection");
504
505 Element meta_elem = (Element) coll_map.get(coll_name);
506 GSXML.mergeMetadataLists(new_node, meta_elem);
507 coll_map.put(coll_name, meta_elem.getNextSibling());
508 }
509 return result;
510 }
511
512 private String[] mergeGroups(UserContext userContext, Element paramList, String[] collArray){
513 Document doc = XMLConverter.newDOM();
514 boolean allSelected = false;
515 Element groupParamList = extractGroupParams(paramList, collArray, doc);
516 Element collParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM);
517 if (collParam != null) {
518 String collValue = GSXML.getValue(collParam);
519 if (collValue != null) {
520 allSelected = collValue.matches(".*\\ball\\b.*");
521 }
522 }
523 //Group param not empty and coll param null or not 'all'
524 if ( allSelected || !groupParamList.hasChildNodes())
525 {
526 return collArray;
527 }
528 //GroupInfo service to get uniq collections
529 String uniqCollServiceName = "UniqueCollections";
530 Element infoResponse = getMRInfo(userContext);
531 Element serviceList = (Element) GSXML.getChildByTagName(infoResponse, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
532 if (serviceList == null) {
533 logger.error("Service list is null!");
534 return collArray;
535 }
536 Element groupInfoService = GSXML.getNamedElement(serviceList, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, uniqCollServiceName);
537 if (groupInfoService == null){
538 logger.error("UniqueCollections service unavailable; Check your groupConfig.xml");
539 return collArray;
540 }
541 Element groupQueryMessage = doc.createElement(GSXML.MESSAGE_ELEM);
542 Element groupQueryRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, uniqCollServiceName, userContext);
543 groupQueryMessage.appendChild(groupQueryRequest);
544 groupQueryRequest.appendChild(groupParamList);
545 if (collParam != null){
546 groupParamList.appendChild(doc.importNode(GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.COLLECTION_ELEM), true));
547 }
548 Element groupQueryResult = (Element) this.router.process(groupQueryMessage);
549 Element groupQueryResponse = (Element) GSXML.getChildByTagName(groupQueryResult, GSXML.RESPONSE_ELEM);
550 Element collList = (Element) GSXML.getChildByTagName(groupQueryResponse, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
551 NodeList collections = GSXML.getChildrenByTagName(collList, GSXML.COLLECTION_ELEM);
552 collArray = new String[collections.getLength()];
553 for (int i = 0; i < collections.getLength(); i++){
554 String collName = ((Element) collections.item(i)).getAttribute(GSXML.NAME_ATT);
555 collArray[i] = collName;
556 }
557 return collArray;
558
559 }
560
561 private Element extractGroupParams(Element paramList, String[] collArray, Document doc) {
562 Element groupParamList = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
563 Element groupParam = null;
564 Element groupNode = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GROUP_PARAM);
565 if (groupNode != null){
566 groupParam = (Element) doc.importNode(groupNode, true);
567 groupParamList.appendChild(groupParam);
568 } else {
569 groupParam = doc.createElement(GSXML.PARAM_ELEM);
570 groupParam.setAttribute(GSXML.NAME_ATT, GSXML.GROUP_ELEM);
571 groupParamList.appendChild(groupParam);
572 }
573 String prefix = GSXML.GROUP_ELEM + "/";
574 for (int i = 0; i < collArray.length; i++) {
575 String collectionParam = collArray[i];
576 if (collectionParam.startsWith(prefix)){
577 String value = groupParam.getAttribute(GSXML.VALUE_ATT);
578 value += "," + collectionParam.substring(prefix.length() - 1 );
579 groupParam.setAttribute(GSXML.VALUE_ATT, value);
580 }
581 }
582 return groupParamList;
583 }
584
585 private Element getMRInfo(UserContext userContext){
586 Document doc = XMLConverter.newDOM();
587 Element infoMessage = doc.createElement(GSXML.MESSAGE_ELEM);
588 Element infoRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
589 Element paramList = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
590 infoRequest.appendChild(paramList);
591 GSXML.addParameterToList(paramList, GSXML.SUBSET_PARAM, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
592 infoMessage.appendChild(infoRequest);
593 Element responseMessage = (Element) this.router.process(infoMessage);
594 if (responseMessage == null)
595 {
596 logger.error("couldn't query the message router!");
597 return null;
598 }
599 Element infoResponse = (Element) GSXML.getChildByTagName(responseMessage, GSXML.RESPONSE_ELEM);
600 if (infoResponse == null)
601 {
602 logger.error("response from message router is null!");
603 return null;
604 }
605
606 return infoResponse;
607
608 }
609
610}
Note: See TracBrowser for help on using the repository browser.