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

Last change on this file since 3603 was 3603, checked in by kjdon, 21 years ago

query action now handles all queries with a generic method. the querystring is not treated differently from any other param

  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 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.NodeList;
8import org.w3c.dom.Text;
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11
12import java.util.HashMap;
13import java.util.Map;
14import java.util.Iterator;
15import java.io.File;
16
17/** action class for queries */
18public class QueryAction extends Action {
19
20 /** process - processes a request.
21 */
22 public Element process (Element message) {
23
24 // get the request - assume there is only one
25 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
26
27 // create the return page tree
28 Element page = doc_.createElement(GSXML.PAGE_ELEM);
29 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
30 // add the lang stuff from message
31 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
32 // add the system stuff from message
33 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
34
35 // query type is the subaction
36 String query_type = request.getAttribute(GSXML.SUBACTION_ATT) + "Query";
37
38 // for now assume all queries can be handled by basic query
39 return basicQuery(page, request, query_type);
40
41 }
42
43 /** a generic query handler
44 * this gets the service description, does the query (just passes all teh
45 * params to the service, then gets the titles for any results
46 */
47 protected Element basicQuery(Element page, Element request, String service_name) {
48
49 // check that the stylesheet is present - cant output the page without one.
50 String stylesheet = GSFile.stylesheetFile(config_, "basicquery.xsl");
51 if (stylesheet==null) {
52 System.err.println("QueryAction Error: basicquery stylesheet not found!");
53 return null;
54 }
55
56 // extract the params from the cgi-request, and check that we have a coll specified
57 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
58
59 HashMap params = GSXML.extractParams(cgi_param_list);
60
61 String collection = (String)params.get("collection");
62 if (collection == null || collection.equals("")) {
63 System.err.println("QueryAction Error: no collection was specified!");
64 return null;
65 }
66
67 // 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
68 Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
69 Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
70 mr_info_message.appendChild(mr_info_request);
71 mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
72 mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
73
74 String to = collection;
75 to = GSPath.appendLink(to, service_name);
76 mr_info_request.setAttribute(GSXML.TO_ATT, to);
77
78 Element mr_info_response = (Element) mr_.process(mr_info_message);
79 System.out.println("describe response from query=");
80 System.out.println(converter_.getString(mr_info_response));
81 String path = GSXML.RESPONSE_ELEM;
82 path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
83 Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
84 Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
85
86 if (pl !=null) {
87 System.out.println("adding shortnames");
88 // add short names to the params in the param list
89 cgi_.addShortNames(pl);
90 // for each param in the description, overwrite teh default value with the currently set value if present
91 Element param = (Element)pl.getFirstChild();
92 while (param !=null) {
93 if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
94 if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) {
95 // get the values for each sub param
96 NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM);
97 for (int i=0; i<subparams.getLength(); i++) {
98 String name = ((Element)subparams.item(i)).getAttribute(GSXML.NAME_ATT);
99 String current = (String)params.get(name);
100 if (current !=null && !current.equals("")) {
101 Element e = GSXML.createTextElement(pl.getOwnerDocument(), GSXML.DEFAULT_ELEM, current);
102 e.setAttribute(GSXML.NAME_ATT, name);
103 param.appendChild(e);
104 }
105 }
106 } else {
107
108 String name = param.getAttribute(GSXML.NAME_ATT);
109 String current = (String)params.get(name);
110 if (current !=null && !current.equals("")) {
111 param.setAttribute(GSXML.DEFAULT_ATT, current);
112 }
113 }
114 }
115 param = (Element)param.getNextSibling();
116 }
117 }
118 System.out.println(converter_.getString(pl));
119
120 // part of the data for the description is the cgi-params
121 // if we have this here, do we need to do the previous step?
122 Element cgi_request = (Element)doc_.importNode(request, true);
123 page.appendChild(cgi_request);
124
125 page.appendChild(description);
126
127 // now we always do the query - we have no way of knowing whether
128 // a query was specified or not cos we cant assume that q is the
129 // query string - maybe we could check that any required params have
130 // a value??
131
132 Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
133 Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
134 mr_query_message.appendChild(mr_query_request);
135
136 mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
137 mr_query_request.setAttribute(GSXML.TO_ATT, to);
138 mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
139 // paramList
140 Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
141 mr_query_request.appendChild(query_param_list);
142
143
144 Element mr_query_response = (Element)mr_.process(mr_query_message);
145
146 // this result is the list of docs.
147 // now take that list, and get the Titles
148 // for now, just create a whole new request
149
150 // check that there are some resources - for now check the list, but later should use a numdocs metadata elem
151 path = GSXML.RESPONSE_ELEM;
152 path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
153 path = GSPath.appendLink(path, GSXML.RESOURCE_ELEM+GSXML.LIST_MODIFIER);
154
155 Element resource_list = (Element)GSXML.getNodeByPath(mr_query_response,
156 path); // resourceList not present if no docs found
157 if (resource_list == null) {
158
159 Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
160
161 page.appendChild(doc_.importNode(result_response, true));
162
163 Document style_doc = converter_.getDOM(new File(stylesheet));
164 GSXSLT.absoluteIncludePaths(style_doc, config_);
165 return (Element)transformer_.transform(style_doc, page);
166
167 }
168
169 // we have a doc list, so get the metadata - for now, get title.
170 // can we dynamically decide what metadata to get?
171 Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
172 Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
173 mr_metadata_message.appendChild(mr_metadata_request);
174
175 mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
176 mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
177 to = GSPath.appendLink(collection, "MetadataRetrieve");
178 mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
179
180 Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
181 mr_metadata_request.appendChild(meta_content);
182
183 // the first part of the content is the doc list
184 meta_content.appendChild(doc_.importNode(resource_list, true));
185
186 // the second part of the content is the metadata list
187 Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
188 Element title = doc_.createElement(GSXML.METADATA_ELEM);
189 title.setAttribute(GSXML.NAME_ATT, "Title");
190 metadata_list.appendChild(title);
191 meta_content.appendChild(metadata_list);
192
193 Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
194
195 Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
196
197 page.appendChild(doc_.importNode(result_response, true));
198
199
200 // output the page
201 // process using the stylesheet
202 Document style_doc = converter_.getDOM(new File(stylesheet));
203 GSXSLT.absoluteIncludePaths(style_doc, config_);
204 return (Element)transformer_.transform(style_doc, page);
205
206 }
207
208
209}
Note: See TracBrowser for help on using the repository browser.