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

Last change on this file since 4093 was 4093, checked in by kjdon, 21 years ago

moved something to the general process method from the specific basicQuery method

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 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.Vector;
14import java.util.Map;
15import java.util.Iterator;
16import java.io.File;
17
18/** action class for queries */
19public class QueryAction extends Action {
20
21 /** process - processes a request.
22 */
23 public Element process (Element message) {
24
25 // get the request - assume there is only one
26 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
27
28 // create the return page tree
29 Element page = doc_.createElement(GSXML.PAGE_ELEM);
30 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
31 // add the lang stuff from message
32 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
33 // add the system stuff from message
34 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
35
36 // part of the data for the page is the cgi-params
37 // if we have this here, do we need to overwrite default values in the
38 // param list down below??
39 Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
40 page.appendChild(page_request);
41
42 // if want to have a different type of query here, check the subaction att of request
43
44 // for now assume all queries can be handled by basic query
45 return basicQuery(page, request);
46
47 }
48
49 /** a generic query handler
50 * this gets the service description, does the query (just passes all teh
51 * params to the service, then gets the titles for any results
52 */
53 protected Element basicQuery(Element page, Element request) {
54
55 // check that the stylesheet is present - cant output the page without one.
56 String stylesheet = GSFile.stylesheetFile(config_, "basicquery.xsl");
57 if (stylesheet==null) {
58 System.err.println("QueryAction Error: basicquery stylesheet not found!");
59 return null;
60 }
61 Document style_doc = converter_.getDOM(new File(stylesheet));
62
63
64 // extract the params from the cgi-request, and check that we have a coll specified
65 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
66 HashMap params = GSXML.extractParams(cgi_param_list, false);
67
68 String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG);
69 String service_name = (String)params.get(GSCGI.SERVICE_ARG);
70 String collection = (String)params.get(GSCGI.COLLECTION_ARG);
71 if (collection == null || collection.equals("")) {
72 System.err.println("QueryAction Error: no collection was specified!");
73 return null;
74 }
75
76 String lang = request.getAttribute(GSXML.LANG_ATT);
77 String to = GSPath.appendLink(collection, service_name);
78
79 //create teh pageREsponse
80 Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM);
81 page.appendChild(page_response);
82
83 // the second part of the page is the service description
84 // for now get this again from the service.
85 // this will probably need to be cached somehow later on.
86 Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
87 Element mr_info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
88 mr_info_message.appendChild(mr_info_request);
89
90 // also get the format stuff now if there is some
91 Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang);
92 mr_info_message.appendChild(format_request);
93
94 // also get the coll description
95 Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang);
96 mr_info_message.appendChild(coll_about_request);
97
98 // process the messages
99 Element mr_info_response = (Element) mr_.process(mr_info_message);
100
101 // the three responses
102 NodeList responses = mr_info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
103 Element service_response = (Element)responses.item(0);
104 Element format_response = (Element)responses.item(1);
105 Element coll_response = (Element)responses.item(2);
106
107 Element service_description = (Element)doc_.importNode(GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM), true);
108 page_response.appendChild(service_description);
109
110 Element pl = (Element)GSXML.getChildByTagName(service_description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
111
112 if (pl !=null) {
113 // add short names to the params in the param list
114 cgi_.paramListAddShortNames(pl);
115 // for each param in the description, overwrite teh default value with the currently set value if present
116 Element param = (Element)pl.getFirstChild();
117 while (param !=null) {
118 if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
119 if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) {
120 // get the values for each sub param
121 NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM);
122 for (int i=0; i<subparams.getLength(); i++) {
123 String name = ((Element)subparams.item(i)).getAttribute(GSXML.NAME_ATT);
124 String current = (String)params.get(name);
125 if (current !=null && !current.equals("")) {
126 Element e = GSXML.createTextElement(pl.getOwnerDocument(), GSXML.DEFAULT_ELEM, current);
127 e.setAttribute(GSXML.NAME_ATT, name);
128 param.appendChild(e);
129 }
130 }
131 } else {
132
133 String name = param.getAttribute(GSXML.NAME_ATT);
134 String current = (String)params.get(name);
135 if (current !=null && !current.equals("")) {
136 param.setAttribute(GSXML.DEFAULT_ATT, current);
137 }
138 }
139 }
140 param = (Element)param.getNextSibling();
141 }
142 }
143
144
145 // add the coll-response to the page
146 Element coll_description = (Element)GSXML.getChildByTagName(coll_response, GSXML.COLLECTION_ELEM);
147 page_response.appendChild(doc_.importNode(coll_description, true));
148
149
150 // just a display request
151 if (request_type.equals("d")) {
152 System.out.println("(QueryAction) Finished page:\n" + converter_.getPrettyString(page));
153 // output the page
154 // process using the stylesheet
155 GSXSLT.absoluteIncludePaths(style_doc, config_);
156 return (Element)transformer_.transform(style_doc, page);
157 }
158
159 // add in the format info to the stylesheet if there is any
160 Element format_elem = getAndTransformFormat(format_response);
161 if (format_elem != null) {
162 GSXSLT.mergeStylesheets(style_doc, format_elem);
163 }
164
165
166 // do the query
167 Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
168 Element mr_query_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, page.getAttribute(GSXML.LANG_ATT));
169 mr_query_message.appendChild(mr_query_request);
170
171 // paramList
172 Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
173 mr_query_request.appendChild(query_param_list);
174
175 // do the query
176 Element mr_query_response = (Element)mr_.process(mr_query_message);
177
178 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
179 Element query_result_metadata_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
180 if (query_result_metadata_list == null) {
181 System.err.println("QueryAction: Warning: No query result metadata.\n");
182 } else { // add it into the page response
183 page_response.appendChild(doc_.importNode(query_result_metadata_list, true));
184 }
185
186 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
187 Element query_term_info_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
188 if (query_term_info_list == null) {
189 System.err.println("QueryAction: Warning: No query term information.\n");
190 } else { // add it into the page response
191 page_response.appendChild(doc_.importNode(query_term_info_list, true));
192 }
193
194 // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
195 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
196
197 Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
198 path);
199 // documentList not present if no docs found
200 if (document_list == null) {
201 GSXSLT.absoluteIncludePaths(style_doc, config_);
202 return (Element)transformer_.transform(style_doc, page);
203
204 }
205
206
207 // we have a doc list, so get the metadata - look through the stylesheet to determine what metadata to ask for
208
209 Vector document_metadata = GSXSLT.extractWantedMetadata(style_doc, GSXML.DOC_NODE_ELEM);
210 if (document_metadata.isEmpty()) {
211 System.err.println("no document metadata specified!!");
212 // we dont need to do the metadata request, just append the original doc list and return the page
213 page_response.appendChild(doc_.importNode(document_list, true));
214 GSXSLT.absoluteIncludePaths(style_doc, config_);
215 return (Element)transformer_.transform(style_doc, page);
216
217 }
218 // we have to do the metadata request
219
220 Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
221 Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
222 mr_metadata_message.appendChild(mr_metadata_request);
223
224 mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
225 mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
226 to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired?
227 mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
228
229 Element dm_param_list = GSXML.createMetadataParamList(doc_, document_metadata);
230 mr_metadata_request.appendChild(dm_param_list);
231
232
233 // add in the doc node list too
234 mr_metadata_request.appendChild(doc_.importNode(document_list, true));
235 Element mr_metadata_response = (Element) mr_.process(mr_metadata_message);
236
237 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
238 Element query_result_document_list = (Element) GSXML.getNodeByPath(mr_metadata_response, path);
239
240 if (query_result_document_list != null) {
241 page_response.appendChild(doc_.importNode(query_result_document_list, true));
242 }
243
244 // System.out.println("Query page:\n" + converter_.getPrettyString(page));
245
246 // output the page
247 // process using the stylesheet
248 GSXSLT.absoluteIncludePaths(style_doc, config_);
249 return (Element)transformer_.transform(style_doc, page);
250 }
251}
Note: See TracBrowser for help on using the repository browser.