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