source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/DefaultReceptionist.java@ 36642

Last change on this file since 36642 was 36642, checked in by kjdon, 19 months ago

adding pathList into all pages, if not there already. - want to have group info present for breadcrumb display

  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5import org.greenstone.gsdl3.service.CollectionGroups;
6// XML classes
7import org.w3c.dom.Node;
8import org.w3c.dom.NodeList;
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11
12// other java classes
13import java.io.File;
14import java.io.Serializable;
15import java.util.HashMap;
16import java.util.Enumeration;
17
18import org.apache.log4j.*;
19
20/** The default greenstone receptionist - needs some extra info for each page */
21public class DefaultReceptionist extends TransformingReceptionist
22{
23
24 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.DefaultReceptionist.class.getName());
25
26 // this gets set to the groupInfo service name if there is one.
27 protected String groupInfoServiceName = null;
28
29 /**
30 * add in the collection description to the page, then for each service, add
31 * in the service description
32 */
33 protected void addExtraInfo(Element page)
34 {
35 super.addExtraInfo(page);
36 // the document for the page
37 Document doc = page.getOwnerDocument();
38 // a new document to create messages to send to MR
39 Document msg_doc = XMLConverter.newDOM();
40 Element page_request = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
41 // if it is a system request, then we don't bother with this.
42 String action = page_request.getAttribute(GSXML.ACTION_ATT);
43 if (action.equals("s"))
44 {
45 logger.error("HACK: don't ask for coll info if system action");
46 return;
47 }
48 logger.debug("add extra info, page request=" + this.converter.getString(page_request));
49 // is a collection defined?
50 Element param_list = (Element) GSXML.getChildByTagName(page_request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
51 if (param_list == null)
52 { // must be the original home page
53 logger.debug(" no param list, assuming home page");
54 return;
55 }
56 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
57 String coll_name = (String)params.get(GSParams.COLLECTION);
58 if (coll_name == null)
59 {
60 logger.debug(" coll param is null, returning");
61 return;
62 }
63
64 UserContext userContext = new UserContext(page_request);
65
66 if (coll_name.equals(""))
67 {
68 coll_name = (String)params.get("p.c");
69 }
70
71 boolean get_service_description = false;
72 Element page_response = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
73
74 Element coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
75 if (coll_description == null)
76 {
77 // try cluster
78 coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.CLUSTER_ELEM);
79 }
80 if (coll_description == null)
81 {
82 logger.debug("getting the coll description");
83 // we dont have one yet - get it
84
85 Element coll_about_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
86 Element coll_about_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
87 coll_about_message.appendChild(coll_about_request);
88
89 Node coll_about_response_message = this.mr.process(coll_about_message);
90 Element coll_about_response = (Element) GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
91 if (coll_about_response == null)
92 {
93 return;
94 }
95 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
96 if (coll_description == null)
97 { // may be a cluster
98 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
99 }
100
101 if (coll_description == null)
102 {
103 logger.error(" no collection description, returning");
104 return;
105 }
106 // have found one, append it to the page response
107 coll_description = (Element) doc.importNode(coll_description, true);
108 page_response.appendChild(coll_description);
109 get_service_description = true;
110 }
111
112 // have we got group param, and pathList??
113 String group = (String)params.get(GSParams.GROUP);
114 if (group != null && !group.equals("")) {
115 // ...yes we do. add group path info if not already present
116 Element path_list = (Element) GSXML.getChildByTagName(page_response, GSXML.PATH_ELEM + GSXML.LIST_MODIFIER);
117 if (path_list == null) {
118 // lets get it now
119
120 Element group_info_response = getGroupInfo(group, userContext);
121 if (group_info_response != null) {
122 path_list = (Element) GSXML.getChildByTagName(group_info_response,GSXML.PATH_ELEM + GSXML.LIST_MODIFIER);
123 if (path_list != null) {
124 page_response.appendChild(doc.importNode(path_list, true));
125 }
126 }
127 }
128 }
129
130 // have got a coll description
131 // now get the dispay info for the services
132 Element service_list = (Element) GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
133 if (service_list == null)
134 {
135 logger.error(" no service list, returning");
136 // something weird has gone wrong
137 return;
138 }
139
140 NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
141 if (services.getLength() == 0)
142 {
143 logger.error("DefaultReceoptionist: no services found for colllection/cluster " + coll_name);
144 return;
145 }
146 // check one service for display items
147 if (!get_service_description)
148 {
149 // we dont know yet if we need to get these
150 int i = 1;
151 Element test_s = (Element) services.item(0);
152 while (i < services.getLength() && (test_s.getAttribute(GSXML.TYPE_ATT).equals(GSXML.SERVICE_TYPE_RETRIEVE) || test_s.getAttribute(GSXML.TYPE_ATT).equals(GSXML.SERVICE_TYPE_OAI)))
153 {
154 test_s = (Element) services.item(i);
155 i++;
156 }
157 if (i == services.getLength())
158 {
159 // we have only found retrieve or oai services, so dont need descripitons anyway
160 return;
161 }
162 if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) != null)
163 {
164 // have got descriptions already,
165 return;
166 }
167 }
168
169 // if get here, we need to get the service descriptions
170
171 // we will send all the requests in a single message
172 Element info_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
173 for (int i = 0; i < services.getLength(); i++)
174 {
175 Element c = (Element) services.item(i);
176 String name = c.getAttribute(GSXML.NAME_ATT);
177 String address = GSPath.appendLink(coll_name, name);
178 Element info_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, address, userContext);
179 //Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
180 //req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
181 //info_request.appendChild(req_param_list);
182 info_message.appendChild(info_request);
183
184 }
185
186 Element info_response = (Element) this.mr.process(info_message);
187
188 NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
189 // check that have same number of responses as collections
190 if (services.getLength() != service_responses.getLength())
191 {
192 logger.error(" didn't get a response for each service - somethings gone wrong!");
193 // for now, dont use the metadata
194 }
195 else
196 {
197 for (int i = 0; i < services.getLength(); i++)
198 {
199 Element c1 = (Element) services.item(i);
200 Element c2 = (Element) GSXML.getChildByTagName((Element) service_responses.item(i), GSXML.SERVICE_ELEM);
201 if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT)))
202 {
203 //add the service data into the original response
204 GSXML.mergeElements(c1, c2);
205 }
206 else
207 {
208 logger.debug(" response does not correspond to request!");
209 }
210
211 }
212 }
213 }
214
215 // copied from pageaction - put in a single place
216 /** sends a request to GroupCurrentContent service to get group info. null group will give top level info,
217 otherwise will just give info for specified group */
218 protected Element getGroupInfo(String group, UserContext userContext) {
219
220 if (groupInfoServiceName == null) {
221 // this is the first time we have come here, set this up
222
223 if (this.mr instanceof MessageRouter && ((MessageRouter)this.mr).pingModule(CollectionGroups.GROUP_CONTENT_SERVICE)) {
224 this.groupInfoServiceName = CollectionGroups.GROUP_CONTENT_SERVICE;
225 } else {
226 this.groupInfoServiceName = "NO_GROUPS";
227 }
228 }
229 if (this.groupInfoServiceName.equals ("NO_GROUPS")) {
230 return null;
231 }
232 // otherwise lets get the group info
233 Document doc = XMLConverter.newDOM();
234 // collections and groups list
235 Element group_info_message = doc.createElement(GSXML.MESSAGE_ELEM);
236 Element group_info_request = GSXML.createBasicRequest(doc, GSXML.TO_ATT, this.groupInfoServiceName, userContext);
237 group_info_message.appendChild(group_info_request);
238 if (group != null) {
239 Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
240 GSXML.addParameterToList(param_list, GSParams.GROUP, group);
241 group_info_request.appendChild(param_list);
242 }
243 // send off the request
244 Element group_info_response_message = (Element) this.mr.process(group_info_message);
245 Element group_info_response = (Element) GSXML.getChildByTagName(group_info_response_message, GSXML.RESPONSE_ELEM);
246 return group_info_response;
247 }
248
249}
Note: See TracBrowser for help on using the repository browser.