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

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

all stuff for the page generated by teh Receptionist (config and display) is now put into a pageExtra element, so actions only need to append one extra piece to the page

  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 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 page extra stuff from message
32 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.PAGE_EXTRA_ELEM), true));
33
34 // part of the data for the page is the cgi-params
35 // if we have this here, do we need to overwrite default values in the
36 // param list down below??
37 Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
38 page.appendChild(page_request);
39
40 // if want to have a different type of query here, check the subaction att of request
41
42 // for now assume all queries can be handled by basic query
43 return basicQuery(page, request);
44
45 }
46
47 /** a generic query handler
48 * this gets the service description, does the query (just passes all teh
49 * params to the service, then gets the titles for any results
50 */
51 protected Element basicQuery(Element page, Element request) {
52
53 // check that the stylesheet is present - cant output the page without one.
54 String stylesheet = GSFile.stylesheetFile(config_, "basicquery.xsl");
55 if (stylesheet==null) {
56 System.err.println("QueryAction Error: basicquery stylesheet not found!");
57 return null;
58 }
59 Document style_doc = converter_.getDOM(new File(stylesheet));
60
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 the 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 // add in the format info to the stylesheet if there is any
158 Element format_elem = getAndTransformFormat(format_response);
159 if (format_elem != null) {
160 GSXSLT.mergeStylesheets(style_doc, format_elem);
161 }
162
163
164 // do the query
165 Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
166 Element mr_query_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, page.getAttribute(GSXML.LANG_ATT));
167 mr_query_message.appendChild(mr_query_request);
168
169 // paramList
170 Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
171 mr_query_request.appendChild(query_param_list);
172
173 // do the query
174 Element mr_query_response = (Element)mr_.process(mr_query_message);
175
176 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
177 Element query_result_metadata_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
178 if (query_result_metadata_list == null) {
179 System.err.println("QueryAction: Warning: No query result metadata.\n");
180 } else { // add it into the page response
181 page_response.appendChild(doc_.importNode(query_result_metadata_list, true));
182 }
183
184 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
185 Element query_term_info_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
186 if (query_term_info_list == null) {
187 System.err.println("QueryAction: Warning: No query term information.\n");
188 } else { // add it into the page response
189 page_response.appendChild(doc_.importNode(query_term_info_list, true));
190 }
191
192 // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
193 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
194
195 Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
196 path);
197 // documentList not present if no docs found
198 if (document_list == null) {
199 GSXSLT.absoluteIncludePaths(style_doc, config_);
200 return (Element)transformer_.transform(style_doc, page);
201
202 }
203
204
205 // we have a doc list, so get the metadata - look through the stylesheet to determine what metadata to ask for
206
207 Vector document_metadata = GSXSLT.extractWantedMetadata(style_doc, GSXML.DOC_NODE_ELEM);
208 if (document_metadata.isEmpty()) {
209 System.err.println("no document metadata specified!!");
210 // we dont need to do the metadata request, just append the original doc list and return the page
211 page_response.appendChild(doc_.importNode(document_list, true));
212 GSXSLT.absoluteIncludePaths(style_doc, config_);
213 return (Element)transformer_.transform(style_doc, page);
214
215 }
216 // we have to do the metadata request
217
218 Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
219 Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
220 mr_metadata_message.appendChild(mr_metadata_request);
221
222 mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
223 mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
224 to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired?
225 mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
226
227 Element dm_param_list = GSXML.createMetadataParamList(doc_, document_metadata);
228 mr_metadata_request.appendChild(dm_param_list);
229
230
231 // add in the doc node list too
232 mr_metadata_request.appendChild(doc_.importNode(document_list, true));
233 Element mr_metadata_response = (Element) mr_.process(mr_metadata_message);
234
235 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
236 Element query_result_document_list = (Element) GSXML.getNodeByPath(mr_metadata_response, path);
237
238 if (query_result_document_list != null) {
239 page_response.appendChild(doc_.importNode(query_result_document_list, true));
240 }
241
242 // System.out.println("Query page:\n" + converter_.getPrettyString(page));
243
244 // output the page
245 // process using the stylesheet
246 GSXSLT.absoluteIncludePaths(style_doc, config_);
247 return (Element)transformer_.transform(style_doc, page);
248 }
249}
Note: See TracBrowser for help on using the repository browser.