1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.Text;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.Map;
|
---|
13 | //import java.util.Map;
|
---|
14 | import java.util.Iterator;
|
---|
15 | import java.io.File;
|
---|
16 |
|
---|
17 | public 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("page");
|
---|
25 | page.setAttribute("lang", message.getAttribute("lang"));
|
---|
26 | // add the lang stuff from message
|
---|
27 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, "translate"), true));
|
---|
28 | // add the system stuff from message
|
---|
29 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, "config"), true));
|
---|
30 |
|
---|
31 | Element request = (Element)message.getElementsByTagName("request").item(0);
|
---|
32 | // query type is teh subaction
|
---|
33 | String query_type = request.getAttribute("subaction");
|
---|
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, "paramList");
|
---|
62 | cgi_.toLong(cgi_paramList);
|
---|
63 | HashMap params = GSXML.extractParams(cgi_paramList);
|
---|
64 |
|
---|
65 | String collection = (String)params.get("c");
|
---|
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("message");
|
---|
77 | Element mr_info_request = doc_.createElement("request");
|
---|
78 | mr_info_message.appendChild(mr_info_request);
|
---|
79 | mr_info_request.setAttribute("type", "describe");
|
---|
80 |
|
---|
81 | String to = collection;
|
---|
82 | to = GSPath.appendLink(to, "TextQuery");
|
---|
83 | mr_info_request.setAttribute("to", 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("description");
|
---|
89 | // just param list for now - may need content info too
|
---|
90 | Node pl = GSXML.getNodeByPath(mr_info_response, "response/service/paramList");
|
---|
91 | if (pl ==null) {
|
---|
92 | System.out.println("pl is null");
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | System.out.println("pl name is "+pl.getNodeName());
|
---|
96 | }
|
---|
97 | Element imported_paramList = (Element)doc_.importNode(pl, true);
|
---|
98 | // convert the name to short
|
---|
99 | cgi_.addShortNames(imported_paramList);
|
---|
100 |
|
---|
101 | description.appendChild(imported_paramList);
|
---|
102 |
|
---|
103 | // for each param in page_request, set the current value if present
|
---|
104 | //actually, prob dont need to know the default value, just which one to display - overwrite the default att?
|
---|
105 |
|
---|
106 | Element param = (Element)imported_paramList.getFirstChild();
|
---|
107 | while (param !=null) {
|
---|
108 | if (param.getNodeName().equals("param")) { // just in case
|
---|
109 | String name = param.getAttribute("name");
|
---|
110 | String current = (String)params.get(name);
|
---|
111 | if (current !=null && !current.equals("")) {
|
---|
112 | param.setAttribute("default", current);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | param = (Element)param.getNextSibling();
|
---|
116 | }
|
---|
117 |
|
---|
118 | cgi_request.appendChild(description);
|
---|
119 |
|
---|
120 | // check the query string
|
---|
121 | String query = (String)params.get("q");
|
---|
122 |
|
---|
123 | if (query==null|| query.equals("")) {
|
---|
124 | // if there is no q set, just output the search form
|
---|
125 |
|
---|
126 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
127 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
128 | return transformer_.transform(style_doc, page);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // else do the request, output search box and results
|
---|
132 | Element mr_query_message = doc_.createElement("message");
|
---|
133 | Element mr_query_request = doc_.createElement("request");
|
---|
134 | mr_query_message.appendChild(mr_query_request);
|
---|
135 |
|
---|
136 | mr_query_request.setAttribute("type", "query");
|
---|
137 | mr_query_request.setAttribute("to", to);
|
---|
138 |
|
---|
139 | // paramList
|
---|
140 | Element paramList = doc_.createElement("paramList");
|
---|
141 | mr_query_request.appendChild(paramList);
|
---|
142 |
|
---|
143 | Iterator i = params.entrySet().iterator();
|
---|
144 | while (i.hasNext()) {
|
---|
145 | Map.Entry e = (Map.Entry)i.next();
|
---|
146 | String name = cgi_.toLong((String)e.getKey());
|
---|
147 | if (!name.equals("")) {
|
---|
148 | String value = (String)e.getValue();
|
---|
149 | // if (!name.equals("c")) {
|
---|
150 | Element p = doc_.createElement("param");
|
---|
151 | p.setAttribute("name", name);
|
---|
152 | p.setAttribute("value", value);
|
---|
153 | paramList.appendChild(p);
|
---|
154 | // }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // content - contains the query string
|
---|
159 | Element query_content = doc_.createElement("content");
|
---|
160 | Text query_text = doc_.createTextNode(query);
|
---|
161 | query_content.appendChild(query_text);
|
---|
162 | mr_query_request.appendChild(query_content);
|
---|
163 |
|
---|
164 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
165 |
|
---|
166 | // this result is the list of docs.
|
---|
167 | // now take that list, and get the Titles
|
---|
168 | // for now, just create a whole new request
|
---|
169 |
|
---|
170 | // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
|
---|
171 | Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response, "response/content/resourceList"); // resourceList not present if no docs found
|
---|
172 | if (resource_list == null) {
|
---|
173 |
|
---|
174 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, "response");
|
---|
175 |
|
---|
176 | page.appendChild(doc_.importNode(result_response, true));
|
---|
177 |
|
---|
178 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
179 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
180 | return transformer_.transform(style_doc, page);
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 | // we have a doc list, so get the metadata - for now, get title.
|
---|
185 | // can we dynamically decide what metadata to get?
|
---|
186 | // get the Title metadata
|
---|
187 | Element mr_metadata_message = doc_.createElement("message");
|
---|
188 | Element mr_metadata_request = doc_.createElement("request");
|
---|
189 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
190 |
|
---|
191 | mr_metadata_request.setAttribute("type", "query");
|
---|
192 | to = GSPath.appendLink(collection, "MetadataRetrieve");
|
---|
193 | mr_metadata_request.setAttribute("to", to);
|
---|
194 |
|
---|
195 | Element meta_content = doc_.createElement("content");
|
---|
196 | mr_metadata_request.appendChild(meta_content);
|
---|
197 |
|
---|
198 | // the first part of the content is the doc list
|
---|
199 | meta_content.appendChild(doc_.importNode(resource_list, true));
|
---|
200 |
|
---|
201 | // the second part of the content is the metadata list
|
---|
202 | Element metadata_list = doc_.createElement("metadataList");
|
---|
203 | Element title = doc_.createElement("element");
|
---|
204 | title.setAttribute("name", "Title");
|
---|
205 | metadata_list.appendChild(title);
|
---|
206 | meta_content.appendChild(metadata_list);
|
---|
207 |
|
---|
208 | Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
|
---|
209 |
|
---|
210 | Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, "response");
|
---|
211 |
|
---|
212 | page.appendChild(doc_.importNode(result_response, true));
|
---|
213 |
|
---|
214 | // process using the stylesheet
|
---|
215 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
216 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
217 | return transformer_.transform(style_doc, page);
|
---|
218 |
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected String fieldQuery(Element page, Element request) {
|
---|
222 | return GSHTML.errorPage("field query not implemented");
|
---|
223 |
|
---|
224 |
|
---|
225 | }
|
---|
226 | protected String unknownQuery(Element page, Element request, String query_type) {
|
---|
227 |
|
---|
228 | return GSHTML.errorPage("unknown query subtype - "+query_type+" query not implemented");
|
---|
229 | }
|
---|
230 | }
|
---|