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