1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.io.File;
|
---|
13 |
|
---|
14 | /** action for classifier browsing */
|
---|
15 | public class BrowseAction extends Action {
|
---|
16 |
|
---|
17 | public static final String CLASSIFIER_ARG = "cl";
|
---|
18 |
|
---|
19 | /* add the action specific args to the cgi param list
|
---|
20 | */
|
---|
21 | public void addCGIParams() {
|
---|
22 | cgi_.addStaticParam(CLASSIFIER_ARG);
|
---|
23 | }
|
---|
24 |
|
---|
25 | /** process the request */
|
---|
26 | public Element process (Element message) {
|
---|
27 |
|
---|
28 | // get the request - assume only one
|
---|
29 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
30 |
|
---|
31 | // create the return page tree
|
---|
32 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
33 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
34 | // add the lang stuff from message
|
---|
35 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
36 | // add the system stuff from message
|
---|
37 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
38 |
|
---|
39 | return classifierBrowse(page, request);
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | protected Element classifierBrowse(Element page, Element request) {
|
---|
45 |
|
---|
46 | // check that the stylesheet is present - cant output a page without one. we may adapt this to use unknownquery stylesheet? - or ask for one from the MR
|
---|
47 | String stylesheet = GSFile.stylesheetFile(config_, "classifier.xsl");
|
---|
48 | if (stylesheet==null) {
|
---|
49 | System.err.println("BrowseAction Error: classifier stylesheet not found!");
|
---|
50 | return null;
|
---|
51 | }
|
---|
52 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
53 |
|
---|
54 | // the first part of the data for the page is the cgi-params
|
---|
55 | Element page_request = GSXML.duplicateWithNewName(doc_, request, GSXML.PAGE_REQUEST_ELEM, true);
|
---|
56 | page.appendChild(page_request);
|
---|
57 |
|
---|
58 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
59 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
60 | HashMap params = GSXML.extractParams(cgi_paramList, false);
|
---|
61 |
|
---|
62 | String service_name = (String)params.get(GSCGI.SERVICE_ARG);
|
---|
63 | String collection = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
64 | if (collection == null || collection.equals("")) {
|
---|
65 | System.err.println("BrowseAction Error:classifierbrowse, need to specify a collection!");
|
---|
66 | return null;
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | // the main response for the page is in a pageResponse element
|
---|
71 | Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM);
|
---|
72 | page.appendChild(page_response);
|
---|
73 |
|
---|
74 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
75 | String to = GSPath.appendLink(collection, service_name);
|
---|
76 |
|
---|
77 | // the second part of the page is the service description
|
---|
78 | // for now get this again from the service.
|
---|
79 | // this will probably need to be cached somehow later on.
|
---|
80 |
|
---|
81 | Element info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
82 | Element info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
|
---|
83 | info_message.appendChild(info_request);
|
---|
84 |
|
---|
85 | // also get the collection description (should be just metadata?) for the http address stuff
|
---|
86 | Element coll_info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang);
|
---|
87 | info_message.appendChild(coll_info_request);
|
---|
88 | // also get the format stuff now if there is some
|
---|
89 | Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang);
|
---|
90 | info_message.appendChild(format_request);
|
---|
91 | // process the requests
|
---|
92 |
|
---|
93 | Element info_response = (Element) mr_.process(info_message);
|
---|
94 |
|
---|
95 | // the three responses
|
---|
96 | NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
97 | Element service_response = (Element)responses.item(0);
|
---|
98 | Element coll_response = (Element)responses.item(1);
|
---|
99 | Element format_response = (Element)responses.item(2);
|
---|
100 |
|
---|
101 | Element service_description = (Element)GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
|
---|
102 | page_response.appendChild(doc_.importNode(service_description, true));
|
---|
103 |
|
---|
104 | Element coll_description = (Element)GSXML.getChildByTagName(coll_response, GSXML.COLLECTION_ELEM);
|
---|
105 | page_response.appendChild(doc_.importNode(coll_description, true));
|
---|
106 |
|
---|
107 |
|
---|
108 | // get the node that the user has clicked on
|
---|
109 | String classifier_node = (String)params.get(CLASSIFIER_ARG);
|
---|
110 |
|
---|
111 | // if the node is not defined, return the page that we have so far
|
---|
112 | if (classifier_node ==null || classifier_node.equals("")) {
|
---|
113 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
114 | return (Element)transformer_.transform(style_doc, page);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | // create a classifier elem to return in the page
|
---|
119 | Element page_classifier = doc_.createElement(GSXML.CLASSIFIER_ELEM);
|
---|
120 | page_response.appendChild(page_classifier);
|
---|
121 |
|
---|
122 | // the id of the classifier is the top id of the selected node
|
---|
123 | String top_id = OID.getTop(classifier_node);
|
---|
124 | page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
|
---|
125 |
|
---|
126 | // get the browse structure for the selected node
|
---|
127 | Element classify_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
128 | Element classify_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
129 | classify_message.appendChild(classify_request);
|
---|
130 |
|
---|
131 | classify_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
132 | classify_request.setAttribute(GSXML.TO_ATT, to);
|
---|
133 | classify_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
134 |
|
---|
135 |
|
---|
136 | //Create a parameter list to specify the required structure information
|
---|
137 | // for now, always get ancestors and children
|
---|
138 | Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
139 | classify_request.appendChild(param_list);
|
---|
140 | Element param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
141 | param_list.appendChild(param);
|
---|
142 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
143 | param.setAttribute(GSXML.VALUE_ATT, "ancestors");
|
---|
144 | param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
145 | param_list.appendChild(param);
|
---|
146 | param.setAttribute(GSXML.NAME_ATT, "structure");
|
---|
147 | param.setAttribute(GSXML.VALUE_ATT, "children");
|
---|
148 |
|
---|
149 | // put the classifier node into a classifier node list
|
---|
150 | Element classifier_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
151 | Element classifier = doc_.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
152 | classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
|
---|
153 | classifier_list.appendChild(classifier);
|
---|
154 | classify_request.appendChild(classifier_list);
|
---|
155 |
|
---|
156 | // process the request
|
---|
157 | Element classify_response = (Element)mr_.process(classify_message);
|
---|
158 | // get the structure element
|
---|
159 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
160 | path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
|
---|
161 | path = GSPath.appendLink(path, GSXML.NODE_STRUCTURE_ELEM);
|
---|
162 | // assume that we always get back the top level CL1 node - want to throw this away
|
---|
163 | path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
|
---|
164 | Element cl_structure = (Element)GSXML.getNodeByPath(classify_response,
|
---|
165 | path);
|
---|
166 | if (cl_structure ==null) {
|
---|
167 | System.err.println("BrowseAction: classifier structure request returned no structure");
|
---|
168 |
|
---|
169 | // return the page so far
|
---|
170 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
171 | return (Element)transformer_.transform(style_doc, page);
|
---|
172 | }
|
---|
173 |
|
---|
174 | // add the classifier nodes from the structure request into teh page_classifier
|
---|
175 | NodeList cl_nodes = cl_structure.getChildNodes();
|
---|
176 | for (int i=0; i<cl_nodes.getLength(); i++) {
|
---|
177 | Node n = cl_nodes.item(i);
|
---|
178 | if (n.getNodeType() == Node.TEXT_NODE) {
|
---|
179 | // ignore
|
---|
180 | continue;
|
---|
181 | }
|
---|
182 | page_classifier.appendChild(doc_.importNode(n, true));
|
---|
183 | }
|
---|
184 |
|
---|
185 | // get the metadata for each classifier node,
|
---|
186 | // then for each document node
|
---|
187 | Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
188 |
|
---|
189 | boolean did_classifier = false;
|
---|
190 | boolean did_documents = false;
|
---|
191 |
|
---|
192 | // Create a parameter list to specify the required metadata information
|
---|
193 | // for now get Title
|
---|
194 | Element meta_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
195 | Element meta_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
196 | meta_param_list.appendChild(meta_param);
|
---|
197 | meta_param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
198 | meta_param.setAttribute(GSXML.VALUE_ATT, "Source");
|
---|
199 | meta_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
200 | meta_param_list.appendChild(meta_param);
|
---|
201 | meta_param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
202 | meta_param.setAttribute(GSXML.VALUE_ATT, "Keyword");
|
---|
203 | meta_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
204 | meta_param_list.appendChild(meta_param);
|
---|
205 | meta_param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
206 | meta_param.setAttribute(GSXML.VALUE_ATT, "Title");
|
---|
207 |
|
---|
208 | // if there are classifier nodes (I think there always will be)
|
---|
209 | // create a metadata request for the classifier, and add it to
|
---|
210 | // the the message
|
---|
211 | cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
|
---|
212 |
|
---|
213 | if (cl_nodes.getLength() > 0) {
|
---|
214 | did_classifier = true;
|
---|
215 | Element cl_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
216 | metadata_message.appendChild(cl_meta_request);
|
---|
217 | cl_meta_request.setAttribute(GSXML.TO_ATT, to+"MetadataRetrieve");
|
---|
218 | cl_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
219 | cl_meta_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
220 |
|
---|
221 | cl_meta_request.appendChild(meta_param_list);
|
---|
222 |
|
---|
223 | Element new_cl_nodes_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
224 | cl_meta_request.appendChild(new_cl_nodes_list);
|
---|
225 |
|
---|
226 | for (int c=0; c<cl_nodes.getLength(); c++) {
|
---|
227 |
|
---|
228 | Element cl = doc_.createElement(GSXML.CLASS_NODE_ELEM);
|
---|
229 | cl.setAttribute(GSXML.NODE_ID_ATT, ((Element)cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
|
---|
230 | new_cl_nodes_list.appendChild(cl);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | // if there are document nodes in the classification (happens
|
---|
235 | // sometimes), create a second request for document metadata and
|
---|
236 | // append to the message
|
---|
237 | NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
238 | if (doc_nodes.getLength() > 0) {
|
---|
239 | did_documents = true;
|
---|
240 | Element doc_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
241 | metadata_message.appendChild(doc_meta_request);
|
---|
242 | doc_meta_request.setAttribute(GSXML.TO_ATT, GSPath.appendLink(collection, "DocumentMetadataRetrieve"));
|
---|
243 | doc_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
244 | doc_meta_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
245 |
|
---|
246 | // for now, use the same metadata list - this will probably change once we look at format statements
|
---|
247 | doc_meta_request.appendChild(meta_param_list.cloneNode(true));
|
---|
248 |
|
---|
249 | Element doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
250 | doc_meta_request.appendChild(doc_list);
|
---|
251 |
|
---|
252 | for (int c=0; c<doc_nodes.getLength(); c++) {
|
---|
253 |
|
---|
254 | Element d = doc_.createElement(GSXML.DOC_NODE_ELEM);
|
---|
255 | d.setAttribute(GSXML.NODE_ID_ATT, ((Element)doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
|
---|
256 | doc_list.appendChild(d);
|
---|
257 | }
|
---|
258 |
|
---|
259 | }
|
---|
260 |
|
---|
261 | // process the metadata requests
|
---|
262 | Element metadata_response = (Element)mr_.process(metadata_message);
|
---|
263 | if (did_classifier) {
|
---|
264 | // the classifier one will be the first response
|
---|
265 | // add the metadata lists for each node back into the
|
---|
266 | // page_classifier nodes
|
---|
267 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
|
---|
268 | GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
269 | NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
|
---|
270 | for (int i = 0; i < cl_nodes.getLength(); i++) {
|
---|
271 | GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
|
---|
272 | }
|
---|
273 | }
|
---|
274 | if (did_documents) {
|
---|
275 | NodeList meta_response_docs = null;
|
---|
276 | if (!did_classifier) {
|
---|
277 | // its the first response
|
---|
278 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
|
---|
279 | GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
280 | meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
|
---|
281 | } else { // its the second response
|
---|
282 | meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER).getChildNodes();
|
---|
283 | }
|
---|
284 |
|
---|
285 | for (int i = 0; i < doc_nodes.getLength(); i++) {
|
---|
286 | GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | // add in the format info
|
---|
291 | Element format_elem = getAndTransformFormat(format_response);
|
---|
292 | if (format_elem != null) {
|
---|
293 | Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
|
---|
294 | if (this_format != null) {
|
---|
295 | GSXSLT.mergeStylesheets(style_doc, this_format);
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | System.out.println("(BrowseAction) Page:\n" + converter_.getPrettyString(page));
|
---|
300 |
|
---|
301 | // transform the page
|
---|
302 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
303 | return (Element)transformer_.transform(style_doc, page);
|
---|
304 | }
|
---|
305 |
|
---|
306 | protected Element unknownBrowse(Element page, Element request, String browse_type) {
|
---|
307 | System.err.println("BrowseAction Error: unknown browse subtype: "+browse_type);
|
---|
308 | return null;
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|