NIHVIVO-1234 Removed separation of body and root maps in Freemarker controllers. Combine everything into one map, which includes the name of the body template also, to be invoked from the page template. Simplifies the logic considerably, in both controllers and templates, and allows the template author to dump the name of the body template rather than having to inspect the controller code.

This commit is contained in:
rjy7 2010-10-27 01:27:52 +00:00
parent fe1d574021
commit df3a3dd1c7
11 changed files with 94 additions and 123 deletions

View file

@ -32,9 +32,7 @@ public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
VitroRequest vreq = new VitroRequest(request);
Configuration config = getConfig(vreq);
// root is the map used to create the page shell - header, footer, menus, etc.
Map<String, Object> root = getSharedVariables(vreq, new HashMap<String, Object>());
root.putAll(getRootValues(vreq));
Map<String, Object> root = getPageValues(vreq, new HashMap<String, Object>());
request.setAttribute("ftl_identity", get("identity", root, config));
request.setAttribute("ftl_menu", get("menu", root, config));

View file

@ -253,38 +253,19 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
}
// RY *** A lot of this is shared with doException(). Factor out shared parts.
protected void doTemplate(VitroRequest vreq, HttpServletResponse response, ResponseValues values) {
Configuration config = getConfig(vreq);
Map<String, Object> bodyMap = values.getMap();
// We can't use shared variables in the Freemarker configuration to store anything
// except theme-specific data, because multiple portals or apps might share the same theme. So instead
// just put the shared variables in both root and body.
Map<String, Object> sharedVariables = getSharedVariables(vreq, bodyMap);
// root is the map used to create the page shell - header, footer, menus, etc.
Map<String, Object> root = new HashMap<String, Object>(sharedVariables);
// body is the map used to create the page body
Map<String, Object> body = new HashMap<String, Object>(sharedVariables);
root.putAll(getRootValues(vreq));
Map<String, Object> map = new HashMap<String, Object>();
map.putAll(getPageValues(vreq, bodyMap));
// Add the values that we got, and merge to the template.
String bodyTemplate = values.getTemplateName();
String bodyString;
if (bodyTemplate != null) {
body.putAll(bodyMap);
bodyString = mergeMapToTemplate(bodyTemplate, body, config);
} else {
// The subcontroller has not defined a body template. All markup for the page
// is specified in the main page template.
bodyString = "";
}
root.put("body", bodyString);
// Add the values from the subcontroller.
map.putAll(bodyMap);
map.put("bodyTemplate", values.getTemplateName());
writePage(root, config, response);
writePage(map, config, response);
}
protected void doRedirect(HttpServletRequest request, HttpServletResponse response, ResponseValues values)
@ -330,49 +311,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
doTemplate(vreq, response, trv);
}
// We can't use shared variables in the Freemarker configuration to store anything
// except theme-specific data, because multiple portals or apps might share the same theme. So instead
// we'll get all the shared variables here, and put them in both root and body maps.
protected Map<String, Object> getSharedVariables(VitroRequest vreq, Map<String, Object> bodyMap) {
Map<String, Object> map = new HashMap<String, Object>();
Portal portal = vreq.getPortal();
// Ideally, templates wouldn't need portal id. Currently used as a hidden input value
// in the site search box, so needed for now.
map.put("portalId", portal.getPortalId());
String siteName = portal.getAppName();
map.put("siteName", siteName);
// In some cases the title is determined during the subclass processRequest() method; e.g.,
// for an Individual profile page, the title should be the Individual's label. In that case,
// put that title in the sharedVariables map because it's needed by the page root map as well
// to generate the <title> element. Otherwise, use the getTitle() method to generate the title.
String title = (String) bodyMap.get("title");
if (StringUtils.isEmpty(title)) {
title = getTitle(siteName);
}
map.put("title", title);
String themeDir = getThemeDir(portal);
UrlBuilder urlBuilder = new UrlBuilder(portal);
map.put("urls", getUrls(themeDir, urlBuilder));
map.put("themeDir", themeDir);
map.put("themeDir", themeDir);
map.put("stylesheets", getStylesheetList(themeDir));
map.put("scripts", getScriptList(themeDir));
map.put("headScripts", getScriptList(themeDir));
map.putAll(getDirectives());
return map;
}
public String getThemeDir(Portal portal) {
return portal.getThemeDir().replaceAll("/$", "");
}
@ -443,43 +381,64 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return map;
}
// Add variables that should be available only to the page's root map, not to the body.
// Values needed to generate the page frame - header, footer, menus, etc. Some may also be used in the
// page body.
// RY This is protected instead of private so FreeMarkerComponentGenerator can access.
// Once we don't need that (i.e., jsps have been eliminated) we can make it private.
protected Map<String, Object> getRootValues(VitroRequest vreq) {
protected Map<String, Object> getPageValues(VitroRequest vreq, Map<String, Object> bodyMap) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> root = new HashMap<String, Object>();
root.put("tabMenu", getTabMenu(vreq));
Portal portal = vreq.getPortal();
// Ideally, templates wouldn't need portal id. Currently used as a hidden input value
// in the site search box, so needed for now.
map.put("portalId", portal.getPortalId());
String siteName = portal.getAppName();
map.put("siteName", siteName);
// In some cases the title is determined during the subclass processRequest() method; e.g.,
// for an Individual profile page, the title should be the Individual's label. In that case,
// put that title in the sharedVariables map because it's needed by the page root map as well
// to generate the <title> element. Otherwise, use the getTitle() method to generate the title.
String title = (String) bodyMap.get("title");
if (StringUtils.isEmpty(title)) {
title = getTitle(siteName);
}
map.put("title", title);
String themeDir = getThemeDir(portal);
UrlBuilder urlBuilder = new UrlBuilder(portal);
map.put("urls", getUrls(themeDir, urlBuilder));
map.put("themeDir", themeDir);
map.put("stylesheets", getStylesheetList(themeDir));
map.put("scripts", getScriptList(themeDir));
map.put("headScripts", getScriptList(themeDir));
map.putAll(getDirectives());
map.put("tabMenu", getTabMenu(vreq));
ApplicationBean appBean = vreq.getAppBean();
PortalWebUtil.populateSearchOptions(portal, appBean, vreq.getWebappDaoFactory().getPortalDao());
PortalWebUtil.populateNavigationChoices(portal, vreq, appBean, vreq.getWebappDaoFactory().getPortalDao());
root.putAll(getLoginValues(vreq));
map.putAll(getLoginValues(vreq));
root.put("copyright", getCopyrightInfo(portal));
root.put("siteTagline", portal.getShortHand());
root.put("breadcrumbs", BreadCrumbsUtil.getBreadCrumbsDiv(vreq));
String themeDir = getThemeDir(portal);
map.put("copyright", getCopyrightInfo(portal));
map.put("siteTagline", portal.getShortHand());
map.put("breadcrumbs", BreadCrumbsUtil.getBreadCrumbsDiv(vreq));
// This value is used only in stylesheets.ftl and already contains the context path.
root.put("stylesheetPath", UrlBuilder.getUrl(themeDir + "/css"));
map.put("stylesheetPath", UrlBuilder.getUrl(themeDir + "/css"));
String bannerImage = portal.getBannerImage();
if ( ! StringUtils.isEmpty(bannerImage)) {
root.put("bannerImage", UrlBuilder.getUrl(themeDir + "site_icons/" + bannerImage));
map.put("bannerImage", UrlBuilder.getUrl(themeDir + "site_icons/" + bannerImage));
}
return root;
return map;
}
private TabMenu getTabMenu(VitroRequest vreq) {
// RY There's a vreq.getPortalId() method, but not sure if it returns
// same value as this.
int portalId = vreq.getPortal().getPortalId();
return new TabMenu(vreq, portalId);
}

