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