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.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 lang stuff from message page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true)); // add the system stuff from message page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true)); // query type is the subaction String query_type = request.getAttribute(GSXML.SUBACTION_ATT) + "Query"; // for now assume all queries can be handled by basic query return basicQuery(page, request, query_type); } /** 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, String service_name) { // 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; } // 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); String collection = (String)params.get("collection"); if (collection == null || collection.equals("")) { System.err.println("QueryAction Error: no collection was specified!"); return null; } // get the service info from the MR - this will probably need to be cached somehow later on. and add the service node to the page Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM); Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM); mr_info_message.appendChild(mr_info_request); mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE); mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT)); String to = collection; to = GSPath.appendLink(to, service_name); mr_info_request.setAttribute(GSXML.TO_ATT, to); Element mr_info_response = (Element) mr_.process(mr_info_message); System.out.println("describe response from query="); System.out.println(converter_.getString(mr_info_response)); String path = GSXML.RESPONSE_ELEM; path = GSPath.appendLink(path, GSXML.SERVICE_ELEM); Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true); Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); if (pl !=null) { System.out.println("adding shortnames"); // add short names to the params in the param list cgi_.addShortNames(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