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.Vector;
|
---|
14 | import java.util.Map;
|
---|
15 | import java.util.Iterator;
|
---|
16 | import java.io.File;
|
---|
17 |
|
---|
18 | /** action class for queries */
|
---|
19 | public class QueryAction extends Action {
|
---|
20 |
|
---|
21 | /** process - processes a request.
|
---|
22 | */
|
---|
23 | public Element process (Element message) {
|
---|
24 |
|
---|
25 | // get the request - assume there is only one
|
---|
26 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
27 |
|
---|
28 | // create the return message
|
---|
29 | Element result = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
30 | // for now we only have one type of query - subaction not used
|
---|
31 | Element response = basicQuery(request);
|
---|
32 | result.appendChild(doc_.importNode(response, true));
|
---|
33 | return result;
|
---|
34 | }
|
---|
35 |
|
---|
36 | /** a generic query handler
|
---|
37 | * this gets the service description, does the query (just passes all teh
|
---|
38 | * params to the service, then gets the titles for any results
|
---|
39 | */
|
---|
40 | protected Element basicQuery(Element request) {
|
---|
41 |
|
---|
42 | // the result
|
---|
43 | Element page_response = doc_.createElement(GSXML.RESPONSE_ELEM);
|
---|
44 |
|
---|
45 | // extract the params from the cgi-request, and check that we have a coll specified
|
---|
46 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
47 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
48 |
|
---|
49 | String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG);
|
---|
50 | String service_name = (String)params.get(GSCGI.SERVICE_ARG);
|
---|
51 | String collection = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
52 |
|
---|
53 | if (collection == null || collection.equals("")) {
|
---|
54 | System.err.println("QueryAction Error: no collection was specified!");
|
---|
55 | return page_response; // an empty response
|
---|
56 | }
|
---|
57 |
|
---|
58 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
59 | String to = GSPath.appendLink(collection, service_name);
|
---|
60 |
|
---|
61 | // part of the response is the service description
|
---|
62 | // for now get this again from the service.
|
---|
63 | // this will probably need to be cached somehow later on.
|
---|
64 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
65 | Element mr_info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
|
---|
66 | mr_info_message.appendChild(mr_info_request);
|
---|
67 |
|
---|
68 | // also get the format stuff now if there is some
|
---|
69 | Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang);
|
---|
70 | mr_info_message.appendChild(format_request);
|
---|
71 |
|
---|
72 | // process the messages
|
---|
73 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
74 |
|
---|
75 | // the two responses
|
---|
76 | NodeList responses = mr_info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
77 | Element service_response = (Element)responses.item(0);
|
---|
78 | Element format_response = (Element)responses.item(1);
|
---|
79 |
|
---|
80 | Element service_description = (Element)doc_.importNode(GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM), true);
|
---|
81 | page_response.appendChild(service_description);
|
---|
82 |
|
---|
83 | if (request_type.equals("d")) {// just a display request
|
---|
84 | return page_response;
|
---|
85 | }
|
---|
86 |
|
---|
87 | // add in the format info to the stylesheet if there is any
|
---|
88 | Element format_elem = (Element)GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
89 | if (format_elem != null) {
|
---|
90 | System.out.println("QueryAction: found a format element, adding it to the page response");
|
---|
91 | // for now just add to the response
|
---|
92 | page_response.appendChild(doc_.importNode(format_elem, true));
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | // do the query
|
---|
97 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
98 | Element mr_query_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, lang);
|
---|
99 | mr_query_message.appendChild(mr_query_request);
|
---|
100 |
|
---|
101 | // paramList
|
---|
102 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
103 | mr_query_request.appendChild(query_param_list);
|
---|
104 |
|
---|
105 | // do the query
|
---|
106 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
107 |
|
---|
108 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
109 | Element query_result_metadata_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
|
---|
110 | if (query_result_metadata_list == null) {
|
---|
111 | System.err.println("QueryAction: Warning: No query result metadata.\n");
|
---|
112 | } else { // add it into the page response
|
---|
113 | page_response.appendChild(doc_.importNode(query_result_metadata_list, true));
|
---|
114 | }
|
---|
115 |
|
---|
116 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.TERM_ELEM+GSXML.LIST_MODIFIER);
|
---|
117 | Element query_term_info_list = (Element) GSXML.getNodeByPath(mr_query_response, path);
|
---|
118 | if (query_term_info_list == null) {
|
---|
119 | System.err.println("QueryAction: Warning: No query term information.\n");
|
---|
120 | } else { // add it into the page response
|
---|
121 | page_response.appendChild(doc_.importNode(query_term_info_list, true));
|
---|
122 | }
|
---|
123 |
|
---|
124 | // check that there are some documents - for now check the list, but later should use a numdocs metadata elem
|
---|
125 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
126 |
|
---|
127 | Element document_list = (Element)GSXML.getNodeByPath(mr_query_response,
|
---|
128 | path);
|
---|
129 | // documentList not present if no docs found
|
---|
130 | if (document_list == null) {
|
---|
131 | return page_response;
|
---|
132 | }
|
---|
133 |
|
---|
134 | // do the metadata request
|
---|
135 | Element mr_metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
136 | Element mr_metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
137 | mr_metadata_message.appendChild(mr_metadata_request);
|
---|
138 |
|
---|
139 | mr_metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
140 | mr_metadata_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
141 | to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired?
|
---|
142 | mr_metadata_request.setAttribute(GSXML.TO_ATT, to);
|
---|
143 |
|
---|
144 | // just get all for now - the receptionist should perhaps pass in some
|
---|
145 | // metadata that it wants, and QueryAction should look through the format stuff to see if there is any other?
|
---|
146 | Element dm_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
147 | Element param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
148 | dm_param_list.appendChild(param);
|
---|
149 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
150 | param.setAttribute(GSXML.VALUE_ATT, "all");
|
---|
151 | mr_metadata_request.appendChild(dm_param_list);
|
---|
152 |
|
---|
153 |
|
---|
154 | // add in the doc node list too
|
---|
155 | mr_metadata_request.appendChild(doc_.importNode(document_list, true));
|
---|
156 | Element mr_metadata_response = (Element) mr_.process(mr_metadata_message);
|
---|
157 |
|
---|
158 | path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
159 | Element query_result_document_list = (Element) GSXML.getNodeByPath(mr_metadata_response, path);
|
---|
160 |
|
---|
161 | if (query_result_document_list != null) {
|
---|
162 | page_response.appendChild(doc_.importNode(query_result_document_list, true));
|
---|
163 | }
|
---|
164 |
|
---|
165 | // System.out.println("Query page:\n" + converter_.getPrettyString(page_response));
|
---|
166 | return page_response;
|
---|
167 | }
|
---|
168 | }
|
---|