source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/BrowseAction.java@ 4980

Last change on this file since 4980 was 4696, checked in by kjdon, 21 years ago

now we set a type on the format element

  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 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.Document;
9import org.w3c.dom.Element;
10
11import java.util.HashMap;
12import java.util.Vector;
13import java.io.File;
14
15/** action for classifier browsing */
16public class BrowseAction extends Action {
17
18 public static final String CLASSIFIER_ARG = "cl";
19 public static final String SIBLING_ARG = "sib";
20
21 /** process the request */
22 public Element process (Element message) {
23
24 // get the request - assume only one
25 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
26
27 // the result
28 Element result = doc_.createElement(GSXML.MESSAGE_ELEM);
29 Element response = classifierBrowse(request);
30 result.appendChild(response);
31 return result;
32 }
33
34
35 protected Element classifierBrowse(Element request) {
36
37 Element page_response = doc_.createElement(GSXML.RESPONSE_ELEM);
38
39 // extract the params from the cgi-request, and check that we have a coll specified
40 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
41 HashMap params = GSXML.extractParams(cgi_paramList, false);
42
43 String service_name = (String)params.get(GSCGI.SERVICE_ARG);
44 String collection = (String)params.get(GSCGI.COLLECTION_ARG);
45 if (collection == null || collection.equals("")) {
46 System.err.println("BrowseAction Error:classifierBrowse, need to specify a collection!");
47 return page_response;
48
49 }
50
51 //whether to retrieve siblings or not
52 boolean get_siblings = false;
53 String sibs = (String) params.get(SIBLING_ARG);
54 if (sibs != null && sibs.equals("1")) {
55 get_siblings = true;
56 }
57
58 String lang = request.getAttribute(GSXML.LANG_ATT);
59 String to = GSPath.appendLink(collection, service_name);
60
61 // the first part of the response is the service description
62 // for now get this again from the service.
63 // this should be cached somehow later on.
64
65 Element info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
66 Element info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
67 info_message.appendChild(info_request);
68
69 // also get the format stuff now if there is some
70 Element format_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_FORMAT, to, lang);
71 info_message.appendChild(format_request);
72 // process the requests
73
74 Element info_response = (Element) mr_.process(info_message);
75
76 // the two responses
77 NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
78 Element service_response = (Element)responses.item(0);
79 Element format_response = (Element)responses.item(1);
80
81 Element service_description = (Element)GSXML.getChildByTagName(service_response, GSXML.SERVICE_ELEM);
82 page_response.appendChild(doc_.importNode(service_description, true));
83
84 // if rt=d, then we are just displaying the service
85 String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG);
86 if (request_type.equals("d")) {
87 //return the page that we have so far
88 return page_response;
89 }
90
91 // get the node that the user has clicked on
92 String classifier_node = (String)params.get(CLASSIFIER_ARG);
93
94 // if the node is not defined, return the page that we have so far
95 if (classifier_node ==null || classifier_node.equals("")) {
96 return page_response;
97 }
98
99 // the id of the classifier is the top id of the selected node
100 String top_id = OID.getTop(classifier_node);
101
102 // add the format info into the response
103 Element format_elem = (Element)GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
104 if (format_elem != null) {
105 // find the one for the classifier we are in
106 Element this_format = GSXML.getNamedElement(format_elem, GSXML.CLASSIFIER_ELEM, GSXML.NAME_ATT, top_id);
107 if (this_format != null) {
108 Element new_format = GSXML.duplicateWithNewName(doc_, this_format, GSXML.FORMAT_ELEM, false);
109 // set teh format type
110 new_format.setAttribute(GSXML.TYPE_ATT, "browse");
111
112 page_response.appendChild(new_format);
113 }
114 }
115
116 // get the browse structure for the selected node
117 Element classify_message = doc_.createElement(GSXML.MESSAGE_ELEM);
118 Element classify_request = doc_.createElement(GSXML.REQUEST_ELEM);
119 classify_message.appendChild(classify_request);
120
121 classify_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
122 classify_request.setAttribute(GSXML.TO_ATT, to);
123 classify_request.setAttribute(GSXML.LANG_ATT, lang);
124
125
126 //Create a parameter list to specify the required structure information
127 // for now, always get ancestors and children
128 Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
129 classify_request.appendChild(param_list);
130 Element param = doc_.createElement(GSXML.PARAM_ELEM);
131 param_list.appendChild(param);
132 param.setAttribute(GSXML.NAME_ATT, "structure");
133 param.setAttribute(GSXML.VALUE_ATT, "ancestors");
134 param = doc_.createElement(GSXML.PARAM_ELEM);
135 param_list.appendChild(param);
136 param.setAttribute(GSXML.NAME_ATT, "structure");
137 param.setAttribute(GSXML.VALUE_ATT, "children");
138 if (get_siblings) {
139 param = doc_.createElement(GSXML.PARAM_ELEM);
140 param_list.appendChild(param);
141 param.setAttribute(GSXML.NAME_ATT, "structure");
142 param.setAttribute(GSXML.VALUE_ATT, "siblings");
143 }
144
145 // put the classifier node into a classifier node list
146 Element classifier_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
147 Element classifier = doc_.createElement(GSXML.CLASS_NODE_ELEM);
148 classifier.setAttribute(GSXML.NODE_ID_ATT, classifier_node);
149 classifier_list.appendChild(classifier);
150 classify_request.appendChild(classifier_list);
151
152 // process the request
153 Element classify_response = (Element)mr_.process(classify_message);
154 // get the structure element
155 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
156 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
157 path = GSPath.appendLink(path, GSXML.NODE_STRUCTURE_ELEM);
158 // assume that we always get back the top level CL1 node - this becomes the page_classifier node
159 path = GSPath.appendLink(path, GSXML.CLASS_NODE_ELEM);
160 Element cl_structure = (Element)GSXML.getNodeByPath(classify_response,
161 path);
162 if (cl_structure ==null) {
163 System.err.println("BrowseAction: classifier structure request returned no structure");
164 return page_response;
165 }
166
167 // add the classifier node as the page classifier
168 Element page_classifier = GSXML.duplicateWithNewName(doc_, cl_structure, GSXML.CLASSIFIER_ELEM, true);
169 page_response.appendChild(page_classifier);
170 page_classifier.setAttribute(GSXML.NAME_ATT, top_id);
171
172 // get the metadata for each classifier node,
173 // then for each document node
174
175 Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
176
177 boolean did_classifier = false;
178 boolean did_documents = false;
179
180
181 // if there are classifier nodes
182 // create a metadata request for the classifier, and add it to
183 // the the message
184 NodeList cl_nodes = page_classifier.getElementsByTagName(GSXML.CLASS_NODE_ELEM);
185
186 if (cl_nodes.getLength() > 0) {
187 did_classifier = true;
188 Element cl_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
189 metadata_message.appendChild(cl_meta_request);
190 cl_meta_request.setAttribute(GSXML.TO_ATT, to+"MetadataRetrieve");
191 cl_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
192 cl_meta_request.setAttribute(GSXML.LANG_ATT, lang);
193
194 Element new_cl_nodes_list = doc_.createElement(GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
195 cl_meta_request.appendChild(new_cl_nodes_list);
196
197 for (int c=0; c<cl_nodes.getLength(); c++) {
198
199 Element cl = doc_.createElement(GSXML.CLASS_NODE_ELEM);
200 cl.setAttribute(GSXML.NODE_ID_ATT, ((Element)cl_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
201 new_cl_nodes_list.appendChild(cl);
202 }
203
204 // create and add in the param list - for now get all the metadata
205 // should be based on info sent in from the recept, and the
206 // format stuff
207 Element cl_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
208 Element p = doc_.createElement(GSXML.PARAM_ELEM);
209 cl_param_list.appendChild(p);
210 p.setAttribute(GSXML.NAME_ATT, "metadata");
211 p.setAttribute(GSXML.VALUE_ATT, "all");
212 cl_meta_request.appendChild(cl_param_list);
213
214 }
215
216 // if there are document nodes in the classification (happens
217 // sometimes), create a second request for document metadata and
218 // append to the message
219 NodeList doc_nodes = page_classifier.getElementsByTagName(GSXML.DOC_NODE_ELEM);
220 if (doc_nodes.getLength() > 0) {
221 did_documents = true;
222 Element doc_meta_request = doc_.createElement(GSXML.REQUEST_ELEM);
223 metadata_message.appendChild(doc_meta_request);
224 doc_meta_request.setAttribute(GSXML.TO_ATT, GSPath.appendLink(collection, "DocumentMetadataRetrieve"));
225 doc_meta_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
226 doc_meta_request.setAttribute(GSXML.LANG_ATT, lang);
227
228 Element doc_list = doc_.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
229 doc_meta_request.appendChild(doc_list);
230
231 for (int c=0; c<doc_nodes.getLength(); c++) {
232
233 Element d = doc_.createElement(GSXML.DOC_NODE_ELEM);
234 d.setAttribute(GSXML.NODE_ID_ATT, ((Element)doc_nodes.item(c)).getAttribute(GSXML.NODE_ID_ATT));
235 doc_list.appendChild(d);
236 }
237
238 // create and add in the param list - add all for now
239 Element doc_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
240 Element p = doc_.createElement(GSXML.PARAM_ELEM);
241 doc_param_list.appendChild(p);
242 p.setAttribute(GSXML.NAME_ATT, "metadata");
243 p.setAttribute(GSXML.VALUE_ATT, "all");
244 doc_meta_request.appendChild(doc_param_list);
245
246 }
247
248 // process the metadata requests
249 Element metadata_response = (Element)mr_.process(metadata_message);
250 if (did_classifier) {
251 // the classifier one will be the first response
252 // add the metadata lists for each node back into the
253 // page_classifier nodes
254 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
255 GSXML.CLASS_NODE_ELEM+GSXML.LIST_MODIFIER);
256 NodeList meta_response_cls = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
257 for (int i = 0; i < cl_nodes.getLength(); i++) {
258 GSXML.mergeMetadataLists(cl_nodes.item(i), meta_response_cls.item(i));
259 }
260 }
261 if (did_documents) {
262 NodeList meta_response_docs = null;
263 if (!did_classifier) {
264 // its the first response
265 path = GSPath.appendLink(GSXML.RESPONSE_ELEM,
266 GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
267 meta_response_docs = GSXML.getNodeByPath(metadata_response, path).getChildNodes();
268 } else { // its the second response
269 meta_response_docs = GSXML.getChildByTagName(metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1), GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER).getChildNodes();
270 }
271
272 for (int i = 0; i < doc_nodes.getLength(); i++) {
273 GSXML.mergeMetadataLists(doc_nodes.item(i), meta_response_docs.item(i));
274 }
275 }
276
277
278 System.out.println("(BrowseAction) Page:\n" + converter_.getPrettyString(page_response));
279 return page_response;
280 }
281
282 protected Element unknownBrowse(Element page, Element request, String browse_type) {
283 System.err.println("BrowseAction Error: unknown browse subtype: "+browse_type);
284 return null;
285 }
286}
287
288
Note: See TracBrowser for help on using the repository browser.