View file

@ -19,13 +19,15 @@ public class HomePageController extends FreemarkerHttpServlet {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(HomePageController.class);
private static final String PAGE_TEMPLATE = "page-home.ftl";
// private static final String BODY_TEMPLATE = "home.ftl";
private static final String BODY_TEMPLATE = "home.ftl";
@Override
protected ResponseValues processRequest(VitroRequest vreq) {
Map<String, Object> body = new HashMap<String, Object>();
// return new TemplateResponseValues(BODY_TEMPLATE, body);
return new TemplateResponseValues(null, body);
Map<String, Object> body = new HashMap<String, Object>();
// Add home page data to body here
return new TemplateResponseValues(BODY_TEMPLATE, body);
}
@Override

View file

@ -3,8 +3,6 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -40,7 +38,6 @@ import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
@ -64,13 +61,11 @@ import edu.cornell.mannlib.vitro.webapp.web.templatemodels.IndividualTemplateMod
*
*/
public class IndividualController extends FreemarkerHttpServlet {
private static final Log log = LogFactory.getLog(IndividualController.class);
private String default_jsp = Controllers.BASIC_JSP;
private String default_body_jsp = Controllers.ENTITY_JSP;
private ApplicationBean appBean;
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(IndividualController.class);
private static final String TEMPLATE_INDIVIDUAL = "individual.ftl";
private static final String TEMPLATE_INDIVIDUAL_DEFAULT = "individual.ftl";
private static final String TEMPLATE_HELP = "individual-help.ftl";
@Override
@ -117,9 +112,10 @@ public class IndividualController extends FreemarkerHttpServlet {
body.put("relatedSubject", getRelatedSubject(vreq));
body.put("individual", getIndividualTemplateModel(vreq, individual));
IndividualTemplateModel ind = getIndividualTemplateModel(vreq, individual);
body.put("individual", ind);
return new TemplateResponseValues(TEMPLATE_INDIVIDUAL, body);
return new TemplateResponseValues(TEMPLATE_INDIVIDUAL_DEFAULT, body);
} catch (Throwable e) {
log.error(e);
@ -195,10 +191,10 @@ public class IndividualController extends FreemarkerHttpServlet {
// String vclassName = "unknown";
// String customView = null;
//
// if( indiv.getVClass() != null ){
// vclassName = indiv.getVClass().getName();
// List<VClass> clasList = indiv.getVClasses(true);
// String customCss = null;
// if( individual.getVClass() != null ){
// vclassName = individual.getVClass().getName();
// List<VClass> clasList = individual.getVClasses(true);
// for (VClass clas : clasList) {
// customView = clas.getCustomDisplayView();
// if (customView != null) {
@ -212,7 +208,7 @@ public class IndividualController extends FreemarkerHttpServlet {
// }
// }
// if (customView == null) { //still
// clasList = indiv.getVClasses(false);
// clasList = individual.getVClasses(false);
// for (VClass clas : clasList) {
// customView = clas.getCustomDisplayView();
// if (customView != null) {
@ -226,9 +222,9 @@ public class IndividualController extends FreemarkerHttpServlet {
// }
// }
// }
// } else {
// log.error("Entity " + indiv.getURI() + " with vclass URI " +
// indiv.getVClassURI() + ", no vclass with that URI exists");
// } else if (individual.getVClassURI() != null) {
// log.debug("Individual " + individual.getURI() + " with class URI " +
// individual.getVClassURI() + ": no class found with that URI");
// }
// if (customView!=null) {
// // insert test for whether a css files of the same name exists, and populate the customCss string for use when construction the header
@ -246,7 +242,7 @@ public class IndividualController extends FreemarkerHttpServlet {
OntModel ontModel = null;
HttpSession session = vreq.getSession(false);
if( session != null )
ontModel =(OntModel)session.getAttribute("jenaOntModel");
ontModel = (OntModel)session.getAttribute("jenaOntModel");
if( ontModel == null)
ontModel = (OntModel)getServletContext().getAttribute("jenaOntModel");

View file

@ -156,14 +156,11 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
private String doTemplate(VitroRequest vreq, TemplateResponseValues values) {
// Set it up like FreeMarkerHttpServlet.doGet() would do.
Configuration config = getConfig(vreq);
Map<String, Object> sharedVariables = getSharedVariables(vreq, new HashMap<String, Object>());
Map<String, Object> root = new HashMap<String, Object>(sharedVariables);
Map<String, Object> body = new HashMap<String, Object>(sharedVariables);
root.putAll(getRootValues(vreq));
Map<String, Object> root = getPageValues(vreq, new HashMap<String, Object>());
// Add the values that we got, and merge to the template.
body.putAll(values.getMap());
return mergeMapToTemplate(values.getTemplateName(), body, config);
// Add the TemplateResponseValues
root.putAll(values.getMap());
return mergeMapToTemplate(values.getTemplateName(), root, config);
}
/**

View file

@ -188,5 +188,8 @@ public class IndividualTemplateModel extends BaseTemplateModel {
}
return models;
}
// public Map< > getPropertyList
// public Map< > getPropertyGroupList
}