1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.io.File;
|
---|
13 |
|
---|
14 | public 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 String getActionShortName() {
|
---|
21 | return SHORT_ACTION_NAME;
|
---|
22 | }
|
---|
23 | public Element process (Element message) {
|
---|
24 |
|
---|
25 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
26 | // the page name is the subaction
|
---|
27 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
28 | if (page_name.equals("")) { // if no page specified, assume home page
|
---|
29 | page_name = HOME_PAGE;
|
---|
30 | }
|
---|
31 |
|
---|
32 | // create the return page tree
|
---|
33 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
34 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
35 | // add the lang stuff from message
|
---|
36 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
37 | // add the system stuff from message
|
---|
38 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
39 | // add the cgi args request
|
---|
40 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
41 | page.appendChild(cgi_request);
|
---|
42 |
|
---|
43 | if (page_name.equals(HOME_PAGE)) {
|
---|
44 | return homePage(page, request);
|
---|
45 | } else if (page_name.equals(ABOUT_PAGE)) {
|
---|
46 | return aboutPage(page, request);
|
---|
47 | } else {
|
---|
48 | System.err.println("PageAction Error: unimplemented page specified!");
|
---|
49 | return null;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected Element homePage(Element page, Element orig_message) {
|
---|
54 |
|
---|
55 | // first, get the message router info
|
---|
56 | Element coll_list_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
57 | Element coll_list_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
58 | coll_list_message.appendChild(coll_list_request);
|
---|
59 | coll_list_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
60 | coll_list_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
61 | System.out.println("PageAction: getting mesage router description.");
|
---|
62 | Element coll_list_response = (Element)mr_.process(coll_list_message);
|
---|
63 | if (coll_list_response==null) {
|
---|
64 | System.err.println("PageAction Error: couldn't query the message router!");
|
---|
65 | return null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // second, get the metadata for each collection - we only want specific
|
---|
69 | // elements but for now, we'll just get it all
|
---|
70 | NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
71 | // we will send all the requests in a single message
|
---|
72 | Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
73 | for (int i=0; i<colls.getLength(); i++) {
|
---|
74 | Element metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
75 | metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
76 | metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
77 | Element c = (Element)colls.item(i);
|
---|
78 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
79 | metadata_request.setAttribute(GSXML.TO_ATT, name); // overwrites the old value
|
---|
80 | // add this request to the message
|
---|
81 | metadata_message.appendChild(metadata_request);
|
---|
82 | }
|
---|
83 | System.out.println("PageAction: getting metadata for each collection.");
|
---|
84 | Element metadata_response = (Element)mr_.process(metadata_message);
|
---|
85 |
|
---|
86 | NodeList coll_responses = metadata_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
87 | // check that have same number of responses as collections
|
---|
88 | if (colls.getLength() != coll_responses.getLength()) {
|
---|
89 | System.err.println("PageAction Error: didn't get a response for each collection - somethings gone wrong!");
|
---|
90 | // for now, dont use the metadata
|
---|
91 | } else {
|
---|
92 | for (int i=0; i<colls.getLength(); i++) {
|
---|
93 | Element c1 = (Element)colls.item(i);
|
---|
94 | Element c2 = (Element)coll_responses.item(i);
|
---|
95 | if (c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT))) {
|
---|
96 | // add the metadata to the original response
|
---|
97 | GSXML.mergeMetadataLists(c1, c2); // add the metadata to the original response
|
---|
98 | } else {
|
---|
99 | System.err.println("PageAction Error: response does not correspond to request!");
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | //now the full response is in coll_list_response
|
---|
106 |
|
---|
107 | // add it in to the page xml tree
|
---|
108 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_list_response, GSXML.RESPONSE_ELEM), true));
|
---|
109 |
|
---|
110 | String stylesheet = GSFile.stylesheetFile(config_, "home.xsl");
|
---|
111 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
112 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
113 | return (Element)transformer_.transform(style_doc, page);
|
---|
114 |
|
---|
115 | } // homePage
|
---|
116 |
|
---|
117 | protected Element aboutPage(Element page, Element request) {
|
---|
118 |
|
---|
119 | // extract the params from the cgi-request,
|
---|
120 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
121 | HashMap params = GSXML.extractParams(cgi_paramList);
|
---|
122 |
|
---|
123 | String coll_name = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
124 | if (coll_name == null || coll_name.equals("")) {
|
---|
125 | System.err.println("PageAction Error: about page requested with no collection or cluster specified!");
|
---|
126 | return null;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // get the collection or cluster description
|
---|
130 | Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
131 | Element coll_about_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
132 | coll_about_message.appendChild(coll_about_request);
|
---|
133 | coll_about_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
134 | coll_about_request.setAttribute(GSXML.TO_ATT, coll_name);
|
---|
135 | coll_about_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
136 |
|
---|
137 | Element coll_about_response = (Element)mr_.process(coll_about_message);
|
---|
138 |
|
---|
139 | // add the response to the page
|
---|
140 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM), true));
|
---|
141 |
|
---|
142 | // process using the stylesheet
|
---|
143 | String stylesheet = GSFile.stylesheetFile(config_, "about.xsl");
|
---|
144 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
145 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
146 | return (Element)transformer_.transform(style_doc, page);
|
---|
147 |
|
---|
148 |
|
---|
149 | }
|
---|
150 | }
|
---|