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

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

modified

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