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

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

GSXML.createBasicRequest now expects a user id

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