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

Last change on this file since 30730 was 30730, checked in by kjdon, 8 years ago

get root_assocfilepath by default, in case we have links to source files, images etc. use root_assocfilepath instead of assocfilepath in case we have classified sections

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