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

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

All of the actions that use format statements will now merge in the global format statement

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