source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/GS2BrowseAction.java@ 6300

Last change on this file since 6300 was 6300, checked in by kjdon, 20 years ago

GSXML.createBasicRequest now expects a user id

  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 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.util.HashSet;
13import java.util.Vector;
14import java.io.File;
15
16/** action for GS2 style classifier browsing */
17public class GS2BrowseAction extends Action {
18
19 public static final String CLASSIFIER_ARG = "cl";
20
21 /** process the request */
22 public Element process (Element message) {
23
24 // get the request - assume only one
25 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
26
27 // the result
28 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
29 Element response = classifierBrowse(request);
30 result.appendChild(response);
31 return result;
32 }
33
34
35 protected Element classifierBrowse(Element request) {
36
37 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
38
39 // extract the params from the cgi-request, and check that we have a coll specified
40 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
41 HashMap params = GSXML.extractParams(cgi_paramList, false);
42
43 String service_name = (String)params.get(GSParams.SERVICE);
44 String collection = (String)params.get(GSParams.COLLECTION);
45 if (collection == null || collection.equals("")) {
46 System.err.println("GS2BrowseAction Error:classifierBrowse, need to specify a collection!");
47 return page_response;
48
49 }
50
51 String lang = request.getAttribute(GSXML.LANG_ATT);
52 String uid = request.getAttribute(GSXML.USER_ID_ATT);
53 String to = GSPath.appendLink(collection, service_name);
54
55 // the first part of the response is the service description
56 // for now get this again from the service.
57 // this should be cached somehow later on.
58
59 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
60 Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, uid);
61 info_message.appendChild(info_request);
62
63 // also get the format stuff now if there is some
64 Element format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, lang, uid);
65 info_message.appendChild(format_request);
66 // process the requests
67
68 Element info_response = (Element) this.mr.process(info_message);
69
70 // the two responses
71 NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
72 Element service_response = (Element)responses.item(0);
73 Element format_response = (Element)responses.item(1);
74
75 Element service_description = (Element)GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
76 page_response.appendChild(this.doc.importNode(service_description, true));
77
78 // if rt=d, then we are just displaying the service
79 String request_type = (String)params.get(GSParams.REQUEST_TYPE);
80 if (request_type.equals("d")) {
81 //return the page that we have so far
82 return page_response;
83 }
84
85 // get the node that the user has clicked on
86 String classifier_node = (String)params.get(CLASSIFIER_ARG);
87
88 // if the node is not defined, return the page that we have so far
89 if (classifier_node ==null || classifier_node.equals("")) {
90 return page_response;
91 }
92
93 // the id of the classifier is the top id of the selected node
94 String top_id = OID.getTop(classifier_node);
95
96 HashSet doc_meta_names = new HashSet();
97 HashSet class_meta_names = new HashSet();
98 // add in the defaults
99 doc_meta_names.add("Title");
100 class_meta_names.add("Title");
101
102 // add the format info into the response
103 Element format_elem = (Element)GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
104 if (format_elem != null) {
105
106 // find the one for the classifier we are in
107 Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
108 if (this_format == null) {
109 this_format = (Element)GSXML.getChildByTagName(format_elem, GSXML.DEFAULT_ELEM);
110 }
111
112 if (this_format != null) {
113 Element new_format = GSXML.duplicateWithNewName(this.doc, this_format, GSXML.FORMAT_ELEM, false);
114 extractMetadataNames(new_format, doc_meta_names, class_meta_names);
115 // set the format type
116 new_format.setAttribute(GSXML.TYPE_ATT, "browse");
117
118 page_response.appendChild(new_format);
119 }
120 }
121
122 // find out if this classifier is horizontal at top
123 Element class_list = (Element)GSXML.getChildByTagName(service_description, GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
124 Element this_classifier = GSXML.getNamedElement(class_list, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
125 boolean horizontal_at_top = false;
126 if (!this_classifier.getAttribute("horizontalAtTop").equals("")) {
127 horizontal_at_top = true;
128 }
129
130 // get the browse structure for the selected node
131 Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
132 Element classify_request = this.doc.createElement(GSXML.REQUEST_ELEM);
133 classify_message.appendChild(classify_request);
134
135 classify_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
136 classify_request.setAttribute(GSXML.TO_ATT, to);
137 classify_request.setAttribute(GSXML.LANG_ATT, lang);
138
139
140 //Create a parameter list to specify the required structure information
141 // for now, always get ancestors and children
142 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
143 classify_request.appendChild(param_list);
144 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
145 param_list.appendChild(param);
146 param.setAttribute(GSXML.NAME_ATT, "structure");
147 param.setAttribute(GSXML.VALUE_ATT, "ancestors");
148 param = this.doc.createElement(GSXML.PARAM_ELEM);
149 param_list.appendChild(param);
150 param.setAttribute(GSXML.NAME_ATT, "structure");
151 param.setAttribute(GSXML.VALUE_ATT, "children");
152
153 // put the classifier node into a classifier node list
154 Element classifier_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
155 Element classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
156 classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
157 classifier_list.appendChild(classifier);
158 classify_request.appendChild(classifier_list);
159
160 if (horizontal_at_top && !classifier_node.equals(top_id)) {
161 // also put the top id in, to get the persistant horizontal info
162 classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
163 classifier.setAttribute(GSXML.NODE_ID_ATT, top_id);
164 classifier_list.appendChild(classifier);
165 }
166 // process the request
167 Element classify_response = (Element)this.mr.process(classify_message);
168 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
169 Element class_node_list = (Element)GSXML.getNodeByPath(classify_response, path);
170
171 path = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
172 // assume that we always get back the top level CL1 node - this becomes the page_classifier node
173 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
174 Element cl_structure = (Element)GSXML.getNodeByPath(class_node_list,
175 path);
176 if (cl_structure ==null) {
177 System.err.println("GS2BrowseAction: classifier structure request returned no structure");
178 return page_response;
179 }
180
181 Element page_classifier = null;
182 if (horizontal_at_top && !classifier_node.equals(top_id)) {
183 // get the info for the top node
184 Element top_node = GSXML.getNamedElement(class_node_list, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, top_id);
185 if (top_node !=null) {
186 path = GSPath.appendLink(GSXML.NODE_STRUCTURE_ELEM, GSXML.CLASS_NODE_ELEM);
187 Element top_structure = (Element)GSXML.getNodeByPath(top_node, path);
188 // add this as the classifier elem
189 page_classifier = GSXML.duplicateWithNewName(this.doc, top_structure, GSXML.CLASSIFIER_ELEM, true);
190 page_response.appendChild(page_classifier);
191 // now replace the child with the structure from the other request
192 Element new_classifier = (Element)GSXML.getChildByTagName(cl_structure, GSXML.CLASS_NODE_ELEM);
193 String replace_name = new_classifier.getAttribute(GSXML.NODE_ID_ATT);
194
195 // find the appropriate child
196 Element old_classifier = GSXML.getNamedElement(page_classifier, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, replace_name);
197 page_classifier.replaceChild(this.doc.importNode(new_classifier, true), old_classifier);
198 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
199 } else {
200 // add the single classifier node as the page classifier
201 page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
202 page_response.appendChild(page_classifier);
203 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
204 }
205
206 } else {
207 // add the single classifier node as the page classifier
208 page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
209 page_response.appendChild(page_classifier);
210 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
211 }
212 // get the metadata for each classifier node,
213 // then for each document node
214
215 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
216
217 boolean did_classifier = false;
218 boolean did_documents = false;
219
220
221 // if there are classifier nodes
222 // create a metadata request for the classifier, and add it to
223 // the the message
224 NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
225
226 if (cl_nodes.getLength() > 0) {
227 did_classifier = true;
228 Element cl_meta_request = this.doc.createElement(GSXML.REQUEST_ELEM);
229 metadata_message.appendChild(cl_meta_request);
230 cl_meta_request.setAttribute(GSXML.TO_ATT, to+"MetadataRetrieve");
231 cl_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
232 cl_meta_request.setAttribute(GSXML.LANG_ATT, lang);
233
234 Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
235 cl_meta_request.appendChild(new_cl_nodes_list);
236
237 for (int c=0; c<cl_nodes.getLength(); c++) {
238
239 Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
240 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element)cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
241 new_cl_nodes_list.appendChild(cl);
242 }
243
244 // create and add in the param list - for now get all the metadata
245 // should be based on info sent in from the recept, and the
246 // format stuff
247 Element cl_param_list = createMetadataParamList(class_meta_names);
248 cl_meta_request.appendChild(cl_param_list);
249
250 }
251
252 // if there are document nodes in the classification (happens
253 // sometimes), create a second request for document metadata and
254 // append to the message
255 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
256 if (doc_nodes.getLength() > 0) {
257 did_documents = true;
258 Element doc_meta_request = this.doc.createElement(GSXML.REQUEST_ELEM);
259 metadata_message.appendChild(doc_meta_request);
260 doc_meta_request.setAttribute(GSXML.TO_ATT, GSPath.appendLink(collection, "DocumentMetadataRetrieve"));
261 doc_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
262 doc_meta_request.setAttribute(GSXML.LANG_ATT, lang);
263
264 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
265 doc_meta_request.appendChild(doc_list);
266
267 for (int c=0; c<doc_nodes.getLength(); c++) {
268
269 Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
270 d.setAttribute(GSXML.NODE_ID_ATT, ((Element)doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
271 doc_list.appendChild(d);
272 }
273
274 // create and add in the param list - add all for now
275 Element doc_param_list = createMetadataParamList(doc_meta_names);
276 doc_meta_request.appendChild(doc_param_list);
277
278 }
279
280 // process the metadata requests
281 Element metadata_response = (Element)this.mr.process(metadata_message);
282 if (did_classifier) {
283 // the classifier one will be the first response
284 // add the metadata lists for each node back into the
285 // page_classifier nodes
286 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
287 GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
288 NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
289 for (int i = 0; i < cl_nodes.getLength(); i++) {
290 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
291 }
292 }
293 if (did_documents) {
294 NodeList meta_response_docs = null;
295 if (!did_classifier) {
296 // its the first response
297 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
298 GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
299 meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
300 } else { // its the second response
301 meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER).getChildNodes();
302 }
303
304 for (int i = 0; i < doc_nodes.getLength(); i++) {
305 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
306 }
307 }
308
309
310 ///ystem.out.println("(GS2BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
311 return page_response;
312 }
313
314
315 protected void extractMetadataNames(Element new_format, HashSet doc_meta_names, HashSet class_meta_names) {
316
317 NodeList templates = new_format.getElementsByTagName("gsf:template");
318 for (int i=0; i<templates.getLength(); i++) {
319 Element template = (Element)templates.item(i);
320 String match = template.getAttribute("match");
321 if (match.equals("documentNode")) {
322 extractMetadataNames(template, doc_meta_names);
323 } else if (match.equals("classifierNode")) {
324 extractMetadataNames(template, class_meta_names);
325 }
326 }
327 }
328
329}
330
331
Note: See TracBrowser for help on using the repository browser.