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.HashSet;
|
---|
14 | import java.util.Vector;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.Iterator;
|
---|
17 | import java.io.File;
|
---|
18 |
|
---|
19 | import org.apache.log4j.*;
|
---|
20 |
|
---|
21 | /** action class for queries */
|
---|
22 | public class QueryAction extends Action {
|
---|
23 |
|
---|
24 |
|
---|
25 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.QueryAction.class.getName());
|
---|
26 |
|
---|
27 | /** process - processes a request.
|
---|
28 | */
|
---|
29 | public Element process (Element message) {
|
---|
30 |
|
---|
31 | // get the request - assume there is only one
|
---|
32 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
33 |
|
---|
34 | // create the return message
|
---|
35 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
36 | Element response = basicQuery(request);
|
---|
37 | result.appendChild(this.doc.importNode(response, true));
|
---|
38 | return result;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /** a generic query handler
|
---|
42 | * this gets the service description, does the query (just passes all the
|
---|
43 | * params to the service, then gets the titles for any results
|
---|
44 | */
|
---|
45 | protected Element basicQuery(Element request) {
|
---|
46 |
|
---|
47 | // the result
|
---|
48 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
49 |
|
---|
50 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
51 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
52 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
53 |
|
---|
54 | String request_type = (String)params.get(GSParams.REQUEST_TYPE);
|
---|
55 | String service_name = (String)params.get(GSParams.SERVICE);
|
---|
56 | String collection = (String)params.get(GSParams.COLLECTION);
|
---|
57 |
|
---|
58 | // collection may be null or empty when we are doing cross coll services
|
---|
59 | if (collection == null || collection.equals("")) {
|
---|
60 | collection = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
64 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
65 | String to = service_name;
|
---|
66 | if (collection != null) {
|
---|
67 | to = GSPath.prependLink(to, collection);
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (request_type.indexOf("d")!=-1) {
|
---|
71 | // we have been asked for the service description
|
---|
72 | Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
73 | Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, uid);
|
---|
74 | mr_info_message.appendChild(mr_info_request);
|
---|
75 |
|
---|
76 | // process the message
|
---|
77 | Element mr_info_response = (Element) this.mr.process(mr_info_message);
|
---|
78 | // the response
|
---|
79 | Element service_response = (Element)GSXML.getChildByTagName(mr_info_response, GSXML.RESPONSE_ELEM);
|
---|
80 |
|
---|
81 | Element service_description = (Element)this.doc.importNode(GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM), true);
|
---|
82 | page_response.appendChild(service_description);
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (request_type.indexOf("r") == -1) {
|
---|
86 | // just a display request, no actual processing to do
|
---|
87 | return page_response;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // check that we have some service params
|
---|
91 | HashMap service_params = (HashMap)params.get("s1");
|
---|
92 | if (service_params == null) { // no query
|
---|
93 | return page_response;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // create the query request
|
---|
97 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
98 | Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
|
---|
99 | mr_query_message.appendChild(mr_query_request);
|
---|
100 |
|
---|
101 | Element query_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
102 | GSXML.addParametersToList(this.doc, query_param_list, service_params);
|
---|
103 | mr_query_request.appendChild(query_param_list);
|
---|
104 |
|
---|
105 | // also get the format stuff now if there is some
|
---|
106 | Element format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, lang, uid);
|
---|
107 | mr_query_message.appendChild(format_request);
|
---|
108 |
|
---|
109 | logger.info(GSXML.xmlNodeToString(mr_query_message));
|
---|
110 |
|
---|
111 | // do the query
|
---|
112 | Element mr_query_response = (Element)this.mr.process(mr_query_message);
|
---|
113 |
|
---|
114 | // check for errors
|
---|
115 | if (processErrorElements(mr_query_response, page_response)) {
|
---|
116 | return page_response;
|
---|
117 | }
|
---|
118 |
|
---|
119 | NodeList responses = mr_query_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
120 | Element query_response = (Element) responses.item(0);
|
---|
121 | Element format_response = (Element) responses.item(1);
|
---|
122 |
|
---|
123 | Element query_result_metadata_list = (Element) GSXML.getChildByTagName(query_response, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
124 | if (query_result_metadata_list == null) {
|
---|
125 | logger.error("No query result metadata.\n");
|
---|
126 | } else { // add it into the page response
|
---|
127 | page_response.appendChild(this.doc.importNode(query_result_metadata_list, true));
|
---|
128 | }
|
---|
129 |
|
---|
130 | Element query_term_info_list = (Element) GSXML.getChildByTagName(query_response, GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
131 | if (query_term_info_list == null) {
|
---|
132 | logger.error("No query term information.\n");
|
---|
133 | } else { // add it into the page response
|
---|
134 | page_response.appendChild(this.doc.importNode(query_term_info_list, true));
|
---|
135 | }
|
---|
136 |
|
---|
137 | // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
|
---|
138 | Element document_list = (Element)GSXML.getChildByTagName(query_response, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
139 | // documentList not present if no docs found
|
---|
140 | if (document_list == null) {
|
---|
141 | // add in a dummy doc node list - used by the display. need to think about this
|
---|
142 | page_response.appendChild(this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER));
|
---|
143 | return page_response;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // now we check to see if there is metadata already - some search services return predefined metadata. if there is some, don't do a metadata request
|
---|
147 | NodeList doc_metadata = document_list.getElementsByTagName(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
148 | if (doc_metadata.getLength()>0) {
|
---|
149 | logger.error("have already found metadata!");
|
---|
150 | // append the doc list to the result
|
---|
151 | page_response.appendChild(this.doc.importNode(document_list, true));
|
---|
152 | return page_response;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // get the metadata elements needed from the format statement if any
|
---|
156 | HashSet metadata_names = new HashSet();
|
---|
157 | metadata_names.add("Title");
|
---|
158 | // add in the format info to the stylesheet if there is any
|
---|
159 | Element format_elem = (Element)GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
160 | if (format_elem != null) {
|
---|
161 | // set the format type
|
---|
162 | format_elem.setAttribute(GSXML.TYPE_ATT, "search");
|
---|
163 | // for now just add to the response
|
---|
164 | page_response.appendChild(this.doc.importNode(format_elem, true));
|
---|
165 | extractMetadataNames(format_elem, metadata_names);
|
---|
166 | }
|
---|
167 |
|
---|
168 | // paging of the results is done here - we filter the list to remove unwanted entries before retrieving metadata
|
---|
169 | Element filtered_doc_list = filterDocList(params, service_params, document_list);
|
---|
170 |
|
---|
171 | // do the metadata request on the filtered list
|
---|
172 | Element mr_metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
173 | to = "DocumentMetadataRetrieve";
|
---|
174 | if (collection != null) {
|
---|
175 | to = GSPath.prependLink(to, collection);
|
---|
176 | }
|
---|
177 | Element mr_metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
|
---|
178 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
179 |
|
---|
180 | // just get all for now - the receptionist should perhaps pass in some
|
---|
181 | // metadata that it wants, and QueryAction should look through the format stuff to see if there is any other?
|
---|
182 |
|
---|
183 | Element dm_param_list = createMetadataParamList(metadata_names);
|
---|
184 |
|
---|
185 | mr_metadata_request.appendChild(dm_param_list);
|
---|
186 |
|
---|
187 | // add in the doc node list too
|
---|
188 | mr_metadata_request.appendChild(filtered_doc_list);
|
---|
189 |
|
---|
190 | Element mr_metadata_response = (Element) this.mr.process(mr_metadata_message);
|
---|
191 | // check for errors
|
---|
192 | processErrorElements(mr_metadata_response, page_response);
|
---|
193 |
|
---|
194 | Element metadata_response = (Element) GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
|
---|
195 |
|
---|
196 | Element query_result_document_list = (Element) GSXML.getChildByTagName(metadata_response, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
197 |
|
---|
198 | if (query_result_document_list != null) {
|
---|
199 | page_response.appendChild(this.doc.importNode(query_result_document_list, true));
|
---|
200 | }
|
---|
201 |
|
---|
202 | logger.debug("Query page:\n" + this.converter.getPrettyString(page_response));
|
---|
203 | return page_response;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** this filters out some of the doc results for result paging */
|
---|
207 | protected Element filterDocList(HashMap params, HashMap service_params, Element orig_doc_list) {
|
---|
208 |
|
---|
209 | // check the hits_per_page param - is it a service param??
|
---|
210 | String hits_pp = (String) service_params.get("hitsPerPage");
|
---|
211 | if (hits_pp == null) {
|
---|
212 | // the service is doing the paging, so we want to display all of the returned docs(???)
|
---|
213 | // return (Element)this.doc.importNode(orig_doc_list, true);
|
---|
214 | // try hitsPerPage in the globle param
|
---|
215 | hits_pp = (String)params.get("hitsPerPage");
|
---|
216 | }
|
---|
217 |
|
---|
218 | int hits = 20;
|
---|
219 | if (hits_pp != null && !hits_pp.equals("")) {
|
---|
220 | try {
|
---|
221 | hits = Integer.parseInt(hits_pp);
|
---|
222 | } catch (Exception e) {
|
---|
223 | hits=20;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (hits == -1) { // all
|
---|
228 | return (Element)this.doc.importNode(orig_doc_list, true);
|
---|
229 | }
|
---|
230 | NodeList result_docs = orig_doc_list.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
231 |
|
---|
232 | int num_docs = result_docs.getLength();
|
---|
233 | if (num_docs <= hits) {
|
---|
234 | // too few docs to do paging
|
---|
235 | return (Element)this.doc.importNode(orig_doc_list, true);
|
---|
236 | }
|
---|
237 |
|
---|
238 | // now we need our own doc list
|
---|
239 | Element result_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
240 |
|
---|
241 | String start_p = (String) service_params.get("startPage");
|
---|
242 | if(start_p == null){
|
---|
243 | start_p = (String)params.get("startPage");
|
---|
244 | }
|
---|
245 |
|
---|
246 | int start = 1;
|
---|
247 | if (start_p != null && !start_p.equals("")) {
|
---|
248 | try {
|
---|
249 | start = Integer.parseInt(start_p);
|
---|
250 | } catch (Exception e) {
|
---|
251 | start = 1;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | int start_from = (start-1)*hits;
|
---|
256 | int end_at = (start*hits)-1;
|
---|
257 |
|
---|
258 | if (start_from > num_docs) {
|
---|
259 | // something has gone wrong
|
---|
260 | return result_list;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (end_at > num_docs) {
|
---|
264 | end_at = num_docs-1;
|
---|
265 | }
|
---|
266 |
|
---|
267 | // now we finally have the docs numbers to use
|
---|
268 | for (int i=start_from; i<=end_at; i++) {
|
---|
269 | result_list.appendChild(this.doc.importNode(result_docs.item(i), true));
|
---|
270 | }
|
---|
271 |
|
---|
272 | return result_list;
|
---|
273 | }
|
---|
274 |
|
---|
275 | }
|
---|