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

Last change on this file since 32448 was 32448, checked in by kjdon, 6 years ago

params class changed, now returns false by default for shouldsave. so don't need to add any that we don't want saving in the session. turned hard coded strings into static string variables

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