source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/BrowseAction.java@ 24993

Last change on this file since 24993 was 24993, checked in by sjm84, 12 years ago

Adding UserContext to replace the use of lang and uid

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