[4855] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
| 5 | // XML classes
|
---|
[24990] | 6 | import org.w3c.dom.Node;
|
---|
| 7 | import org.w3c.dom.NodeList;
|
---|
| 8 | import org.w3c.dom.Document;
|
---|
| 9 | import org.w3c.dom.Element;
|
---|
[4855] | 10 |
|
---|
| 11 | import java.util.HashMap;
|
---|
| 12 | import java.util.HashSet;
|
---|
| 13 | import java.util.Vector;
|
---|
| 14 | import java.io.File;
|
---|
| 15 |
|
---|
[13124] | 16 | import org.apache.log4j.*;
|
---|
| 17 |
|
---|
[4855] | 18 | /** action for GS2 style classifier browsing */
|
---|
[24990] | 19 | public class GS2BrowseAction extends Action
|
---|
| 20 | {
|
---|
[13124] | 21 |
|
---|
[24990] | 22 | public static final String CLASSIFIER_ARG = "cl";
|
---|
[13124] | 23 |
|
---|
[24990] | 24 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.GS2BrowseAction.class.getName());
|
---|
[4855] | 25 |
|
---|
[24990] | 26 | /** process the request */
|
---|
| 27 | public Node process(Node message_node)
|
---|
| 28 | {
|
---|
[16688] | 29 |
|
---|
[24990] | 30 | Element message = this.converter.nodeToElement(message_node);
|
---|
[4855] | 31 |
|
---|
[24990] | 32 | // get the request - assume only one
|
---|
| 33 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[4855] | 34 |
|
---|
[24990] | 35 | // the result
|
---|
| 36 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 37 | Element response = classifierBrowse(request);
|
---|
| 38 | result.appendChild(response);
|
---|
| 39 | return result;
|
---|
| 40 | }
|
---|
[4855] | 41 |
|
---|
[24990] | 42 | protected Element classifierBrowse(Element request)
|
---|
| 43 | {
|
---|
[4855] | 44 |
|
---|
[24990] | 45 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[4855] | 46 |
|
---|
[24990] | 47 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
| 48 | Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 49 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
[4855] | 50 |
|
---|
[24990] | 51 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
| 52 | String collection = (String) params.get(GSParams.COLLECTION);
|
---|
| 53 | if (collection == null || collection.equals(""))
|
---|
| 54 | {
|
---|
| 55 | logger.error("classifierBrowse, need to specify a collection!");
|
---|
| 56 | return page_response;
|
---|
[4855] | 57 |
|
---|
[24990] | 58 | }
|
---|
[19984] | 59 |
|
---|
[24993] | 60 | UserContext userContext = new UserContext(request);
|
---|
[24990] | 61 | String to = GSPath.appendLink(collection, service_name);
|
---|
[6165] | 62 |
|
---|
[24990] | 63 | // the first part of the response is the service description
|
---|
| 64 | // for now get this again from the service.
|
---|
| 65 | // this should be cached somehow later on.
|
---|
[4855] | 66 |
|
---|
[24990] | 67 | Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 68 | Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
[24990] | 69 | info_message.appendChild(info_request);
|
---|
[8575] | 70 |
|
---|
[24990] | 71 | // also get the format stuff now if there is some
|
---|
[24993] | 72 | Element format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
|
---|
[24990] | 73 | info_message.appendChild(format_request);
|
---|
| 74 | // process the requests
|
---|
[4855] | 75 |
|
---|
[24990] | 76 | Element info_response = (Element) this.mr.process(info_message);
|
---|
[4855] | 77 |
|
---|
[24990] | 78 | // the two responses
|
---|
| 79 | NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
| 80 | Element service_response = (Element) responses.item(0);
|
---|
| 81 | Element format_response = (Element) responses.item(1);
|
---|
[4855] | 82 |
|
---|
[24990] | 83 | Element service_description = (Element) GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
|
---|
| 84 | page_response.appendChild(this.doc.importNode(service_description, true));
|
---|
[4855] | 85 |
|
---|
[24990] | 86 | //append site metadata
|
---|
[24993] | 87 | addSiteMetadata(page_response, userContext);
|
---|
[4855] | 88 |
|
---|
[24990] | 89 | // if rt=d, then we are just displaying the service
|
---|
| 90 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
| 91 | if (request_type.equals("d"))
|
---|
| 92 | {
|
---|
| 93 | //return the page that we have so far
|
---|
| 94 | return page_response;
|
---|
| 95 | }
|
---|
[4855] | 96 |
|
---|
[24990] | 97 | // get the node that the user has clicked on
|
---|
| 98 | String classifier_node = (String) params.get(CLASSIFIER_ARG);
|
---|
[4855] | 99 |
|
---|
[24990] | 100 | // if the node is not defined, return the page that we have so far
|
---|
| 101 | if (classifier_node == null || classifier_node.equals(""))
|
---|
| 102 | {
|
---|
| 103 | return page_response;
|
---|
| 104 | }
|
---|
[4855] | 105 |
|
---|
[24990] | 106 | // the id of the classifier is the top id of the selected node
|
---|
| 107 | String top_id = OID.getTop(classifier_node);
|
---|
| 108 | HashSet doc_meta_names = new HashSet();
|
---|
| 109 | HashSet class_meta_names = new HashSet();
|
---|
| 110 | // add in the defaults
|
---|
| 111 | doc_meta_names.add("Title");
|
---|
| 112 | class_meta_names.add("Title");
|
---|
[13568] | 113 |
|
---|
[24990] | 114 | // add the format info into the response
|
---|
| 115 | Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
| 116 | if (format_elem != null)
|
---|
| 117 | {
|
---|
[13568] | 118 |
|
---|
[24990] | 119 | // find the one for the classifier we are in
|
---|
| 120 | Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
|
---|
| 121 | if (this_format == null)
|
---|
| 122 | {
|
---|
| 123 | this_format = (Element) GSXML.getChildByTagName(format_elem, GSXML.DEFAULT_ELEM);
|
---|
| 124 | }
|
---|
[13568] | 125 |
|
---|
[24990] | 126 | if (this_format != null)
|
---|
| 127 | {
|
---|
| 128 | Element new_format = GSXML.duplicateWithNewName(this.doc, this_format, GSXML.FORMAT_ELEM, false);
|
---|
| 129 | extractMetadataNames(new_format, doc_meta_names, class_meta_names);
|
---|
| 130 | // set the format type
|
---|
| 131 | new_format.setAttribute(GSXML.TYPE_ATT, "browse");
|
---|
| 132 |
|
---|
| 133 | page_response.appendChild(new_format);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | // find out if this classifier is horizontal at top
|
---|
| 138 | Element class_list = (Element) GSXML.getChildByTagName(service_description, GSXML.CLASSIFIER_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 139 | Element this_classifier = GSXML.getNamedElement(class_list, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
|
---|
| 140 | boolean horizontal_at_top = false;
|
---|
| 141 | if (!this_classifier.getAttribute("horizontalAtTop").equals(""))
|
---|
| 142 | {
|
---|
| 143 | horizontal_at_top = true;
|
---|
| 144 | }
|
---|
| 145 | if (top_id.equals(classifier_node) && horizontal_at_top)
|
---|
| 146 | {
|
---|
| 147 | // we have asked for a top node - if the first list is horizontal, we will select the first element of that list
|
---|
| 148 | // this is a hack. also it craps out if the classifier really isn't horizontalAtTop. -
|
---|
| 149 | classifier_node = classifier_node + ".1";
|
---|
| 150 |
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // get the browse structure for the selected node
|
---|
| 154 | Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 155 | Element classify_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
[24990] | 156 | classify_message.appendChild(classify_request);
|
---|
| 157 |
|
---|
| 158 | //Create a parameter list to specify the required structure information
|
---|
| 159 | // for now, always get ancestors and children
|
---|
| 160 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 161 | classify_request.appendChild(param_list);
|
---|
| 162 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 163 | param_list.appendChild(param);
|
---|
| 164 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
| 165 | param.setAttribute(GSXML.VALUE_ATT, "ancestors");
|
---|
| 166 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 167 | param_list.appendChild(param);
|
---|
| 168 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
| 169 | param.setAttribute(GSXML.VALUE_ATT, "children");
|
---|
| 170 |
|
---|
| 171 | // put the classifier node into a classifier node list
|
---|
| 172 | Element classifier_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 173 | Element classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
| 174 | classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
|
---|
| 175 | classifier_list.appendChild(classifier);
|
---|
| 176 | classify_request.appendChild(classifier_list);
|
---|
| 177 |
|
---|
| 178 | if (horizontal_at_top && !classifier_node.equals(top_id))
|
---|
| 179 | {
|
---|
| 180 | // also put the top id in, to get the persistant horizontal info
|
---|
| 181 | classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
| 182 | classifier.setAttribute(GSXML.NODE_ID_ATT, top_id);
|
---|
| 183 | classifier_list.appendChild(classifier);
|
---|
| 184 | }
|
---|
| 185 | // process the request
|
---|
| 186 | Element classify_response = (Element) this.mr.process(classify_message);
|
---|
| 187 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 188 | Element class_node_list = (Element) GSXML.getNodeByPath(classify_response, path);
|
---|
| 189 |
|
---|
| 190 | path = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
|
---|
| 191 | // assume that we always get back the top level CL1 node - this becomes the page_classifier node
|
---|
| 192 | path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
|
---|
| 193 | Element cl_structure = (Element) GSXML.getNodeByPath(class_node_list, path);
|
---|
| 194 | if (cl_structure == null)
|
---|
| 195 | {
|
---|
| 196 | logger.error("classifier structure request returned no structure");
|
---|
| 197 | return page_response;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | Element page_classifier = null;
|
---|
| 201 | if (horizontal_at_top && !classifier_node.equals(top_id))
|
---|
| 202 | {
|
---|
| 203 | // get the info for the top node
|
---|
| 204 | Element top_node = GSXML.getNamedElement(class_node_list, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, top_id);
|
---|
| 205 | if (top_node != null)
|
---|
| 206 | {
|
---|
| 207 | path = GSPath.appendLink(GSXML.NODE_STRUCTURE_ELEM, GSXML.CLASS_NODE_ELEM);
|
---|
| 208 | Element top_structure = (Element) GSXML.getNodeByPath(top_node, path);
|
---|
| 209 | // add this as the classifier elem
|
---|
| 210 | page_classifier = GSXML.duplicateWithNewName(this.doc, top_structure, GSXML.CLASSIFIER_ELEM, true);
|
---|
| 211 | page_response.appendChild(page_classifier);
|
---|
| 212 | // now replace the child with the structure from the other request
|
---|
| 213 | Element new_classifier = (Element) GSXML.getChildByTagName(cl_structure, GSXML.CLASS_NODE_ELEM);
|
---|
| 214 | String replace_name = new_classifier.getAttribute(GSXML.NODE_ID_ATT);
|
---|
| 215 |
|
---|
| 216 | // find the appropriate child
|
---|
| 217 | Element old_classifier = GSXML.getNamedElement(page_classifier, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, replace_name);
|
---|
| 218 | page_classifier.replaceChild(this.doc.importNode(new_classifier, true), old_classifier);
|
---|
| 219 | page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
|
---|
| 220 | }
|
---|
| 221 | else
|
---|
| 222 | {
|
---|
| 223 | // add the single classifier node as the page classifier
|
---|
| 224 | page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
|
---|
| 225 | page_response.appendChild(page_classifier);
|
---|
| 226 | page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | }
|
---|
| 230 | else
|
---|
| 231 | {
|
---|
| 232 | // add the single classifier node as the page classifier
|
---|
| 233 | page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
|
---|
| 234 | page_response.appendChild(page_classifier);
|
---|
| 235 | page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
|
---|
| 236 | }
|
---|
| 237 | // get the metadata for each classifier node,
|
---|
| 238 | // then for each document node
|
---|
| 239 |
|
---|
| 240 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 241 |
|
---|
| 242 | boolean did_classifier = false;
|
---|
| 243 | boolean did_documents = false;
|
---|
| 244 |
|
---|
| 245 | // if there are classifier nodes
|
---|
| 246 | // create a metadata request for the classifier, and add it to
|
---|
| 247 | // the the message
|
---|
| 248 | NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
|
---|
| 249 |
|
---|
| 250 | if (cl_nodes.getLength() > 0)
|
---|
| 251 | {
|
---|
| 252 | did_classifier = true;
|
---|
[24993] | 253 | Element cl_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
|
---|
[24990] | 254 | metadata_message.appendChild(cl_meta_request);
|
---|
| 255 |
|
---|
| 256 | Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 257 | cl_meta_request.appendChild(new_cl_nodes_list);
|
---|
| 258 |
|
---|
| 259 | for (int c = 0; c < cl_nodes.getLength(); c++)
|
---|
| 260 | {
|
---|
| 261 |
|
---|
| 262 | Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
| 263 | cl.setAttribute(GSXML.NODE_ID_ATT, ((Element) cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
|
---|
| 264 | new_cl_nodes_list.appendChild(cl);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | // create and add in the param list - for now get all the metadata
|
---|
| 268 | // should be based on info sent in from the recept, and the
|
---|
| 269 | // format stuff
|
---|
| 270 | Element cl_param_list = createMetadataParamList(class_meta_names);
|
---|
| 271 | cl_meta_request.appendChild(cl_param_list);
|
---|
| 272 |
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | // if there are document nodes in the classification (happens
|
---|
| 276 | // sometimes), create a second request for document metadata and
|
---|
| 277 | // append to the message
|
---|
| 278 | NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
| 279 | if (doc_nodes.getLength() > 0)
|
---|
| 280 | {
|
---|
| 281 | did_documents = true;
|
---|
[24993] | 282 | Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
|
---|
[24990] | 283 | metadata_message.appendChild(doc_meta_request);
|
---|
| 284 |
|
---|
| 285 | Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 286 | doc_meta_request.appendChild(doc_list);
|
---|
| 287 |
|
---|
| 288 | for (int c = 0; c < doc_nodes.getLength(); c++)
|
---|
| 289 | {
|
---|
| 290 |
|
---|
| 291 | Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
| 292 | d.setAttribute(GSXML.NODE_ID_ATT, ((Element) doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
|
---|
| 293 | doc_list.appendChild(d);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | // create and add in the param list - add all for now
|
---|
| 297 | Element doc_param_list = createMetadataParamList(doc_meta_names);
|
---|
| 298 | doc_meta_request.appendChild(doc_param_list);
|
---|
| 299 |
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | // process the metadata requests
|
---|
| 303 | Element metadata_response = (Element) this.mr.process(metadata_message);
|
---|
| 304 | if (did_classifier)
|
---|
| 305 | {
|
---|
| 306 | // the classifier one will be the first response
|
---|
| 307 | // add the metadata lists for each node back into the
|
---|
| 308 | // page_classifier nodes
|
---|
| 309 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 310 | Node pathNode = GSXML.getNodeByPath(metadata_response, path);
|
---|
| 311 | if (pathNode == null)
|
---|
| 312 | {
|
---|
| 313 | return page_response;
|
---|
| 314 | }
|
---|
| 315 | //NodeList meta_response_cls = (Element)pathNode.getChildNodes(); // can't handle empty elements from converting formatted strings (with empty newlines) into XML
|
---|
| 316 | NodeList meta_response_cls = ((Element) pathNode).getElementsByTagName(GSXML.CLASS_NODE_ELEM);
|
---|
| 317 | for (int i = 0; i < cl_nodes.getLength(); i++)
|
---|
| 318 | {
|
---|
| 319 | GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | if (did_documents)
|
---|
| 324 | {
|
---|
| 325 | NodeList meta_response_docs = null;
|
---|
| 326 | if (!did_classifier)
|
---|
| 327 | {
|
---|
| 328 | // its the first response
|
---|
| 329 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 330 | Node pathNode = GSXML.getNodeByPath(metadata_response, path);
|
---|
| 331 | if (pathNode == null)
|
---|
| 332 | {
|
---|
| 333 | return page_response;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | meta_response_docs = pathNode.getChildNodes();
|
---|
| 337 |
|
---|
| 338 | }
|
---|
| 339 | else
|
---|
| 340 | { // its the second response
|
---|
| 341 | Node nodes = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 342 | if (nodes == null)
|
---|
| 343 | {
|
---|
| 344 | return page_response;
|
---|
| 345 | }
|
---|
| 346 | meta_response_docs = nodes.getChildNodes();
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | for (int i = 0; i < doc_nodes.getLength(); i++)
|
---|
| 350 | {
|
---|
| 351 | GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | logger.debug("(GS2BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
|
---|
| 356 | return page_response;
|
---|
[4855] | 357 | }
|
---|
| 358 |
|
---|
[24990] | 359 | protected void extractMetadataNames(Element new_format, HashSet doc_meta_names, HashSet class_meta_names)
|
---|
| 360 | {
|
---|
[4855] | 361 |
|
---|
[24990] | 362 | NodeList templates = new_format.getElementsByTagName("gsf:template");
|
---|
| 363 | for (int i = 0; i < templates.getLength(); i++)
|
---|
| 364 | {
|
---|
| 365 | Element template = (Element) templates.item(i);
|
---|
| 366 | String match = template.getAttribute("match");
|
---|
| 367 | if (match.equals("documentNode"))
|
---|
| 368 | {
|
---|
| 369 | getRequiredMetadataNames(template, doc_meta_names);
|
---|
| 370 | }
|
---|
| 371 | else if (match.equals("classifierNode"))
|
---|
| 372 | {
|
---|
| 373 | getRequiredMetadataNames(template, class_meta_names);
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
[4855] | 376 | }
|
---|
| 377 |
|
---|
| 378 | }
|
---|