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

Last change on this file since 25793 was 25793, checked in by ak19, 12 years ago

Fixed a bug where the requested metadata for a classifierNode format statement wasn't being passed on to getRequiredMetadataNames() since the java code tried to match on a format statement's template named classifierNode exactly, rather than match on a format statement's template name that starts with classifierNode. As a consequence of this bugfix, the request for numleafdocs metadata works again.

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