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.NodeList;
|
---|
8 | import org.w3c.dom.Text;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 | import org.w3c.dom.Element;
|
---|
11 |
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Iterator;
|
---|
15 | import java.io.File;
|
---|
16 |
|
---|
17 | /** action class for queries */
|
---|
18 | public class QueryAction extends Action {
|
---|
19 |
|
---|
20 | /** process - processes a request.
|
---|
21 | */
|
---|
22 | public Element process (Element message) {
|
---|
23 |
|
---|
24 | // get the request - assume there is only one
|
---|
25 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
26 |
|
---|
27 | // create the return page tree
|
---|
28 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
29 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
30 | // add the lang stuff from message
|
---|
31 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
32 | // add the system stuff from message
|
---|
33 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
34 |
|
---|
35 | // if want to have a different type of query here, check the subaction att of request
|
---|
36 |
|
---|
37 | // for now assume all queries can be handled by basic query
|
---|
38 | return basicQuery(page, request);
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | /** a generic query handler
|
---|
43 | * this gets the service description, does the query (just passes all teh
|
---|
44 | * params to the service, then gets the titles for any results
|
---|
45 | */
|
---|
46 | protected Element basicQuery(Element page, Element request) {
|
---|
47 |
|
---|
48 | // check that the stylesheet is present - cant output the page without one.
|
---|
49 | String stylesheet = GSFile.stylesheetFile(config_, "basicquery.xsl");
|
---|
50 | if (stylesheet==null) {
|
---|
51 | System.err.println("QueryAction Error: basicquery stylesheet not found!");
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
56 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
57 |
|
---|
58 | HashMap params = GSXML.extractParams(cgi_param_list);
|
---|
59 |
|
---|
60 | String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG);
|
---|
61 | String service_name = (String)params.get(GSCGI.SERVICE_ARG);
|
---|
62 | String collection = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
63 | if (collection == null || collection.equals("")) {
|
---|
64 | System.err.println("QueryAction Error: no collection was specified!");
|
---|
65 | return null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // get the service info from the MR - this will probably need to be cached somehow later on. and add the service node to the page
|
---|
69 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
70 | Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
71 | mr_info_message.appendChild(mr_info_request);
|
---|
72 | mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
73 | mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
74 |
|
---|
75 | String to = collection;
|
---|
76 | to = GSPath.appendLink(to, service_name);
|
---|
77 | mr_info_request.setAttribute(GSXML.TO_ATT, to);
|
---|
78 |
|
---|
79 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
80 | String path = GSXML.RESPONSE_ELEM;
|
---|
81 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
82 | Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
83 | Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
84 |
|
---|
85 | if (pl !=null) {
|
---|
86 | System.out.println("adding shortnames");
|
---|
87 | // add short names to the params in the param list
|
---|
88 | cgi_.paramListAddShortNames(pl);
|
---|
89 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
90 | Element param = (Element)pl.getFirstChild();
|
---|
91 | while (param !=null) {
|
---|
92 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
93 | if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) {
|
---|
94 | // get the values for each sub param
|
---|
95 | NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
96 | for (int i=0; i<subparams.getLength(); i++) {
|
---|
97 | String name = ((Element)subparams.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
98 | String current = (String)params.get(name);
|
---|
99 | if (current !=null && !current.equals("")) {
|
---|
100 | Element e = GSXML.createTextElement(pl.getOwnerDocument(), GSXML.DEFAULT_ELEM, current);
|
---|
101 | e.setAttribute(GSXML.NAME_ATT, name);
|
---|
102 | param.appendChild(e);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 |
|
---|
107 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
108 | String current = (String)params.get(name);
|
---|
109 | if (current !=null && !current.equals("")) {
|
---|
110 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | param = (Element)param.getNextSibling();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // part of the data for the description is the cgi-params
|
---|
119 | // if we have this here, do we need to do the previous step?
|
---|
120 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
121 | page.appendChild(cgi_request);
|
---|
122 |
|
---|
123 | page.appendChild(description);
|
---|
124 |
|
---|
125 | // just a display request
|
---|
126 | if (request_type.equals("d")) {
|
---|
127 | // output the page
|
---|
128 | // process using the stylesheet
|
---|
129 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
130 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
131 | return (Element)transformer_.transform(style_doc, page);
|
---|
132 | }
|
---|
133 |
|
---|
134 | // do the query
|
---|
135 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
136 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
137 | mr_query_message.appendChild(mr_query_request);
|
---|
138 |
|
---|
139 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
140 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
141 | mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
142 | // paramList
|
---|
143 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
144 | mr_query_request.appendChild(query_param_list);
|
---|
145 |
|
---|
146 |
|
---|
147 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
148 | // System.out.println("Query response: " + converter_.getString(mr_query_response));
|
---|
149 |
|
---|
150 | Element query_result_metadata_list = (Element) GSXML.getNodeByPath(mr_query_response, "response/content/metadataList");
|
---|
151 | if (query_result_metadata_list == null) {
|
---|
152 | System.err.println("Warning: No query result metadata.\n");
|
---|
153 | }
|
---|
154 | else {
|
---|
155 | System.out.println("Query result metadata: " + converter_.getString(query_result_metadata_list) + "\n");
|
---|
156 | }
|
---|
157 |
|
---|
158 | // this result is the list of docs.
|
---|
159 | // now take that list, and get the Titles
|
---|
160 | // for now, just create a whole new request
|
---|
161 |
|
---|
162 | // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
|
---|
163 | path = GSXML.RESPONSE_ELEM;
|
---|
164 | path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
|
---|
165 | path = GSPath.appendLink(path, GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
166 |
|
---|
167 | Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
|
---|
168 | path); // documentList not present if no docs found
|
---|
169 | if (document_list == null) {
|
---|
170 |
|
---|
171 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
172 |
|
---|
173 | page.appendChild(doc_.importNode(result_response, true));
|
---|
174 |
|
---|
175 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
176 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
177 | return (Element)transformer_.transform(style_doc, page);
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | // we have a doc list, so get the metadata - for now, get title.
|
---|
182 | // can we dynamically decide what metadata to get?
|
---|
183 | Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
184 | Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
185 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
186 |
|
---|
187 | mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
188 | mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
189 | to = GSPath.appendLink(collection, "MetadataRetrieve");
|
---|
190 | mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
|
---|
191 |
|
---|
192 | Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
193 | mr_metadata_request.appendChild(meta_content);
|
---|
194 |
|
---|
195 | // the first part of the content is the doc list
|
---|
196 | meta_content.appendChild(doc_.importNode(document_list, true));
|
---|
197 |
|
---|
198 | // the second part of the content is the metadata list
|
---|
199 | Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
200 | Element title = doc_.createElement(GSXML.METADATA_ELEM);
|
---|
201 | title.setAttribute(GSXML.NAME_ATT, "Title");
|
---|
202 | metadata_list.appendChild(title);
|
---|
203 | meta_content.appendChild(metadata_list);
|
---|
204 |
|
---|
205 | // System.out.println("Metadata message: " + converter_.getString(mr_metadata_message));
|
---|
206 | Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
|
---|
207 |
|
---|
208 | Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
|
---|
209 |
|
---|
210 | // If there is some metadata about the query results, add it in
|
---|
211 | if (query_result_metadata_list != null) {
|
---|
212 | Element result_content = (Element) GSXML.getChildByTagName(result_response,
|
---|
213 | GSXML.CONTENT_ELEM);
|
---|
214 | result_content.appendChild(query_result_metadata_list);
|
---|
215 | }
|
---|
216 |
|
---|
217 | page.appendChild(doc_.importNode(result_response, true));
|
---|
218 |
|
---|
219 | // System.out.println("XMLTransformer input: " + converter_.getString(page));
|
---|
220 |
|
---|
221 | // output the page
|
---|
222 | // process using the stylesheet
|
---|
223 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
224 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
225 | // Node temp = transformer_.transform(style_doc, page);
|
---|
226 | // System.out.println("XMLTransformer output: " + converter_.getString(temp));
|
---|
227 | // return (Element) temp;
|
---|
228 | return (Element)transformer_.transform(style_doc, page);
|
---|
229 |
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | }
|
---|