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