package org.greenstone.gsdl3.action; import org.greenstone.gsdl3.core.ModuleInterface; import org.greenstone.gsdl3.util.*; // XML classes import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.w3c.dom.Document; import org.w3c.dom.Element; import java.util.HashMap; import java.util.Vector; import java.util.Map; import java.util.Iterator; import java.io.File; /** action class for queries */ public class QueryAction extends Action { /** process - processes a request. */ public Element process (Element message) { // get the request - assume there is only one Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); // create the return page tree Element page = doc_.createElement(GSXML.PAGE_ELEM); page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); // add the page extra stuff from message page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.PAGE_EXTRA_ELEM), true)); // part of the data for the page is the cgi-params // if we have this here, do we need to overwrite default values in the // param list down below?? Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true); page.appendChild(page_request); // if want to have a different type of query here, check the subaction att of request // for now assume all queries can be handled by basic query return basicQuery(page, request); } /** a generic query handler * this gets the service description, does the query (just passes all teh * params to the service, then gets the titles for any results */ protected Element basicQuery(Element page, Element request) { // check that the stylesheet is present - cant output the page without one. String stylesheet = GSFile.stylesheetFile(config_, "basicquery.xsl"); if (stylesheet==null) { System.err.println("QueryAction Error: basicquery stylesheet not found!"); return null; } Document style_doc = converter_.getDOM(new File(stylesheet)); // extract the params from the cgi-request, and check that we have a coll specified Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); HashMap params = GSXML.extractParams(cgi_param_list, false); String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG); String service_name = (String)params.get(GSCGI.SERVICE_ARG); String collection = (String)params.get(GSCGI.COLLECTION_ARG); if (collection == null || collection.equals("")) { System.err.println("QueryAction Error: no collection was specified!"); return null; } String lang = request.getAttribute(GSXML.LANG_ATT); String to = GSPath.appendLink(collection, service_name); //create the pageResponse Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM); page.appendChild(page_response); // the second part of the page is the service description // for now get this again from the service. // this will probably need to be cached somehow later on. Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM); Element mr_info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang); mr_info_message.appendChild(mr_info_request); // also get the format stuff now if there is some Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang); mr_info_message.appendChild(format_request); // also get the coll description Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang); mr_info_message.appendChild(coll_about_request); // process the messages Element mr_info_response = (Element) mr_.process(mr_info_message); // the three responses NodeList responses = mr_info_response.getElementsByTagName(GSXML.RESPONSE_ELEM); Element service_response = (Element)responses.item(0); Element format_response = (Element)responses.item(1); Element coll_response = (Element)responses.item(2); Element service_description = (Element)doc_.importNode(GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM), true); page_response.appendChild(service_description); Element pl = (Element)GSXML.getChildByTagName(service_description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); if (pl !=null) { // add short names to the params in the param list cgi_.paramListAddShortNames(pl); // for each param in the description, overwrite teh default value with the currently set value if present Element param = (Element)pl.getFirstChild(); while (param !=null) { if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) { // get the values for each sub param NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM); for (int i=0; i