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

Last change on this file since 32549 was 32549, checked in by kjdon, 5 years ago

receptionist now passes languageList (from interfaceConfig) to teh actions, so an action can add it into the page response if it needs to, rather than the receptionist adding it in to every page. its only ever used in prefs page

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