[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
[4695] | 3 | import java.util.HashMap;
|
---|
[4856] | 4 | import java.util.HashSet;
|
---|
| 5 | import java.util.Iterator;
|
---|
[4695] | 6 |
|
---|
[26055] | 7 | import org.apache.log4j.Logger;
|
---|
| 8 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
[29556] | 9 | import org.greenstone.gsdl3.util.Dictionary;
|
---|
[26055] | 10 | import org.greenstone.gsdl3.util.GSConstants;
|
---|
| 11 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 12 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[27144] | 13 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
[26055] | 14 | import org.greenstone.gsdl3.util.UserContext;
|
---|
| 15 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[32063] | 16 | import org.greenstone.gsdl3.util.XMLTransformer;
|
---|
[26055] | 17 | import org.w3c.dom.Document;
|
---|
| 18 | import org.w3c.dom.Element;
|
---|
| 19 | import org.w3c.dom.Node;
|
---|
| 20 | import org.w3c.dom.NodeList;
|
---|
[13124] | 21 |
|
---|
[3568] | 22 | /** base class for Actions */
|
---|
[24867] | 23 | abstract public class Action
|
---|
| 24 | {
|
---|
[24889] | 25 |
|
---|
[24867] | 26 | /** the system set up variables */
|
---|
[27617] | 27 | protected HashMap<String, Object> config_params = null;
|
---|
[28382] | 28 |
|
---|
| 29 |
|
---|
[24867] | 30 | /** a converter class to parse XML and create Docs */
|
---|
| 31 | protected XMLConverter converter = null;
|
---|
[32063] | 32 | /** a transformer class in case the action wants to run XSLT itself */
|
---|
| 33 | protected XMLTransformer transformer = null;
|
---|
[24867] | 34 | /**
|
---|
| 35 | * a reference to the message router that it must talk to to get info. it
|
---|
| 36 | * may be a communicator acting as a proxy, but it doesn't care about that
|
---|
| 37 | */
|
---|
| 38 | protected ModuleInterface mr = null;
|
---|
[3340] | 39 |
|
---|
[24867] | 40 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
|
---|
[3340] | 41 |
|
---|
[24867] | 42 | public Action()
|
---|
| 43 | {
|
---|
| 44 | this.converter = new XMLConverter();
|
---|
[32063] | 45 | this.transformer = new XMLTransformer();
|
---|
[24867] | 46 | }
|
---|
[13124] | 47 |
|
---|
[24867] | 48 | /** the config variables must be set before configure is called */
|
---|
[27617] | 49 | public void setConfigParams(HashMap<String, Object> params)
|
---|
[24867] | 50 | {
|
---|
| 51 | this.config_params = params;
|
---|
| 52 | }
|
---|
[20402] | 53 |
|
---|
[24867] | 54 | /** sets the message router */
|
---|
| 55 | public void setMessageRouter(ModuleInterface m)
|
---|
| 56 | {
|
---|
| 57 | this.mr = m;
|
---|
| 58 | }
|
---|
[3893] | 59 |
|
---|
[24867] | 60 | public boolean configure()
|
---|
| 61 | {
|
---|
| 62 | // does nothing yet
|
---|
| 63 | return true;
|
---|
| 64 | }
|
---|
[4980] | 65 |
|
---|
[24867] | 66 | /**
|
---|
| 67 | * process takes an xml representation of cgi args and returns the page of
|
---|
| 68 | * results - may be in html/xml/other depending on the output att of the
|
---|
| 69 | * request
|
---|
| 70 | */
|
---|
| 71 | public String process(String xml_in)
|
---|
| 72 | {
|
---|
[6248] | 73 |
|
---|
[24867] | 74 | Document message_doc = this.converter.getDOM(xml_in);
|
---|
| 75 | if (message_doc == null)
|
---|
| 76 | {
|
---|
| 77 | logger.error("Couldn't parse request");
|
---|
| 78 | logger.error(xml_in);
|
---|
| 79 | return null;
|
---|
| 80 | }
|
---|
| 81 | Node result = process(message_doc);
|
---|
| 82 | return this.converter.getString(result);
|
---|
[24219] | 83 | }
|
---|
[24087] | 84 |
|
---|
[24867] | 85 | /** the main process method - must be implemented in subclass */
|
---|
| 86 | abstract public Node process(Node xml_in);
|
---|
[24087] | 87 |
|
---|
[24867] | 88 | /**
|
---|
| 89 | * tell the param class what its arguments are if an action has its own
|
---|
| 90 | * arguments, this should add them to the params object - particularly
|
---|
| 91 | * important for args that should not be saved
|
---|
| 92 | */
|
---|
[25302] | 93 | public boolean addActionParameters(GSParams params)
|
---|
[24867] | 94 | {
|
---|
| 95 | return true;
|
---|
| 96 | }
|
---|
[24087] | 97 |
|
---|
[30589] | 98 | protected void getRequiredMetadataNames(Element format, HashSet<String> meta_names) {
|
---|
| 99 | GSXSLT.findExtraMetadataNames(format, meta_names);
|
---|
| 100 | }
|
---|
[24889] | 101 |
|
---|
[26491] | 102 |
|
---|
[28382] | 103 | protected Element createMetadataParamList(Document doc, HashSet<String> metadata_names)
|
---|
[24867] | 104 | {
|
---|
[28382] | 105 | Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[24867] | 106 |
|
---|
| 107 | Element param = null;
|
---|
[25635] | 108 | Iterator<String> i = metadata_names.iterator();
|
---|
[24867] | 109 | while (i.hasNext())
|
---|
| 110 | {
|
---|
[25635] | 111 | String name = i.next();
|
---|
[28382] | 112 | param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
[24867] | 113 | param_list.appendChild(param);
|
---|
| 114 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
| 115 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
| 116 |
|
---|
| 117 | }
|
---|
| 118 | return param_list;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | protected boolean processErrorElements(Element message, Element page)
|
---|
| 122 | {
|
---|
| 123 | NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
|
---|
| 124 | if (error_nodes.getLength() == 0)
|
---|
| 125 | {
|
---|
| 126 | return false;
|
---|
| 127 | }
|
---|
| 128 | Document owner = page.getOwnerDocument();
|
---|
| 129 | for (int i = 0; i < error_nodes.getLength(); i++)
|
---|
| 130 | {
|
---|
| 131 | page.appendChild(owner.importNode(error_nodes.item(i), true));
|
---|
| 132 | }
|
---|
| 133 | return true;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Takes an XML element and adds the metadata of the current site to it.
|
---|
| 138 | * Useful for adding the current site's metadata to a response before
|
---|
| 139 | * sending it
|
---|
| 140 | *
|
---|
| 141 | * @param element
|
---|
| 142 | * the element to add site metadata to
|
---|
| 143 | * @param lang
|
---|
| 144 | * the current language
|
---|
| 145 | * @param uid
|
---|
| 146 | * the current user id
|
---|
| 147 | */
|
---|
[24993] | 148 | protected void addSiteMetadata(Element element, UserContext userContext)
|
---|
[24867] | 149 | {
|
---|
[28382] | 150 | Document doc = element.getOwnerDocument();
|
---|
| 151 |
|
---|
[24867] | 152 | //ADD SITE METADATA
|
---|
[28382] | 153 | Element metadata_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
|
---|
[24867] | 154 | //create the element to put the params in
|
---|
[28382] | 155 | Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[31137] | 156 | // want to get metadataList and displayItemList
|
---|
| 157 | GSXML.addParameterToList(param_list, GSXML.SUBSET_PARAM , GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 158 | GSXML.addParameterToList(param_list, GSXML.SUBSET_PARAM , GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 159 |
|
---|
[24867] | 160 | metadata_request.appendChild(param_list);
|
---|
| 161 | //create the message
|
---|
[28382] | 162 | Element metadata_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24867] | 163 | metadata_message.appendChild(metadata_request);
|
---|
| 164 | //get response
|
---|
| 165 | Element metadata_response_message = (Element) this.mr.process(metadata_message);
|
---|
| 166 | //drill down to response
|
---|
| 167 | Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
|
---|
| 168 | //merge in metadata
|
---|
[31137] | 169 | // *************** need to merge the displayItem lists too
|
---|
[24867] | 170 | GSXML.mergeMetadataLists(element, metadata_response);
|
---|
[30835] | 171 | GSXML.mergeSpecifiedLists(element, metadata_response, GSXML.DISPLAY_TEXT_ELEM);
|
---|
[24867] | 172 | }
|
---|
| 173 |
|
---|
[25128] | 174 | protected void addInterfaceOptions(Element elem)
|
---|
| 175 | {
|
---|
[28382] | 176 | Document doc = elem.getOwnerDocument();
|
---|
| 177 |
|
---|
| 178 | Element documentOptionList = doc.createElement("interfaceOptions");
|
---|
[26026] | 179 | for (Object key : this.config_params.keySet())
|
---|
[25128] | 180 | {
|
---|
[28382] | 181 | Element option = doc.createElement("option");
|
---|
[26026] | 182 | option.setAttribute(GSXML.NAME_ATT, (String) key);
|
---|
[25128] | 183 | option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
|
---|
| 184 | documentOptionList.appendChild(option);
|
---|
| 185 | }
|
---|
| 186 | elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
|
---|
| 187 | }
|
---|
[27144] | 188 |
|
---|
[27998] | 189 | protected Element getFormatInfo(String to, UserContext userContext)
|
---|
| 190 | {
|
---|
[28382] | 191 | // Eclipse call hierarchy shows the element returned from this method is
|
---|
| 192 | // subsequently used in a 'importNode'. For this reason it is safe here
|
---|
| 193 | // for call up our own document DOM.
|
---|
| 194 | //
|
---|
| 195 | // If this pattern changes for any reason, then the DOM will need to be
|
---|
| 196 | // passed in as a parameter
|
---|
| 197 |
|
---|
[28964] | 198 | Document doc = XMLConverter.newDOM();
|
---|
[28382] | 199 |
|
---|
| 200 | Element mr_format_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 201 | Element mr_format_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
|
---|
[27998] | 202 | mr_format_message.appendChild(mr_format_request);
|
---|
[27144] | 203 |
|
---|
[27998] | 204 | // process the message
|
---|
| 205 | Element mr_response_message = (Element) this.mr.process(mr_format_message);
|
---|
| 206 | // the response
|
---|
| 207 |
|
---|
| 208 | Element format_response = (Element) GSXML.getChildByTagName(mr_response_message, GSXML.RESPONSE_ELEM);
|
---|
| 209 |
|
---|
| 210 | Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
| 211 | if (format_elem != null)
|
---|
| 212 | {
|
---|
| 213 | Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
|
---|
| 214 | if (global_format_elem != null)
|
---|
| 215 | {
|
---|
| 216 | GSXSLT.mergeFormatElements(format_elem, global_format_elem, false);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | return format_elem;
|
---|
[27144] | 220 | }
|
---|
[29556] | 221 |
|
---|
| 222 | protected String getTextString(String key, String lang, String dictionary, String[] args)
|
---|
| 223 | {
|
---|
| 224 | logger.error("lang = "+lang);
|
---|
| 225 | if (dictionary != null)
|
---|
| 226 | {
|
---|
| 227 | // just try the one specified dictionary
|
---|
| 228 | Dictionary dict = new Dictionary(dictionary, lang);
|
---|
| 229 | String result = dict.get(key, args);
|
---|
| 230 | if (result == null)
|
---|
| 231 | { // not found
|
---|
| 232 | return "_" + key + "_";
|
---|
| 233 | }
|
---|
| 234 | return result;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | // otherwise we try class names for dictionary names
|
---|
| 238 | String class_name = this.getClass().getName();
|
---|
| 239 | class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
|
---|
| 240 | Dictionary dict = new Dictionary(class_name, lang);
|
---|
| 241 | String result = dict.get(key, args);
|
---|
| 242 | if (result != null)
|
---|
| 243 | {
|
---|
| 244 | return result;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | // we have to try super classes
|
---|
| 248 | Class c = this.getClass().getSuperclass();
|
---|
| 249 | while (result == null && c != null)
|
---|
| 250 | {
|
---|
| 251 | class_name = c.getName();
|
---|
| 252 | class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
|
---|
| 253 | if (class_name.equals("ServiceRack"))
|
---|
| 254 | {
|
---|
| 255 | // this is as far as we go
|
---|
| 256 | break;
|
---|
| 257 | }
|
---|
| 258 | dict = new Dictionary(class_name, lang);
|
---|
| 259 | result = dict.get(key, args);
|
---|
| 260 | c = c.getSuperclass();
|
---|
| 261 | }
|
---|
| 262 | if (result == null)
|
---|
| 263 | {
|
---|
| 264 | return "_" + key + "_";
|
---|
| 265 | }
|
---|
| 266 | return result;
|
---|
| 267 |
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[24867] | 270 | }
|
---|