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 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
55 |
|
---|
56 | // part of the data for the page is the cgi-params
|
---|
57 | // if we have this here, do we need to overwrite default values in the
|
---|
58 | // param list down below??
|
---|
59 | Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
|
---|
60 | page.appendChild(page_request);
|
---|
61 |
|
---|
62 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
63 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
64 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
65 |
|
---|
66 | String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG);
|
---|
67 | String service_name = (String)params.get(GSCGI.SERVICE_ARG);
|
---|
68 | String collection = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
69 | if (collection == null || collection.equals("")) {
|
---|
70 | System.err.println("QueryAction Error: no collection was specified!");
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 |
|
---|
74 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
75 | String to = GSPath.appendLink(collection, service_name);
|
---|
76 |
|
---|
77 | //create teh pageREsponse
|
---|
78 | Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM);
|
---|
79 | page.appendChild(page_response);
|
---|
80 |
|
---|
81 | // the second part of the page is the service description
|
---|
82 | // for now get this again from the service.
|
---|
83 | // this will probably need to be cached somehow later on.
|
---|
84 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
85 | Element mr_info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
|
---|
86 | mr_info_message.appendChild(mr_info_request);
|
---|
87 |
|
---|
88 | // also get the format stuff now if there is some
|
---|
89 | Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang);
|
---|
90 | mr_info_message.appendChild(format_request);
|
---|
91 |
|
---|
92 | // also get the coll description
|
---|
93 | Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang);
|
---|
94 | mr_info_message.appendChild(coll_about_request);
|
---|
95 |
|
---|
96 | // process the messages
|
---|
97 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
98 |
|
---|
99 | // the three responses
|
---|
100 | NodeList responses = mr_info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
101 | Element service_response = (Element)responses.item(0);
|
---|
102 | Element format_response = (Element)responses.item(1);
|
---|
103 | Element coll_response = (Element)responses.item(2);
|
---|
104 |
|
---|
105 | Element service_description = (Element)doc_.importNode(GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM), true);
|
---|
106 | page_response.appendChild(service_description);
|
---|
107 |
|
---|
108 | Element pl = (Element)GSXML.getChildByTagName(service_description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
109 |
|
---|
110 | if (pl !=null) {
|
---|
111 | // add short names to the params in the param list
|
---|
112 | cgi_.paramListAddShortNames(pl);
|
---|
113 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
114 | Element param = (Element)pl.getFirstChild();
|
---|
115 | while (param !=null) {
|
---|
116 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
117 | if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) {
|
---|
118 | // get the values for each sub param
|
---|
119 | NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
120 | for (int i=0; i<subparams.getLength(); i++) {
|
---|
121 | String name = ((Element)subparams.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
122 | String current = (String)params.get(name);
|
---|
123 | if (current !=null && !current.equals("")) {
|
---|
124 | Element e = GSXML.createTextElement(pl.getOwnerDocument(), GSXML.DEFAULT_ELEM, current);
|
---|
125 | e.setAttribute(GSXML.NAME_ATT, name);
|
---|
126 | param.appendChild(e);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | } else {
|
---|
130 |
|
---|
131 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
132 | String current = (String)params.get(name);
|
---|
133 | if (current !=null && !current.equals("")) {
|
---|
134 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | param = (Element)param.getNextSibling();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | // add the coll-response to the page
|
---|
144 | Element coll_description = (Element)GSXML.getChildByTagName(coll_response, GSXML.COLLECTION_ELEM);
|
---|
145 | page_response.appendChild(doc_.importNode(coll_description, true));
|
---|
146 |
|
---|
147 |
|
---|
148 | // just a display request
|
---|
149 | if (request_type.equals("d")) {
|
---|
150 | System.out.println("(QueryAction) Finished page:\n" + converter_.getPrettyString(page));
|
---|
151 | // output the page
|
---|
152 | // process using the stylesheet
|
---|
153 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
154 | return (Element)transformer_.transform(style_doc, page);
|
---|
155 | }
|
---|
156 |
|
---|
157 | // do the query
|
---|
158 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
159 | Element mr_query_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, page.getAttribute(GSXML.LANG_ATT));
|
---|
160 | mr_query_message.appendChild(mr_query_request);
|
---|
161 |
|
---|
162 | // paramList
|
---|
163 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
164 | mr_query_request.appendChild(query_param_list);
|
---|
165 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
166 |
|
---|
167 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
168 | Element query_result_metadata_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
|
---|
169 | if (query_result_metadata_list == null) {
|
---|
170 | System.err.println("QueryAction: Warning: No query result metadata.\n");
|
---|
171 | }
|
---|
172 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
173 | Element query_term_info_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
|
---|
174 | if (query_term_info_list == null) {
|
---|
175 | System.err.println("QueryAction: Warning: No query term information.\n");
|
---|
176 | }
|
---|
177 |
|
---|
178 | // this result is the list of docs.
|
---|
179 | // now take that list, and get the Titles
|
---|
180 | // for now, just create a whole new request
|
---|
181 |
|
---|
182 | // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
|
---|
183 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
184 |
|
---|
185 | Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
|
---|
186 | path);
|
---|
187 | // documentList not present if no docs found
|
---|
188 | if (document_list == null) {
|
---|
189 |
|
---|
190 | //Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
191 |
|
---|
192 | //page.appendChild(doc_.importNode(result_response, true));
|
---|
193 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
194 | return (Element)transformer_.transform(style_doc, page);
|
---|
195 |
|
---|
196 | }
|
---|
197 |
|
---|
198 | // add in the format info
|
---|
199 | Element format_elem = getAndTransformFormat(format_response);
|
---|
200 | if (format_elem != null) {
|
---|
201 | GSXSLT.mergeStylesheets(style_doc, format_elem);
|
---|
202 | }
|
---|
203 |
|
---|
204 | // we have a doc list, so get the metadata - for now, get title.
|
---|
205 | // can we dynamically decide what metadata to get?
|
---|
206 | Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
207 | Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
208 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
209 |
|
---|
210 | mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
211 | mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
212 | to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired?
|
---|
213 | mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
|
---|
214 |
|
---|
215 | // Create a parameter list to specify the required metadata information
|
---|
216 | Element dm_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
217 | mr_metadata_request.appendChild(dm_param_list);
|
---|
218 | Element dm_param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
219 | dm_param_list.appendChild(dm_param);
|
---|
220 | dm_param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
221 | dm_param.setAttribute(GSXML.VALUE_ATT, "Title");
|
---|
222 |
|
---|
223 | mr_metadata_request.appendChild(doc_.importNode(document_list, true));
|
---|
224 | Element mr_metadata_response = (Element) mr_.process(mr_metadata_message);
|
---|
225 |
|
---|
226 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
227 | Element query_result_document_list = (Element) GSXML.getNodeByPath(mr_metadata_response, path);
|
---|
228 |
|
---|
229 | if (query_result_document_list != null) {
|
---|
230 | page_response.appendChild(doc_.importNode(query_result_document_list, true));
|
---|
231 | }
|
---|
232 | // If there is some metadata about the query results, add it in
|
---|
233 | if (query_result_metadata_list != null) {
|
---|
234 | page_response.appendChild(doc_.importNode(query_result_metadata_list, true));
|
---|
235 | }
|
---|
236 | // If there is some metadata about the query results, add it in
|
---|
237 | if (query_term_info_list != null) {
|
---|
238 | page_response.appendChild(doc_.importNode(query_term_info_list, true));
|
---|
239 | }
|
---|
240 |
|
---|
241 | // System.out.println("Query page:\n" + converter_.getPrettyString(page));
|
---|
242 |
|
---|
243 | // output the page
|
---|
244 | // process using the stylesheet
|
---|
245 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
246 | return (Element)transformer_.transform(style_doc, page);
|
---|
247 | }
|
---|
248 | }
|
---|