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 | /** 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(GSCGI.COLLECTION_ARG);
|
---|
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 | String path = GSXML.RESPONSE_ELEM;
|
---|
80 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
81 | Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
82 | Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
83 |
|
---|
84 | if (pl !=null) {
|
---|
85 | System.out.println("adding shortnames");
|
---|
86 | // add short names to the params in the param list
|
---|
87 | cgi_.paramListAddShortNames(pl);
|
---|
88 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
89 | Element param = (Element)pl.getFirstChild();
|
---|
90 | while (param !=null) {
|
---|
91 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
92 | if (param.getAttribute(GSXML.TYPE_ATT).equals(GSXML.PARAM_TYPE_MULTI)) {
|
---|
93 | // get the values for each sub param
|
---|
94 | NodeList subparams = param.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
95 | for (int i=0; i<subparams.getLength(); i++) {
|
---|
96 | String name = ((Element)subparams.item(i)).getAttribute(GSXML.NAME_ATT);
|
---|
97 | String current = (String)params.get(name);
|
---|
98 | if (current !=null && !current.equals("")) {
|
---|
99 | Element e = GSXML.createTextElement(pl.getOwnerDocument(), GSXML.DEFAULT_ELEM, current);
|
---|
100 | e.setAttribute(GSXML.NAME_ATT, name);
|
---|
101 | param.appendChild(e);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | } else {
|
---|
105 |
|
---|
106 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
107 | String current = (String)params.get(name);
|
---|
108 | if (current !=null && !current.equals("")) {
|
---|
109 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | param = (Element)param.getNextSibling();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | // part of the data for the description is the cgi-params
|
---|
118 | // if we have this here, do we need to do the previous step?
|
---|
119 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
120 | page.appendChild(cgi_request);
|
---|
121 |
|
---|
122 | page.appendChild(description);
|
---|
123 |
|
---|
124 | // now we always do the query - we have no way of knowing whether
|
---|
125 | // a query was specified or not cos we cant assume that q is the
|
---|
126 | // query string - maybe we could check that any required params have
|
---|
127 | // a value??
|
---|
128 |
|
---|
129 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
130 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
131 | mr_query_message.appendChild(mr_query_request);
|
---|
132 |
|
---|
133 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
134 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
135 | mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
136 | // paramList
|
---|
137 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
138 | mr_query_request.appendChild(query_param_list);
|
---|
139 |
|
---|
140 |
|
---|
141 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
142 |
|
---|
143 | // this result is the list of docs.
|
---|
144 | // now take that list, and get the Titles
|
---|
145 | // for now, just create a whole new request
|
---|
146 |
|
---|
147 | // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
|
---|
148 | path = GSXML.RESPONSE_ELEM;
|
---|
149 | path = GSPath.appendLink(path, GSXML.CONTENT_ELEM);
|
---|
150 | path = GSPath.appendLink(path, GSXML.DOCUMENT_ELEM+GSXML.LIST_MODIFIER);
|
---|
151 |
|
---|
152 | Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
|
---|
153 | path); // documentList not present if no docs found
|
---|
154 | if (document_list == null) {
|
---|
155 |
|
---|
156 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
157 |
|
---|
158 | page.appendChild(doc_.importNode(result_response, true));
|
---|
159 |
|
---|
160 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
161 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
162 | return (Element)transformer_.transform(style_doc, page);
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | // we have a doc list, so get the metadata - for now, get title.
|
---|
167 | // can we dynamically decide what metadata to get?
|
---|
168 | Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
169 | Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
170 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
171 |
|
---|
172 | mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
173 | mr_metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
174 | to = GSPath.appendLink(collection, "MetadataRetrieve");
|
---|
175 | mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
|
---|
176 |
|
---|
177 | Element meta_content = doc_.createElement(GSXML.CONTENT_ELEM);
|
---|
178 | mr_metadata_request.appendChild(meta_content);
|
---|
179 |
|
---|
180 | // the first part of the content is the doc list
|
---|
181 | meta_content.appendChild(doc_.importNode(document_list, true));
|
---|
182 |
|
---|
183 | // the second part of the content is the metadata list
|
---|
184 | Element metadata_list = doc_.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
185 | Element title = doc_.createElement(GSXML.METADATA_ELEM);
|
---|
186 | title.setAttribute(GSXML.NAME_ATT, "Title");
|
---|
187 | metadata_list.appendChild(title);
|
---|
188 | meta_content.appendChild(metadata_list);
|
---|
189 |
|
---|
190 | Element mr_metadata_response = (Element)mr_.process(mr_metadata_message);
|
---|
191 |
|
---|
192 | Element result_response = (Element)GSXML.getChildByTagName(mr_metadata_response, GSXML.RESPONSE_ELEM);
|
---|
193 |
|
---|
194 | page.appendChild(doc_.importNode(result_response, true));
|
---|
195 |
|
---|
196 |
|
---|
197 | // output the page
|
---|
198 | // process using the stylesheet
|
---|
199 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
200 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
201 | return (Element)transformer_.transform(style_doc, page);
|
---|
202 |
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | }
|
---|