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

Last change on this file since 26026 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: 18.2 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/** action for GS2 style classifier browsing */
19public class GS2BrowseAction extends Action
20{
21
22 public static final String CLASSIFIER_ARG = "cl";
23
24 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.GS2BrowseAction.class.getName());
25
26 /** process the request */
27 public Node process(Node message_node)
28 {
29
30 Element message = this.converter.nodeToElement(message_node);
31
32 // get the request - assume only one
33 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
34
35 // the result
36 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
37 Element response = classifierBrowse(request);
38 result.appendChild(response);
39 return result;
40 }
41
42 protected Element classifierBrowse(Element request)
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 // find the one for the classifier we are in
118 Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
119 if (this_format == null)
120 {
121 this_format = (Element) GSXML.getChildByTagName(format_elem, GSXML.DEFAULT_ELEM);
122 }
123
124 if (this_format != null)
125 {
126 Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
127 if(global_format_elem != null)
128 {
129 GSXSLT.mergeFormatElements(this_format, global_format_elem, false);
130 }
131
132 Element new_format = GSXML.duplicateWithNewName(this.doc, this_format, GSXML.FORMAT_ELEM, false);
133 extractMetadataNames(new_format, doc_meta_names, class_meta_names);
134
135 Element extraMetaListElem = (Element) GSXML.getChildByTagName(request, GSXML.EXTRA_METADATA + GSXML.LIST_MODIFIER);
136 if(extraMetaListElem != null)
137 {
138 NodeList extraMetaList = extraMetaListElem.getElementsByTagName(GSXML.EXTRA_METADATA);
139 for(int i = 0; i < extraMetaList.getLength(); i++)
140 {
141 class_meta_names.add(((Element)extraMetaList.item(i)).getAttribute(GSXML.NAME_ATT));
142 }
143 }
144
145 // set the format type
146 new_format.setAttribute(GSXML.TYPE_ATT, "browse");
147
148 page_response.appendChild(new_format);
149 }
150 }
151
152 // find out if this classifier is horizontal at top
153 Element class_list = (Element) GSXML.getChildByTagName(service_description, GSXML.CLASSIFIER_ELEM + GSXML.LIST_MODIFIER);
154 Element this_classifier = GSXML.getNamedElement(class_list, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
155
156 // get the browse structure for the selected node
157 Element classify_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
158 Element classify_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
159 classify_message.appendChild(classify_request);
160
161 //Create a parameter list to specify the required structure information
162 // for now, always get ancestors and children
163 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
164 classify_request.appendChild(param_list);
165 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
166 param_list.appendChild(param);
167 param.setAttribute(GSXML.NAME_ATT, "structure");
168 param.setAttribute(GSXML.VALUE_ATT, "ancestors");
169 param = this.doc.createElement(GSXML.PARAM_ELEM);
170 param_list.appendChild(param);
171 param.setAttribute(GSXML.NAME_ATT, "structure");
172 param.setAttribute(GSXML.VALUE_ATT, "children");
173
174 // put the classifier node into a classifier node list
175 Element classifier_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
176 Element classifier = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
177 classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
178 classifier_list.appendChild(classifier);
179 classify_request.appendChild(classifier_list);
180
181 // process the request
182 Element classify_response = (Element) this.mr.process(classify_message);
183
184 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
185 Element class_node_list = (Element) GSXML.getNodeByPath(classify_response, path);
186
187 path = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
188 // assume that we always get back the top level CL1 node - this becomes the page_classifier node
189 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
190 Element cl_structure = (Element) GSXML.getNodeByPath(class_node_list, path);
191 if (cl_structure == null)
192 {
193 logger.error("classifier structure request returned no structure");
194 return page_response;
195 }
196
197 //If the user is viewing a horizontal classifier then we need to get extra information
198 if (cl_structure.getAttribute(GSXML.CHILD_TYPE_ATT).equals(GSXML.HLIST))
199 {
200 //If we have a horizontal classifier and we have had the top-level node requested (e.g. CL1, CL2 etc.)
201 //then we want to get the children of the first classifier node (e.g. the children of CL2.1)
202 if (OID.isTop(classifier_node))
203 {
204 boolean firstChildIsClassifierNode = false;
205 NodeList classifierChildrenNodes = GSXML.getChildrenByTagName(cl_structure, GSXML.CLASS_NODE_ELEM);
206 for (int i = 0; i < classifierChildrenNodes.getLength(); i++)
207 {
208 Element currentChild = (Element) classifierChildrenNodes.item(i);
209 if (currentChild.getAttribute(GSXML.NODE_ID_ATT).endsWith(".1"))
210 {
211 firstChildIsClassifierNode = true;
212 }
213 }
214
215 if (firstChildIsClassifierNode)
216 {
217 Element childStructure = getClassifierStructureFromID(classifier_node + ".1", request, collection, service_name);
218
219 Element replacementElem = null;
220 NodeList childClassifierNodes = childStructure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
221 for (int i = 0; i < childClassifierNodes.getLength(); i++)
222 {
223 Element currentElem = (Element) childClassifierNodes.item(i);
224 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(classifier_node + ".1"))
225 {
226 replacementElem = currentElem;
227 break;
228 }
229 }
230
231 NodeList nodesToSearch = cl_structure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
232 for (int i = 0; i < nodesToSearch.getLength(); i++)
233 {
234 Element currentElem = (Element) nodesToSearch.item(i);
235 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(classifier_node + ".1"))
236 {
237 Element parent = (Element) currentElem.getParentNode();
238 parent.insertBefore(replacementElem, currentElem);
239 parent.removeChild(currentElem);
240 break;
241 }
242 }
243 }
244 }
245 //If we have a horizontal classifier and we have NOT had the top-level node requested then we need to
246 //make sure we get the full list of top-level children to display (e.g. if the user has requested
247 //CL2.1.1 we also need to make sure we have CL2.2, CL2.3, CL2.4 etc.)
248 else
249 {
250 Element childStructure = getClassifierStructureFromID(OID.getTop(classifier_node), request, collection, service_name);
251
252 String[] idParts = classifier_node.split("\\.");
253 String idToSearchFor = idParts[0] + "." + idParts[1];
254
255 Element replacementElem = null;
256 NodeList childClassifierNodes = cl_structure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
257 for (int i = 0; i < childClassifierNodes.getLength(); i++)
258 {
259 Element currentElem = (Element) childClassifierNodes.item(i);
260 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(idToSearchFor))
261 {
262 replacementElem = currentElem;
263 break;
264 }
265 }
266
267 if (replacementElem != null)
268 {
269 NodeList nodesToSearch = childStructure.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
270 for (int i = 0; i < nodesToSearch.getLength(); i++)
271 {
272 Element currentElem = (Element) nodesToSearch.item(i);
273 if (currentElem.getAttribute(GSXML.NODE_ID_ATT).equals(idToSearchFor))
274 {
275 Element parent = (Element) currentElem.getParentNode();
276 parent.insertBefore(replacementElem, currentElem);
277 parent.removeChild(currentElem);
278 break;
279 }
280 }
281
282 cl_structure = childStructure;
283 }
284 }
285 }
286
287 Element page_classifier = null;
288 // add the single classifier node as the page classifier
289 page_classifier = GSXML.duplicateWithNewName(this.doc, cl_structure, GSXML.CLASSIFIER_ELEM, true);
290 page_response.appendChild(page_classifier);
291 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
292
293 // get the metadata for each classifier node,
294 // then for each document node
295
296 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
297
298 boolean did_classifier = false;
299 boolean did_documents = false;
300
301 // if there are classifier nodes
302 // create a metadata request for the classifier, and add it to
303 // the the message
304 NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
305
306 if (cl_nodes.getLength() > 0)
307 {
308 did_classifier = true;
309 Element cl_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to + "MetadataRetrieve", userContext);
310 metadata_message.appendChild(cl_meta_request);
311
312 Element new_cl_nodes_list = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
313 cl_meta_request.appendChild(new_cl_nodes_list);
314
315 for (int c = 0; c < cl_nodes.getLength(); c++)
316 {
317
318 Element cl = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
319 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element) cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
320 new_cl_nodes_list.appendChild(cl);
321 }
322
323 // create and add in the param list - for now get all the metadata
324 // should be based on info sent in from the recept, and the
325 // format stuff
326 Element cl_param_list = createMetadataParamList(class_meta_names);
327 cl_meta_request.appendChild(cl_param_list);
328
329 }
330
331 // if there are document nodes in the classification (happens
332 // sometimes), create a second request for document metadata and
333 // append to the message
334 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
335 if (doc_nodes.getLength() > 0)
336 {
337 did_documents = true;
338 Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(collection, "DocumentMetadataRetrieve"), userContext);
339 metadata_message.appendChild(doc_meta_request);
340
341 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
342 doc_meta_request.appendChild(doc_list);
343
344 for (int c = 0; c < doc_nodes.getLength(); c++)
345 {
346
347 Element d = this.doc.createElement(GSXML.DOC_NODE_ELEM);
348 d.setAttribute(GSXML.NODE_ID_ATT, ((Element) doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
349 doc_list.appendChild(d);
350 }
351
352 // create and add in the param list - add all for now
353 Element doc_param_list = createMetadataParamList(doc_meta_names);
354 doc_meta_request.appendChild(doc_param_list);
355
356 }
357
358 // process the metadata requests
359 Element metadata_response = (Element) this.mr.process(metadata_message);
360 if (did_classifier)
361 {
362 // the classifier one will be the first response
363 // add the metadata lists for each node back into the
364 // page_classifier nodes
365 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
366 Node pathNode = GSXML.getNodeByPath(metadata_response, path);
367 if (pathNode == null)
368 {
369 return page_response;
370 }
371 //NodeList meta_response_cls = (Element)pathNode.getChildNodes(); // can't handle empty elements from converting formatted strings (with empty newlines) into XML
372 NodeList meta_response_cls = ((Element) pathNode).getElementsByTagName(GSXML.CLASS_NODE_ELEM);
373 for (int i = 0; i < cl_nodes.getLength(); i++)
374 {
375 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
376 }
377 }
378
379 if (did_documents)
380 {
381 NodeList meta_response_docs = null;
382 if (!did_classifier)
383 {
384 // its the first response
385 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
386 Node pathNode = GSXML.getNodeByPath(metadata_response, path);
387 if (pathNode == null)
388 {
389 return page_response;
390 }
391
392 meta_response_docs = pathNode.getChildNodes();
393
394 }
395 else
396 { // its the second response
397 Node nodes = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
398 if (nodes == null)
399 {
400 return page_response;
401 }
402 meta_response_docs = nodes.getChildNodes();
403 }
404
405 for (int i = 0; i < doc_nodes.getLength(); i++)
406 {
407 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
408 }
409 }
410
411 logger.debug("(GS2BrowseAction) Page:\n" + this.converter.getPrettyString(page_response));
412 return page_response;
413 }
414
415 private Element getClassifierStructureFromID(String id, Element request, String collection, String service_name)
416 {
417 UserContext userContext = new UserContext(request);
418 String to = GSPath.appendLink(collection, service_name);
419
420 Element firstClassifierNodeChildrenMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
421 Element firstClassifierNodeChildrenRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
422 firstClassifierNodeChildrenMessage.appendChild(firstClassifierNodeChildrenRequest);
423
424 Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
425 firstClassifierNodeChildrenRequest.appendChild(paramList);
426
427 Element ancestorParam = this.doc.createElement(GSXML.PARAM_ELEM);
428 paramList.appendChild(ancestorParam);
429 ancestorParam.setAttribute(GSXML.NAME_ATT, "structure");
430 ancestorParam.setAttribute(GSXML.VALUE_ATT, "ancestors");
431
432 Element childrenParam = this.doc.createElement(GSXML.PARAM_ELEM);
433 paramList.appendChild(childrenParam);
434 childrenParam.setAttribute(GSXML.NAME_ATT, "structure");
435 childrenParam.setAttribute(GSXML.VALUE_ATT, "children");
436
437 Element classifierToGetList = this.doc.createElement(GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
438 Element classifierToGet = this.doc.createElement(GSXML.CLASS_NODE_ELEM);
439 classifierToGet.setAttribute(GSXML.NODE_ID_ATT, id);
440 classifierToGetList.appendChild(classifierToGet);
441 firstClassifierNodeChildrenRequest.appendChild(classifierToGetList);
442
443 Element firstClassifierNodeChildrenResponse = (Element) this.mr.process(firstClassifierNodeChildrenMessage);
444
445 String nsPath = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM + GSXML.LIST_MODIFIER);
446 Element topClassifierNode = (Element) GSXML.getNodeByPath(firstClassifierNodeChildrenResponse, nsPath);
447 nsPath = GSPath.appendLink(GSXML.CLASS_NODE_ELEM, GSXML.NODE_STRUCTURE_ELEM);
448 nsPath = GSPath.appendLink(nsPath, GSXML.CLASS_NODE_ELEM);
449 Element childStructure = (Element) GSXML.getNodeByPath(topClassifierNode, nsPath);
450
451 return childStructure;
452 }
453
454 protected void extractMetadataNames(Element new_format, HashSet<String> doc_meta_names, HashSet<String> class_meta_names)
455 {
456 NodeList templates = new_format.getElementsByTagName("gsf:template");
457 for (int i = 0; i < templates.getLength(); i++)
458 {
459 Element template = (Element) templates.item(i);
460 String match = template.getAttribute("match");
461 if (match.startsWith("documentNode"))
462 {
463 getRequiredMetadataNames(template, doc_meta_names);
464 }
465 else if (match.startsWith("classifierNode")) // not match.equals, as we want to match nodes like: classifierNode[@classifierStyle = 'VList']
466 {
467 getRequiredMetadataNames(template, class_meta_names);
468 }
469 }
470 }
471
472}
Note: See TracBrowser for help on using the repository browser.