1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.Document;
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 |
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.io.File;
|
---|
12 | public class BrowseAction extends Action {
|
---|
13 |
|
---|
14 |
|
---|
15 | public String process (Element message) {
|
---|
16 |
|
---|
17 | // create the return page tree
|
---|
18 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
19 | page.setAttribute(GSXML.LANG_ATT, message.getAttribute(GSXML.LANG_ATT));
|
---|
20 | // add the lang stuff from message
|
---|
21 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.TRANSLATION_ELEM), true));
|
---|
22 | // add the system stuff from message
|
---|
23 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
24 |
|
---|
25 | Element request = (Element)message.getElementsByTagName(GSXML.REQUEST_ELEM).item(0);
|
---|
26 |
|
---|
27 | // the browse type is the subaction
|
---|
28 | String browse_type = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
29 |
|
---|
30 | if (browse_type.equals("classifier")) {
|
---|
31 | return classifierBrowse(page, request);
|
---|
32 | }
|
---|
33 | return unknownBrowse(page, request, browse_type);
|
---|
34 |
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | protected String classifierBrowse(Element page, Element request) {
|
---|
40 |
|
---|
41 | // check that the stylesheet is present - cant output a page without one. we may adapt this to use unknownquery stylesheet? - or ask for one from the MR
|
---|
42 | String stylesheet = GSFile.stylesheetFile(config_, "classifier.xsl");
|
---|
43 | if (stylesheet==null) {
|
---|
44 | return GSHTML.errorPage("classifier stylesheet not found!");
|
---|
45 | }
|
---|
46 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
47 |
|
---|
48 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
49 | // first convert short to long names
|
---|
50 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
51 | cgi_.toLong(cgi_paramList);
|
---|
52 | HashMap params = GSXML.extractParams(cgi_paramList);
|
---|
53 |
|
---|
54 | String collection = (String)params.get("collection");
|
---|
55 | if (collection == null || collection.equals("")) {
|
---|
56 | return GSHTML.errorPage("classifierbrowse, need to specify a collection!");
|
---|
57 | }
|
---|
58 |
|
---|
59 | // the first part of the data for the page is the cgi-params
|
---|
60 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
61 | page.appendChild(cgi_request);
|
---|
62 |
|
---|
63 | // get the service info from the MR - this will probably need to be cached somehow later on. and add as a description node to the cgi-request - this doesn't work. change to add the specified value as a current attribute to the param - need to do somthing different for query
|
---|
64 |
|
---|
65 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
66 | Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
67 | mr_info_message.appendChild(mr_info_request);
|
---|
68 | mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
69 |
|
---|
70 | String to = collection;
|
---|
71 | to = GSPath.appendLink(to, "ClassifierBrowse");
|
---|
72 | mr_info_request.setAttribute(GSXML.TO_ATT, to);
|
---|
73 |
|
---|
74 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
75 | Element description = doc_.createElement(GSXML.DESCRIPTION_ELEM);
|
---|
76 |
|
---|
77 | String path = GSXML.RESPONSE_ELEM;
|
---|
78 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
79 | path = GSPath.appendLink(path, GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
|
---|
80 | Node cl = GSXML.getNodeByPath(mr_info_response, path);
|
---|
81 |
|
---|
82 | Element imported_classList = (Element)doc_.importNode(cl, true);
|
---|
83 |
|
---|
84 | description.appendChild(imported_classList);
|
---|
85 | cgi_request.appendChild(description);
|
---|
86 |
|
---|
87 |
|
---|
88 | // if there is a cl field in the cgi params, get the classifier info
|
---|
89 | String node = (String)params.get("classifier");
|
---|
90 |
|
---|
91 | if (node==null|| node.equals("")) {
|
---|
92 | System.out.println("no cl node selected");
|
---|
93 | // if there is no cl set, just output the list
|
---|
94 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
95 | return transformer_.transform(style_doc, page);
|
---|
96 | }
|
---|
97 |
|
---|
98 | // get the info for the selected node
|
---|
99 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
100 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
101 | mr_query_message.appendChild(mr_query_request);
|
---|
102 |
|
---|
103 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
104 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
105 |
|
---|
106 | // paramList - empty for now
|
---|
107 | Element paramList = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
108 | mr_query_request.appendChild(paramList);
|
---|
109 |
|
---|
110 |
|
---|
111 | // content - contains the classifier node, may be more than one
|
---|
112 | // call this resource list for now
|
---|
113 | Element query_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
114 | mr_query_request.appendChild(query_content);
|
---|
115 |
|
---|
116 | Element resource_list = doc_.createElement(GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
|
---|
117 | Element resource = doc_.createElement(GSXML.RESOURCE_ELEM);
|
---|
118 | resource.setAttribute(GSXML.NAME_ATT, node);
|
---|
119 | resource_list.appendChild(resource);
|
---|
120 | query_content.appendChild(resource_list);
|
---|
121 |
|
---|
122 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
123 |
|
---|
124 | Element response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
125 | Node new_style = GSXML.getChildByTagName(response, "stylesheet");
|
---|
126 | if (new_style !=null) {
|
---|
127 | GSXSLT.mergeStylesheets(style_doc, (Element)new_style);
|
---|
128 | response.removeChild(new_style);
|
---|
129 | }
|
---|
130 | // add the response to the page data
|
---|
131 | page.appendChild(doc_.importNode(response, true));
|
---|
132 |
|
---|
133 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
134 | return transformer_.transform(style_doc, page);
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected String unknownBrowse(Element page, Element request, String browse_type) {
|
---|
138 |
|
---|
139 | return GSHTML.errorPage("unknown browse subtype: "+browse_type);
|
---|
140 | }
|
---|
141 | }
|
---|