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

Last change on this file since 30832 was 30589, checked in by kjdon, 8 years ago

use the new GSXSLT.findExtraMetadataNames in action and transformingReceptionist instead of these two places each implementing this functionality and doing it differently

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 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 a hashmap of params
151 HashMap subset_params = new HashMap(1);
152 subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
153 //create the element to put the params in
154 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
155 //put them in
156 GSXML.addParametersToList(param_list, subset_params);
157 metadata_request.appendChild(param_list);
158 //create the message
159 Element metadata_message = doc.createElement(GSXML.MESSAGE_ELEM);
160 metadata_message.appendChild(metadata_request);
161 //get response
162 Element metadata_response_message = (Element) this.mr.process(metadata_message);
163 //drill down to response
164 Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
165 //merge in metadata
166 GSXML.mergeMetadataLists(element, metadata_response);
167 }
168
169 protected void addInterfaceOptions(Element elem)
170 {
171 Document doc = elem.getOwnerDocument();
172
173 Element documentOptionList = doc.createElement("interfaceOptions");
174 for (Object key : this.config_params.keySet())
175 {
176 Element option = doc.createElement("option");
177 option.setAttribute(GSXML.NAME_ATT, (String) key);
178 option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
179 documentOptionList.appendChild(option);
180 }
181 elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
182 }
183
184 protected Element getFormatInfo(String to, UserContext userContext)
185 {
186 // Eclipse call hierarchy shows the element returned from this method is
187 // subsequently used in a 'importNode'. For this reason it is safe here
188 // for call up our own document DOM.
189 //
190 // If this pattern changes for any reason, then the DOM will need to be
191 // passed in as a parameter
192
193 Document doc = XMLConverter.newDOM();
194
195 Element mr_format_message = doc.createElement(GSXML.MESSAGE_ELEM);
196 Element mr_format_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
197 mr_format_message.appendChild(mr_format_request);
198
199 // process the message
200 Element mr_response_message = (Element) this.mr.process(mr_format_message);
201 // the response
202
203 Element format_response = (Element) GSXML.getChildByTagName(mr_response_message, GSXML.RESPONSE_ELEM);
204
205 Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
206 if (format_elem != null)
207 {
208 Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
209 if (global_format_elem != null)
210 {
211 GSXSLT.mergeFormatElements(format_elem, global_format_elem, false);
212 }
213 }
214 return format_elem;
215 }
216
217 protected String getTextString(String key, String lang, String dictionary, String[] args)
218 {
219 logger.error("lang = "+lang);
220 if (dictionary != null)
221 {
222 // just try the one specified dictionary
223 Dictionary dict = new Dictionary(dictionary, lang);
224 String result = dict.get(key, args);
225 if (result == null)
226 { // not found
227 return "_" + key + "_";
228 }
229 return result;
230 }
231
232 // otherwise we try class names for dictionary names
233 String class_name = this.getClass().getName();
234 class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
235 Dictionary dict = new Dictionary(class_name, lang);
236 String result = dict.get(key, args);
237 if (result != null)
238 {
239 return result;
240 }
241
242 // we have to try super classes
243 Class c = this.getClass().getSuperclass();
244 while (result == null && c != null)
245 {
246 class_name = c.getName();
247 class_name = class_name.substring(class_name.lastIndexOf('.') + 1);
248 if (class_name.equals("ServiceRack"))
249 {
250 // this is as far as we go
251 break;
252 }
253 dict = new Dictionary(class_name, lang);
254 result = dict.get(key, args);
255 c = c.getSuperclass();
256 }
257 if (result == null)
258 {
259 return "_" + key + "_";
260 }
261 return result;
262
263 }
264
265}
Note: See TracBrowser for help on using the repository browser.