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

Last change on this file since 8714 was 6695, checked in by nzdl, 20 years ago

all request creation now done through GSXML.createBasicRequest - we can enforce certain atts this way

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