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

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

uses new GSFile methods

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 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.stylesheetFile(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 // convert the name to short
100 cgi_.addShortNames(imported_paramList);
101
102 description.appendChild(imported_paramList);
103
104 // for each param in page_request, set the current value if present
105 //actually, prob dont need to know the default value, just which one to display - overwrite the default att?
106
107 Element param = (Element)imported_paramList.getFirstChild();
108 while (param !=null) {
109 if (param.getNodeName().equals("param")) { // just in case
110 String name = param.getAttribute("name");
111 String current = (String)params.get(name);
112 if (current !=null && !current.equals("")) {
113 param.setAttribute("default", current);
114 }
115 }
116 param = (Element)param.getNextSibling();
117 }
118
119 cgi_request.appendChild(description);
120
121 // check the query string
122 String query = (String)params.get("q");
123
124 if (query==null|| query.equals("")) {
125 // if there is no q set, just output the search form
126 return transformer_.transform(stylesheet, page);
127 }
128
129 // else do the request, output search box and results
130 Element mr_query_message = doc_.createElement("message");
131 Element mr_query_request = doc_.createElement("request");
132 mr_query_message.appendChild(mr_query_request);
133
134 mr_query_request.setAttribute("type", "query");
135 mr_query_request.setAttribute("to", to);
136
137 // paramList
138 Element paramList = doc_.createElement("paramList");
139 mr_query_request.appendChild(paramList);
140
141 Iterator i = params.entrySet().iterator();
142 while (i.hasNext()) {
143 Map.Entry e = (Map.Entry)i.next();
144 String name = cgi_.toLong((String)e.getKey());
145 if (!name.equals("")) {
146 String value = (String)e.getValue();
147 // if (!name.equals("c")) {
148 Element p = doc_.createElement("param");
149 p.setAttribute("name", name);
150 p.setAttribute("value", value);
151 paramList.appendChild(p);
152 // }
153 }
154 }
155
156 // content - contains the query string
157 Element query_content = doc_.createElement("content");
158 Text query_text = doc_.createTextNode(query);
159 query_content.appendChild(query_text);
160 mr_query_request.appendChild(query_content);
161
162 Element mr_query_response = (Element)mr_.process(mr_query_message);
163
164 // this result is the list of docs.
165 // now take that list, and get the Titles
166 // for now, just create a whole new request
167
168 // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
169 Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response, "response/content/resourceList"); // resourceList not present if no docs found
170 if (resource_list == null) {
171
172 Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, "response");
173
174 page.appendChild(doc_.importNode(result_response, true));
175 return transformer_.transform(stylesheet, page);
176 }
177
178 // we have a doc list, so get the metadata - for now, get title.
179 // can we dynamically decide what metadata to get?
180 // get the Title metadata
181 Element mr_metadata_message = doc_.createElement("message");
182 Element mr_metadata_request = doc_.createElement("request");
183 mr_metadata_message.appendChild(mr_metadata_request);
184
185 mr_metadata_request.setAttribute("type", "query");
186 to = GSPath.appendLink(collection, "MetadataRetrieve");
187 mr_metadata_request.setAttribute("to", to);
188
189 Element meta_content = doc_.createElement("content");
190 mr_metadata_request.appendChild(meta_content);
191
192 // the first part of the content is the doc list
193 meta_content.appendChild(doc_.importNode(resource_list, true));
194
195 // the second part of the content is the metadata list
196 Element metadata_list = doc_.createElement("metadataList");
197 Element title = doc_.createElement("element");
198 title.setAttribute("name", "Title");
199 metadata_list.appendChild(title);
200 meta_content.appendChild(metadata_list);
201
202 Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
203
204 Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, "response");
205
206 page.appendChild(doc_.importNode(result_response, true));
207
208 return transformer_.transform(stylesheet, page);
209
210 }
211
212 protected String fieldQuery(Element page, Element request) {
213 return GSHTML.errorPage("field query not implemented");
214
215
216 }
217 protected String unknownQuery(Element page, Element request) {
218
219 return GSHTML.errorPage("unknown query subtype - unknown query not implemented");
220 }
221}
Note: See TracBrowser for help on using the repository browser.