1 | /*
|
---|
2 | * CrossCollectionSearch.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 | import org.greenstone.gsdl3.util.GSXML;
|
---|
22 | import org.greenstone.gsdl3.util.GSPath;
|
---|
23 |
|
---|
24 | import org.w3c.dom.Element;
|
---|
25 | import org.w3c.dom.NodeList;
|
---|
26 |
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Map;
|
---|
29 | import java.util.Iterator;
|
---|
30 | import java.util.Set;
|
---|
31 | import java.util.HashMap;
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | *
|
---|
36 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
37 | */
|
---|
38 |
|
---|
39 | public class CrossCollectionSearch
|
---|
40 | extends ServiceRack {
|
---|
41 |
|
---|
42 | protected static final String QUERY_PARAM = "query";
|
---|
43 | protected static final String COLLECTION_PARAM = "collection";
|
---|
44 |
|
---|
45 | // the services on offer - these proxy the actual collection ones
|
---|
46 | protected static final String TEXT_QUERY_SERVICE = "TextQuery";
|
---|
47 | protected static final String ADV_QUERY_SERVICE = "AdvTextQuery";
|
---|
48 | protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
|
---|
49 |
|
---|
50 | protected String [] coll_ids_list = null;
|
---|
51 | protected String [] coll_names_list = null;
|
---|
52 |
|
---|
53 |
|
---|
54 | /** constructor */
|
---|
55 | public CrossCollectionSearch()
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | public boolean configure(Element info, Element extra_info)
|
---|
60 | {
|
---|
61 | // any parameters? colls to include??
|
---|
62 | System.out.println("Configuring CrossCollectionSearch...");
|
---|
63 | // query service
|
---|
64 | Element ccs_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
65 | ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
66 | ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
67 | this.short_service_info.appendChild(ccs_service);
|
---|
68 |
|
---|
69 | // metadata service
|
---|
70 | Element dmr_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
71 | dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
72 | dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
|
---|
73 | this.short_service_info.appendChild(dmr_service);
|
---|
74 |
|
---|
75 | // add in the default format statement
|
---|
76 | String format_info = "<format xmlns:gsf='http://www.greenstone.org/greenstone3/schema/ConfigFormat' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><gsf:template match='documentNode'><td><a><xsl:attribute name='href'>?a=d&c=<xsl:value-of select='@collection'/>&d=<xsl:value-of select='@nodeID'/><xsl:if test=\"@nodeType='leaf'\">&sib=1</xsl:if>&dt=<xsl:value-of select='@docType'/>&p.a=q&&p.s="+TEXT_QUERY_SERVICE+"&p.c=";
|
---|
77 | if (this.cluster_name!=null) {
|
---|
78 | format_info += this.cluster_name;
|
---|
79 | }
|
---|
80 | format_info += "</xsl:attribute><gsf:icon/></a></td><td><gsf:metadata name='Title'/> (<xsl:value-of select='@collection'/>, rank:<xsl:value-of select='@rank'/>) </td></gsf:template></format>";
|
---|
81 | this.format_info_map.put(TEXT_QUERY_SERVICE, this.doc.importNode(this.converter.getDOM(format_info).getDocumentElement(), true));
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected Element getServiceDescription(String service, String lang, String subset)
|
---|
86 | {
|
---|
87 | if (service.equals(TEXT_QUERY_SERVICE)) {
|
---|
88 |
|
---|
89 | Element ccs_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
90 | ccs_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
|
---|
91 | ccs_service.setAttribute(GSXML.NAME_ATT, TEXT_QUERY_SERVICE);
|
---|
92 |
|
---|
93 | // display info
|
---|
94 | if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
95 | ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(TEXT_QUERY_SERVICE+".name", lang)));
|
---|
96 | ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_SUBMIT, getTextString(TEXT_QUERY_SERVICE+".submit", lang)));
|
---|
97 | ccs_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION, getTextString(TEXT_QUERY_SERVICE+".description", lang)));
|
---|
98 | }
|
---|
99 | // param info
|
---|
100 | if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
101 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
102 | // collection list
|
---|
103 | if (coll_ids_list ==null ) {
|
---|
104 | initCollectionList();
|
---|
105 | }
|
---|
106 | Element param = GSXML.createParameterDescription(this.doc, COLLECTION_PARAM, getTextString("param."+COLLECTION_PARAM, lang), GSXML.PARAM_TYPE_ENUM_MULTI, null, coll_ids_list, coll_names_list);
|
---|
107 | param_list.appendChild(param);
|
---|
108 | // query param
|
---|
109 | param = GSXML.createParameterDescription(this.doc, QUERY_PARAM, getTextString("param."+QUERY_PARAM, lang), GSXML.PARAM_TYPE_STRING, null, null, null);
|
---|
110 | param_list.appendChild(param);
|
---|
111 | ccs_service.appendChild(param_list);
|
---|
112 | }
|
---|
113 |
|
---|
114 | ///ystem.err.println("service description="+this.converter.getPrettyString(ccs_service));
|
---|
115 | return ccs_service;
|
---|
116 | }
|
---|
117 | // these ones are probably never called, but put them here just in case
|
---|
118 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
119 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
120 | service_elem.setAttribute(GSXML.NAME_ATT, service);
|
---|
121 | return service_elem;
|
---|
122 |
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | protected Element processTextQuery(Element request) {
|
---|
128 | // Create a new (empty) result message
|
---|
129 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
130 | result.setAttribute(GSXML.FROM_ATT, TEXT_QUERY_SERVICE);
|
---|
131 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
132 |
|
---|
133 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
134 | // Get the parameters of the request
|
---|
135 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
136 | if (param_list == null) {
|
---|
137 | System.err.println("Error: TextQuery request had no paramList.");
|
---|
138 | return result; // Return the empty result
|
---|
139 | }
|
---|
140 |
|
---|
141 | // get the collection list
|
---|
142 | Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, COLLECTION_PARAM);
|
---|
143 | String coll_list = GSXML.getValue(coll_param);
|
---|
144 |
|
---|
145 | String [] colls = coll_list.split(",");
|
---|
146 | Element query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
147 | // we are sending the same request to each collection - build up the to
|
---|
148 | // attribute for the request
|
---|
149 | StringBuffer to_att = new StringBuffer();
|
---|
150 | for (int i=0; i<colls.length; i++) {
|
---|
151 | if (i>0) {
|
---|
152 | to_att.append(",");
|
---|
153 | }
|
---|
154 | to_att.append(GSPath.appendLink(colls[i], "TextQuery"));
|
---|
155 |
|
---|
156 | }
|
---|
157 | // send the query to all colls
|
---|
158 | Element query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to_att.toString(), lang, "");
|
---|
159 | query_message.appendChild(query_request);
|
---|
160 | // should we add params individually?
|
---|
161 | Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
162 | query_request.appendChild(new_param_list);
|
---|
163 | new_param_list.appendChild(this.doc.importNode(GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, QUERY_PARAM), true));
|
---|
164 | Element query_result = this.router.process(query_message);
|
---|
165 |
|
---|
166 | // gather up the data from each response
|
---|
167 | int numDocsMatched = 0;
|
---|
168 | int numDocsReturned = 0;
|
---|
169 |
|
---|
170 | //term info??
|
---|
171 |
|
---|
172 | NodeList metadata = query_result.getElementsByTagName(GSXML.METADATA_ELEM);
|
---|
173 | for (int j=0; j<metadata.getLength(); j++) {
|
---|
174 | Element meta = (Element)metadata.item(j);
|
---|
175 | if (meta.getAttribute(GSXML.NAME_ATT).equals("numDocsReturned")) {
|
---|
176 | numDocsReturned += Integer.parseInt(GSXML.getValue(meta));
|
---|
177 | } else if (meta.getAttribute(GSXML.NAME_ATT).equals("numDocsMatched")) {
|
---|
178 | numDocsMatched += Integer.parseInt(GSXML.getValue(meta));
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | Element metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
183 | result.appendChild(metadata_list);
|
---|
184 | GSXML.addMetadata(this.doc, metadata_list, "numDocsReturned", ""+numDocsReturned);
|
---|
185 | //GSXML.addMetadata(this.doc, metadata_list, "numDocsMatched", ""+numDocsMatched);
|
---|
186 |
|
---|
187 | Element doc_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
188 | result.appendChild(doc_node_list);
|
---|
189 |
|
---|
190 | NodeList responses = query_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
191 |
|
---|
192 | for (int k=0; k<responses.getLength(); k++) {
|
---|
193 | String coll_name = GSPath.getFirstLink(((Element)responses.item(k)).getAttribute(GSXML.FROM_ATT));
|
---|
194 | NodeList nodes = ((Element)responses.item(k)).getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
195 | if (nodes==null || nodes.getLength()==0) continue;
|
---|
196 | Element last_node = null;
|
---|
197 | Element this_node = null;
|
---|
198 | for (int n=0; n<nodes.getLength(); n++) {
|
---|
199 | this_node = (Element)nodes.item(n);
|
---|
200 | this_node.setAttribute("collection", coll_name);
|
---|
201 | if (k==0) {
|
---|
202 |
|
---|
203 | doc_node_list.appendChild(this.doc.importNode(this_node, true));
|
---|
204 | } else {
|
---|
205 | if (last_node==null) {
|
---|
206 | last_node = (Element)GSXML.getChildByTagName(doc_node_list, GSXML.DOC_NODE_ELEM);
|
---|
207 | }
|
---|
208 | last_node = GSXML.insertIntoOrderedList(doc_node_list, GSXML.DOC_NODE_ELEM, last_node, this_node, "rank", true);
|
---|
209 | }
|
---|
210 |
|
---|
211 | }
|
---|
212 | }
|
---|
213 | return result;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // protected Element processAdvTextQuery(Element request)
|
---|
217 | // {
|
---|
218 |
|
---|
219 | // }
|
---|
220 | protected boolean initCollectionList() {
|
---|
221 | String lang="en";
|
---|
222 | String uid = "";
|
---|
223 | // first, get the message router info
|
---|
224 | Element coll_list_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
225 | Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, ""); // uid
|
---|
226 | coll_list_message.appendChild(coll_list_request);
|
---|
227 | ///ystem.err.println("coll list request = "+this.converter.getPrettyString(coll_list_request));
|
---|
228 | Element coll_list_response = (Element)this.router.process(coll_list_message);
|
---|
229 | if (coll_list_response==null) {
|
---|
230 | System.err.println("PageAction Error: couldn't query the message router!");
|
---|
231 | return false;
|
---|
232 | }
|
---|
233 | ///ystem.err.println("coll list response = "+this.converter.getPrettyString(coll_list_response));
|
---|
234 | // second, get some info from each collection. we want the coll name
|
---|
235 | // and whether its got a text query service
|
---|
236 |
|
---|
237 | NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
238 | // we will send all the requests in a single message
|
---|
239 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
240 | for (int i=0; i<colls.getLength(); i++) {
|
---|
241 | Element c = (Element)colls.item(i);
|
---|
242 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
243 | Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, name, lang, uid);
|
---|
244 | metadata_message.appendChild(metadata_request);
|
---|
245 | }
|
---|
246 | ///ystem.err.println("metadata request = "+this.converter.getPrettyString(metadata_message));
|
---|
247 | Element metadata_response = (Element)this.router.process(metadata_message);
|
---|
248 | ///ystem.err.println("metadata response = "+this.converter.getPrettyString(metadata_response));
|
---|
249 |
|
---|
250 | NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
251 | ArrayList valid_colls = new ArrayList();
|
---|
252 | ArrayList valid_coll_names = new ArrayList();
|
---|
253 | for (int i=0; i<coll_responses.getLength(); i++) {
|
---|
254 | Element coll = (Element)coll_responses.item(i);
|
---|
255 | Element service_list = (Element)GSXML.getChildByTagName(coll, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
|
---|
256 | if (service_list==null) continue;
|
---|
257 | Element query_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, TEXT_QUERY_SERVICE); // should be AbstractSearch.TEXT_QUERY_SERVICE
|
---|
258 | if (query_service == null) continue;
|
---|
259 | String coll_id = coll.getAttribute(GSXML.NAME_ATT);
|
---|
260 | String coll_name = coll_id+": "+GSXML.getDisplayText(coll, GSXML.DISPLAY_TEXT_NAME, "en", "en"); // just use english for now until we do some caching or something
|
---|
261 | valid_colls.add(coll_id);
|
---|
262 | valid_coll_names.add(coll_name);
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (valid_colls.size()==0) {
|
---|
266 | return false;
|
---|
267 | }
|
---|
268 | this.coll_ids_list = new String[1];
|
---|
269 | this.coll_names_list = new String[1];
|
---|
270 | this.coll_ids_list = (String []) valid_colls.toArray(coll_ids_list);
|
---|
271 | this.coll_names_list = (String []) valid_coll_names.toArray(coll_names_list);
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | protected Element processDocumentMetadataRetrieve(Element request) {
|
---|
276 | // Create a new (empty) result message
|
---|
277 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
278 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
|
---|
279 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
280 |
|
---|
281 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
282 | // Get the parameters of the request
|
---|
283 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
284 | if (param_list == null) {
|
---|
285 | System.err.println("Error: DocumentMetadataRetrieve request had no paramList.");
|
---|
286 | return result; // Return the empty result
|
---|
287 | }
|
---|
288 |
|
---|
289 | NodeList query_doc_list = request.getElementsByTagName(GSXML.DOC_NODE_ELEM);
|
---|
290 | if (query_doc_list.getLength()==0) {
|
---|
291 | System.err.println("Error: DocumentMetadataRetrieve request had no documentNodes.");
|
---|
292 | return result; // Return the empty result
|
---|
293 | }
|
---|
294 |
|
---|
295 | // the resulting doc node list
|
---|
296 | Element result_node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
297 | result.appendChild(result_node_list);
|
---|
298 |
|
---|
299 | // get all the metadata params
|
---|
300 | Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
301 | Element param = GSXML.createParameter(this.doc, "metadata", "Title");
|
---|
302 | new_param_list.appendChild(param);
|
---|
303 |
|
---|
304 | // organise the nodes into collection lists
|
---|
305 | HashMap coll_map = new HashMap();
|
---|
306 |
|
---|
307 | for (int i=0; i<query_doc_list.getLength(); i++) {
|
---|
308 | Element doc_node = (Element)query_doc_list.item(i);
|
---|
309 | String coll_name = doc_node.getAttribute("collection");
|
---|
310 | Element coll_items = (Element)coll_map.get(coll_name);
|
---|
311 | if (coll_items==null) {
|
---|
312 | coll_items=this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
313 | coll_map.put(coll_name, coll_items);
|
---|
314 | }
|
---|
315 | coll_items.appendChild(this.doc.importNode(doc_node, true));
|
---|
316 | }
|
---|
317 |
|
---|
318 | // create teh individual requests
|
---|
319 | Element meta_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
320 | Set mapping_set = coll_map.entrySet();
|
---|
321 | Iterator iter = mapping_set.iterator();
|
---|
322 |
|
---|
323 | while (iter.hasNext()) {
|
---|
324 | Map.Entry e = (Map.Entry)iter.next();
|
---|
325 | String cname = (String)e.getKey();
|
---|
326 | Element doc_nodes = (Element)e.getValue();
|
---|
327 | Element meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, GSPath.appendLink(cname, DOCUMENT_METADATA_RETRIEVE_SERVICE), lang, "");
|
---|
328 | meta_request.appendChild(doc_nodes);
|
---|
329 | meta_request.appendChild(new_param_list.cloneNode(true));
|
---|
330 | meta_request_message.appendChild(meta_request);
|
---|
331 |
|
---|
332 | }
|
---|
333 | Element meta_result = this.router.process(meta_request_message);
|
---|
334 |
|
---|
335 | // now need to put the doc nodes back in the right order
|
---|
336 | // go through the original list again. keep an element pointer to
|
---|
337 | // the next element in each collections list
|
---|
338 | NodeList meta_responses = meta_result.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
339 | for (int i=0; i<meta_responses.getLength(); i++) {
|
---|
340 | String collname = GSPath.getFirstLink(((Element)meta_responses.item(i)).getAttribute(GSXML.FROM_ATT));
|
---|
341 | Element first_elem = (Element)GSXML.getNodeByPath(meta_responses.item(i), "documentNodeList/documentNode");
|
---|
342 | coll_map.put(collname, first_elem);
|
---|
343 | }
|
---|
344 |
|
---|
345 | for (int i=0; i<query_doc_list.getLength(); i++) {
|
---|
346 | Element doc_node = (Element)query_doc_list.item(i);
|
---|
347 | Element new_node = (Element)this.doc.importNode(doc_node, false);
|
---|
348 | result_node_list.appendChild(new_node);
|
---|
349 | String coll_name = doc_node.getAttribute("collection");
|
---|
350 |
|
---|
351 | Element meta_elem = (Element)coll_map.get(coll_name);
|
---|
352 | GSXML.mergeMetadataLists(new_node, meta_elem);
|
---|
353 | coll_map.put(coll_name, meta_elem.getNextSibling());
|
---|
354 | }
|
---|
355 | return result;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|