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

Last change on this file since 27998 was 27998, checked in by sjm84, 11 years ago

Reformatting this file

  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 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.GSConstants;
10import org.greenstone.gsdl3.util.GSParams;
11import org.greenstone.gsdl3.util.GSXML;
12import org.greenstone.gsdl3.util.GSXSLT;
13import org.greenstone.gsdl3.util.UserContext;
14import org.greenstone.gsdl3.util.XMLConverter;
15import org.w3c.dom.Document;
16import org.w3c.dom.Element;
17import org.w3c.dom.Node;
18import org.w3c.dom.NodeList;
19
20/** base class for Actions */
21abstract public class Action
22{
23
24 /** the system set up variables */
25 protected HashMap<String, Object> config_params = null;
26 /** container Document to create XML Nodes */
27 protected Document doc = null;
28 /** a converter class to parse XML and create Docs */
29 protected XMLConverter converter = null;
30 /**
31 * a reference to the message router that it must talk to to get info. it
32 * may be a communicator acting as a proxy, but it doesn't care about that
33 */
34 protected ModuleInterface mr = null;
35
36 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
37
38 public Action()
39 {
40 this.converter = new XMLConverter();
41 this.doc = this.converter.newDOM();
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 {
96 extractMetadataNames(format, meta_names);
97 addLinkMetadataNames(format, meta_names);
98 }
99
100 // should change to metadataList?? and use attributes for select rather than
101 // prepending parent_ etc
102 protected void extractMetadataNames(Element format, HashSet<String> meta_names)
103 {
104 NodeList metadata_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "metadata"); // gsf:metadata
105 for (int i = 0; i < metadata_nodes.getLength(); i++)
106 {
107 Element elem = (Element) metadata_nodes.item(i);
108 String name = elem.getAttribute("name");
109 String select = elem.getAttribute("select");
110
111 if (!select.equals(""))
112 {
113 name = select + GSConstants.META_RELATION_SEP + name;
114 }
115 meta_names.add(name);
116 }
117
118 NodeList foreach_metadata_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "foreach-metadata"); // gsf:foreach-metadata
119 for (int i = 0; i < foreach_metadata_nodes.getLength(); i++)
120 {
121 Element elem = (Element) foreach_metadata_nodes.item(i);
122 String name = elem.getAttribute("name");
123 String select = elem.getAttribute("select");
124
125 if (!select.equals(""))
126 {
127 name = select + GSConstants.META_RELATION_SEP + name;
128 }
129 meta_names.add(name);
130 }
131 }
132
133 protected void addLinkMetadataNames(Element format, HashSet<String> meta_names)
134 {
135 // The XSL tranform for
136 // gsf:link type="source"
137 // makes use of 'assocfilepath' so need to make sure it's asked for
138
139 boolean getEquivLinkMeta = false;
140
141 NodeList link_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "link");
142 for (int i = 0; i < link_nodes.getLength(); i++)
143 {
144 Element elem = (Element) link_nodes.item(i);
145 String type = elem.getAttribute("type");
146 if (type.equals("source"))
147 {
148 meta_names.add("assocfilepath");
149 meta_names.add("srclinkFile");
150 }
151 else if (type.equals("web"))
152 {
153 meta_names.add("weblink");
154 meta_names.add("webicon");
155 meta_names.add("/weblink");
156 }
157 else if (type.equals("equivdoc"))
158 {
159 getEquivLinkMeta = true;
160 }
161 }
162
163 // get all the metadata necessary for when the user has used "gsf:equivlink"
164 // so that we can build up the equivlink from the metadata components it needs
165 link_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "equivlinkgs3");
166 if (getEquivLinkMeta || link_nodes.getLength() > 0)
167 {
168 String[] equivlink_metanames = { "equivDocIcon", "equivDocLink", "/equivDocLink" };
169
170 for (int i = 0; i < equivlink_metanames.length; i++)
171 {
172 StringBuffer metadata = new StringBuffer();
173 metadata.append("all"); // this means the attr multiple = true;
174 metadata.append(GSConstants.META_RELATION_SEP);
175
176 metadata.append(GSConstants.META_SEPARATOR_SEP);
177 metadata.append(','); // attr separator = ","
178 metadata.append(GSConstants.META_SEPARATOR_SEP);
179 metadata.append(GSConstants.META_RELATION_SEP);
180
181 // the name of the metadata we're retrieving
182 metadata.append(equivlink_metanames[i]);
183 meta_names.add(metadata.toString());
184 }
185 }
186
187 if (format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "image").getLength() > 0)
188 {
189 meta_names.add("Thumb");
190 meta_names.add("Screen");
191 meta_names.add("SourceFile");
192 }
193 }
194
195 protected Element createMetadataParamList(HashSet<String> metadata_names)
196 {
197 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
198
199 Element param = null;
200 Iterator<String> i = metadata_names.iterator();
201 while (i.hasNext())
202 {
203 String name = i.next();
204 param = this.doc.createElement(GSXML.PARAM_ELEM);
205 param_list.appendChild(param);
206 param.setAttribute(GSXML.NAME_ATT, "metadata");
207 param.setAttribute(GSXML.VALUE_ATT, name);
208
209 }
210 return param_list;
211 }
212
213 protected boolean processErrorElements(Element message, Element page)
214 {
215 NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
216 if (error_nodes.getLength() == 0)
217 {
218 return false;
219 }
220 Document owner = page.getOwnerDocument();
221 for (int i = 0; i < error_nodes.getLength(); i++)
222 {
223 page.appendChild(owner.importNode(error_nodes.item(i), true));
224 }
225 return true;
226 }
227
228 /**
229 * Takes an XML element and adds the metadata of the current site to it.
230 * Useful for adding the current site's metadata to a response before
231 * sending it
232 *
233 * @param element
234 * the element to add site metadata to
235 * @param lang
236 * the current language
237 * @param uid
238 * the current user id
239 */
240 protected void addSiteMetadata(Element element, UserContext userContext)
241 {
242 //ADD SITE METADATA
243 Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
244 //create a hashmap of params
245 HashMap subset_params = new HashMap(1);
246 subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
247 //create the element to put the params in
248 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
249 //put them in
250 GSXML.addParametersToList(this.doc, param_list, subset_params);
251 metadata_request.appendChild(param_list);
252 //create the message
253 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
254 metadata_message.appendChild(metadata_request);
255 //get response
256 Element metadata_response_message = (Element) this.mr.process(metadata_message);
257 //drill down to response
258 Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
259 //merge in metadata
260 GSXML.mergeMetadataLists(element, metadata_response);
261 }
262
263 protected void addInterfaceOptions(Element elem)
264 {
265 Element documentOptionList = this.doc.createElement("interfaceOptions");
266 for (Object key : this.config_params.keySet())
267 {
268 Element option = this.doc.createElement("option");
269 option.setAttribute(GSXML.NAME_ATT, (String) key);
270 option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
271 documentOptionList.appendChild(option);
272 }
273 elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
274 }
275
276 protected Element getFormatInfo(String to, UserContext userContext)
277 {
278 Element mr_format_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
279 Element mr_format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
280 mr_format_message.appendChild(mr_format_request);
281
282 // process the message
283 Element mr_response_message = (Element) this.mr.process(mr_format_message);
284 // the response
285
286 Element format_response = (Element) GSXML.getChildByTagName(mr_response_message, GSXML.RESPONSE_ELEM);
287
288 Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
289 if (format_elem != null)
290 {
291 Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
292 if (global_format_elem != null)
293 {
294 GSXSLT.mergeFormatElements(format_elem, global_format_elem, false);
295 }
296 }
297 return format_elem;
298 }
299}
Note: See TracBrowser for help on using the repository browser.