From df3a3dd1c7241b1cb3950284aa4ebe4f060a7ede Mon Sep 17 00:00:00 2001 From: rjy7 Date: Wed, 27 Oct 2010 01:27:52 +0000 Subject: [PATCH] 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. --- .../FreemarkerComponentGenerator.java | 4 +- .../freemarker/FreemarkerHttpServlet.java | 129 ++++++------------ .../freemarker/HomePageController.java | 10 +- .../freemarker/IndividualController.java | 34 ++--- .../controller/login/LoginTemplateHelper.java | 11 +- .../IndividualTemplateModel.java | 3 + webapp/web/templates/freemarker/body/home.ftl | 5 + .../body/individual/individual-properties.ftl | 9 ++ .../freemarker/body/individual/individual.ftl | 4 +- .../templates/freemarker/page/page-home.ftl | 2 + webapp/web/templates/freemarker/page/page.ftl | 6 +- 11 files changed, 94 insertions(+), 123 deletions(-) create mode 100644 webapp/web/templates/freemarker/body/home.ftl create mode 100644 webapp/web/templates/freemarker/body/individual/individual-properties.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java index 678728242..6dcc4815b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java @@ -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 root = getSharedVariables(vreq, new HashMap()); - root.putAll(getRootValues(vreq)); + Map root = getPageValues(vreq, new HashMap()); request.setAttribute("ftl_identity", get("identity", root, config)); request.setAttribute("ftl_menu", get("menu", root, config)); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java index bb96d973b..3eccd374f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerHttpServlet.java @@ -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 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 sharedVariables = getSharedVariables(vreq, bodyMap); - - // root is the map used to create the page shell - header, footer, menus, etc. - Map root = new HashMap(sharedVariables); - - // body is the map used to create the page body - Map body = new HashMap(sharedVariables); - root.putAll(getRootValues(vreq)); + Map map = new HashMap(); + 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 getSharedVariables(VitroRequest vreq, Map bodyMap) { - - Map map = new HashMap(); - - 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 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); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java index 26ef160ca..b7472ec67 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java @@ -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 diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualController.java index 104d8c624..14a22f92e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualController.java @@ -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"); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java index 7af6d4c3e..0729fad0c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java @@ -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); } /** diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/IndividualTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/IndividualTemplateModel.java index ff9c51b33..9e3356aaf 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/IndividualTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/IndividualTemplateModel.java @@ -188,5 +188,8 @@ public class IndividualTemplateModel extends BaseTemplateModel { } return models; } + + // public Map< > getPropertyList + // public Map< > getPropertyGroupList } diff --git a/webapp/web/templates/freemarker/body/home.ftl b/webapp/web/templates/freemarker/body/home.ftl new file mode 100644 index 000000000..4965d4a9e --- /dev/null +++ b/webapp/web/templates/freemarker/body/home.ftl @@ -0,0 +1,5 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for home page body contents --> + +<p>This is the Vitro home page.</p> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/individual/individual-properties.ftl b/webapp/web/templates/freemarker/body/individual/individual-properties.ftl new file mode 100644 index 000000000..d04a2b35e --- /dev/null +++ b/webapp/web/templates/freemarker/body/individual/individual-properties.ftl @@ -0,0 +1,9 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for property listing on individual profile page --> + +<#if individual.propertyGroups??> + +<#elseif individual.properties??> + +</#if> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/individual/individual.ftl b/webapp/web/templates/freemarker/body/individual/individual.ftl index 0232d309e..d8df4ded1 100644 --- a/webapp/web/templates/freemarker/body/individual/individual.ftl +++ b/webapp/web/templates/freemarker/body/individual/individual.ftl @@ -71,7 +71,9 @@ </ul> </div> </#if> - + + <#include "individual-properties.ftl"> + <#-- Keywords --> <#if individual.keywords?has_content> <p id="keywords">Keywords: ${individual.keywordString}</p> diff --git a/webapp/web/templates/freemarker/page/page-home.ftl b/webapp/web/templates/freemarker/page/page-home.ftl index a4c9d5965..a401fe61f 100644 --- a/webapp/web/templates/freemarker/page/page-home.ftl +++ b/webapp/web/templates/freemarker/page/page-home.ftl @@ -1,3 +1,5 @@ <#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> +<#-- Page template for home page --> + <#include "page.ftl"> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/page/page.ftl b/webapp/web/templates/freemarker/page/page.ftl index 03bc28fc6..6bb4bc8f4 100644 --- a/webapp/web/templates/freemarker/page/page.ftl +++ b/webapp/web/templates/freemarker/page/page.ftl @@ -22,10 +22,8 @@ <hr class="hidden" /> <div id="contentwrap"> - <div id="content"> - <#-- We don't do title here because some pages don't get a title, or it may not be the same as the <title> text. - <h2>${title}</h2> --> - ${body!} + <div id="content"> + <#include bodyTemplate> </div> <!-- content --> </div> <!-- contentwrap -->