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