source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/PageAction.java@ 11230

Last change on this file since 11230 was 11230, checked in by kjdon, 18 years ago

home page now gets descriptions of clusters and services so cna display them if necessary

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 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.io.File;
13
14public class PageAction extends Action {
15
16 public static final String HOME_PAGE = "home";
17 public static final String ABOUT_PAGE = "about";
18 public static final String PREFS_PAGE = "pref";
19 public Element process (Element message) {
20
21 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
22 // the page name is the subaction
23 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
24 if (page_name.equals("")) { // if no page specified, assume home page
25 page_name = HOME_PAGE;
26 }
27 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
28 Element response;
29 if (page_name.equals(HOME_PAGE)) {
30 response = homePage(request);
31 } else if (page_name.equals(ABOUT_PAGE)) {
32 response = aboutPage(request);
33// } else if (page_name.equals(PREFS_PAGE)) {
34// response = prefsPage(request);
35 } else { // unknown page
36
37 System.err.println("PageAction Error: unknown page specified!");
38 response = unknownPage(request);
39 }
40
41 result.appendChild(this.doc.importNode(response, true));
42 ///ystem.out.println("page action result: "+this.converter.getPrettyString(result));
43 return result;
44 }
45
46
47 protected Element homePage(Element request) {
48
49 String lang = request.getAttribute(GSXML.LANG_ATT);
50 String uid = request.getAttribute(GSXML.USER_ID_ATT);
51 // first, get the message router info
52 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
53 Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
54 info_message.appendChild(coll_list_request);
55 Element info_response_message = (Element)this.mr.process(info_message);
56 if (info_response_message==null) {
57 System.err.println("PageAction Error: couldn't query the message router!");
58 return null;
59 }
60 Element info_response = (Element)GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
61 if (info_response==null) {
62 System.err.println("PageAction Error: couldn't query the message router!");
63 return null;
64 }
65
66 // second, get the metadata for each collection - we only want specific
67 // elements but for now, we'll just get it all
68 Element collection_list = (Element)GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
69 if (collection_list != null) {
70 NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM);
71 if (colls.getLength()>0) {
72 sendMultipleRequests(colls, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
73 }
74 }
75
76 // get metadata for any services
77 Element service_list = (Element)GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
78 if (service_list != null) {
79 NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM);
80 if (services.getLength() > 0) {
81 sendMultipleRequests(services, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
82 }
83 }
84
85 // get metadata for service clusters
86 Element cluster_list = (Element)GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
87 if (cluster_list != null) {
88 NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM);
89 if (clusters.getLength() > 0) {
90 sendMultipleRequests(clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
91
92 }
93 }
94
95 // all the components have been merged into info_response
96 return info_response;
97
98 } // homePage
99
100 protected Element aboutPage(Element request) {
101
102 String lang = request.getAttribute(GSXML.LANG_ATT);
103 String uid = request.getAttribute(GSXML.USER_ID_ATT);
104 // extract the params from the cgi-request,
105 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
106 HashMap params = GSXML.extractParams(cgi_paramList, false);
107
108 String coll_name = (String)params.get(GSParams.COLLECTION);
109 if (coll_name == null || coll_name.equals("")) {
110 System.err.println("PageAction Error: about page requested with no collection or cluster specified!");
111 // return an empty response
112 return this.doc.createElement(GSXML.RESPONSE_ELEM);
113 }
114
115 // get the collection or cluster description
116 Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
117
118 Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
119 coll_about_message.appendChild(coll_about_request);
120
121 Element coll_about_response = (Element)this.mr.process(coll_about_message);
122
123 if (coll_about_response == null) {
124 return null;
125 }
126
127 // second, get the info for each service - we only want display items
128 // but for now, we'll just get it all
129 NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM);
130 if (services.getLength() > 0) {
131 sendMultipleRequests(services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
132 }
133
134 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
135 return response;
136 }
137
138// protected Element prefsPage(Element request) {
139
140// return null;
141// }
142
143 /** if we dont know the page type, use this method */
144 protected Element unknownPage(Element request) {
145
146 String lang = request.getAttribute(GSXML.LANG_ATT);
147 String uid = request.getAttribute(GSXML.USER_ID_ATT);
148 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
149
150 // extract the params from the cgi-request,
151 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
152 HashMap params = GSXML.extractParams(cgi_paramList, false);
153
154 String coll_name = (String)params.get(GSParams.COLLECTION);
155 if (coll_name == null || coll_name.equals("")) {
156 // just return an empty response
157 return this.doc.createElement(GSXML.RESPONSE_ELEM);
158 }
159
160 // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
161
162 // if there is a service specified should we get the service description instead??
163 // get the collection or cluster description
164 Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
165
166 Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
167 coll_about_message.appendChild(coll_about_request);
168
169 Element coll_about_response = (Element)this.mr.process(coll_about_message);
170
171 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
172 return response;
173
174 }
175
176
177 protected boolean sendMultipleRequests(NodeList items, String path_prefix, String request_type, String lang, String uid) {
178
179 // we will send all the requests in a single message
180 Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
181 for (int i=0; i<items.getLength(); i++) {
182 Element c = (Element)items.item(i);
183 String path = c.getAttribute(GSXML.NAME_ATT);
184 if (path_prefix != null) {
185 path = GSPath.appendLink(path_prefix, path);
186 }
187 Element request = GSXML.createBasicRequest(this.doc, request_type, path, lang, uid);
188 message.appendChild(request);
189 }
190
191 Element response_message = (Element)this.mr.process(message);
192
193 NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM);
194 // check that have same number of responses as requests
195 if (items.getLength() != responses.getLength()) {
196 System.err.println("PageAction Error: didn't get a response for each request - somethings gone wrong!");
197 return false;
198 }
199
200 for (int i=0; i<items.getLength(); i++) {
201 Element c1 = (Element)items.item(i);
202 Element c2 = (Element)GSXML.getChildByTagName((Element)responses.item(i), c1.getTagName());
203 if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT))) {
204 //add the new data into the original element
205 GSXML.mergeElements(c1, c2);
206 } else {
207 System.err.println("PageAction Error: response does not correspond to request!");
208 }
209
210 }
211
212 return true;
213
214 }
215}
Note: See TracBrowser for help on using the repository browser.