source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/Action.java@ 31137

Last change on this file since 31137 was 31137, checked in by kjdon, 7 years ago

can't use a HashMap to store two params with the same name. doh. lets just add them individually

  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Iterator;
6
7import org.apache.log4j.Logger;
8import org.greenstone.gsdl3.core.ModuleInterface;
9import org.greenstone.gsdl3.util.Dictionary;
10import org.greenstone.gsdl3.util.GSConstants;
11import org.greenstone.gsdl3.util.GSParams;
12import org.greenstone.gsdl3.util.GSXML;
13import org.greenstone.gsdl3.util.GSXSLT;
14import org.greenstone.gsdl3.util.UserContext;
15import org.greenstone.gsdl3.util.XMLConverter;
16import org.w3c.dom.Document;
17import org.w3c.dom.Element;
18import org.w3c.dom.Node;
19import org.w3c.dom.NodeList;
20
21/** base class for Actions */
22abstract public class Action
23{
24
25 /** the system set up variables */
26 protected HashMap<String, Object> config_params = null;
27
28
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;
36
37 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
38
39 public Action()
40 {
41 this.converter = new XMLConverter();
42 }
43
44 /** the config variables must be set before configure is called */
45 public void setConfigParams(HashMap<String, Object> params)
46 {
47 this.config_params = params;
48 }
49
50 /** sets the message router */
51 public void setMessageRouter(ModuleInterface m)
52 {
53 this.mr = m;
54 }
55
56 public boolean configure()
57 {
58 // does nothing yet
59 return true;
60 }
61
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 {
69
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);
79 }
80
81 /** the main process method - must be implemented in subclass */
82 abstract public Node process(Node xml_in);
83
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 */
89 public boolean addActionParameters(GSParams params)
90 {
91 return true;
92 }
93
94 protected void getRequiredMetadataNames(Element format, HashSet<String> meta_names) {
95 GSXSLT.findExtraMetadataNames(format, meta_names);
96 }
97
98
99 protected Element createMetadataParamList(Document doc, HashSet<String> metadata_names)
100 {
101 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
102
103 Element param = null;
104 Iterator<String> i = metadata_names.iterator();
105 while (i.hasNext())
106 {
107 String name = i.next();
108 param = doc.createElement(GSXML.PARAM_ELEM);
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 */
144 protected void addSiteMetadata(Element element, UserContext userContext)
145 {
146 Document doc = element.getOwnerDocument();
147
148 //ADD SITE METADATA
149 Element metadata_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
150 //create the element to put the params in
151 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
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
156 metadata_request.appendChild(param_list);
157 //create the message
158 Element metadata_message = doc.createElement(GSXML.MESSAGE_ELEM);
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
165 // *************** need to merge the displayItem lists too
166 GSXML.mergeMetadataLists(element, metadata_response);
167 GSXML.mergeSpecifiedLists(element, metadata_response, GSXML.DISPLAY_TEXT_ELEM);
168 }
169
170 protected void addInterfaceOptions(Element elem)
171 {
172 Document doc = elem.getOwnerDocument();
173
174 Element documentOptionList = doc.createElement("interfaceOptions");
175 for (Object key : this.config_params.keySet())
176 {
177 Element option = doc.createElement("option");
178 option.setAttribute(GSXML.NAME_ATT, (String) key);
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 }
184
185 protected Element getFormatInfo(String to, UserContext userContext)
186 {
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
194 Document doc = XMLConverter.newDOM();
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);
198 mr_format_message.appendChild(mr_format_request);
199
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;
216 }
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
266}
Note: See TracBrowser for help on using the repository browser.