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.GSConstants;
|
---|
10 | import org.greenstone.gsdl3.util.GSParams;
|
---|
11 | import org.greenstone.gsdl3.util.GSXML;
|
---|
12 | import org.greenstone.gsdl3.util.UserContext;
|
---|
13 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
14 | import org.w3c.dom.Document;
|
---|
15 | import org.w3c.dom.Element;
|
---|
16 | import org.w3c.dom.Node;
|
---|
17 | import org.w3c.dom.NodeList;
|
---|
18 |
|
---|
19 | /** base class for Actions */
|
---|
20 | abstract public class Action
|
---|
21 | {
|
---|
22 |
|
---|
23 | /** the system set up variables */
|
---|
24 | protected HashMap<String, Comparable> config_params = null;
|
---|
25 | /** container Document to create XML Nodes */
|
---|
26 | protected Document doc = null;
|
---|
27 | /** a converter class to parse XML and create Docs */
|
---|
28 | protected XMLConverter converter = null;
|
---|
29 | /**
|
---|
30 | * a reference to the message router that it must talk to to get info. it
|
---|
31 | * may be a communicator acting as a proxy, but it doesn't care about that
|
---|
32 | */
|
---|
33 | protected ModuleInterface mr = null;
|
---|
34 |
|
---|
35 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
|
---|
36 |
|
---|
37 | public Action()
|
---|
38 | {
|
---|
39 | this.converter = new XMLConverter();
|
---|
40 | this.doc = this.converter.newDOM();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /** the config variables must be set before configure is called */
|
---|
44 | public void setConfigParams(HashMap<String, Comparable> params)
|
---|
45 | {
|
---|
46 | this.config_params = params;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /** sets the message router */
|
---|
50 | public void setMessageRouter(ModuleInterface m)
|
---|
51 | {
|
---|
52 | this.mr = m;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public boolean configure()
|
---|
56 | {
|
---|
57 | // does nothing yet
|
---|
58 | return true;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * process takes an xml representation of cgi args and returns the page of
|
---|
63 | * results - may be in html/xml/other depending on the output att of the
|
---|
64 | * request
|
---|
65 | */
|
---|
66 | public String process(String xml_in)
|
---|
67 | {
|
---|
68 |
|
---|
69 | Document message_doc = this.converter.getDOM(xml_in);
|
---|
70 | if (message_doc == null)
|
---|
71 | {
|
---|
72 | logger.error("Couldn't parse request");
|
---|
73 | logger.error(xml_in);
|
---|
74 | return null;
|
---|
75 | }
|
---|
76 | Node result = process(message_doc);
|
---|
77 | return this.converter.getString(result);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** the main process method - must be implemented in subclass */
|
---|
81 | abstract public Node process(Node xml_in);
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * tell the param class what its arguments are if an action has its own
|
---|
85 | * arguments, this should add them to the params object - particularly
|
---|
86 | * important for args that should not be saved
|
---|
87 | */
|
---|
88 | public boolean addActionParameters(GSParams params)
|
---|
89 | {
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void getRequiredMetadataNames(Element format, HashSet<String> meta_names)
|
---|
94 | {
|
---|
95 | extractMetadataNames(format, meta_names);
|
---|
96 | addLinkMetadataNames(format, meta_names);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // should change to metadataList?? and use attributes for select rather than
|
---|
100 | // prepending parent_ etc
|
---|
101 | protected void extractMetadataNames(Element format, HashSet<String> meta_names)
|
---|
102 | {
|
---|
103 |
|
---|
104 | NodeList metadata_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "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 | name = select+GSConstants.META_RELATION_SEP+name;
|
---|
113 | }
|
---|
114 | meta_names.add(name);
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected void addLinkMetadataNames(Element format, HashSet<String> meta_names)
|
---|
120 | {
|
---|
121 | // The XSL tranform for
|
---|
122 | // gsf:link type="source"
|
---|
123 | // makes use of 'assocfilepath' so need to make sure it's asked for
|
---|
124 |
|
---|
125 | boolean getEquivLinkMeta = false;
|
---|
126 |
|
---|
127 | NodeList link_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "link");
|
---|
128 | for (int i = 0; i < link_nodes.getLength(); i++)
|
---|
129 | {
|
---|
130 | Element elem = (Element) link_nodes.item(i);
|
---|
131 | String type = elem.getAttribute("type");
|
---|
132 | if (type.equals("source"))
|
---|
133 | {
|
---|
134 | meta_names.add("assocfilepath");
|
---|
135 | meta_names.add("srclinkFile");
|
---|
136 | }
|
---|
137 | else if (type.equals("web"))
|
---|
138 | {
|
---|
139 | meta_names.add("weblink");
|
---|
140 | meta_names.add("webicon");
|
---|
141 | meta_names.add("/weblink");
|
---|
142 | }
|
---|
143 | else if (type.equals("equivdoc"))
|
---|
144 | {
|
---|
145 | getEquivLinkMeta = true;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | // get all the metadata necessary for when the user has used "gsf:equivlink"
|
---|
150 | // so that we can build up the equivlink from the metadata components it needs
|
---|
151 | link_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "equivlinkgs3");
|
---|
152 | if (getEquivLinkMeta || link_nodes.getLength() > 0)
|
---|
153 | {
|
---|
154 | String[] equivlink_metanames = { "equivDocIcon", "equivDocLink", "/equivDocLink" };
|
---|
155 |
|
---|
156 | for (int i = 0; i < equivlink_metanames.length; i++)
|
---|
157 | {
|
---|
158 | StringBuffer metadata = new StringBuffer();
|
---|
159 | metadata.append("all"); // this means the attr multiple = true;
|
---|
160 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
161 |
|
---|
162 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
163 | metadata.append(','); // attr separator = ","
|
---|
164 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
165 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
166 |
|
---|
167 | // the name of the metadata we're retrieving
|
---|
168 | metadata.append(equivlink_metanames[i]);
|
---|
169 | meta_names.add(metadata.toString());
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "image").getLength() > 0)
|
---|
174 | {
|
---|
175 | meta_names.add("Thumb");
|
---|
176 | meta_names.add("Screen");
|
---|
177 | meta_names.add("SourceFile");
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | protected Element createMetadataParamList(HashSet<String> metadata_names)
|
---|
182 | {
|
---|
183 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
184 |
|
---|
185 | Element param = null;
|
---|
186 | Iterator<String> i = metadata_names.iterator();
|
---|
187 | while (i.hasNext())
|
---|
188 | {
|
---|
189 | String name = i.next();
|
---|
190 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
191 | param_list.appendChild(param);
|
---|
192 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
193 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
194 |
|
---|
195 | }
|
---|
196 | return param_list;
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected boolean processErrorElements(Element message, Element page)
|
---|
200 | {
|
---|
201 | NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
|
---|
202 | if (error_nodes.getLength() == 0)
|
---|
203 | {
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 | Document owner = page.getOwnerDocument();
|
---|
207 | for (int i = 0; i < error_nodes.getLength(); i++)
|
---|
208 | {
|
---|
209 | page.appendChild(owner.importNode(error_nodes.item(i), true));
|
---|
210 | }
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Takes an XML element and adds the metadata of the current site to it.
|
---|
216 | * Useful for adding the current site's metadata to a response before
|
---|
217 | * sending it
|
---|
218 | *
|
---|
219 | * @param element
|
---|
220 | * the element to add site metadata to
|
---|
221 | * @param lang
|
---|
222 | * the current language
|
---|
223 | * @param uid
|
---|
224 | * the current user id
|
---|
225 | */
|
---|
226 | protected void addSiteMetadata(Element element, UserContext userContext)
|
---|
227 | {
|
---|
228 | //ADD SITE METADATA
|
---|
229 | Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
|
---|
230 | //create a hashmap of params
|
---|
231 | HashMap subset_params = new HashMap(1);
|
---|
232 | subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
233 | //create the element to put the params in
|
---|
234 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
235 | //put them in
|
---|
236 | GSXML.addParametersToList(this.doc, param_list, subset_params);
|
---|
237 | metadata_request.appendChild(param_list);
|
---|
238 | //create the message
|
---|
239 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
240 | metadata_message.appendChild(metadata_request);
|
---|
241 | //get response
|
---|
242 | Element metadata_response_message = (Element) this.mr.process(metadata_message);
|
---|
243 | //drill down to response
|
---|
244 | Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
|
---|
245 | //merge in metadata
|
---|
246 | GSXML.mergeMetadataLists(element, metadata_response);
|
---|
247 | }
|
---|
248 |
|
---|
249 | protected void addInterfaceOptions(Element elem)
|
---|
250 | {
|
---|
251 | Element documentOptionList = this.doc.createElement("interfaceOptions");
|
---|
252 | for (Object key : this.config_params.keySet())
|
---|
253 | {
|
---|
254 | Element option = this.doc.createElement("option");
|
---|
255 | option.setAttribute(GSXML.NAME_ATT, (String) key);
|
---|
256 | option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
|
---|
257 | documentOptionList.appendChild(option);
|
---|
258 | }
|
---|
259 | elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
|
---|
260 | }
|
---|
261 | }
|
---|