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