source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/DefaultReceptionist.java@ 13959

Last change on this file since 13959 was 13959, checked in by shaoqun, 17 years ago

add a guide

  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 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
19 */
20public class DefaultReceptionist extends TransformingReceptionist {
21
22 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.DefaultReceptionist.class.getName());
23
24 protected void addExtraInfo(Element page) {
25 // we want to add in the collection description for each page - cos
26 // our default xslt needs this.
27 super.addExtraInfo(page);
28
29 Element page_request = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
30 // if it is a system request, then we don't bother with this.
31 String action = page_request.getAttribute(GSXML.ACTION_ATT);
32 if (action.equals("s")) {
33 logger.error("HACK: don't ask for coll info if system action");
34 return;
35 }
36 logger.debug("add extra info, page request="+this.converter.getString(page_request));
37 // is a collection defined?
38 Element param_list = (Element)GSXML.getChildByTagName(page_request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
39 if (param_list==null) { // must be the original home page
40 logger.debug(" no param list, assuming home page");
41 return;
42 }
43 Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSParams.COLLECTION);
44 if (coll_param == null) {
45 logger.debug(" coll param is null, returning");
46 return;
47 }
48
49 // see if the collection/cluster element is already there
50 String coll_name = coll_param.getAttribute(GSXML.VALUE_ATT);
51 String lang = page_request.getAttribute(GSXML.LANG_ATT);
52 String uid = page_request.getAttribute(GSXML.USER_ID_ATT);
53
54 boolean get_service_description = false;
55 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
56 if (this.language_list != null) {
57 page_response.appendChild(this.language_list);
58 }
59 Element coll_description = (Element)GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
60 if (coll_description == null) {
61 // try cluster
62 coll_description = (Element)GSXML.getChildByTagName(page_response, GSXML.CLUSTER_ELEM);
63 }
64 if (coll_description == null) {
65 // we dont have one yet - get it
66 Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
67 Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
68 coll_about_message.appendChild(coll_about_request);
69
70 Element coll_about_response_message = this.mr.process(coll_about_message);
71 Element coll_about_response = (Element)GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
72 if (coll_about_response == null) {
73 return;
74 }
75 coll_description = (Element)GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
76 if (coll_description==null) { // may be a cluster
77 coll_description = (Element)GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
78 }
79
80 if (coll_description == null) {
81 logger.error(" no collection description, returning");
82 return;
83 }
84 // have found one, append it to the page response
85 coll_description = (Element)this.doc.importNode(coll_description, true);
86 page_response.appendChild(coll_description);
87 get_service_description = true;
88 }
89
90 // have got a coll description
91
92 // now get the dispay info for the services
93 Element service_list = (Element)GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
94 if (service_list == null) {
95 logger.error(" no service list, returning");
96 // something weird has gone wrong
97 return;
98 }
99
100 NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
101 if (services.getLength()==0) {
102 logger.error("DefaultReceoptionist: no services found for colllection/cluster "+ coll_name);
103 return;
104 }
105 // check one service for display items
106 if (!get_service_description) {
107 // we dont know yet if we need to get these
108 int i=1;
109 Element test_s = (Element)services.item(0);
110 while (i<services.getLength() && test_s.getAttribute(GSXML.TYPE_ATT).equals(GSXML.SERVICE_TYPE_RETRIEVE)) {
111 test_s = (Element)services.item(i); i++;
112 }
113 if (i==services.getLength()) {
114 // we have only found retrieve services, so dont need descripitons anyway
115 return;
116 }
117 if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) !=null) {
118 // have got descriptions already,
119 return;
120 }
121 }
122
123 // if get here, we need to get the service descriptions
124 logger.debug("getting services description");
125
126 // we will send all the requests in a single message
127 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
128 for (int i=0; i<services.getLength(); i++) {
129 Element c = (Element)services.item(i);
130 String name = c.getAttribute(GSXML.NAME_ATT);
131 String address = GSPath.appendLink(coll_name, name);
132 Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, address, lang, uid);
133 Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
134 req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
135 info_request.appendChild(req_param_list);
136 info_message.appendChild(info_request);
137
138 }
139
140 Element info_response = (Element)this.mr.process(info_message);
141
142 NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
143 // check that have same number of responses as collections
144 if (services.getLength() != service_responses.getLength()) {
145 logger.error(" didn't get a response for each service - somethings gone wrong!");
146 // for now, dont use the metadata
147 } else {
148 for (int i=0; i<services.getLength(); i++) {
149 Element c1 = (Element)services.item(i);
150 Element c2 = (Element)GSXML.getChildByTagName((Element)service_responses.item(i), GSXML.SERVICE_ELEM);
151 if (c1 !=null && c2 !=null && c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT))) {
152 //add the service data into the original response
153 GSXML.mergeElements(c1, c2);
154 } else {
155 logger.error(" response does not correspond to request!");
156 }
157
158 }
159 }
160
161 }
162}
163
Note: See TracBrowser for help on using the repository browser.