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

Last change on this file since 3512 was 3512, checked in by kjdon, 22 years ago

all modules now talk the same messages; all xml elems and atts have been made constants and moved to GSXML.java; SOAPCommunicator can be used outside a MessageRouter

  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 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.Text;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11import java.util.HashMap;
12import java.util.Map;
13//import java.util.Map;
14import java.util.Iterator;
15import java.io.File;
16
17public class QueryAction extends Action {
18
19
20 public String process (Element message) {
21
22
23 // create the return page tree
24 Element page = doc_.createElement(GSXML.PAGE_ELEM);
25 page.setAttribute(GSXML.LANG_ATT, message.getAttribute(GSXML.LANG_ATT));
26 // add the lang stuff from message
27 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.TRANSLATION_ELEM), true));
28 // add the system stuff from message
29 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
30
31 Element request = (Element)message.getElementsByTagName(GSXML.REQUEST_ELEM).item(0);
32 // query type is teh subaction
33 String query_type = request.getAttribute(GSXML.SUBACTION_ATT);
34
35 if (query_type.equals("")) {
36 query_type = "text";
37 }
38
39 if (query_type.equals("text")) {
40 return textQuery(page, request);
41 }
42 if (query_type.equals("field")) {
43 return fieldQuery(page, request);
44 }
45 return unknownQuery(page, request, query_type);
46
47
48 }
49
50 // we may be able to generalize this for all query types?
51 protected String textQuery(Element page, Element request) {
52
53 // chack that the stylesheet is present - cant output a page without one. we may adapt this to use unknownquery stylesheet? - or ask for one from the MR
54 String stylesheet = GSFile.stylesheetFile(config_, "textquery.xsl");
55 if (stylesheet==null) {
56 return GSHTML.errorPage("textquery stylesheet not found!");
57 }
58
59 // extract the params from the cgi-request, and check that we have a coll specified
60 // first convert short to long names
61 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
62 cgi_.toLong(cgi_paramList);
63 HashMap params = GSXML.extractParams(cgi_paramList);
64
65 String collection = (String)params.get("collection");
66 if (collection == null || collection.equals("")) {
67 return GSHTML.errorPage("textquery, need to specify a colleciton!");
68 }
69
70 // the first part of the data for the page is the cgi-params
71 Element cgi_request = (Element)doc_.importNode(request, true);
72 page.appendChild(cgi_request);
73
74 // get the service info from the MR - this will probably need to be cached somehow later on. and add as a description node to the cgi-request - this doesn't work. change to add the specified value as a current attribute to the param - need to do somthing different for query
75
76 Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
77 Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
78 mr_info_message.appendChild(mr_info_request);
79 mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
80
81 String to = collection;
82 to = GSPath.appendLink(to, "TextQuery");
83 mr_info_request.setAttribute(GSXML.TO_ATT, to);
84
85 Element mr_info_response = (Element) mr_.process(mr_info_message);
86 System.err.println("describe response for service");
87 System.err.println(converter_.getString(mr_info_response));
88 Element description = doc_.createElement(GSXML.DESCRIPTION_ELEM);
89 // just param list for now - may need content info too
90 String path = GSXML.RESPONSE_ELEM;
91 path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
92 path = GSPath.appendLink(path, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
93 Node pl = GSXML.getNodeByPath(mr_info_response, path);
94 if (pl ==null) {
95 System.out.println("pl is null");
96 }
97 else {
98 System.out.println("pl name is "+pl.getNodeName());
99
100 Element imported_paramList = (Element)doc_.importNode(pl, true);
101 // convert the name to short
102 cgi_.addShortNames(imported_paramList);
103
104 description.appendChild(imported_paramList);
105
106 // for each param in page_request, set the current value if present
107 //actually, prob dont need to know the default value, just which one to display - overwrite the default att?
108
109 Element param = (Element)imported_paramList.getFirstChild();
110 while (param !=null) {
111 if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
112 String name = param.getAttribute(GSXML.NAME_ATT);
113 String current = (String)params.get(name);
114 if (current !=null && !current.equals("")) {
115 param.setAttribute(GSXML.DEFAULT_ATT, current);
116 }
117 }
118 param = (Element)param.getNextSibling();
119 }
120 }
121 cgi_request.appendChild(description);
122
123 // check the query string
124 String query = (String)params.get(GSXML.REQUEST_TYPE_QUERY);
125
126 if (query==null|| query.equals("")) {
127 // if there is no q set, just output the search form
128
129 Document style_doc = converter_.getDOM(new File(stylesheet));
130 GSXSLT.absoluteIncludePaths(style_doc, config_);
131 return transformer_.transform(style_doc, page);
132 }
133
134 // else do the request, output search box and results
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
142 // paramList
143 Element paramList = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
144 mr_query_request.appendChild(paramList);
145
146 Iterator i = params.entrySet().iterator();
147 while (i.hasNext()) {
148 Map.Entry e = (Map.Entry)i.next();
149 String name = cgi_.toLong((String)e.getKey());
150 if (!name.equals("")) {
151 String value = (String)e.getValue();
152 // if (!name.equals("c")) {
153 Element p = doc_.createElement(GSXML.PARAM_ELEM);
154 p.setAttribute(GSXML.NAME_ATT, name);
155 p.setAttribute(GSXML.VALUE_ATT, value);
156 paramList.appendChild(p);
157 // }
158 }
159 }
160
161 // content - contains the query string
162 Element query_content = doc_.createElement(GSXML.CONTENT_ELEM);
163 Text query_text = doc_.createTextNode(query);
164 query_content.appendChild(query_text);
165 mr_query_request.appendChild(query_content);
166
167 Element mr_query_response = (Element)mr_.process(mr_query_message);
168
169 // this result is the list of docs.
170 // now take that list, and get the Titles
171 // for now, just create a whole new request
172
173 // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
174 path = GSXML.RESPONSE_ELEM;
175 path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
176 path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
177
178 Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response,
179 path); // resourceList not present if no docs found
180 if (resource_list == null) {
181
182 Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
183
184 page.appendChild(doc_.importNode(result_response, true));
185
186 Document style_doc = converter_.getDOM(new File(stylesheet));
187 GSXSLT.absoluteIncludePaths(style_doc, config_);
188 return transformer_.transform(style_doc, page);
189
190 }
191
192 // we have a doc list, so get the metadata - for now, get title.
193 // can we dynamically decide what metadata to get?
194 // get the Title metadata
195 Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
196 Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
197 mr_metadata_message.appendChild(mr_metadata_request);
198
199 mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
200 to = GSPath.appendLink(collection, "MetadataRetrieve");
201 mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
202
203 Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
204 mr_metadata_request.appendChild(meta_content);
205
206 // the first part of the content is the doc list
207 meta_content.appendChild(doc_.importNode(resource_list, true));
208
209 // the second part of the content is the metadata list
210 Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
211 Element title = doc_.createElement(GSXML.METADATA_ELEM);
212 title.setAttribute(GSXML.NAME_ATT, "Title");
213 metadata_list.appendChild(title);
214 meta_content.appendChild(metadata_list);
215
216 Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
217
218 Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
219
220 page.appendChild(doc_.importNode(result_response, true));
221
222 // process using the stylesheet
223 Document style_doc = converter_.getDOM(new File(stylesheet));
224 GSXSLT.absoluteIncludePaths(style_doc, config_);
225 return transformer_.transform(style_doc, page);
226
227 }
228
229 protected String fieldQuery(Element page, Element request) {
230 return GSHTML.errorPage("field query not implemented");
231
232
233 }
234 protected String unknownQuery(Element page, Element request, String query_type) {
235
236 return GSHTML.errorPage("unknown query subtype - "+query_type+" query not implemented");
237 }
238}
Note: See TracBrowser for help on using the repository browser.