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

Last change on this file since 4144 was 4144, checked in by kjdon, 21 years ago

all stuff for the page generated by teh Receptionist (config and display) is now put into a pageExtra element, so actions only need to append one extra piece to the page

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 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 SHORT_ACTION_NAME = "p";
17 public static final String HOME_PAGE = "home";
18 public static final String ABOUT_PAGE = "about";
19
20 public Element process (Element message) {
21
22 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
23 // the page name is the subaction
24 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
25 if (page_name.equals("")) { // if no page specified, assume home page
26 page_name = HOME_PAGE;
27 }
28
29 // create the return page tree
30 Element page = doc_.createElement(GSXML.PAGE_ELEM);
31 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
32 // add the page extra stuff from message
33 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.PAGE_EXTRA_ELEM), true));
34 // add the cgi args request
35 Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
36 page.appendChild(page_request);
37
38 if (page_name.equals(HOME_PAGE)) {
39 return homePage(page, request);
40 } else if (page_name.equals(ABOUT_PAGE)) {
41 return aboutPage(page, request);
42 } else {
43 System.err.println("PageAction Error: unimplemented page specified!");
44 return null;
45 }
46 }
47
48
49 protected Element homePage(Element page, Element orig_message) {
50
51 String lang = page.getAttribute(GSXML.LANG_ATT);
52 // first, get the message router info
53 Element coll_list_message = doc_.createElement(GSXML.MESSAGE_ELEM);
54 Element coll_list_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, "", lang);
55 coll_list_message.appendChild(coll_list_request);
56 Element coll_list_response = (Element)mr_.process(coll_list_message);
57 if (coll_list_response==null) {
58 System.err.println("PageAction Error: couldn't query the message router!");
59 return null;
60 }
61
62 // second, get the metadata for each collection - we only want specific
63 // elements but for now, we'll just get it all
64 NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
65 // we will send all the requests in a single message
66 Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
67 for (int i=0; i<colls.getLength(); i++) {
68 Element c = (Element)colls.item(i);
69 String name = c.getAttribute(GSXML.NAME_ATT);
70
71 Element metadata_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, name, lang);
72 metadata_message.appendChild(metadata_request);
73 }
74
75 Element metadata_response = (Element)mr_.process(metadata_message);
76
77 NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
78 // check that have same number of responses as collections
79 if (colls.getLength() != coll_responses.getLength()) {
80 System.err.println("PageAction Error: didn't get a response for each collection - somethings gone wrong!");
81 // for now, dont use the metadata
82 } else {
83 for (int i=0; i<colls.getLength(); i++) {
84 Element c1 = (Element)colls.item(i);
85 Element c2 = (Element)coll_responses.item(i);
86 if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.FROM_ATT))) {
87 // add the metadata to the original response
88 GSXML.mergeMetadataLists(c1, GSXML.getChildByTagName(c2, GSXML.COLLECTION_ELEM)); // add the metadata to the original response
89 } else {
90 System.err.println("PageAction Error: response does not correspond to request!");
91 }
92
93 }
94 }
95
96 //now the full response is in coll_list_response
97
98 // add it in to the page xml tree
99 Element response = (Element) GSXML.getChildByTagName(coll_list_response, GSXML.RESPONSE_ELEM);
100 Element page_response = GSXML.duplicateWithNewName(doc_, response, "pageResponse", false);
101 page.appendChild(page_response);
102
103 String stylesheet = GSFile.stylesheetFile(config_, "home.xsl");
104 Document style_doc = converter_.getDOM(new File(stylesheet));
105 GSXSLT.absoluteIncludePaths(style_doc, config_);
106 return (Element)transformer_.transform(style_doc, page);
107
108 } // homePage
109
110 protected Element aboutPage(Element page, Element request) {
111
112 // extract the params from the cgi-request,
113 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
114 HashMap params = GSXML.extractParams(cgi_paramList, false);
115
116 String coll_name = (String)params.get(GSCGI.COLLECTION_ARG);
117 if (coll_name == null || coll_name.equals("")) {
118 System.err.println("PageAction Error: about page requested with no collection or cluster specified!");
119 return null;
120 }
121
122 // get the collection or cluster description
123 Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
124
125 Element coll_about_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, page.getAttribute(GSXML.LANG_ATT));
126 coll_about_message.appendChild(coll_about_request);
127
128 Element coll_about_response = (Element)mr_.process(coll_about_message);
129
130 // add the response to the page
131 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
132 Element page_response = GSXML.duplicateWithNewName(doc_, response, "pageResponse", false);
133 page.appendChild(page_response);
134
135 // process using the stylesheet
136 String stylesheet = GSFile.stylesheetFile(config_, "about.xsl");
137 Document style_doc = converter_.getDOM(new File(stylesheet));
138 GSXSLT.absoluteIncludePaths(style_doc, config_);
139 return (Element)transformer_.transform(style_doc, page);
140
141
142 }
143}
Note: See TracBrowser for help on using the repository browser.