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

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

browse services now handled differently - metadata and format statements are retrieved in separate requests

  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 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 = createDescribeRequest(to, lang, null);
78 Element info_response = (Element) mr_.process(info_message);
79 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
80 Element description = (Element)doc_.importNode(GSXML.getNodeByPath(info_response, path), true);
81 page.appendChild(description);
82
83
84// Element info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
85// Element info_request = doc_.createElement(GSXML.REQUEST_ELEM);
86// info_message.appendChild(info_request);
87// info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
88// info_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
89
90// String to = collection;
91// to = GSPath.appendLink(to, service_name);
92// info_request.setAttribute(GSXML.TO_ATT, to);
93
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, "Title");
190
191 // if there are classifier nodes (I think there always will be)
192 // create a metadata request for the classifier, and add it to
193 // the the message
194 cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
195
196 if (cl_nodes.getLength() > 0) {
197 did_classifier = true;
198 Element cl_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
199 metadata_message.appendChild(cl_meta_request);
200 cl_meta_request.setAttribute(GSXML.TO_ATT, to+"MetadataRetrieve");
201 cl_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
202 cl_meta_request.setAttribute(GSXML.LANG_ATT, lang);
203
204 cl_meta_request.appendChild(meta_param_list);
205
206 Element new_cl_nodes_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
207 cl_meta_request.appendChild(new_cl_nodes_list);
208
209 for (int c=0; c<cl_nodes.getLength(); c++) {
210
211 Element cl = doc_.createElement(GSXML.CLASS_NODE_ELEM);
212 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element)cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
213 new_cl_nodes_list.appendChild(cl);
214 }
215 }
216
217 // if there are document nodes in the classification (happens
218 // sometimes), create a second request for document metadata and
219 // append to the message
220 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
221 if (doc_nodes.getLength() > 0) {
222 did_documents = true;
223 Element doc_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
224 metadata_message.appendChild(doc_meta_request);
225 doc_meta_request.setAttribute(GSXML.TO_ATT, GSPath.appendLink(collection, "DocumentMetadataRetrieve"));
226 doc_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
227 doc_meta_request.setAttribute(GSXML.LANG_ATT, lang);
228
229 // for now, use the same metadata list - this will probably change once we look at format statements
230 doc_meta_request.appendChild(meta_param_list.cloneNode(true));
231
232 Element doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
233 doc_meta_request.appendChild(doc_list);
234
235 for (int c=0; c<doc_nodes.getLength(); c++) {
236
237 Element d = doc_.createElement(GSXML.DOC_NODE_ELEM);
238 d.setAttribute(GSXML.NODE_ID_ATT, ((Element)doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
239 doc_list.appendChild(d);
240 }
241
242 }
243
244 // process the metadata requests
245 Element metadata_response = (Element)mr_.process(metadata_message);
246 if (did_classifier) {
247 // the classifier one will be the first response
248 // add the metadata lists for each node back into the
249 // page_classifier nodes
250 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
251 GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
252 NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
253 for (int i = 0; i < cl_nodes.getLength(); i++) {
254 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
255 }
256 }
257 if (did_documents) {
258 NodeList meta_response_docs = null;
259 if (!did_classifier) {
260 // its the first response
261 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
262 GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
263 meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
264 } else { // its the second response
265 meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER).getChildNodes();
266 }
267
268 for (int i = 0; i < doc_nodes.getLength(); i++) {
269 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
270 }
271 }
272
273
274 //Node new_style = GSXML.getChildByTagName(response, GSXML.STYLESHEET_ELEM);
275 //if (new_style !=null) {
276 // GSXSLT.mergeStylesheets(style_doc, (Element)new_style);
277 // response.removeChild(new_style);
278 //}
279 // add the response to the page data
280 //page.appendChild(doc_.importNode(response, true));
281
282
283 // transform the page
284 GSXSLT.absoluteIncludePaths(style_doc, config_);
285 //System.out.println("browse page=");
286 //System.out.println(converter_.getString(page));
287 return (Element)transformer_.transform(style_doc, page);
288 }
289
290 protected Element unknownBrowse(Element page, Element request, String browse_type) {
291 System.err.println("BrowseAction Error: unknown browse subtype: "+browse_type);
292 return null;
293 }
294}
295
296
Note: See TracBrowser for help on using the repository browser.