source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/BrowseAction.java@ 3913

Last change on this file since 3913 was 3913, checked in by kjdon, 21 years ago

changed the way it gets format info

  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import org.greenstone.gsdl3.core.ModuleInterface;
4import org.greenstone.gsdl3.util.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11import java.util.HashMap;
12import java.io.File;
13
14/** action for classifier browsing */
15public 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 cgi_request = (Element)doc_.importNode(request, true);
56 page.appendChild(cgi_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 String lang = request.getAttribute(GSXML.LANG_ATT);
71 String to = GSPath.appendLink(collection, service_name);
72
73 // the second part of the page is the service description
74 // for now get this again from the service.
75 // this will probably need to be cached somehow later on.
76
77 Element info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
78 Element info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
79 info_message.appendChild(info_request);
80
81 // also get the format stuff now if there is some
82 Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang);
83 info_message.appendChild(format_request);
84 // process the requests
85 Element info_response = (Element) mr_.process(info_message);
86
87 System.out.println("getting format and description, response=\n"+converter_.getString(info_response));
88 // get out the description
89 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
90 Element description = (Element)doc_.importNode(GSXML.getNodeByPath(info_response, path), true);
91 page.appendChild(description);
92
93 Element format_response = (Element)info_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1); // the second response
94
95 // the main response for the page is in a response element
96 Element response = doc_.createElement(GSXML.RESPONSE_ELEM);
97 page.appendChild(response);
98
99 // get the node that the user has clicked on
100 String classifier_node = (String)params.get(CLASSIFIER_ARG);
101
102 // if the node is not defined, return the page that we have so far
103 if (classifier_node ==null || classifier_node.equals("")) {
104 GSXSLT.absoluteIncludePaths(style_doc, config_);
105 return (Element)transformer_.transform(style_doc, page);
106 }
107
108
109 // create a classifier elem to return in the page
110 Element page_classifier = doc_.createElement(GSXML.CLASSIFIER_ELEM);
111 response.appendChild(page_classifier);
112
113 // the id of the classifier is the top id of the selected node
114 String top_id = OID.getTop(classifier_node);
115 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
116
117 // get the browse structure for the selected node
118 Element classify_message = doc_.createElement(GSXML.MESSAGE_ELEM);
119 Element classify_request = doc_.createElement(GSXML.REQUEST_ELEM);
120 classify_message.appendChild(classify_request);
121
122 classify_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
123 classify_request.setAttribute(GSXML.TO_ATT, to);
124 classify_request.setAttribute(GSXML.LANG_ATT, lang);
125
126
127 //Create a parameter list to specify the required structure information
128 // for now, always get ancestors and children
129 Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
130 classify_request.appendChild(param_list);
131 Element param = doc_.createElement(GSXML.PARAM_ELEM);
132 param_list.appendChild(param);
133 param.setAttribute(GSXML.NAME_ATT, "structure");
134 param.setAttribute(GSXML.VALUE_ATT, "ancestors");
135 param = doc_.createElement(GSXML.PARAM_ELEM);
136 param_list.appendChild(param);
137 param.setAttribute(GSXML.NAME_ATT, "structure");
138 param.setAttribute(GSXML.VALUE_ATT, "children");
139
140 // put the classifier node into a classifier node list
141 Element classifier_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
142 Element classifier = doc_.createElement(GSXML.CLASS_NODE_ELEM);
143 classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
144 classifier_list.appendChild(classifier);
145 classify_request.appendChild(classifier_list);
146
147 // process the request
148 Element classify_response = (Element)mr_.process(classify_message);
149 // get the structure element
150 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
151 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
152 path = GSPath.appendLink(path, GSXML.DOC_NODE_STRUCTURE_ELEM);
153 // assume that we always get back the top level CL1 node - want to throw this away
154 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
155 Element cl_structure = (Element)GSXML.getNodeByPath(classify_response,
156 path);
157 if (cl_structure ==null) {
158 System.out.println("BrowseAction: classifier structure request returned no structure");
159
160 // return the page so far
161 GSXSLT.absoluteIncludePaths(style_doc, config_);
162 return (Element)transformer_.transform(style_doc, page);
163 }
164
165 // add the classifier nodes from the structure request into teh page_classifier
166 NodeList cl_nodes = cl_structure.getChildNodes();
167 for (int i=0; i<cl_nodes.getLength(); i++) {
168 Node n = cl_nodes.item(i);
169 if (n.getNodeType() == Node.TEXT_NODE) {
170 // ignore
171 continue;
172 }
173 page_classifier.appendChild(doc_.importNode(n, true));
174 }
175
176 // get the metadata for each classifier node,
177 // then for each document node
178 Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
179
180 boolean did_classifier = false;
181 boolean did_documents = false;
182
183 // Create a parameter list to specify the required metadata information
184 // for now get Title
185 Element meta_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
186 Element meta_param = doc_.createElement(GSXML.PARAM_ELEM);
187 meta_param_list.appendChild(meta_param);
188 meta_param.setAttribute(GSXML.NAME_ATT, "metadata");
189 meta_param.setAttribute(GSXML.VALUE_ATT, "Source");
190 meta_param = doc_.createElement(GSXML.PARAM_ELEM);
191 meta_param_list.appendChild(meta_param);
192 meta_param.setAttribute(GSXML.NAME_ATT, "metadata");
193 meta_param.setAttribute(GSXML.VALUE_ATT, "Keyword");
194 meta_param = doc_.createElement(GSXML.PARAM_ELEM);
195 meta_param_list.appendChild(meta_param);
196 meta_param.setAttribute(GSXML.NAME_ATT, "metadata");
197 meta_param.setAttribute(GSXML.VALUE_ATT, "Title");
198
199 // if there are classifier nodes (I think there always will be)
200 // create a metadata request for the classifier, and add it to
201 // the the message
202 cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
203
204 if (cl_nodes.getLength() > 0) {
205 did_classifier = true;
206 Element cl_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
207 metadata_message.appendChild(cl_meta_request);
208 cl_meta_request.setAttribute(GSXML.TO_ATT, to+"MetadataRetrieve");
209 cl_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
210 cl_meta_request.setAttribute(GSXML.LANG_ATT, lang);
211
212 cl_meta_request.appendChild(meta_param_list);
213
214 Element new_cl_nodes_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
215 cl_meta_request.appendChild(new_cl_nodes_list);
216
217 for (int c=0; c<cl_nodes.getLength(); c++) {
218
219 Element cl = doc_.createElement(GSXML.CLASS_NODE_ELEM);
220 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element)cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
221 new_cl_nodes_list.appendChild(cl);
222 }
223 }
224
225 // if there are document nodes in the classification (happens
226 // sometimes), create a second request for document metadata and
227 // append to the message
228 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
229 if (doc_nodes.getLength() > 0) {
230 did_documents = true;
231 Element doc_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
232 metadata_message.appendChild(doc_meta_request);
233 doc_meta_request.setAttribute(GSXML.TO_ATT, GSPath.appendLink(collection, "DocumentMetadataRetrieve"));
234 doc_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
235 doc_meta_request.setAttribute(GSXML.LANG_ATT, lang);
236
237 // for now, use the same metadata list - this will probably change once we look at format statements
238 doc_meta_request.appendChild(meta_param_list.cloneNode(true));
239
240 Element doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
241 doc_meta_request.appendChild(doc_list);
242
243 for (int c=0; c<doc_nodes.getLength(); c++) {
244
245 Element d = doc_.createElement(GSXML.DOC_NODE_ELEM);
246 d.setAttribute(GSXML.NODE_ID_ATT, ((Element)doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
247 doc_list.appendChild(d);
248 }
249
250 }
251
252 // process the metadata requests
253 Element metadata_response = (Element)mr_.process(metadata_message);
254 if (did_classifier) {
255 // the classifier one will be the first response
256 // add the metadata lists for each node back into the
257 // page_classifier nodes
258 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
259 GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
260 NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
261 for (int i = 0; i < cl_nodes.getLength(); i++) {
262 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
263 }
264 }
265 if (did_documents) {
266 NodeList meta_response_docs = null;
267 if (!did_classifier) {
268 // its the first response
269 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
270 GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
271 meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
272 } else { // its the second response
273 meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER).getChildNodes();
274 }
275
276 for (int i = 0; i < doc_nodes.getLength(); i++) {
277 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
278 }
279 }
280
281 // add in the format info
282 Element format_elem = (Element)GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
283 if (format_elem != null) {
284 Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
285 if (this_format != null) {
286 GSXSLT.mergeStylesheets(style_doc, this_format);
287 }
288 }
289
290 // transform the page
291 GSXSLT.absoluteIncludePaths(style_doc, config_);
292 return (Element)transformer_.transform(style_doc, page);
293 }
294
295 protected Element unknownBrowse(Element page, Element request, String browse_type) {
296 System.err.println("BrowseAction Error: unknown browse subtype: "+browse_type);
297 return null;
298 }
299}
300
301
Note: See TracBrowser for help on using the repository browser.