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

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 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 HashSet doc_meta_names = new HashSet();
96 HashSet class_meta_names = new HashSet();
97 // add in the defaults
98 doc_meta_names.add("Title");
99 class_meta_names.add("Title");
100
101 // add the format info into the response
102 Element format_elem = (Element)GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
103 if (format_elem != null) {
104
105 // find the one for the classifier we are in
106 Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
107 if (this_format == null) {
108 this_format = (Element)GSXML.getChildByTagName(format_elem, GSXML.DEFAULT_ELEM);
109 }
110
111 if (this_format != null) {
112 Element new_format = GSXML.duplicateWithNewName(this.doc, this_format, GSXML.FORMAT_ELEM, false);
113 extractMetadataNames(new_format, doc_meta_names, class_meta_names);
114 // set the format type
115 new_format.setAttribute(GSXML.TYPE_ATT, "browse");
116
117 page_response.appendChild(new_format);
118 }
119 }
120
121 // find out if this classifier is horizontal at top
122 Element class_list = (Element)GSXML.getChildByTagName(service_description, GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
123 Element this_classifier = GSXML.getNamedElement(class_list, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
124 boolean horizontal_at_top = false;
125 if (!this_classifier.getAttribute("horizontalAtTop").equals("")) {
126 horizontal_at_top = true;
127 }
128 // *** TODO need to fix this
129 //if (top_id.equals(classifier_node) && horizontal_at_top) {
130 // we have asked for a top node - if the first list is horizontal, we will select the first element of that list
131 // this is a hack. also it craps out if the classifier really isn't horizontalAtTop. -
132 //classifier_node = classifier_node+".1";
133
134 //}
135
136 // get the browse structure for the selected node
137 Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
138 Element classify_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
139 classify_message.appendChild(classify_request);
140
141 //Create a parameter list to specify the required structure information
142 // for now, always get ancestors and children
143 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
144 classify_request.appendChild(param_list);
145 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
146 param_list.appendChild(param);
147 param.setAttribute(GSXML.NAME_ATT, "structure");
148 param.setAttribute(GSXML.VALUE_ATT, "ancestors");
149 param = this.doc.createElement(GSXML.PARAM_ELEM);
150 param_list.appendChild(param);
151 param.setAttribute(GSXML.NAME_ATT, "structure");
152 param.setAttribute(GSXML.VALUE_ATT, "children");
153
154 // put the classifier node into a classifier node list
155 Element classifier_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
156 Element classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
157 classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
158 classifier_list.appendChild(classifier);
159 classify_request.appendChild(classifier_list);
160
161 if (horizontal_at_top && !classifier_node.equals(top_id)) {
162 // also put the top id in, to get the persistant horizontal info
163 classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
164 classifier.setAttribute(GSXML.NODE_ID_ATT, top_id);
165 classifier_list.appendChild(classifier);
166 }
167 // process the request
168 Element classify_response = (Element)this.mr.process(classify_message);
169 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
170 Element class_node_list = (Element)GSXML.getNodeByPath(classify_response, path);
171
172 path = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
173 // assume that we always get back the top level CL1 node - this becomes the page_classifier node
174 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
175 Element cl_structure = (Element)GSXML.getNodeByPath(class_node_list,
176 path);
177 if (cl_structure ==null) {
178 System.err.println("GS2BrowseAction: classifier structure request returned no structure");
179 return page_response;
180 }
181
182 Element page_classifier = null;
183 if (horizontal_at_top && !classifier_node.equals(top_id)) {
184 // get the info for the top node
185 Element top_node = GSXML.getNamedElement(class_node_list, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, top_id);
186 if (top_node !=null) {
187 path = GSPath.appendLink(GSXML.NODE_STRUCTURE_ELEM, GSXML.CLASS_NODE_ELEM);
188 Element top_structure = (Element)GSXML.getNodeByPath(top_node, path);
189 // add this as the classifier elem
190 page_classifier = GSXML.duplicateWithNewName(this.doc, top_structure, GSXML.CLASSIFIER_ELEM, true);
191 page_response.appendChild(page_classifier);
192 // now replace the child with the structure from the other request
193 Element new_classifier = (Element)GSXML.getChildByTagName(cl_structure, GSXML.CLASS_NODE_ELEM);
194 String replace_name = new_classifier.getAttribute(GSXML.NODE_ID_ATT);
195
196 // find the appropriate child
197 Element old_classifier = GSXML.getNamedElement(page_classifier, GSXML.CLASS_NODE_ELEM, GSXML.NODE_ID_ATT, replace_name);
198 page_classifier.replaceChild(this.doc.importNode(new_classifier, true), old_classifier);
199 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
200 } else {
201 // add the single classifier node as the page classifier
202 page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
203 page_response.appendChild(page_classifier);
204 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
205 }
206
207 } else {
208 // add the single classifier node as the page classifier
209 page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
210 page_response.appendChild(page_classifier);
211 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
212 }
213 // get the metadata for each classifier node,
214 // then for each document node
215
216 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
217
218 boolean did_classifier = false;
219 boolean did_documents = false;
220
221
222 // if there are classifier nodes
223 // create a metadata request for the classifier, and add it to
224 // the the message
225 NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
226
227 if (cl_nodes.getLength() > 0) {
228 did_classifier = true;
229 Element cl_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to+"MetadataRetrieve", lang, uid);
230 metadata_message.appendChild(cl_meta_request);
231
232 Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
233 cl_meta_request.appendChild(new_cl_nodes_list);
234
235 for (int c=0; c<cl_nodes.getLength(); c++) {
236
237 Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
238 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element)cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
239 new_cl_nodes_list.appendChild(cl);
240 }
241
242 // create and add in the param list - for now get all the metadata
243 // should be based on info sent in from the recept, and the
244 // format stuff
245 Element cl_param_list = createMetadataParamList(class_meta_names);
246 cl_meta_request.appendChild(cl_param_list);
247
248 }
249
250 // if there are document nodes in the classification (happens
251 // sometimes), create a second request for document metadata and
252 // append to the message
253 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
254 if (doc_nodes.getLength() > 0) {
255 did_documents = true;
256 Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), lang, uid);
257 metadata_message.appendChild(doc_meta_request);
258
259 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
260 doc_meta_request.appendChild(doc_list);
261
262 for (int c=0; c<doc_nodes.getLength(); c++) {
263
264 Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
265 d.setAttribute(GSXML.NODE_ID_ATT, ((Element)doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
266 doc_list.appendChild(d);
267 }
268
269 // create and add in the param list - add all for now
270 Element doc_param_list = createMetadataParamList(doc_meta_names);
271 doc_meta_request.appendChild(doc_param_list);
272
273 }
274
275 // process the metadata requests
276 Element metadata_response = (Element)this.mr.process(metadata_message);
277 if (did_classifier) {
278 // the classifier one will be the first response
279 // add the metadata lists for each node back into the
280 // page_classifier nodes
281 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
282 GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
283 NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
284 for (int i = 0; i < cl_nodes.getLength(); i++) {
285 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
286 }
287 }
288 if (did_documents) {
289 NodeList meta_response_docs = null;
290 if (!did_classifier) {
291 // its the first response
292 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
293 GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
294 meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
295 } else { // its the second response
296 meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER).getChildNodes();
297 }
298
299 for (int i = 0; i < doc_nodes.getLength(); i++) {
300 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
301 }
302 }
303
304
305 ///ystem.out.println("(GS2BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
306 return page_response;
307 }
308
309
310 protected void extractMetadataNames(Element new_format, HashSet doc_meta_names, HashSet class_meta_names) {
311
312 NodeList templates = new_format.getElementsByTagName("gsf:template");
313 for (int i=0; i<templates.getLength(); i++) {
314 Element template = (Element)templates.item(i);
315 String match = template.getAttribute("match");
316 if (match.equals("documentNode")) {
317 extractMetadataNames(template, doc_meta_names);
318 } else if (match.equals("classifierNode")) {
319 extractMetadataNames(template, class_meta_names);
320 }
321 }
322 }
323
324}
325
326
Note: See TracBrowser for help on using the repository browser.