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