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

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

what metadata to ask for is now dynamically determined from the xslt using a method from GSXSLT.java

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