source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/QueryAction.java@ 3766

Last change on this file since 3766 was 3766, checked in by mdewsnip, 21 years ago

Support for modified query result format (changes to "numDocsMatched" and query term information).

  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 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.Text;
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11
12import java.util.HashMap;
13import java.util.Map;
14import java.util.Iterator;
15import java.io.File;
16
17/** action class for queries */
18public 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/metadataList");
151 if (query_result_metadata_list == null) {
152 System.err.println("Warning: No query result metadata.\n");
153 }
154
155 Element query_term_info_list = (Element) GSXML.getNodeByPath(mr_query_response, "response/content/termList");
156 if (query_term_info_list == null) {
157 System.err.println("Warning: No query term information.\n");
158 }
159
160 // this result is the list of docs.
161 // now take that list, and get the Titles
162 // for now, just create a whole new request
163
164 // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
165 path = GSXML.RESPONSE_ELEM;
166 path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
167 path = GSPath.appendLink(path, GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
168
169 Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
170 path); // documentList not present if no docs found
171 if (document_list == null) {
172
173 Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
174
175 page.appendChild(doc_.importNode(result_response, true));
176
177 Document style_doc = converter_.getDOM(new File(stylesheet));
178 GSXSLT.absoluteIncludePaths(style_doc, config_);
179 return (Element)transformer_.transform(style_doc, page);
180
181 }
182
183 // we have a doc list, so get the metadata - for now, get title.
184 // can we dynamically decide what metadata to get?
185 Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
186 Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
187 mr_metadata_message.appendChild(mr_metadata_request);
188
189 mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
190 mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
191 to = GSPath.appendLink(collection, "MetadataRetrieve");
192 mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
193
194 Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
195 mr_metadata_request.appendChild(meta_content);
196
197 // the first part of the content is the doc list
198 meta_content.appendChild(doc_.importNode(document_list, true));
199
200 // the second part of the content is the metadata list
201 Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
202 Element title = doc_.createElement(GSXML.METADATA_ELEM);
203 title.setAttribute(GSXML.NAME_ATT, "Title");
204 metadata_list.appendChild(title);
205 meta_content.appendChild(metadata_list);
206
207 // System.out.println("Metadata message: " + converter_.getString(mr_metadata_message));
208 Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
209
210 Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
211
212 // If there is some metadata about the query results, add it in
213 if (query_result_metadata_list != null) {
214 result_response.appendChild(query_result_metadata_list);
215 }
216 // If there is some metadata about the query results, add it in
217 if (query_term_info_list != null) {
218 Element result_content = (Element) GSXML.getChildByTagName(result_response,
219 GSXML.CONTENT_ELEM);
220 result_content.appendChild(query_term_info_list);
221 }
222
223 page.appendChild(doc_.importNode(result_response, true));
224
225 // System.out.println("XMLTransformer input: " + converter_.getString(page));
226
227 // output the page
228 // process using the stylesheet
229 Document style_doc = converter_.getDOM(new File(stylesheet));
230 GSXSLT.absoluteIncludePaths(style_doc, config_);
231 // Node temp = transformer_.transform(style_doc, page);
232 // System.out.println("XMLTransformer output: " + converter_.getString(temp));
233 // return (Element) temp;
234 return (Element)transformer_.transform(style_doc, page);
235
236 }
237
238
239}
Note: See TracBrowser for help on using the repository browser.