[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
| 5 | // XML classes
|
---|
[24987] | 6 | import org.w3c.dom.Node;
|
---|
| 7 | import org.w3c.dom.NodeList;
|
---|
| 8 | import org.w3c.dom.Text;
|
---|
| 9 | import org.w3c.dom.Document;
|
---|
| 10 | import org.w3c.dom.Element;
|
---|
[3340] | 11 |
|
---|
| 12 | import java.util.HashMap;
|
---|
[4857] | 13 | import java.util.HashSet;
|
---|
[4080] | 14 | import java.util.Vector;
|
---|
[3363] | 15 | import java.util.Map;
|
---|
| 16 | import java.util.Iterator;
|
---|
[3455] | 17 | import java.io.File;
|
---|
[3340] | 18 |
|
---|
[13124] | 19 | import org.apache.log4j.*;
|
---|
| 20 |
|
---|
[3568] | 21 | /** action class for queries */
|
---|
[24987] | 22 | public class QueryAction extends Action
|
---|
| 23 | {
|
---|
[13124] | 24 |
|
---|
[24987] | 25 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.QueryAction.class.getName());
|
---|
[13124] | 26 |
|
---|
[24987] | 27 | /**
|
---|
| 28 | * process - processes a request.
|
---|
| 29 | */
|
---|
| 30 | public Node process(Node message_node)
|
---|
| 31 | {
|
---|
[3340] | 32 |
|
---|
[24987] | 33 | Element message = this.converter.nodeToElement(message_node);
|
---|
[16688] | 34 |
|
---|
[24987] | 35 | // get the request - assume there is only one
|
---|
| 36 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[3340] | 37 |
|
---|
[24987] | 38 | // create the return message
|
---|
| 39 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 40 | Element response = basicQuery(request);
|
---|
| 41 | result.appendChild(this.doc.importNode(response, true));
|
---|
| 42 | return result;
|
---|
| 43 | }
|
---|
[19984] | 44 |
|
---|
[24987] | 45 | /**
|
---|
| 46 | * a generic query handler this gets the service description, does the query
|
---|
| 47 | * (just passes all the params to the service, then gets the titles for any
|
---|
| 48 | * results
|
---|
| 49 | */
|
---|
| 50 | protected Element basicQuery(Element request)
|
---|
| 51 | {
|
---|
[19984] | 52 |
|
---|
[24987] | 53 | // the result
|
---|
| 54 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[11013] | 55 |
|
---|
[24987] | 56 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
| 57 | Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 58 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
[19984] | 59 |
|
---|
[24987] | 60 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
| 61 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
| 62 | String collection = (String) params.get(GSParams.COLLECTION);
|
---|
[19984] | 63 |
|
---|
[24987] | 64 | // collection may be null or empty when we are doing cross coll services
|
---|
| 65 | if (collection == null || collection.equals(""))
|
---|
| 66 | {
|
---|
| 67 | collection = null;
|
---|
| 68 | }
|
---|
[4080] | 69 |
|
---|
[24993] | 70 | UserContext userContext = new UserContext(request);
|
---|
[24987] | 71 | String to = service_name;
|
---|
| 72 | if (collection != null)
|
---|
| 73 | {
|
---|
| 74 | to = GSPath.prependLink(to, collection);
|
---|
| 75 | }
|
---|
[4080] | 76 |
|
---|
[24987] | 77 | if (request_type.indexOf("d") != -1)
|
---|
| 78 | {
|
---|
| 79 | // we have been asked for the service description
|
---|
| 80 | Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 81 | Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
[24987] | 82 | mr_info_message.appendChild(mr_info_request);
|
---|
[14946] | 83 |
|
---|
[24987] | 84 | // process the message
|
---|
| 85 | Element mr_info_response = (Element) this.mr.process(mr_info_message);
|
---|
| 86 | // the response
|
---|
[24993] | 87 |
|
---|
[24987] | 88 | Element service_response = (Element) GSXML.getChildByTagName(mr_info_response, GSXML.RESPONSE_ELEM);
|
---|
[3753] | 89 |
|
---|
[24987] | 90 | Element service_description = (Element) this.doc.importNode(GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM), true);
|
---|
| 91 | page_response.appendChild(service_description);
|
---|
| 92 | }
|
---|
[9874] | 93 |
|
---|
[24987] | 94 | if (request_type.indexOf("r") == -1)
|
---|
| 95 | {
|
---|
| 96 | // just a display request, no actual processing to do
|
---|
| 97 | //append site metadata
|
---|
[24993] | 98 | addSiteMetadata(page_response, userContext);
|
---|
[24987] | 99 | return page_response;
|
---|
| 100 | }
|
---|
[5191] | 101 |
|
---|
[24987] | 102 | // check that we have some service params
|
---|
| 103 | HashMap service_params = (HashMap) params.get("s1");
|
---|
| 104 | if (service_params == null)
|
---|
| 105 | { // no query
|
---|
| 106 | //append site metadata
|
---|
[24993] | 107 | addSiteMetadata(page_response, userContext);
|
---|
[24987] | 108 | return page_response;
|
---|
| 109 | }
|
---|
[3987] | 110 |
|
---|
[24987] | 111 | // create the query request
|
---|
| 112 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 113 | Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
[24987] | 114 | mr_query_message.appendChild(mr_query_request);
|
---|
[5191] | 115 |
|
---|
[24987] | 116 | Element query_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 117 | GSXML.addParametersToList(this.doc, query_param_list, service_params);
|
---|
| 118 | mr_query_request.appendChild(query_param_list);
|
---|
[5191] | 119 |
|
---|
[24987] | 120 | // also get the format stuff now if there is some
|
---|
[24993] | 121 | Element format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
|
---|
[24987] | 122 | mr_query_message.appendChild(format_request);
|
---|
[5191] | 123 |
|
---|
[24987] | 124 | logger.debug(GSXML.xmlNodeToString(mr_query_message, false));
|
---|
[14946] | 125 |
|
---|
[24987] | 126 | // do the query
|
---|
| 127 | Element mr_query_response = (Element) this.mr.process(mr_query_message);
|
---|
[5191] | 128 |
|
---|
[24987] | 129 | // check for errors
|
---|
| 130 | if (processErrorElements(mr_query_response, page_response))
|
---|
| 131 | {
|
---|
| 132 | //append site metadata
|
---|
[24993] | 133 | addSiteMetadata(page_response, userContext);
|
---|
[24987] | 134 | return page_response;
|
---|
| 135 | }
|
---|
[5191] | 136 |
|
---|
[24987] | 137 | NodeList responses = mr_query_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
| 138 | Element query_response = (Element) responses.item(0);
|
---|
| 139 | Element format_response = (Element) responses.item(1);
|
---|
| 140 |
|
---|
| 141 | Element query_result_metadata_list = (Element) GSXML.getChildByTagName(query_response, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 142 | if (query_result_metadata_list == null)
|
---|
| 143 | {
|
---|
| 144 | logger.error("No query result metadata.\n");
|
---|
| 145 | }
|
---|
| 146 | else
|
---|
| 147 | { // add it into the page response
|
---|
| 148 | page_response.appendChild(this.doc.importNode(query_result_metadata_list, true));
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | Element query_term_info_list = (Element) GSXML.getChildByTagName(query_response, GSXML.TERM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 152 | if (query_term_info_list == null)
|
---|
| 153 | {
|
---|
| 154 | logger.error("No query term information.\n");
|
---|
| 155 | }
|
---|
| 156 | else
|
---|
| 157 | { // add it into the page response
|
---|
| 158 | page_response.appendChild(this.doc.importNode(query_term_info_list, true));
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
|
---|
| 162 | Element document_list = (Element) GSXML.getChildByTagName(query_response, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 163 | // documentList not present if no docs found
|
---|
| 164 | if (document_list == null)
|
---|
| 165 | {
|
---|
| 166 | // add in a dummy doc node list - used by the display. need to think about this
|
---|
| 167 | page_response.appendChild(this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER));
|
---|
| 168 | //append site metadata
|
---|
[24993] | 169 | addSiteMetadata(page_response, userContext);
|
---|
[24987] | 170 | return page_response;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | // now we check to see if there is metadata already - some search services return predefined metadata. if there is some, don't do a metadata request
|
---|
| 174 | NodeList doc_metadata = document_list.getElementsByTagName(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 175 | if (doc_metadata.getLength() > 0)
|
---|
| 176 | {
|
---|
| 177 | logger.error("have already found metadata!");
|
---|
| 178 | // append the doc list to the result
|
---|
| 179 | page_response.appendChild(this.doc.importNode(document_list, true));
|
---|
| 180 | //append site metadata
|
---|
[24993] | 181 | addSiteMetadata(page_response, userContext);
|
---|
[24987] | 182 | return page_response;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | // get the metadata elements needed from the format statement if any
|
---|
| 186 | HashSet metadata_names = new HashSet();
|
---|
| 187 | metadata_names.add("Title");
|
---|
| 188 | // add in the format info to the stylesheet if there is any
|
---|
| 189 | Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
| 190 | if (format_elem != null)
|
---|
| 191 | {
|
---|
| 192 | // set the format type
|
---|
| 193 | format_elem.setAttribute(GSXML.TYPE_ATT, "search");
|
---|
| 194 | // for now just add to the response
|
---|
| 195 | page_response.appendChild(this.doc.importNode(format_elem, true));
|
---|
| 196 | getRequiredMetadataNames(format_elem, metadata_names);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | // paging of the results is done here - we filter the list to remove unwanted entries before retrieving metadata
|
---|
| 200 | Element filtered_doc_list = filterDocList(params, service_params, document_list);
|
---|
| 201 |
|
---|
| 202 | // do the metadata request on the filtered list
|
---|
| 203 | Element mr_metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 204 | to = "DocumentMetadataRetrieve";
|
---|
| 205 | if (collection != null)
|
---|
| 206 | {
|
---|
| 207 | to = GSPath.prependLink(to, collection);
|
---|
| 208 | }
|
---|
[24993] | 209 | Element mr_metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
[24987] | 210 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
| 211 |
|
---|
| 212 | // just get all for now - the receptionist should perhaps pass in some
|
---|
| 213 | // metadata that it wants, and QueryAction should look through the format stuff to see if there is any other?
|
---|
| 214 |
|
---|
| 215 | Element dm_param_list = createMetadataParamList(metadata_names);
|
---|
| 216 |
|
---|
| 217 | mr_metadata_request.appendChild(dm_param_list);
|
---|
| 218 |
|
---|
| 219 | // add in the doc node list too
|
---|
| 220 | mr_metadata_request.appendChild(filtered_doc_list);
|
---|
| 221 |
|
---|
| 222 | Element mr_metadata_response = (Element) this.mr.process(mr_metadata_message);
|
---|
| 223 | // check for errors
|
---|
| 224 | processErrorElements(mr_metadata_response, page_response);
|
---|
| 225 |
|
---|
| 226 | Element metadata_response = (Element) GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
|
---|
| 227 |
|
---|
| 228 | Element query_result_document_list = (Element) GSXML.getChildByTagName(metadata_response, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 229 |
|
---|
| 230 | if (query_result_document_list != null)
|
---|
| 231 | {
|
---|
| 232 | page_response.appendChild(this.doc.importNode(query_result_document_list, true));
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | logger.debug("Query page:\n" + this.converter.getPrettyString(page_response));
|
---|
| 236 | //append site metadata
|
---|
[24993] | 237 | addSiteMetadata(page_response, userContext);
|
---|
[24987] | 238 | return page_response;
|
---|
[5191] | 239 | }
|
---|
| 240 |
|
---|
[24987] | 241 | /** this filters out some of the doc results for result paging */
|
---|
| 242 | protected Element filterDocList(HashMap params, HashMap service_params, Element orig_doc_list)
|
---|
| 243 | {
|
---|
| 244 |
|
---|
| 245 | // check the hits_per_page param - is it a service param??
|
---|
| 246 | String hits_pp = (String) service_params.get("hitsPerPage");
|
---|
| 247 | if (hits_pp == null)
|
---|
| 248 | {
|
---|
| 249 | // the service is doing the paging, so we want to display all of the returned docs(???)
|
---|
| 250 | // return (Element)this.doc.importNode(orig_doc_list, true);
|
---|
| 251 | // try hitsPerPage in the globle param
|
---|
| 252 | hits_pp = (String) params.get("hitsPerPage");
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | int hits = 20;
|
---|
| 256 | if (hits_pp != null && !hits_pp.equals(""))
|
---|
| 257 | {
|
---|
| 258 | try
|
---|
| 259 | {
|
---|
| 260 | hits = Integer.parseInt(hits_pp);
|
---|
| 261 | }
|
---|
| 262 | catch (Exception e)
|
---|
| 263 | {
|
---|
| 264 | hits = 20;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | if (hits == -1)
|
---|
| 269 | { // all
|
---|
| 270 | return (Element) this.doc.importNode(orig_doc_list, true);
|
---|
| 271 | }
|
---|
| 272 | NodeList result_docs = orig_doc_list.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
| 273 |
|
---|
| 274 | int num_docs = result_docs.getLength();
|
---|
| 275 | if (num_docs <= hits)
|
---|
| 276 | {
|
---|
| 277 | // too few docs to do paging
|
---|
| 278 | return (Element) this.doc.importNode(orig_doc_list, true);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | // now we need our own doc list
|
---|
| 282 | Element result_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 283 |
|
---|
| 284 | String start_p = (String) service_params.get("startPage");
|
---|
| 285 | if (start_p == null)
|
---|
| 286 | {
|
---|
| 287 | start_p = (String) params.get("startPage");
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | int start = 1;
|
---|
| 291 | if (start_p != null && !start_p.equals(""))
|
---|
| 292 | {
|
---|
| 293 | try
|
---|
| 294 | {
|
---|
| 295 | start = Integer.parseInt(start_p);
|
---|
| 296 | }
|
---|
| 297 | catch (Exception e)
|
---|
| 298 | {
|
---|
| 299 | start = 1;
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | int start_from = (start - 1) * hits;
|
---|
| 304 | int end_at = (start * hits) - 1;
|
---|
| 305 |
|
---|
| 306 | if (start_from > num_docs)
|
---|
| 307 | {
|
---|
| 308 | // something has gone wrong
|
---|
| 309 | return result_list;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | if (end_at > num_docs)
|
---|
| 313 | {
|
---|
| 314 | end_at = num_docs - 1;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | // now we finally have the docs numbers to use
|
---|
| 318 | for (int i = start_from; i <= end_at; i++)
|
---|
| 319 | {
|
---|
| 320 | result_list.appendChild(this.doc.importNode(result_docs.item(i), true));
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | return result_list;
|
---|
[5191] | 324 | }
|
---|
| 325 |
|
---|
[3340] | 326 | }
|
---|