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