[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
[25984] | 3 | import java.io.Serializable;
|
---|
[3454] | 4 | import java.util.HashMap;
|
---|
[6371] | 5 | import java.util.HashSet;
|
---|
[3568] | 6 |
|
---|
[25984] | 7 | import org.apache.log4j.Logger;
|
---|
| 8 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 9 | import org.greenstone.gsdl3.util.GSPath;
|
---|
| 10 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[25986] | 11 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
[25984] | 12 | import org.greenstone.gsdl3.util.OID;
|
---|
| 13 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[28382] | 14 | import org.w3c.dom.Document;
|
---|
[25984] | 15 | import org.w3c.dom.Element;
|
---|
| 16 | import org.w3c.dom.Node;
|
---|
| 17 | import org.w3c.dom.NodeList;
|
---|
[25986] | 18 |
|
---|
| 19 | //NOTE: this class not used at present!!!!!
|
---|
[3568] | 20 | /** action for classifier browsing */
|
---|
[25984] | 21 | public class BrowseAction extends Action
|
---|
| 22 | {
|
---|
[13124] | 23 |
|
---|
[25984] | 24 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.BrowseAction.class.getName());
|
---|
[13124] | 25 |
|
---|
[25984] | 26 | public static final String CLASSIFIER_ARG = "cl";
|
---|
| 27 | public static final String SIBLING_ARG = "sib";
|
---|
[3682] | 28 |
|
---|
[25984] | 29 | /** process the request */
|
---|
| 30 | public Node process(Node message_node)
|
---|
| 31 | {
|
---|
[28964] | 32 | Element message = GSXML.nodeToElement(message_node);
|
---|
[28382] | 33 | Document doc = message.getOwnerDocument();
|
---|
| 34 |
|
---|
[25984] | 35 | // get the request - assume only one
|
---|
| 36 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[3454] | 37 |
|
---|
[25984] | 38 | // the result
|
---|
[28382] | 39 | Element result = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[25984] | 40 | Element response = classifierBrowse(request);
|
---|
| 41 | result.appendChild(response);
|
---|
| 42 | return result;
|
---|
| 43 | }
|
---|
[3340] | 44 |
|
---|
[25984] | 45 | protected Element classifierBrowse(Element request)
|
---|
| 46 | {
|
---|
[28382] | 47 | Document doc = request.getOwnerDocument();
|
---|
| 48 |
|
---|
| 49 | Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[3340] | 50 |
|
---|
[25984] | 51 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
| 52 | Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 53 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
|
---|
[3454] | 54 |
|
---|
[25984] | 55 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
| 56 | String collection = (String) params.get(GSParams.COLLECTION);
|
---|
| 57 | if (collection == null || collection.equals(""))
|
---|
| 58 | {
|
---|
| 59 | logger.error("classifierBrowse, need to specify a collection!");
|
---|
| 60 | return page_response;
|
---|
[4189] | 61 |
|
---|
[25984] | 62 | }
|
---|
[3998] | 63 |
|
---|
[25984] | 64 | //whether to retrieve siblings or not
|
---|
| 65 | boolean get_siblings = false;
|
---|
| 66 | String sibs = (String) params.get(SIBLING_ARG);
|
---|
| 67 | if (sibs != null && sibs.equals("1"))
|
---|
| 68 | {
|
---|
| 69 | get_siblings = true;
|
---|
| 70 | }
|
---|
[3913] | 71 |
|
---|
[25984] | 72 | UserContext userContext = new UserContext(request);
|
---|
| 73 | String to = GSPath.appendLink(collection, service_name);
|
---|
[3998] | 74 |
|
---|
[25984] | 75 | // the first part of the response is the service description
|
---|
| 76 | // for now get this again from the service.
|
---|
| 77 | // this should be cached somehow later on.
|
---|
[19984] | 78 |
|
---|
[28382] | 79 | Element info_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 80 | Element info_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
[25984] | 81 | info_message.appendChild(info_request);
|
---|
[4080] | 82 |
|
---|
[25984] | 83 | // also get the format stuff now if there is some
|
---|
[28382] | 84 | Element format_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
|
---|
[25984] | 85 | info_message.appendChild(format_request);
|
---|
| 86 | // process the requests
|
---|
[4696] | 87 |
|
---|
[25984] | 88 | Element info_response = (Element) this.mr.process(info_message);
|
---|
[3454] | 89 |
|
---|
[25984] | 90 | // the two responses
|
---|
| 91 | NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
| 92 | Element service_response = (Element) responses.item(0);
|
---|
| 93 | Element format_response = (Element) responses.item(1);
|
---|
[4080] | 94 |
|
---|
[25984] | 95 | Element service_description = (Element) GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
|
---|
[28382] | 96 | page_response.appendChild(doc.importNode(service_description, true));
|
---|
[3512] | 97 |
|
---|
[25984] | 98 | //append site metadata
|
---|
| 99 | addSiteMetadata(page_response, userContext);
|
---|
| 100 | addInterfaceOptions(page_response);
|
---|
[3894] | 101 |
|
---|
[25984] | 102 | // if rt=d, then we are just displaying the service
|
---|
| 103 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
| 104 | if (request_type.equals("d"))
|
---|
| 105 | {
|
---|
| 106 | //return the page that we have so far
|
---|
| 107 | return page_response;
|
---|
| 108 | }
|
---|
[4080] | 109 |
|
---|
[25984] | 110 | // get the node that the user has clicked on
|
---|
| 111 | String classifier_node = (String) params.get(CLASSIFIER_ARG);
|
---|
[6371] | 112 |
|
---|
[25984] | 113 | // if the node is not defined, return the page that we have so far
|
---|
| 114 | if (classifier_node == null || classifier_node.equals(""))
|
---|
| 115 | {
|
---|
| 116 | return page_response;
|
---|
| 117 | }
|
---|
[4080] | 118 |
|
---|
[25984] | 119 | // the id of the classifier is the top id of the selected node
|
---|
| 120 | String top_id = OID.getTop(classifier_node);
|
---|
[3454] | 121 |
|
---|
[25984] | 122 | HashSet<String> metadata_names = new HashSet<String>();
|
---|
[4080] | 123 |
|
---|
[25984] | 124 | // add the format info into the response
|
---|
| 125 | Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
| 126 | if (format_elem != null)
|
---|
| 127 | {
|
---|
| 128 | // find the one for the classifier we are in
|
---|
| 129 | Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
|
---|
| 130 | if (this_format == null)
|
---|
| 131 | {
|
---|
| 132 | this_format = (Element) GSXML.getChildByTagName(format_elem, GSXML.DEFAULT_ELEM);
|
---|
| 133 | }
|
---|
| 134 | if (this_format != null)
|
---|
| 135 | {
|
---|
[25986] | 136 | Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
|
---|
| 137 | if(global_format_elem != null)
|
---|
| 138 | {
|
---|
| 139 | GSXSLT.mergeFormatElements(this_format, global_format_elem, false);
|
---|
| 140 | }
|
---|
[28382] | 141 | Element new_format = GSXML.duplicateWithNewName(doc, this_format, GSXML.FORMAT_ELEM, false);
|
---|
[25984] | 142 | // set teh format type
|
---|
| 143 | new_format.setAttribute(GSXML.TYPE_ATT, "browse");
|
---|
[3988] | 144 |
|
---|
[25984] | 145 | page_response.appendChild(new_format);
|
---|
| 146 | getRequiredMetadataNames(new_format, metadata_names);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[26026] | 149 |
|
---|
| 150 | Element extraMetaListElem = (Element) GSXML.getChildByTagName(request, GSXML.EXTRA_METADATA + GSXML.LIST_MODIFIER);
|
---|
| 151 | if(extraMetaListElem != null)
|
---|
| 152 | {
|
---|
| 153 | NodeList extraMetaList = extraMetaListElem.getElementsByTagName(GSXML.EXTRA_METADATA);
|
---|
| 154 | for(int i = 0; i < extraMetaList.getLength(); i++)
|
---|
| 155 | {
|
---|
| 156 | metadata_names.add(((Element)extraMetaList.item(i)).getAttribute(GSXML.NAME_ATT));
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
[3454] | 159 |
|
---|
[25984] | 160 | logger.info("extracted meta names, " + metadata_names.toString());
|
---|
| 161 | // get the browse structure for the selected node
|
---|
[28382] | 162 | Element classify_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 163 | Element classify_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
[25984] | 164 | classify_message.appendChild(classify_request);
|
---|
[3865] | 165 |
|
---|
[25984] | 166 | //Create a parameter list to specify the required structure information
|
---|
| 167 | // for now, always get ancestors and children
|
---|
[28382] | 168 | Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25984] | 169 | classify_request.appendChild(param_list);
|
---|
[28382] | 170 | Element param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[25984] | 171 | param_list.appendChild(param);
|
---|
| 172 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
| 173 | param.setAttribute(GSXML.VALUE_ATT, "ancestors");
|
---|
[28382] | 174 | param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[25984] | 175 | param_list.appendChild(param);
|
---|
| 176 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
| 177 | param.setAttribute(GSXML.VALUE_ATT, "children");
|
---|
| 178 | if (get_siblings)
|
---|
| 179 | {
|
---|
[28382] | 180 | param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[25984] | 181 | param_list.appendChild(param);
|
---|
| 182 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
| 183 | param.setAttribute(GSXML.VALUE_ATT, "siblings");
|
---|
| 184 | }
|
---|
[3865] | 185 |
|
---|
[25984] | 186 | // put the classifier node into a classifier node list
|
---|
[28382] | 187 | Element classifier_list = doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 188 | Element classifier = doc.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
[25984] | 189 | classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
|
---|
| 190 | classifier_list.appendChild(classifier);
|
---|
| 191 | classify_request.appendChild(classifier_list);
|
---|
| 192 |
|
---|
| 193 | // process the request
|
---|
| 194 | Element classify_response = (Element) this.mr.process(classify_message);
|
---|
| 195 | // get the structure element
|
---|
| 196 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 197 | path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
|
---|
| 198 | path = GSPath.appendLink(path, GSXML.NODE_STRUCTURE_ELEM);
|
---|
| 199 | // assume that we always get back the top level CL1 node - this becomes the page_classifier node
|
---|
| 200 | path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
|
---|
| 201 | Element cl_structure = (Element) GSXML.getNodeByPath(classify_response, path);
|
---|
| 202 | if (cl_structure == null)
|
---|
| 203 | {
|
---|
| 204 | logger.error("classifier structure request returned no structure");
|
---|
| 205 | return page_response;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | // add the classifier node as the page classifier
|
---|
[28382] | 209 | Element page_classifier = GSXML.duplicateWithNewName(doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
|
---|
[25984] | 210 | page_response.appendChild(page_classifier);
|
---|
| 211 | page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
|
---|
| 212 |
|
---|
| 213 | // get the metadata for each classifier node,
|
---|
| 214 | // then for each document node
|
---|
| 215 |
|
---|
[28382] | 216 | Element metadata_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[25984] | 217 |
|
---|
| 218 | boolean did_classifier = false;
|
---|
| 219 | boolean did_documents = false;
|
---|
| 220 |
|
---|
| 221 | // if there are classifier nodes
|
---|
| 222 | // create a metadata request for the classifier, and add it to
|
---|
| 223 | // the the message
|
---|
| 224 | NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
|
---|
| 225 |
|
---|
| 226 | if (cl_nodes.getLength() > 0)
|
---|
| 227 | {
|
---|
| 228 | did_classifier = true;
|
---|
[28382] | 229 | Element cl_meta_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
|
---|
[25984] | 230 | metadata_message.appendChild(cl_meta_request);
|
---|
| 231 |
|
---|
[28382] | 232 | Element new_cl_nodes_list = doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25984] | 233 | cl_meta_request.appendChild(new_cl_nodes_list);
|
---|
| 234 |
|
---|
| 235 | for (int c = 0; c < cl_nodes.getLength(); c++)
|
---|
| 236 | {
|
---|
| 237 |
|
---|
[28382] | 238 | Element cl = doc.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
[25984] | 239 | cl.setAttribute(GSXML.NODE_ID_ATT, ((Element) cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
|
---|
| 240 | new_cl_nodes_list.appendChild(cl);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | // create and add in the param list - for now get all the metadata
|
---|
| 244 | // should be based on info sent in from the recept, and the
|
---|
| 245 | // format stuff
|
---|
| 246 | Element cl_param_list = null;
|
---|
| 247 | if (metadata_names.isEmpty())
|
---|
| 248 | {
|
---|
[28382] | 249 | cl_param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 250 | Element p = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[25984] | 251 | cl_param_list.appendChild(p);
|
---|
| 252 | p.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
| 253 | p.setAttribute(GSXML.VALUE_ATT, "Title");
|
---|
| 254 | }
|
---|
| 255 | else
|
---|
| 256 | {
|
---|
[28382] | 257 | cl_param_list = createMetadataParamList(doc,metadata_names);
|
---|
[25984] | 258 | }
|
---|
| 259 |
|
---|
| 260 | cl_meta_request.appendChild(cl_param_list);
|
---|
| 261 |
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | // if there are document nodes in the classification (happens
|
---|
| 265 | // sometimes), create a second request for document metadata and
|
---|
| 266 | // append to the message
|
---|
| 267 | NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
| 268 | if (doc_nodes.getLength() > 0)
|
---|
| 269 | {
|
---|
| 270 | did_documents = true;
|
---|
[28382] | 271 | Element doc_meta_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
|
---|
[25984] | 272 | metadata_message.appendChild(doc_meta_request);
|
---|
| 273 |
|
---|
[28382] | 274 | Element doc_list = doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25984] | 275 | doc_meta_request.appendChild(doc_list);
|
---|
| 276 |
|
---|
| 277 | for (int c = 0; c < doc_nodes.getLength(); c++)
|
---|
| 278 | {
|
---|
| 279 |
|
---|
[28382] | 280 | Element d = doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
[25984] | 281 | d.setAttribute(GSXML.NODE_ID_ATT, ((Element) doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
|
---|
| 282 | doc_list.appendChild(d);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | // create and add in the param list - add all for now
|
---|
[28382] | 286 | Element doc_param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 287 | Element p = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[25984] | 288 | doc_param_list.appendChild(p);
|
---|
| 289 | p.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
| 290 | p.setAttribute(GSXML.VALUE_ATT, "all");
|
---|
| 291 | doc_meta_request.appendChild(doc_param_list);
|
---|
| 292 |
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | // process the metadata requests
|
---|
| 296 | Element metadata_response = (Element) this.mr.process(metadata_message);
|
---|
| 297 | if (did_classifier)
|
---|
| 298 | {
|
---|
| 299 | // the classifier one will be the first response
|
---|
| 300 | // add the metadata lists for each node back into the
|
---|
| 301 | // page_classifier nodes
|
---|
| 302 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 303 | NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
|
---|
| 304 | for (int i = 0; i < cl_nodes.getLength(); i++)
|
---|
| 305 | {
|
---|
| 306 | GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | if (did_documents)
|
---|
| 310 | {
|
---|
| 311 | NodeList meta_response_docs = null;
|
---|
| 312 | if (!did_classifier)
|
---|
| 313 | {
|
---|
| 314 | // its the first response
|
---|
| 315 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 316 | meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
|
---|
| 317 | }
|
---|
| 318 | else
|
---|
| 319 | { // its the second response
|
---|
| 320 | meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER).getChildNodes();
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | for (int i = 0; i < doc_nodes.getLength(); i++)
|
---|
| 324 | {
|
---|
| 325 | GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | logger.debug("(BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
|
---|
| 330 | return page_response;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | protected Element unknownBrowse(Element page, Element request, String browse_type)
|
---|
| 334 | {
|
---|
| 335 | logger.error("unknown browse subtype: " + browse_type);
|
---|
| 336 | return null;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|