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

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

Analyse xsl files in advance to find out what metadata we need

  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 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 Element extraMetaListElem = (Element) GSXML.getChildByTagName(request, GSXML.EXTRA_METADATA + GSXML.LIST_MODIFIER);
149 if(extraMetaListElem != null)
150 {
151 NodeList extraMetaList = extraMetaListElem.getElementsByTagName(GSXML.EXTRA_METADATA);
152 for(int i = 0; i < extraMetaList.getLength(); i++)
153 {
154 metadata_names.add(((Element)extraMetaList.item(i)).getAttribute(GSXML.NAME_ATT));
155 }
156 }
157
158 logger.info("extracted meta names, " + metadata_names.toString());
159 // get the browse structure for the selected node
160 Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
161 Element classify_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
162 classify_message.appendChild(classify_request);
163
164 //Create a parameter list to specify the required structure information
165 // for now, always get ancestors and children
166 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
167 classify_request.appendChild(param_list);
168 Element 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, "ancestors");
172 param = this.doc.createElement(GSXML.PARAM_ELEM);
173 param_list.appendChild(param);
174 param.setAttribute(GSXML.NAME_ATT, "structure");
175 param.setAttribute(GSXML.VALUE_ATT, "children");
176 if (get_siblings)
177 {
178 param = this.doc.createElement(GSXML.PARAM_ELEM);
179 param_list.appendChild(param);
180 param.setAttribute(GSXML.NAME_ATT, "structure");
181 param.setAttribute(GSXML.VALUE_ATT, "siblings");
182 }
183
184 // put the classifier node into a classifier node list
185 Element classifier_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
186 Element classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
187 classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
188 classifier_list.appendChild(classifier);
189 classify_request.appendChild(classifier_list);
190
191 // process the request
192 Element classify_response = (Element) this.mr.process(classify_message);
193 // get the structure element
194 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
195 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
196 path = GSPath.appendLink(path, GSXML.NODE_STRUCTURE_ELEM);
197 // assume that we always get back the top level CL1 node - this becomes the page_classifier node
198 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
199 Element cl_structure = (Element) GSXML.getNodeByPath(classify_response, path);
200 if (cl_structure == null)
201 {
202 logger.error("classifier structure request returned no structure");
203 return page_response;
204 }
205
206 // add the classifier node as the page classifier
207 Element page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
208 page_response.appendChild(page_classifier);
209 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
210
211 // get the metadata for each classifier node,
212 // then for each document node
213
214 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
215
216 boolean did_classifier = false;
217 boolean did_documents = false;
218
219 // if there are classifier nodes
220 // create a metadata request for the classifier, and add it to
221 // the the message
222 NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
223
224 if (cl_nodes.getLength() > 0)
225 {
226 did_classifier = true;
227 Element cl_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
228 metadata_message.appendChild(cl_meta_request);
229
230 Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
231 cl_meta_request.appendChild(new_cl_nodes_list);
232
233 for (int c = 0; c < cl_nodes.getLength(); c++)
234 {
235
236 Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
237 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element) cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
238 new_cl_nodes_list.appendChild(cl);
239 }
240
241 // create and add in the param list - for now get all the metadata
242 // should be based on info sent in from the recept, and the
243 // format stuff
244 Element cl_param_list = null;
245 if (metadata_names.isEmpty())
246 {
247 cl_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
248 Element p = this.doc.createElement(GSXML.PARAM_ELEM);
249 cl_param_list.appendChild(p);
250 p.setAttribute(GSXML.NAME_ATT, "metadata");
251 p.setAttribute(GSXML.VALUE_ATT, "Title");
252 }
253 else
254 {
255 cl_param_list = createMetadataParamList(metadata_names);
256 }
257
258 cl_meta_request.appendChild(cl_param_list);
259
260 }
261
262 // if there are document nodes in the classification (happens
263 // sometimes), create a second request for document metadata and
264 // append to the message
265 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
266 if (doc_nodes.getLength() > 0)
267 {
268 did_documents = true;
269 Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
270 metadata_message.appendChild(doc_meta_request);
271
272 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
273 doc_meta_request.appendChild(doc_list);
274
275 for (int c = 0; c < doc_nodes.getLength(); c++)
276 {
277
278 Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
279 d.setAttribute(GSXML.NODE_ID_ATT, ((Element) doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
280 doc_list.appendChild(d);
281 }
282
283 // create and add in the param list - add all for now
284 Element doc_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
285 Element p = this.doc.createElement(GSXML.PARAM_ELEM);
286 doc_param_list.appendChild(p);
287 p.setAttribute(GSXML.NAME_ATT, "metadata");
288 p.setAttribute(GSXML.VALUE_ATT, "all");
289 doc_meta_request.appendChild(doc_param_list);
290
291 }
292
293 // process the metadata requests
294 Element metadata_response = (Element) this.mr.process(metadata_message);
295 if (did_classifier)
296 {
297 // the classifier one will be the first response
298 // add the metadata lists for each node back into the
299 // page_classifier nodes
300 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
301 NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
302 for (int i = 0; i < cl_nodes.getLength(); i++)
303 {
304 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
305 }
306 }
307 if (did_documents)
308 {
309 NodeList meta_response_docs = null;
310 if (!did_classifier)
311 {
312 // its the first response
313 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
314 meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
315 }
316 else
317 { // its the second response
318 meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER).getChildNodes();
319 }
320
321 for (int i = 0; i < doc_nodes.getLength(); i++)
322 {
323 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
324 }
325 }
326
327 logger.debug("(BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
328 return page_response;
329 }
330
331 protected Element unknownBrowse(Element page, Element request, String browse_type)
332 {
333 logger.error("unknown browse subtype: " + browse_type);
334 return null;
335 }
336}
Note: See TracBrowser for help on using the repository browser.