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

Last change on this file since 31344 was 31344, checked in by Georgiy Litvinov, 7 years ago

Update to previous commit

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