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

Last change on this file since 26910 was 24993, checked in by sjm84, 12 years ago

Adding UserContext to replace the use of lang and uid

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
14import java.util.Enumeration;
15
16import org.apache.log4j.*;
17
18/** The default greenstone receptionist - needs some extra info for each page */
19public class DefaultReceptionist extends TransformingReceptionist
20{
21
22 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.DefaultReceptionist.class.getName());
23
24 /**
25 * add in the collection description to the page, then for each service, add
26 * in the service description
27 */
28 protected void addExtraInfo(Element page)
29 {
30 super.addExtraInfo(page);
31
32 Element page_request = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
33 // if it is a system request, then we don't bother with this.
34 String action = page_request.getAttribute(GSXML.ACTION_ATT);
35 if (action.equals("s"))
36 {
37 logger.error("HACK: don't ask for coll info if system action");
38 return;
39 }
40 logger.debug("add extra info, page request=" + this.converter.getString(page_request));
41 // is a collection defined?
42 Element param_list = (Element) GSXML.getChildByTagName(page_request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
43 if (param_list == null)
44 { // must be the original home page
45 logger.debug(" no param list, assuming home page");
46 return;
47 }
48 Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSParams.COLLECTION);
49 if (coll_param == null)
50 {
51 logger.debug(" coll param is null, returning");
52 return;
53 }
54
55 // see if the collection/cluster element is already there
56 String coll_name = coll_param.getAttribute(GSXML.VALUE_ATT);
57 UserContext userContext = new UserContext(page_request);
58
59 if (coll_name.equals(""))
60 {
61 Element pc_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "p.c");
62 if (pc_param != null)
63 {
64 coll_name = pc_param.getAttribute(GSXML.VALUE_ATT);
65 }
66 }
67
68 boolean get_service_description = false;
69 Element page_response = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
70 if (this.language_list != null)
71 {
72 page_response.appendChild(this.language_list);
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
83 // we dont have one yet - get it
84 Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
85 Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
86 coll_about_message.appendChild(coll_about_request);
87
88 Node coll_about_response_message = this.mr.process(coll_about_message);
89 Element coll_about_response = (Element) GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
90 if (coll_about_response == null)
91 {
92 return;
93 }
94 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
95 if (coll_description == null)
96 { // may be a cluster
97 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
98 }
99
100 if (coll_description == null)
101 {
102 logger.error(" no collection description, returning");
103 return;
104 }
105 // have found one, append it to the page response
106 coll_description = (Element) this.doc.importNode(coll_description, true);
107 page_response.appendChild(coll_description);
108 get_service_description = true;
109 }
110
111 // have got a coll description
112 // now get the dispay info for the services
113 Element service_list = (Element) GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
114 if (service_list == null)
115 {
116 logger.error(" no service list, returning");
117 // something weird has gone wrong
118 return;
119 }
120
121 NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
122 if (services.getLength() == 0)
123 {
124 logger.error("DefaultReceoptionist: no services found for colllection/cluster " + coll_name);
125 return;
126 }
127 // check one service for display items
128 if (!get_service_description)
129 {
130 // we dont know yet if we need to get these
131 int i = 1;
132 Element test_s = (Element) services.item(0);
133 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)))
134 {
135 test_s = (Element) services.item(i);
136 i++;
137 }
138 if (i == services.getLength())
139 {
140 // we have only found retrieve or oai services, so dont need descripitons anyway
141 return;
142 }
143 if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) != null)
144 {
145 // have got descriptions already,
146 return;
147 }
148 }
149
150 // if get here, we need to get the service descriptions
151
152 // we will send all the requests in a single message
153 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
154 for (int i = 0; i < services.getLength(); i++)
155 {
156 Element c = (Element) services.item(i);
157 String name = c.getAttribute(GSXML.NAME_ATT);
158 String address = GSPath.appendLink(coll_name, name);
159 Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, address, userContext);
160 //Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
161 //req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
162 //info_request.appendChild(req_param_list);
163 info_message.appendChild(info_request);
164
165 }
166
167 Element info_response = (Element) this.mr.process(info_message);
168
169 NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
170 // check that have same number of responses as collections
171 if (services.getLength() != service_responses.getLength())
172 {
173 logger.error(" didn't get a response for each service - somethings gone wrong!");
174 // for now, dont use the metadata
175 }
176 else
177 {
178 for (int i = 0; i < services.getLength(); i++)
179 {
180 Element c1 = (Element) services.item(i);
181 Element c2 = (Element) GSXML.getChildByTagName((Element) service_responses.item(i), GSXML.SERVICE_ELEM);
182 if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT)))
183 {
184 //add the service data into the original response
185 GSXML.mergeElements(c1, c2);
186 }
187 else
188 {
189 logger.debug(" response does not correspond to request!");
190 }
191
192 }
193 }
194 }
195}
Note: See TracBrowser for help on using the repository browser.