[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
| 5 | // XML classes
|
---|
| 6 | import org.w3c.dom.Node;
|
---|
[3568] | 7 | import org.w3c.dom.NodeList;
|
---|
[3340] | 8 | import org.w3c.dom.Text;
|
---|
| 9 | import org.w3c.dom.Document;
|
---|
| 10 | import org.w3c.dom.Element;
|
---|
| 11 |
|
---|
| 12 | import java.util.HashMap;
|
---|
[3363] | 13 | import java.util.Map;
|
---|
| 14 | import java.util.Iterator;
|
---|
[3455] | 15 | import java.io.File;
|
---|
[3340] | 16 |
|
---|
[3568] | 17 | /** action class for queries */
|
---|
[3340] | 18 | public class QueryAction extends Action {
|
---|
| 19 |
|
---|
[3603] | 20 | /** process - processes a request.
|
---|
| 21 | */
|
---|
[3568] | 22 | public Element process (Element message) {
|
---|
[3340] | 23 |
|
---|
[3568] | 24 | // get the request - assume there is only one
|
---|
| 25 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[3340] | 26 |
|
---|
[3363] | 27 | // create the return page tree
|
---|
[3512] | 28 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
[3568] | 29 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
[3363] | 30 | // add the lang stuff from message
|
---|
[3568] | 31 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
[3363] | 32 | // add the system stuff from message
|
---|
[3568] | 33 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
[3363] | 34 |
|
---|
[3603] | 35 | // query type is the subaction
|
---|
| 36 | String query_type = request.getAttribute(GSXML.SUBACTION_ATT) + "Query";
|
---|
[3340] | 37 |
|
---|
[3603] | 38 | // for now assume all queries can be handled by basic query
|
---|
| 39 | return basicQuery(page, request, query_type);
|
---|
[3340] | 40 |
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[3603] | 43 | /** a generic query handler
|
---|
| 44 | * this gets the service description, does the query (just passes all teh
|
---|
| 45 | * params to the service, then gets the titles for any results
|
---|
| 46 | */
|
---|
| 47 | protected Element basicQuery(Element page, Element request, String service_name) {
|
---|
[3340] | 48 |
|
---|
[3603] | 49 | // check that the stylesheet is present - cant output the page without one.
|
---|
| 50 | String stylesheet = GSFile.stylesheetFile(config_, "basicquery.xsl");
|
---|
[3340] | 51 | if (stylesheet==null) {
|
---|
[3603] | 52 | System.err.println("QueryAction Error: basicquery stylesheet not found!");
|
---|
[3568] | 53 | return null;
|
---|
[3340] | 54 | }
|
---|
[3363] | 55 |
|
---|
| 56 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
[3568] | 57 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3603] | 58 |
|
---|
[3568] | 59 | HashMap params = GSXML.extractParams(cgi_param_list);
|
---|
[3363] | 60 |
|
---|
[3491] | 61 | String collection = (String)params.get("collection");
|
---|
[3363] | 62 | if (collection == null || collection.equals("")) {
|
---|
[3603] | 63 | System.err.println("QueryAction Error: no collection was specified!");
|
---|
[3568] | 64 | return null;
|
---|
[3363] | 65 | }
|
---|
| 66 |
|
---|
[3568] | 67 | // 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
|
---|
[3512] | 68 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 69 | Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
[3363] | 70 | mr_info_message.appendChild(mr_info_request);
|
---|
[3512] | 71 | mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
[3568] | 72 | mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
[3363] | 73 |
|
---|
| 74 | String to = collection;
|
---|
[3603] | 75 | to = GSPath.appendLink(to, service_name);
|
---|
[3512] | 76 | mr_info_request.setAttribute(GSXML.TO_ATT, to);
|
---|
[3363] | 77 |
|
---|
| 78 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
[3603] | 79 | System.out.println("describe response from query=");
|
---|
| 80 | System.out.println(converter_.getString(mr_info_response));
|
---|
[3512] | 81 | String path = GSXML.RESPONSE_ELEM;
|
---|
| 82 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
[3568] | 83 | Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
| 84 | Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3491] | 85 |
|
---|
[3568] | 86 | if (pl !=null) {
|
---|
[3603] | 87 | System.out.println("adding shortnames");
|
---|
[3568] | 88 | // add short names to the params in the param list
|
---|
| 89 | cgi_.addShortNames(pl);
|
---|
| 90 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
| 91 | Element param = (Element)pl.getFirstChild();
|
---|
[3491] | 92 | while (param !=null) {
|
---|
[3512] | 93 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
[3603] | 94 | if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) {
|
---|
| 95 | // get the values for each sub param
|
---|
| 96 | NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
| 97 | for (int i=0; i<subparams.getLength(); i++) {
|
---|
| 98 | String name = ((Element)subparams.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
| 99 | String current = (String)params.get(name);
|
---|
| 100 | if (current !=null && !current.equals("")) {
|
---|
| 101 | Element e = GSXML.createTextElement(pl.getOwnerDocument(), GSXML.DEFAULT_ELEM, current);
|
---|
| 102 | e.setAttribute(GSXML.NAME_ATT, name);
|
---|
| 103 | param.appendChild(e);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | } else {
|
---|
| 107 |
|
---|
| 108 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
| 109 | String current = (String)params.get(name);
|
---|
| 110 | if (current !=null && !current.equals("")) {
|
---|
| 111 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
| 112 | }
|
---|
[3491] | 113 | }
|
---|
[3363] | 114 | }
|
---|
[3491] | 115 | param = (Element)param.getNextSibling();
|
---|
[3363] | 116 | }
|
---|
| 117 | }
|
---|
[3603] | 118 | System.out.println(converter_.getString(pl));
|
---|
[3568] | 119 |
|
---|
| 120 | // part of the data for the description is the cgi-params
|
---|
| 121 | // if we have this here, do we need to do the previous step?
|
---|
| 122 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
| 123 | page.appendChild(cgi_request);
|
---|
| 124 |
|
---|
| 125 | page.appendChild(description);
|
---|
[3603] | 126 |
|
---|
| 127 | // now we always do the query - we have no way of knowing whether
|
---|
| 128 | // a query was specified or not cos we cant assume that q is the
|
---|
| 129 | // query string - maybe we could check that any required params have
|
---|
| 130 | // a value??
|
---|
[3363] | 131 |
|
---|
[3603] | 132 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 133 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
| 134 | mr_query_message.appendChild(mr_query_request);
|
---|
| 135 |
|
---|
| 136 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
| 137 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
| 138 | mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
| 139 | // paramList
|
---|
| 140 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
| 141 | mr_query_request.appendChild(query_param_list);
|
---|
[3568] | 142 |
|
---|
| 143 |
|
---|
[3603] | 144 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
[3568] | 145 |
|
---|
[3603] | 146 | // this result is the list of docs.
|
---|
| 147 | // now take that list, and get the Titles
|
---|
| 148 | // for now, just create a whole new request
|
---|
| 149 |
|
---|
| 150 | // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
|
---|
| 151 | path = GSXML.RESPONSE_ELEM;
|
---|
| 152 | path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
|
---|
| 153 | path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 154 |
|
---|
| 155 | Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response,
|
---|
| 156 | path); // resourceList not present if no docs found
|
---|
| 157 | if (resource_list == null) {
|
---|
[3568] | 158 |
|
---|
[3603] | 159 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
[3568] | 160 |
|
---|
[3603] | 161 | page.appendChild(doc_.importNode(result_response, true));
|
---|
[3568] | 162 |
|
---|
[3603] | 163 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
| 164 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
| 165 | return (Element)transformer_.transform(style_doc, page);
|
---|
[3568] | 166 |
|
---|
[3603] | 167 | }
|
---|
[3363] | 168 |
|
---|
[3603] | 169 | // we have a doc list, so get the metadata - for now, get title.
|
---|
| 170 | // can we dynamically decide what metadata to get?
|
---|
| 171 | Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 172 | Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
| 173 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
[3340] | 174 |
|
---|
[3603] | 175 | mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
| 176 | mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
| 177 | to = GSPath.appendLink(collection, "MetadataRetrieve");
|
---|
| 178 | mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
|
---|
| 179 |
|
---|
| 180 | Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
| 181 | mr_metadata_request.appendChild(meta_content);
|
---|
| 182 |
|
---|
| 183 | // the first part of the content is the doc list
|
---|
| 184 | meta_content.appendChild(doc_.importNode(resource_list, true));
|
---|
| 185 |
|
---|
| 186 | // the second part of the content is the metadata list
|
---|
| 187 | Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 188 | Element title = doc_.createElement(GSXML.METADATA_ELEM);
|
---|
| 189 | title.setAttribute(GSXML.NAME_ATT, "Title");
|
---|
| 190 | metadata_list.appendChild(title);
|
---|
| 191 | meta_content.appendChild(metadata_list);
|
---|
| 192 |
|
---|
| 193 | Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
|
---|
| 194 |
|
---|
| 195 | Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
|
---|
| 196 |
|
---|
| 197 | page.appendChild(doc_.importNode(result_response, true));
|
---|
| 198 |
|
---|
| 199 |
|
---|
[3568] | 200 | // output the page
|
---|
[3455] | 201 | // process using the stylesheet
|
---|
| 202 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
| 203 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
[3568] | 204 | return (Element)transformer_.transform(style_doc, page);
|
---|
[3340] | 205 |
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | }
|
---|