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

Last change on this file was 37514, checked in by kjdon, 14 months ago

usign the new GetRequiredMEtadataNames - has an extra arg, and we no longer need to do teh extraMEtadataList bit ourselves, as its now in getRequiredMetadataNames

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