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

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

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

  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 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 if (request_type.equals("d")) {
54 // display the query page
55 // the only info we need to return is the collection list cos the xslt does teh rest
56
57 Element coll_list = getCollectionList(lang);
58 page_response.appendChild(this.doc.importNode(coll_list, true));
59 return page_response;
60
61 }
62
63 // else we have a query
64 String service_name = (String)params.get(GSParams.SERVICE);
65 if (service_name == null || service_name.equals("")) {
66 service_name = "TextQuery";
67 }
68 String query_coll_list = (String)params.get(GSParams.COLLECTION);
69
70 if (query_coll_list == null || query_coll_list.equals("")) {
71 System.err.println("NoCollQueryAction Error: no collections were specified!");
72 Element coll_list = getCollectionList(lang);
73 page_response.appendChild(this.doc.importNode(coll_list, true));
74 return page_response;
75 }
76
77 // service paramList
78 HashMap service_params = (HashMap)params.get("s1");
79 if (service_params == null) { // no query
80 return page_response;
81 }
82 Element query_param_list = GSXML.createParameterList(this.doc, service_params);
83
84 // we will do a query for each coll
85 String [] colls = query_coll_list.split(",");
86
87 Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
88
89 for (int i=0; i< colls.length; i++) {
90 String to = GSPath.appendLink(colls[i], service_name);
91 Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang);
92 mr_query_message.appendChild(mr_query_request);
93 mr_query_request.appendChild(query_param_list.cloneNode(true));
94
95 }
96
97 // do the query
98 Element mr_query_response = (Element)this.mr.process(mr_query_message);
99
100
101 // get the Title metadata for each node - need Node title and root title
102 Element mr_meta_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
103 NodeList responses = mr_query_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
104 for (int j=0; j<responses.getLength(); j++) {
105 Element document_list = (Element)GSXML.getChildByTagName((Element)responses.item(j), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
106 if (document_list != null) {
107 String coll_name = extractCollName(((Element)responses.item(j)).getAttribute(GSXML.FROM_ATT));
108 String path = GSPath.appendLink(coll_name, "DocumentMetadataRetrieve");
109 Element mr_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, path, lang);
110 mr_meta_message.appendChild(mr_meta_request);
111 mr_meta_request.appendChild(this.doc.importNode(document_list, true));
112 // metadata params
113 Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
114 Element param = GSXML.createParameter(this.doc, "metadata", "Title");
115 param_list.appendChild(param);
116 param = GSXML.createParameter(this.doc, "metadata", "root:Title");
117 param_list.appendChild(param);
118 mr_meta_request.appendChild(param_list);
119
120 }
121 }
122
123 // do the request
124 Element mr_meta_response = (Element)this.mr.process(mr_meta_message);
125
126 // the result
127 Element result_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
128 page_response.appendChild(result_doc_list);
129
130 responses = mr_meta_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
131 for (int j=0; j<responses.getLength(); j++) {
132 Element document_list = (Element)GSXML.getChildByTagName((Element)responses.item(j), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
133 String coll_name = extractCollName(((Element)responses.item(j)).getAttribute(GSXML.FROM_ATT));
134
135 mergeDocLists(result_doc_list, document_list, coll_name );
136 }
137
138
139 return page_response;
140 }
141
142 protected String extractCollName(String path) {
143 return GSPath.removeLastLink(path);
144 }
145
146 protected void mergeDocLists(Element result_list, Element from_list, String collection) {
147
148 Document owner = result_list.getOwnerDocument();
149 Node child = from_list.getFirstChild();
150 while (child != null) {
151 ((Element)child).setAttribute("collection", collection);
152 result_list.appendChild(owner.importNode(child, true));
153 child = child.getNextSibling();
154 }
155
156 }
157
158
159 protected Element getCollectionList(String lang) {
160
161 // first, get the message router info
162 Element coll_list_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
163 Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang);
164 coll_list_message.appendChild(coll_list_request);
165 Element coll_list_response = (Element)this.mr.process(coll_list_message);
166 if (coll_list_response==null) {
167 System.err.println("NoCollQueryAction Error: couldn't query the message router!");
168 return null;
169 }
170
171 // second, get the display info for each collection
172 NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
173
174 Element coll_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
175 Element param = GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER);
176 coll_param_list.appendChild(param);
177 // we will send all the requests in a single message
178 Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
179 for (int i=0; i<colls.getLength(); i++) {
180 Element c = (Element)colls.item(i);
181 String name = c.getAttribute(GSXML.NAME_ATT);
182
183 Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, lang);
184 metadata_request.appendChild(coll_param_list.cloneNode(true));
185 metadata_message.appendChild(metadata_request);
186 }
187
188 Element metadata_response = (Element)this.mr.process(metadata_message);
189
190 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
191 // check that have same number of responses as collections
192 if (colls.getLength() != coll_responses.getLength()) {
193 System.err.println("NoCollQueryAction Error: didn't get a response for each collection - somethings gone wrong!");
194 // for now, dont use the metadata
195 } else {
196 for (int i=0; i<colls.getLength(); i++) {
197 Element c1 = (Element)colls.item(i);
198 Element c2 = (Element)coll_responses.item(i);
199 if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.FROM_ATT))) {
200 //add the collection data into the original response
201 GSXML.mergeElements(c1, (Element)GSXML.getChildByTagName(c2, GSXML.COLLECTION_ELEM));
202 } else {
203 System.err.println("NoCollQueryAction Error: response does not correspond to request!");
204 }
205
206 }
207 }
208
209 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
210 Element response = (Element) GSXML.getNodeByPath(coll_list_response, path);
211 return response;
212
213 }
214}
Note: See TracBrowser for help on using the repository browser.