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

Last change on this file since 13270 was 13270, checked in by shaoqun, 17 years ago

replace Category class which is deprecated with Logger class

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