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 String process (Element message) {
|
---|
17 |
|
---|
18 | Element request = (Element)message.getElementsByTagName("request").item(0);
|
---|
19 | // the page name is the subaction
|
---|
20 | String page_name = request.getAttribute("subaction");
|
---|
21 |
|
---|
22 | // create the return page tree
|
---|
23 | Element page = doc_.createElement("page");
|
---|
24 | page.setAttribute("lang", message.getAttribute("lang"));
|
---|
25 | // add the lang stuff from xml_in
|
---|
26 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, "translate"), true));
|
---|
27 | // add the system stuff from xml_in
|
---|
28 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, "config"), true));
|
---|
29 |
|
---|
30 | if (page_name.equals("")) {
|
---|
31 | // for now send an error. change to always display the home page?
|
---|
32 | return GSHTML.errorPage(" page action specified, but no page specified");
|
---|
33 | }
|
---|
34 | if (page_name.equals("home")) {
|
---|
35 | return homePage(page, request);
|
---|
36 | } else if (page_name.equals("about")) {
|
---|
37 | return aboutPage(page, request);
|
---|
38 | } else {
|
---|
39 | return GSHTML.errorPage("page: "+page_name+" not yet implemented");
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | protected String homePage(Element page, Element orig_message) {
|
---|
45 |
|
---|
46 | // first, get the list of collections from mr
|
---|
47 |
|
---|
48 | Element coll_list_message = doc_.createElement("message");
|
---|
49 | coll_list_message.setAttribute("lang", page.getAttribute("lang"));
|
---|
50 | Element coll_list_request = doc_.createElement("request");
|
---|
51 | coll_list_message.appendChild(coll_list_request);
|
---|
52 | coll_list_request.setAttribute("type", "describe");
|
---|
53 | //coll_list_request.setAttribute("info", "collectionList");
|
---|
54 |
|
---|
55 | Element coll_list_response = (Element)mr_.process(coll_list_message);
|
---|
56 | if (coll_list_response==null) {
|
---|
57 | return GSHTML.errorPage("couldn't query the mr about collections");
|
---|
58 | }
|
---|
59 |
|
---|
60 | // second, get the metadata for each collection
|
---|
61 | NodeList colls = coll_list_response.getElementsByTagName("collection");
|
---|
62 |
|
---|
63 | Element metadata_message = doc_.createElement("message");
|
---|
64 | metadata_message.setAttribute("lang", page.getAttribute("lang"));
|
---|
65 | Element metadata_request = doc_.createElement("request");
|
---|
66 | metadata_message.appendChild(metadata_request);
|
---|
67 | metadata_request.setAttribute("type", "describe");
|
---|
68 | for (int i=0; i<colls.getLength(); i++) {
|
---|
69 | // get the metadata for each one
|
---|
70 | Element c = (Element)colls.item(i);
|
---|
71 | String name = c.getAttribute("name");
|
---|
72 |
|
---|
73 | metadata_request.setAttribute("to", name); // overwrites the old value
|
---|
74 | Element metadata_response = (Element)mr_.process(metadata_message);
|
---|
75 | Element coll = (Element)metadata_response.getElementsByTagName("collection").item(0);
|
---|
76 | GSXML.mergeMetadataLists(c, coll); // add the metadata to the original response
|
---|
77 | }
|
---|
78 |
|
---|
79 | //now the full response is in coll_list_response
|
---|
80 |
|
---|
81 | // add it in to the page xml tree
|
---|
82 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_list_response, "response"), true));
|
---|
83 |
|
---|
84 | String stylesheet = GSFile.stylesheetFile(config_, "home.xsl");
|
---|
85 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
86 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
87 | //System.out.println("page=");
|
---|
88 | //System.out.println(converter_.getString(page));
|
---|
89 | return transformer_.transform(style_doc, page);
|
---|
90 |
|
---|
91 |
|
---|
92 | } // homePage
|
---|
93 |
|
---|
94 | protected String aboutPage(Element page, Element request) {
|
---|
95 |
|
---|
96 | // extract the params from the cgi-request,
|
---|
97 | // first convert short to long names
|
---|
98 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, "paramList");
|
---|
99 | cgi_.toLong(cgi_paramList);
|
---|
100 | HashMap params = GSXML.extractParams(cgi_paramList);
|
---|
101 |
|
---|
102 | String coll_name = (String)params.get("collection");
|
---|
103 | if (coll_name == null || coll_name.equals("")) {
|
---|
104 | coll_name = (String)params.get("serviceCluster"); // cluster name
|
---|
105 | }
|
---|
106 | if (coll_name == null || coll_name.equals("")) {
|
---|
107 | return GSHTML.errorPage("about page - need to specify coll name or cluster name");
|
---|
108 | }
|
---|
109 |
|
---|
110 | Element coll_about_message = doc_.createElement("message");
|
---|
111 | coll_about_message.setAttribute("lang", page.getAttribute("lang"));
|
---|
112 |
|
---|
113 | Element coll_about_request = doc_.createElement("request");
|
---|
114 | coll_about_message.appendChild(coll_about_request);
|
---|
115 | coll_about_request.setAttribute("type", "describe");
|
---|
116 | coll_about_request.setAttribute("to", coll_name);
|
---|
117 |
|
---|
118 | Element coll_about_response = (Element)mr_.process(coll_about_message);
|
---|
119 |
|
---|
120 | // System.out.println("coll response=");
|
---|
121 | //System.out.println(converter_.getString(coll_about_response));
|
---|
122 | // add the response to the page
|
---|
123 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_about_response, "response"), true));
|
---|
124 |
|
---|
125 | // process using the stylesheet
|
---|
126 | String stylesheet = GSFile.stylesheetFile(config_, "about.xsl");
|
---|
127 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
128 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
129 | return transformer_.transform(style_doc, page);
|
---|
130 |
|
---|
131 |
|
---|
132 | }
|
---|
133 | }
|
---|