[3340] | 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;
|
---|
[3455] | 12 | import java.io.File;
|
---|
| 13 |
|
---|
[3340] | 14 | public class PageAction extends Action {
|
---|
| 15 |
|
---|
[3645] | 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 | }
|
---|
[3568] | 23 | public Element process (Element message) {
|
---|
[3340] | 24 |
|
---|
[3568] | 25 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[3467] | 26 | // the page name is the subaction
|
---|
[3512] | 27 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
[3568] | 28 | if (page_name.equals("")) { // if no page specified, assume home page
|
---|
[3645] | 29 | page_name = HOME_PAGE;
|
---|
[3568] | 30 | }
|
---|
| 31 |
|
---|
[3363] | 32 | // create the return page tree
|
---|
[3512] | 33 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
[3568] | 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
|
---|
[3512] | 38 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
[3645] | 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)) {
|
---|
[3363] | 44 | return homePage(page, request);
|
---|
[3645] | 45 | } else if (page_name.equals(ABOUT_PAGE)) {
|
---|
[3363] | 46 | return aboutPage(page, request);
|
---|
[3340] | 47 | } else {
|
---|
[3568] | 48 | System.err.println("PageAction Error: unimplemented page specified!");
|
---|
| 49 | return null;
|
---|
[3340] | 50 | }
|
---|
| 51 | }
|
---|
[3568] | 52 |
|
---|
| 53 | protected Element homePage(Element page, Element orig_message) {
|
---|
[3340] | 54 |
|
---|
[3568] | 55 | // first, get the message router info
|
---|
[3512] | 56 | Element coll_list_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 57 | Element coll_list_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
[3340] | 58 | coll_list_message.appendChild(coll_list_request);
|
---|
[3512] | 59 | coll_list_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
[3568] | 60 | coll_list_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
| 61 | System.out.println("PageAction: getting mesage router description.");
|
---|
[3340] | 62 | Element coll_list_response = (Element)mr_.process(coll_list_message);
|
---|
| 63 | if (coll_list_response==null) {
|
---|
[3568] | 64 | System.err.println("PageAction Error: couldn't query the message router!");
|
---|
| 65 | return null;
|
---|
[3340] | 66 | }
|
---|
| 67 |
|
---|
[3568] | 68 | // second, get the metadata for each collection - we only want specific
|
---|
| 69 | // elements but for now, we'll just get it all
|
---|
[3512] | 70 | NodeList colls = coll_list_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
[3568] | 71 | // we will send all the requests in a single message
|
---|
| 72 | Element metadata_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
[3340] | 73 | for (int i=0; i<colls.getLength(); i++) {
|
---|
[3512] | 74 | Element metadata_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
| 75 | metadata_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
[3568] | 76 | metadata_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
[3340] | 77 | Element c = (Element)colls.item(i);
|
---|
[3568] | 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 |
|
---|
[3512] | 102 | }
|
---|
[3340] | 103 | }
|
---|
| 104 |
|
---|
[3363] | 105 | //now the full response is in coll_list_response
|
---|
| 106 |
|
---|
| 107 | // add it in to the page xml tree
|
---|
[3512] | 108 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_list_response, GSXML.RESPONSE_ELEM), true));
|
---|
[3363] | 109 |
|
---|
[3441] | 110 | String stylesheet = GSFile.stylesheetFile(config_, "home.xsl");
|
---|
[3455] | 111 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
| 112 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
[3568] | 113 | return (Element)transformer_.transform(style_doc, page);
|
---|
[3455] | 114 |
|
---|
[3340] | 115 | } // homePage
|
---|
| 116 |
|
---|
[3568] | 117 | protected Element aboutPage(Element page, Element request) {
|
---|
[3340] | 118 |
|
---|
[3491] | 119 | // extract the params from the cgi-request,
|
---|
[3512] | 120 | Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3491] | 121 | HashMap params = GSXML.extractParams(cgi_paramList);
|
---|
| 122 |
|
---|
[3645] | 123 | String coll_name = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
[3467] | 124 | if (coll_name == null || coll_name.equals("")) {
|
---|
[3568] | 125 | System.err.println("PageAction Error: about page requested with no collection or cluster specified!");
|
---|
| 126 | return null;
|
---|
[3467] | 127 | }
|
---|
[3568] | 128 |
|
---|
| 129 | // get the collection or cluster description
|
---|
[3512] | 130 | Element coll_about_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 131 | Element coll_about_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
[3340] | 132 | coll_about_message.appendChild(coll_about_request);
|
---|
[3512] | 133 | coll_about_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
| 134 | coll_about_request.setAttribute(GSXML.TO_ATT, coll_name);
|
---|
[3568] | 135 | coll_about_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
| 136 |
|
---|
[3340] | 137 | Element coll_about_response = (Element)mr_.process(coll_about_message);
|
---|
[3363] | 138 |
|
---|
| 139 | // add the response to the page
|
---|
[3512] | 140 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM), true));
|
---|
[3363] | 141 |
|
---|
| 142 | // process using the stylesheet
|
---|
[3441] | 143 | String stylesheet = GSFile.stylesheetFile(config_, "about.xsl");
|
---|
[3455] | 144 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
| 145 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
[3568] | 146 | return (Element)transformer_.transform(style_doc, page);
|
---|
[3340] | 147 |
|
---|
| 148 |
|
---|
| 149 | }
|
---|
| 150 | }
|
---|