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.NodeList;
|
---|
8 | import org.w3c.dom.Text;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 | import org.w3c.dom.Element;
|
---|
11 |
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Iterator;
|
---|
15 | import java.io.File;
|
---|
16 |
|
---|
17 | /** action class for queries */
|
---|
18 | public class QueryAction extends Action {
|
---|
19 |
|
---|
20 | public Element process (Element message) {
|
---|
21 |
|
---|
22 | // get the request - assume there is only one
|
---|
23 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
24 |
|
---|
25 | // create the return page tree
|
---|
26 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
27 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
28 | // add the lang stuff from message
|
---|
29 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
30 | // add the system stuff from message
|
---|
31 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
32 |
|
---|
33 | // query type is teh subaction
|
---|
34 | String query_type = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
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, query_type);
|
---|
47 |
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | // we may be able to generalize this for all query types?
|
---|
52 | protected Element textQuery(Element page, Element request) {
|
---|
53 |
|
---|
54 | // check 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 | System.err.println("QueryAction Error: textquery stylesheet not found!");
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
62 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
63 | HashMap params = GSXML.extractParams(cgi_param_list);
|
---|
64 |
|
---|
65 | String collection = (String)params.get("collection");
|
---|
66 | if (collection == null || collection.equals("")) {
|
---|
67 | System.err.println("QueryAction Error: no colleciton was specified!");
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | // get the service info from the MR - this will probably need to be cached somehow later on. and add the service node to the page
|
---|
72 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
73 | Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
74 | mr_info_message.appendChild(mr_info_request);
|
---|
75 | mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
76 | mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
77 |
|
---|
78 | String to = collection;
|
---|
79 | to = GSPath.appendLink(to, "TextQuery");
|
---|
80 | mr_info_request.setAttribute(GSXML.TO_ATT, to);
|
---|
81 |
|
---|
82 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
83 |
|
---|
84 | String path = GSXML.RESPONSE_ELEM;
|
---|
85 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
86 | Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
87 | Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
88 |
|
---|
89 | if (pl !=null) {
|
---|
90 | // add short names to the params in the param list
|
---|
91 | cgi_.addShortNames(pl);
|
---|
92 |
|
---|
93 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
94 | Element param = (Element)pl.getFirstChild();
|
---|
95 | while (param !=null) {
|
---|
96 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
97 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
98 | String current = (String)params.get(name);
|
---|
99 | if (current !=null && !current.equals("")) {
|
---|
100 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | param = (Element)param.getNextSibling();
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | // part of the data for the description is the cgi-params
|
---|
108 | // if we have this here, do we need to do the previous step?
|
---|
109 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
110 | page.appendChild(cgi_request);
|
---|
111 |
|
---|
112 | page.appendChild(description);
|
---|
113 |
|
---|
114 | // check the query string
|
---|
115 | String query = (String)params.get(GSXML.REQUEST_TYPE_QUERY);
|
---|
116 |
|
---|
117 | if (query!=null&& !query.equals("")) {
|
---|
118 | // do the query
|
---|
119 |
|
---|
120 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
121 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
122 | mr_query_message.appendChild(mr_query_request);
|
---|
123 |
|
---|
124 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
125 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
126 | mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
127 | // paramList
|
---|
128 | // should we remove the query from the param list?
|
---|
129 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
130 | mr_query_request.appendChild(query_param_list);
|
---|
131 |
|
---|
132 | // content - contains the query string
|
---|
133 | Element query_content = GSXML.createTextElement(doc_, GSXML.CONTENT_ELEM, query);
|
---|
134 | mr_query_request.appendChild(query_content);
|
---|
135 |
|
---|
136 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
137 |
|
---|
138 | // this result is the list of docs.
|
---|
139 | // now take that list, and get the Titles
|
---|
140 | // for now, just create a whole new request
|
---|
141 |
|
---|
142 | // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
|
---|
143 | path = GSXML.RESPONSE_ELEM;
|
---|
144 | path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
|
---|
145 | path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
|
---|
146 |
|
---|
147 | Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response,
|
---|
148 | path); // resourceList not present if no docs found
|
---|
149 | if (resource_list == null) {
|
---|
150 |
|
---|
151 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
152 |
|
---|
153 | page.appendChild(doc_.importNode(result_response, true));
|
---|
154 |
|
---|
155 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
156 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
157 | return (Element)transformer_.transform(style_doc, page);
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | // we have a doc list, so get the metadata - for now, get title.
|
---|
162 | // can we dynamically decide what metadata to get?
|
---|
163 | Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
164 | Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
165 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
166 |
|
---|
167 | mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
168 | mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
169 | to = GSPath.appendLink(collection, "MetadataRetrieve");
|
---|
170 | mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
|
---|
171 |
|
---|
172 | Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
173 | mr_metadata_request.appendChild(meta_content);
|
---|
174 |
|
---|
175 | // the first part of the content is the doc list
|
---|
176 | meta_content.appendChild(doc_.importNode(resource_list, true));
|
---|
177 |
|
---|
178 | // the second part of the content is the metadata list
|
---|
179 | Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
180 | Element title = doc_.createElement(GSXML.METADATA_ELEM);
|
---|
181 | title.setAttribute(GSXML.NAME_ATT, "Title");
|
---|
182 | metadata_list.appendChild(title);
|
---|
183 | meta_content.appendChild(metadata_list);
|
---|
184 |
|
---|
185 | Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
|
---|
186 |
|
---|
187 | Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
|
---|
188 |
|
---|
189 | page.appendChild(doc_.importNode(result_response, true));
|
---|
190 |
|
---|
191 | } // do teh query
|
---|
192 |
|
---|
193 |
|
---|
194 | // output the page
|
---|
195 | // process using the stylesheet
|
---|
196 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
197 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
198 | return (Element)transformer_.transform(style_doc, page);
|
---|
199 |
|
---|
200 | }
|
---|
201 |
|
---|
202 | protected Element fieldQuery(Element page, Element request) {
|
---|
203 | String p = GSHTML.errorPage("field query not implemented");
|
---|
204 |
|
---|
205 | return converter_.getDOM(p).getDocumentElement();
|
---|
206 | }
|
---|
207 | protected Element unknownQuery(Element page, Element request, String query_type) {
|
---|
208 |
|
---|
209 | String p = GSHTML.errorPage("unknown query subtype - "+query_type+" query not implemented");
|
---|
210 | return null;//converter_.getDOM(p).getDocumentElement();
|
---|
211 | }
|
---|
212 | }
|
---|