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