source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/GS2BrowseAction.java@ 25861

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

Some import tidying

  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 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.OID;
12import org.greenstone.gsdl3.util.UserContext;
13import org.w3c.dom.Element;
14import org.w3c.dom.Node;
15import org.w3c.dom.NodeList;
16
17/** action for GS2 style classifier browsing */
18public class GS2BrowseAction extends Action
19{
20
21 public static final String CLASSIFIER_ARG = "cl";
22
23 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.GS2BrowseAction.class.getName());
24
25 /** process the request */
26 public Node process(Node message_node)
27 {
28
29 Element message = this.converter.nodeToElement(message_node);
30
31 // get the request - assume only one
32 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
33
34 // the result
35 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
36 Element response = classifierBrowse(request);
37 result.appendChild(response);
38 return result;
39 }
40
41 protected Element classifierBrowse(Element request)
42 {
43
44 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
45
46 // extract the params from the cgi-request, and check that we have a coll specified
47 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
48 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
49
50 String service_name = (String) params.get(GSParams.SERVICE);
51 String collection = (String) params.get(GSParams.COLLECTION);
52 if (collection == null || collection.equals(""))
53 {
54 logger.error("classifierBrowse, need to specify a collection!");
55 return page_response;
56 }
57
58 UserContext userContext = new UserContext(request);
59 String to = GSPath.appendLink(collection, service_name);
60
61 // the first part of the response is the service description
62 // for now get this again from the service.
63 // this should be cached somehow later on.
64
65 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
66 Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
67 info_message.appendChild(info_request);
68
69 // also get the format stuff now if there is some
70 Element format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
71 info_message.appendChild(format_request);
72 // process the requests
73
74 Element info_response = (Element) this.mr.process(info_message);
75
76 // the two responses
77 NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
78 Element service_response = (Element) responses.item(0);
79 Element format_response = (Element) responses.item(1);
80
81 Element service_description = (Element) GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
82 page_response.appendChild(this.doc.importNode(service_description, true));
83
84 //append site metadata
85 addSiteMetadata(page_response, userContext);
86 addInterfaceOptions(page_response);
87
88 // if rt=d, then we are just displaying the service
89 String request_type = (String) params.get(GSParams.REQUEST_TYPE);
90 if (request_type.equals("d"))
91 {
92 //return the page that we have so far
93 return page_response;
94 }
95
96 // get the node that the user has clicked on
97 String classifier_node = (String) params.get(CLASSIFIER_ARG);
98
99 // if the node is not defined, return the page that we have so far
100 if (classifier_node == null || classifier_node.equals(""))
101 {
102 return page_response;
103 }
104
105 // the id of the classifier is the top id of the selected node
106 String top_id = OID.getTop(classifier_node);
107 HashSet<String> doc_meta_names = new HashSet<String>();
108 HashSet<String> class_meta_names = new HashSet<String>();
109 // add in the defaults
110 doc_meta_names.add("Title");
111 class_meta_names.add("Title");
112
113 // add the format info into the response
114 Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
115 if (format_elem != null)
116 {
117
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 {
122 this_format = (Element) GSXML.getChildByTagName(format_elem, GSXML.DEFAULT_ELEM);
123 }
124
125 if (this_format != null)
126 {
127 Element new_format = GSXML.duplicateWithNewName(this.doc, this_format, GSXML.FORMAT_ELEM, false);
128 extractMetadataNames(new_format, doc_meta_names, class_meta_names);
129 // set the format type
130 new_format.setAttribute(GSXML.TYPE_ATT, "browse");
131
132 page_response.appendChild(new_format);
133 }
134 }
135
136 // find out if this classifier is horizontal at top
137 Element class_list = (Element) GSXML.getChildByTagName(service_description, GSXML.CLASSIFIER_ELEM + GSXML.LIST_MODIFIER);
138 Element this_classifier = GSXML.getNamedElement(class_list, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
139
140 // get the browse structure for the selected node
141 Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
142 Element classify_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
143 classify_message.appendChild(classify_request);
144
145 //Create a parameter list to specify the required structure information
146 // for now, always get ancestors and children
147 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
148 classify_request.appendChild(param_list);
149 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
150 param_list.appendChild(param);
151 param.setAttribute(GSXML.NAME_ATT, "structure");
152 param.setAttribute(GSXML.VALUE_ATT, "ancestors");
153 param = this.doc.createElement(GSXML.PARAM_ELEM);
154 param_list.appendChild(param);
155 param.setAttribute(GSXML.NAME_ATT, "structure");
156 param.setAttribute(GSXML.VALUE_ATT, "children");
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
168 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
169 Element class_node_list = (Element) GSXML.getNodeByPath(classify_response, path);
170
171 path = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
172 // assume that we always get back the top level CL1 node - this becomes the page_classifier node
173 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
174 Element cl_structure = (Element) GSXML.getNodeByPath(class_node_list, path);
175 if (cl_structure == null)
176 {
177 logger.error("classifier structure request returned no structure");
178 return page_response;
179 }
180
181 //If the user is viewing a horizontal classifier then we need to get extra information
182 if (cl_structure.getAttribute(GSXML.CHILD_TYPE_ATT).equals(GSXML.HLIST))
183 {
184 //If we have a horizontal classifier and we have had the top-level node requested (e.g. CL1, CL2 etc.)
185 //then we want to get the children of the first classifier node (e.g. the children of CL2.1)
186 if (OID.isTop(classifier_node))
187 {
188 boolean firstChildIsClassifierNode = false;
189 NodeList classifierChildrenNodes = GSXML.getChildrenByTagName(cl_structure, GSXML.CLASS_NODE_ELEM);
190 for (int i = 0; i < classifierChildrenNodes.getLength(); i++)
191 {
192 Element currentChild = (Element) classifierChildrenNodes.item(i);
193 if (currentChild.getAttribute(GSXML.NODE_ID_ATT).endsWith(".1"))
194 {
195 firstChildIsClassifierNode = true;
196 }
197 }
198
199 if (firstChildIsClassifierNode)
200 {
201 Element childStructure = getClassifierStructureFromID(classifier_node + ".1", request, collection, service_name);
202
203 Element replacementElem = null;
204 NodeList childClassifierNodes = childStructure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
205 for (int i = 0; i < childClassifierNodes.getLength(); i++)
206 {
207 Element currentElem = (Element) childClassifierNodes.item(i);
208 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(classifier_node + ".1"))
209 {
210 replacementElem = currentElem;
211 break;
212 }
213 }
214
215 NodeList nodesToSearch = cl_structure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
216 for (int i = 0; i < nodesToSearch.getLength(); i++)
217 {
218 Element currentElem = (Element) nodesToSearch.item(i);
219 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(classifier_node + ".1"))
220 {
221 Element parent = (Element) currentElem.getParentNode();
222 parent.insertBefore(replacementElem, currentElem);
223 parent.removeChild(currentElem);
224 break;
225 }
226 }
227 }
228 }
229 //If we have a horizontal classifier and we have NOT had the top-level node requested then we need to
230 //make sure we get the full list of top-level children to display (e.g. if the user has requested
231 //CL2.1.1 we also need to make sure we have CL2.2, CL2.3, CL2.4 etc.)
232 else
233 {
234 Element childStructure = getClassifierStructureFromID(OID.getTop(classifier_node), request, collection, service_name);
235
236 String[] idParts = classifier_node.split("\\.");
237 String idToSearchFor = idParts[0] + "." + idParts[1];
238
239 Element replacementElem = null;
240 NodeList childClassifierNodes = cl_structure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
241 for (int i = 0; i < childClassifierNodes.getLength(); i++)
242 {
243 Element currentElem = (Element) childClassifierNodes.item(i);
244 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(idToSearchFor))
245 {
246 replacementElem = currentElem;
247 break;
248 }
249 }
250
251 if (replacementElem != null)
252 {
253 NodeList nodesToSearch = childStructure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
254 for (int i = 0; i < nodesToSearch.getLength(); i++)
255 {
256 Element currentElem = (Element) nodesToSearch.item(i);
257 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(idToSearchFor))
258 {
259 Element parent = (Element) currentElem.getParentNode();
260 parent.insertBefore(replacementElem, currentElem);
261 parent.removeChild(currentElem);
262 break;
263 }
264 }
265
266 cl_structure = childStructure;
267 }
268 }
269 }
270
271 Element page_classifier = null;
272 // add the single classifier node as the page classifier
273 page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
274 page_response.appendChild(page_classifier);
275 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
276
277 // get the metadata for each classifier node,
278 // then for each document node
279
280 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
281
282 boolean did_classifier = false;
283 boolean did_documents = false;
284
285 // if there are classifier nodes
286 // create a metadata request for the classifier, and add it to
287 // the the message
288 NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
289
290 if (cl_nodes.getLength() > 0)
291 {
292 did_classifier = true;
293 Element cl_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
294 metadata_message.appendChild(cl_meta_request);
295
296 Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
297 cl_meta_request.appendChild(new_cl_nodes_list);
298
299 for (int c = 0; c < cl_nodes.getLength(); c++)
300 {
301
302 Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
303 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element) cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
304 new_cl_nodes_list.appendChild(cl);
305 }
306
307 // create and add in the param list - for now get all the metadata
308 // should be based on info sent in from the recept, and the
309 // format stuff
310 Element cl_param_list = createMetadataParamList(class_meta_names);
311 cl_meta_request.appendChild(cl_param_list);
312
313 }
314
315 // if there are document nodes in the classification (happens
316 // sometimes), create a second request for document metadata and
317 // append to the message
318 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
319 if (doc_nodes.getLength() > 0)
320 {
321 did_documents = true;
322 Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
323 metadata_message.appendChild(doc_meta_request);
324
325 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
326 doc_meta_request.appendChild(doc_list);
327
328 for (int c = 0; c < doc_nodes.getLength(); c++)
329 {
330
331 Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
332 d.setAttribute(GSXML.NODE_ID_ATT, ((Element) doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
333 doc_list.appendChild(d);
334 }
335
336 // create and add in the param list - add all for now
337 Element doc_param_list = createMetadataParamList(doc_meta_names);
338 doc_meta_request.appendChild(doc_param_list);
339
340 }
341
342 // process the metadata requests
343 Element metadata_response = (Element) this.mr.process(metadata_message);
344 if (did_classifier)
345 {
346 // the classifier one will be the first response
347 // add the metadata lists for each node back into the
348 // page_classifier nodes
349 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
350 Node pathNode = GSXML.getNodeByPath(metadata_response, path);
351 if (pathNode == null)
352 {
353 return page_response;
354 }
355 //NodeList meta_response_cls = (Element)pathNode.getChildNodes(); // can't handle empty elements from converting formatted strings (with empty newlines) into XML
356 NodeList meta_response_cls = ((Element) pathNode).getElementsByTagName(GSXML.CLASS_NODE_ELEM);
357 for (int i = 0; i < cl_nodes.getLength(); i++)
358 {
359 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
360 }
361 }
362
363 if (did_documents)
364 {
365 NodeList meta_response_docs = null;
366 if (!did_classifier)
367 {
368 // its the first response
369 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
370 Node pathNode = GSXML.getNodeByPath(metadata_response, path);
371 if (pathNode == null)
372 {
373 return page_response;
374 }
375
376 meta_response_docs = pathNode.getChildNodes();
377
378 }
379 else
380 { // its the second response
381 Node nodes = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
382 if (nodes == null)
383 {
384 return page_response;
385 }
386 meta_response_docs = nodes.getChildNodes();
387 }
388
389 for (int i = 0; i < doc_nodes.getLength(); i++)
390 {
391 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
392 }
393 }
394
395 logger.debug("(GS2BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
396 return page_response;
397 }
398
399 private Element getClassifierStructureFromID(String id, Element request, String collection, String service_name)
400 {
401 UserContext userContext = new UserContext(request);
402 String to = GSPath.appendLink(collection, service_name);
403
404 Element firstClassifierNodeChildrenMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
405 Element firstClassifierNodeChildrenRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
406 firstClassifierNodeChildrenMessage.appendChild(firstClassifierNodeChildrenRequest);
407
408 Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
409 firstClassifierNodeChildrenRequest.appendChild(paramList);
410
411 Element ancestorParam = this.doc.createElement(GSXML.PARAM_ELEM);
412 paramList.appendChild(ancestorParam);
413 ancestorParam.setAttribute(GSXML.NAME_ATT, "structure");
414 ancestorParam.setAttribute(GSXML.VALUE_ATT, "ancestors");
415
416 Element childrenParam = this.doc.createElement(GSXML.PARAM_ELEM);
417 paramList.appendChild(childrenParam);
418 childrenParam.setAttribute(GSXML.NAME_ATT, "structure");
419 childrenParam.setAttribute(GSXML.VALUE_ATT, "children");
420
421 Element classifierToGetList = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
422 Element classifierToGet = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
423 classifierToGet.setAttribute(GSXML.NODE_ID_ATT, id);
424 classifierToGetList.appendChild(classifierToGet);
425 firstClassifierNodeChildrenRequest.appendChild(classifierToGetList);
426
427 Element firstClassifierNodeChildrenResponse = (Element) this.mr.process(firstClassifierNodeChildrenMessage);
428
429 String nsPath = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
430 Element topClassifierNode = (Element) GSXML.getNodeByPath(firstClassifierNodeChildrenResponse, nsPath);
431 nsPath = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
432 nsPath = GSPath.appendLink(nsPath, GSXML.CLASS_NODE_ELEM);
433 Element childStructure = (Element) GSXML.getNodeByPath(topClassifierNode, nsPath);
434
435 return childStructure;
436 }
437
438 protected void extractMetadataNames(Element new_format, HashSet<String> doc_meta_names, HashSet<String> class_meta_names)
439 {
440 NodeList templates = new_format.getElementsByTagName("gsf:template");
441 for (int i = 0; i < templates.getLength(); i++)
442 {
443 Element template = (Element) templates.item(i);
444 String match = template.getAttribute("match");
445 if (match.startsWith("documentNode"))
446 {
447 getRequiredMetadataNames(template, doc_meta_names);
448 }
449 else if (match.startsWith("classifierNode")) // not match.equals, as we want to match nodes like: classifierNode[@classifierStyle = 'VList']
450 {
451 getRequiredMetadataNames(template, class_meta_names);
452 }
453 }
454 }
455
456}
Note: See TracBrowser for help on using the repository browser.