/* * CrossCollectionSearch.java * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package org.greenstone.gsdl3.service; import org.greenstone.gsdl3.util.GSXML; import org.greenstone.gsdl3.util.GSPath; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.ArrayList; import java.util.Map; import java.util.Iterator; import java.util.Set; import java.util.HashMap; import org.apache.log4j.*; /** * * @author Katherine Don */ public class CrossCollectionSearch extends ServiceRack { static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.CrossCollectionSearch.class.getName()); protected static final String QUERY_PARAM = "query"; protected static final String COLLECTION_PARAM = "collection"; // the services on offer - these proxy the actual collection ones protected static final String TEXT_QUERY_SERVICE = "TextQuery"; protected static final String ADV_QUERY_SERVICE = "AdvTextQuery"; protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve"; protected String [] coll_ids_list = null; protected String [] coll_ids_list_no_all = null; protected String [] coll_names_list = null; /** constructor */ public CrossCollectionSearch() { } public boolean configure(Element info, Element extra_info) { // any parameters? colls to include?? logger.info("Configuring CrossCollectionSearch..."); // query service Element ccs_service = this.doc.createElement(GSXML.SERVICE_ELEM); ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY); ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE); this.short_service_info.appendChild(ccs_service); // metadata service Element dmr_service = this.doc.createElement(GSXML.SERVICE_ELEM); dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE); dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE); this.short_service_info.appendChild(dmr_service); // get any format info Element format_info = (Element)GSXML.getChildByTagName(info, GSXML.FORMAT_ELEM); if (format_info != null) { this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(format_info, true)); } else { // add in a default format statement String format_string = "?a=d&c=&d=&sib=1&dt=&p.a=q&p.s="+TEXT_QUERY_SERVICE+"&p.c="; if (this.cluster_name!=null) { format_string += this.cluster_name; } format_string += " () "; this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(this.converter.getDOM(format_string).getDocumentElement(), true)); } return true; } protected Element getServiceDescription(String service, String lang, String subset) { if (service.equals(TEXT_QUERY_SERVICE)) { Element ccs_service = this.doc.createElement(GSXML.SERVICE_ELEM); ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY); ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE); // display info if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) { ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(TEXT_QUERY_SERVICE+".name", lang))); ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(TEXT_QUERY_SERVICE+".submit", lang))); ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(TEXT_QUERY_SERVICE+".description", lang))); } // param info if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) { Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); // collection list if (coll_ids_list ==null ) { initCollectionList(); } 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); param_list.appendChild(param); // query param param = GSXML.createParameterDescription(this.doc, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null); param_list.appendChild(param); ccs_service.appendChild(param_list); } logger.debug("service description="+this.converter.getPrettyString(ccs_service)); return ccs_service; } // these ones are probably never called, but put them here just in case Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM); service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE); service_elem.setAttribute(GSXML.NAME_ATT, service); return service_elem; } protected Element processTextQuery(Element request) { // Create a new (empty) result message Element result = this.doc.createElement(GSXML.RESPONSE_ELEM); result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE); result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); String lang = request.getAttribute(GSXML.LANG_ATT); // Get the parameters of the request Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); if (param_list == null) { logger.error("TextQuery request had no paramList."); return result; // Return the empty result } // get the collection list String [] colls_list = coll_ids_list_no_all; Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM); if (coll_param != null) { String coll_list = GSXML.getValue(coll_param); if (!coll_list.equals("all") && !coll_list.equals("")) { colls_list = coll_list.split(","); } } Element query_message = this.doc.createElement(GSXML.MESSAGE_ELEM); // we are sending the same request to each collection - build up the to // attribute for the request StringBuffer to_att = new StringBuffer(); for (int i=0; i0) { to_att.append(","); } to_att.append(GSPath.appendLink(colls_list[i], "TextQuery")); } // send the query to all colls Element query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to_att.toString(), lang, ""); query_message.appendChild(query_request); // should we add params individually? Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); query_request.appendChild(new_param_list); new_param_list.appendChild(this.doc.importNode(GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, QUERY_PARAM), true)); Element query_result = (Element)this.router.process(query_message); // gather up the data from each response int numDocsMatched = 0; int numDocsReturned = 0; //term info?? NodeList metadata = query_result.getElementsByTagName(GSXML.METADATA_ELEM); for (int j=0; j