source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/NoCollQueryAction.java@ 8714

Last change on this file since 8714 was 8677, checked in by kjdon, 20 years ago

SXML.createParameterList changed to addParametersToList

  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 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.HashSet;
14import java.util.Vector;
15import java.util.Map;
16import java.util.Iterator;
17import java.io.File;
18
19/** action class for queries
20 * this is used when querying isn't collection specific, but it occurs across all collections in the site. The service description is assumed to be known by the xslt so we dont ask for it. we just pass all the service params to the TextQuery service of all the collections */
21public class NoCollQueryAction extends Action {
22
23 /** process - processes a request.
24 */
25 public Element process (Element message) {
26
27 // get the request - assume there is only one
28 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
29
30 // create the return message
31 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
32 // for now we only have one type of query - subaction not used
33 Element response = basicQuery(request);
34 result.appendChild(this.doc.importNode(response, true));
35 return result;
36 }
37
38 /** a generic query handler
39 * this gets the service description, does the query (just passes all teh
40 * params to the service, then gets the titles for any results
41 */
42 protected Element basicQuery(Element request) {
43
44 // the result
45 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
46
47 // extract the params from the cgi-request, and check that we have a coll specified
48 Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
49 HashMap params = GSXML.extractParams(cgi_param_list, false);
50
51 String request_type = (String)params.get(GSParams.REQUEST_TYPE);
52 String lang = request.getAttribute(GSXML.LANG_ATT);
53 String uid = request.getAttribute(GSXML.USER_ID_ATT);
54 if (request_type.equals("d")) {
55 // display the query page
56 // the only info we need to return is the collection list cos the xslt does teh rest
57
58 Element coll_list = getCollectionList(lang, uid);
59 page_response.appendChild(this.doc.importNode(coll_list, true));
60 return page_response;
61
62 }
63
64 // else we have a query
65 String service_name = (String)params.get(GSParams.SERVICE);
66 if (service_name == null || service_name.equals("")) {
67 service_name = "TextQuery";
68 }
69 String query_coll_list = (String)params.get(GSParams.COLLECTION);
70
71 if (query_coll_list == null || query_coll_list.equals("")) {
72 System.err.println("NoCollQueryAction Error: no collections were specified!");
73 Element coll_list = getCollectionList(lang, uid);
74 page_response.appendChild(this.doc.importNode(coll_list, true));
75 return page_response;
76 }
77
78 // service paramList
79 HashMap service_params = (HashMap)params.get("s1");
80 if (service_params == null) { // no query
81 return page_response;
82 }
83 Element query_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
84 GSXML.addParametersToList(this.doc, query_param_list, service_params);
85
86 // we will do a query for each coll
87 String [] colls = query_coll_list.split(",");
88
89 Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
90
91 for (int i=0; i< colls.length; i++) {
92 String to = GSPath.appendLink(colls[i], service_name);
93 Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
94 mr_query_message.appendChild(mr_query_request);
95 mr_query_request.appendChild(query_param_list.cloneNode(true));
96
97 }
98
99 // do the query
100 Element mr_query_response = (Element)this.mr.process(mr_query_message);
101
102
103 // get the Title metadata for each node - need Node title and root title
104 Element mr_meta_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
105 NodeList responses = mr_query_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
106 for (int j=0; j<responses.getLength(); j++) {
107 Element document_list = (Element)GSXML.getChildByTagName((Element)responses.item(j), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
108 if (document_list != null) {
109 String coll_name = extractCollName(((Element)responses.item(j)).getAttribute(GSXML.FROM_ATT));
110 String path = GSPath.appendLink(coll_name, "DocumentMetadataRetrieve");
111 Element mr_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, path, lang, uid);
112 mr_meta_message.appendChild(mr_meta_request);
113 mr_meta_request.appendChild(this.doc.importNode(document_list, true));
114 // metadata params
115 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
116 Element param = GSXML.createParameter(this.doc, "metadata", "Title");
117 param_list.appendChild(param);
118 param = GSXML.createParameter(this.doc, "metadata", "root_Title");
119 param_list.appendChild(param);
120 mr_meta_request.appendChild(param_list);
121
122 }
123 }
124
125 // do the request
126 Element mr_meta_response = (Element)this.mr.process(mr_meta_message);
127
128 // the result
129 Element result_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
130 page_response.appendChild(result_doc_list);
131
132 responses = mr_meta_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
133 for (int j=0; j<responses.getLength(); j++) {
134 Element document_list = (Element)GSXML.getChildByTagName((Element)responses.item(j), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
135 String coll_name = extractCollName(((Element)responses.item(j)).getAttribute(GSXML.FROM_ATT));
136
137 mergeDocLists(result_doc_list, document_list, coll_name );
138 }
139
140
141 return page_response;
142 }
143
144 protected String extractCollName(String path) {
145 return GSPath.removeLastLink(path);
146 }
147
148 protected void mergeDocLists(Element result_list, Element from_list, String collection) {
149
150 Document owner = result_list.getOwnerDocument();
151 Node child = from_list.getFirstChild();
152 while (child != null) {
153 ((Element)child).setAttribute("collection", collection);
154 result_list.appendChild(owner.importNode(child, true));
155 child = child.getNextSibling();
156 }
157
158 }
159
160
161 protected Element getCollectionList(String lang, String uid) {
162
163 // first, get the message router info
164 Element coll_list_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
165 Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
166 coll_list_message.appendChild(coll_list_request);
167 Element coll_list_response = (Element)this.mr.process(coll_list_message);
168 if (coll_list_response==null) {
169 System.err.println("NoCollQueryAction Error: couldn't query the message router!");
170 return null;
171 }
172
173 // second, get the display info for each collection
174 NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
175
176 Element coll_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
177 Element param = GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
178 coll_param_list.appendChild(param);
179 // we will send all the requests in a single message
180 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
181 for (int i=0; i<colls.getLength(); i++) {
182 Element c = (Element)colls.item(i);
183 String name = c.getAttribute(GSXML.NAME_ATT);
184
185 Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, lang, uid);
186 metadata_request.appendChild(coll_param_list.cloneNode(true));
187 metadata_message.appendChild(metadata_request);
188 }
189
190 Element metadata_response = (Element)this.mr.process(metadata_message);
191
192 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
193 // check that have same number of responses as collections
194 if (colls.getLength() != coll_responses.getLength()) {
195 System.err.println("NoCollQueryAction Error: didn't get a response for each collection - somethings gone wrong!");
196 // for now, dont use the metadata
197 } else {
198 for (int i=0; i<colls.getLength(); i++) {
199 Element c1 = (Element)colls.item(i);
200 Element c2 = (Element)coll_responses.item(i);
201 if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.FROM_ATT))) {
202 //add the collection data into the original response
203 GSXML.mergeElements(c1, (Element)GSXML.getChildByTagName(c2, GSXML.COLLECTION_ELEM));
204 } else {
205 System.err.println("NoCollQueryAction Error: response does not correspond to request!");
206 }
207
208 }
209 }
210
211 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
212 Element response = (Element) GSXML.getNodeByPath(coll_list_response, path);
213 return response;
214
215 }
216}
Note: See TracBrowser for help on using the repository browser.