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(GSXML.REQUEST_ELEM).item(0);
|
---|
19 | // the page name is the subaction
|
---|
20 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
21 |
|
---|
22 | // create the return page tree
|
---|
23 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
24 | page.setAttribute(GSXML.LANG_ATT, message.getAttribute(GSXML.LANG_ATT));
|
---|
25 | // add the lang stuff from xml_in
|
---|
26 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.TRANSLATION_ELEM), true));
|
---|
27 | // add the system stuff from xml_in
|
---|
28 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), 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(GSXML.MESSAGE_ELEM);
|
---|
49 | coll_list_message.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
50 | Element coll_list_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
51 | coll_list_message.appendChild(coll_list_request);
|
---|
52 | coll_list_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_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(GSXML.COLLECTION_ELEM);
|
---|
62 |
|
---|
63 | //Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
64 | //metadata_message.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
65 | //Element metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
66 | //metadata_message.appendChild(metadata_request);
|
---|
67 | //metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
68 | for (int i=0; i<colls.getLength(); i++) {
|
---|
69 | Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
70 | metadata_message.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
71 | Element metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
72 | metadata_message.appendChild(metadata_request);
|
---|
73 | metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
74 |
|
---|
75 | // get the metadata for each one
|
---|
76 | Element c = (Element)colls.item(i);
|
---|
77 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
78 | System.out.println("getting metadata for coll "+name);
|
---|
79 |
|
---|
80 | metadata_request.setAttribute(GSXML.TO_ATT, name); // overwrites the old value
|
---|
81 | System.out.println("metadata message = \n"+converter_.getString(metadata_message));
|
---|
82 | Element metadata_response = (Element)mr_.process(metadata_message);
|
---|
83 | Element coll = (Element)metadata_response.getElementsByTagName(GSXML.COLLECTION_ELEM).item(0);
|
---|
84 | if (coll==null) {
|
---|
85 | System.out.println("coll is null, in \n"+converter_.getString(metadata_response));
|
---|
86 | }
|
---|
87 | GSXML.mergeMetadataLists(c, coll); // add the metadata to the original response
|
---|
88 | }
|
---|
89 |
|
---|
90 | //now the full response is in coll_list_response
|
---|
91 |
|
---|
92 | // add it in to the page xml tree
|
---|
93 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_list_response, GSXML.RESPONSE_ELEM), true));
|
---|
94 |
|
---|
95 | String stylesheet = GSFile.stylesheetFile(config_, "home.xsl");
|
---|
96 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
97 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
98 | //System.out.println("page=");
|
---|
99 | //System.out.println(converter_.getString(page));
|
---|
100 | return transformer_.transform(style_doc, page);
|
---|
101 |
|
---|
102 |
|
---|
103 | } // homePage
|
---|
104 |
|
---|
105 | protected String aboutPage(Element page, Element request) {
|
---|
106 |
|
---|
107 | // extract the params from the cgi-request,
|
---|
108 | // first convert short to long names
|
---|
109 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
110 | cgi_.toLong(cgi_paramList);
|
---|
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 | return GSHTML.errorPage("about page - need to specify coll name or cluster name");
|
---|
119 | }
|
---|
120 |
|
---|
121 | Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
122 | coll_about_message.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
123 |
|
---|
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 |
|
---|
129 | Element coll_about_response = (Element)mr_.process(coll_about_message);
|
---|
130 |
|
---|
131 | // System.out.println("coll response=");
|
---|
132 | //System.out.println(converter_.getString(coll_about_response));
|
---|
133 | // add the response to the page
|
---|
134 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM), true));
|
---|
135 |
|
---|
136 | // process using the stylesheet
|
---|
137 | String stylesheet = GSFile.stylesheetFile(config_, "about.xsl");
|
---|
138 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
139 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
140 | return transformer_.transform(style_doc, page);
|
---|
141 |
|
---|
142 |
|
---|
143 | }
|
---|
144 | }
|
---|