From 3269252aaaeb5979bacd050df25dd69183368cab Mon Sep 17 00:00:00 2001 From: rjy7 Date: Wed, 8 Sep 2010 20:37:28 +0000 Subject: [PATCH] Initial work moving individual profile page to Freemarker. Includes temporary servlet mappings for new Freemarker page. Allow adding script tags to the page head element. --- .../freemarker/FreemarkerHttpServlet.java | 1 + .../freemarker/IndividualController.java | 227 +++++++++--------- .../web/templatemodels/files/Files.java | 4 +- webapp/web/templates/entity/entityBasic.jsp | 3 +- .../templates/freemarker/body/individual.ftl | 24 +- .../freemarker/page/partials/head.ftl | 1 + .../freemarker/page/partials/headScripts.ftl | 5 + .../freemarker/page/partials/scripts.ftl | 2 + .../freemarker/page/partials/stylesheets.ftl | 2 + 9 files changed, 151 insertions(+), 118 deletions(-) create mode 100644 webapp/web/templates/freemarker/page/partials/headScripts.ftl 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 8b40744bd..0701b6eb1 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 @@ -220,6 +220,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { map.put("stylesheets", getStylesheetList(themeDir)); map.put("scripts", getScriptList(themeDir)); + map.put("headScripts", getScriptList(themeDir)); addDirectives(map); 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 b5a7a4569..6aeff4c26 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 @@ -6,10 +6,13 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.mail.Session; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; @@ -43,6 +46,8 @@ 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; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditSubmission; import edu.cornell.mannlib.vitro.webapp.filestorage.model.FileInfo; import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQuery; import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQueryWrapper; @@ -50,6 +55,7 @@ import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper; import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory; import edu.cornell.mannlib.vitro.webapp.web.ContentType; import edu.cornell.mannlib.vitro.webapp.web.jsptags.StringProcessorTag; +import freemarker.template.Configuration; /** * Handles requests for entity information. @@ -58,77 +64,83 @@ import edu.cornell.mannlib.vitro.webapp.web.jsptags.StringProcessorTag; * @author bdc34 * */ - -/* IMPLEMENTATION NOTES - -- See NIHVIVO-512: -build up the list of actually rendered items first. Only then, if there are any, add the label on top. - -*/ - public class IndividualController extends FreemarkerHttpServlet { - - private static final long serialVersionUID = 1L; - private static final Log log = LogFactory.getLog(IndividualController.class.getName()); + 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; + - public void doGet( HttpServletRequest req, HttpServletResponse res ) - throws IOException, ServletException { - try { - super.doGet(req, res); - - VitroRequest vreq = new VitroRequest(req); - //get URL without hostname or servlet context - String url = req.getRequestURI().substring(req.getContextPath().length()); - - //Check to see if the request is for a non-information resource, redirect if it is. - String redirectURL = checkForRedirect ( url, req.getHeader("accept") ); - if( redirectURL != null ){ - doRedirect( req, res, redirectURL ); - return; - } - - ContentType rdfFormat = checkForLinkedDataRequest(url,req.getHeader("accept")); - if( rdfFormat != null ){ - doRdf( vreq, res, rdfFormat ); - return; - } - - Individual indiv = null; - try{ - indiv = getEntityFromRequest( vreq); - }catch(Throwable th){ - doHelp(res); - return; - } - - if( indiv == null || checkForHidden(vreq, indiv) || checkForSunset(vreq, indiv)){ - doNotFound(vreq, res); - return; - } - - // If this is an uploaded file, redirect to its "alias URL". - String aliasUrl = getAliasUrlForBytestreamIndividual(req, indiv); - if (aliasUrl != null) { - res.sendRedirect(req.getContextPath() + aliasUrl); - return; - } - - doHtml( vreq, res , indiv); - return; - - } catch (Throwable e) { - log.error(e); - req.setAttribute("javax.servlet.jsp.jspException",e); - RequestDispatcher rd = req.getRequestDispatcher("/error.jsp"); - rd.forward(req, res); - } + protected String getBody(VitroRequest vreq, Map body, Configuration config) { + try { + + cleanUpSession(vreq); + + // get URL without hostname or servlet context + String url = vreq.getRequestURI().substring(vreq.getContextPath().length()); + + // Title = individual label + + //Check to see if the request is for a non-information resource, redirect if it is. + String redirectURL = checkForRedirect ( url, vreq.getHeader("accept") ); + if( redirectURL != null ){ + //doRedirect( vreq, res, redirectURL ); + return ""; + } + + ContentType rdfFormat = checkForLinkedDataRequest(url,vreq.getHeader("accept")); + if( rdfFormat != null ){ + //doRdf( vreq, res, rdfFormat ); + return ""; + } + + Individual indiv = null; + try{ + indiv = getEntityFromRequest( vreq); + }catch(Throwable th){ + //doHelp(res); + return ""; + } + + if( indiv == null || checkForHidden(vreq, indiv) || checkForSunset(vreq, indiv)){ + //doNotFound(vreq, res); + return ""; + } + + // If this is an uploaded file, redirect to its "alias URL". + String aliasUrl = getAliasUrlForBytestreamIndividual(vreq, indiv); + if (aliasUrl != null) { + //res.sendRedirect(vreq.getContextPath() + aliasUrl); + return ""; + } + + body.put("individual", getIndividualData( vreq, indiv)); + body.put("title", indiv.getName()); + + String bodyTemplate = "individual.ftl"; + return mergeBodyToTemplate(bodyTemplate, body, config); + + } catch (Throwable e) { + log.error(e); + //vreq.setAttribute("javax.servlet.jsp.jspException",e); + // RequestDispatcher rd = vreq.getRequestDispatcher("/error.jsp"); + //rd.forward(vreq, res); + return ""; + } } - private void doHtml(VitroRequest vreq, HttpServletResponse res, Individual indiv) throws ServletException, IOException { + private void cleanUpSession(HttpServletRequest request) { + // Session cleanup: anytime we are at an entity page we shouldn't have an editing config or submission + HttpSession session = request.getSession(); + session.removeAttribute("editjson"); + EditConfiguration.clearAllConfigsInSession(session); + EditSubmission.clearAllEditSubmissionsInSession(session); + } + + private Map getIndividualData(VitroRequest vreq, Individual indiv) throws ServletException, IOException { + Map data = new HashMap(); + IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao(); ObjectPropertyDao opDao = vreq.getWebappDaoFactory().getObjectPropertyDao(); @@ -140,14 +152,14 @@ public class IndividualController extends FreemarkerHttpServlet { if (relatedSubjectUri != null) { Individual relatedSubjectInd = iwDao.getIndividualByURI(relatedSubjectUri); if (relatedSubjectInd != null) { - vreq.setAttribute("relatedSubject", relatedSubjectInd); + data.put("relatedSubject", relatedSubjectInd); } } String relatingPredicateUri = vreq.getParameter("relatingPredicateUri"); if (relatingPredicateUri != null) { ObjectProperty relatingPredicateProp = opDao.getObjectPropertyByURI(relatingPredicateUri); if (relatingPredicateProp != null) { - vreq.setAttribute("relatingPredicate", relatingPredicateProp); + data.put("relatingPredicate", relatingPredicateProp); } } @@ -156,7 +168,7 @@ public class IndividualController extends FreemarkerHttpServlet { String vclassName = "unknown"; String customView = null; - String customCss = null; + if( indiv.getVClass() != null ){ vclassName = indiv.getVClass().getName(); List clasList = indiv.getVClasses(true); @@ -196,48 +208,36 @@ public class IndividualController extends FreemarkerHttpServlet { } String netid = iwDao.getNetId(indiv.getURI()); - vreq.setAttribute("netid", netid); - vreq.setAttribute("vclassName", vclassName); - vreq.setAttribute("entity",indiv); + data.put("netid", netid); + data.put("vclassName", vclassName); + data.put("entity",indiv); Portal portal = vreq.getPortal(); - vreq.setAttribute("portal",String.valueOf(portal)); + data.put("portal",String.valueOf(portal)); String view= getViewFromRequest(vreq); if( view == null){ if (customView == null) { view = default_jsp; - vreq.setAttribute("bodyJsp","/"+Controllers.ENTITY_JSP); + data.put("bodyJsp","/"+Controllers.ENTITY_JSP); log.debug("no custom view and no view parameter in request for rendering "+indiv.getName()); } else { view = default_jsp; log.debug("setting custom view templates/entity/"+ customView + " for rendering "+indiv.getName()); - vreq.setAttribute("bodyJsp", "/templates/entity/"+customView); + data.put("bodyJsp", "/templates/entity/"+customView); } - vreq.setAttribute("entityPropsListJsp",Controllers.ENTITY_PROP_LIST_JSP); - vreq.setAttribute("entityDatapropsListJsp",Controllers.ENTITY_DATAPROP_LIST_JSP); - vreq.setAttribute("entityMergedPropsListJsp",Controllers.ENTITY_MERGED_PROP_LIST_GROUPED_JSP); - vreq.setAttribute("entityKeywordsListJsp",Controllers.ENTITY_KEYWORDS_LIST_JSP); + data.put("entityPropsListJsp",Controllers.ENTITY_PROP_LIST_JSP); + data.put("entityDatapropsListJsp",Controllers.ENTITY_DATAPROP_LIST_JSP); + data.put("entityMergedPropsListJsp",Controllers.ENTITY_MERGED_PROP_LIST_GROUPED_JSP); + data.put("entityKeywordsListJsp",Controllers.ENTITY_KEYWORDS_LIST_JSP); } else { log.debug("Found view parameter "+view+" in request for rendering "+indiv.getName()); } - //set title before we do the highlighting so we don't get markup in it. - vreq.setAttribute("title",indiv.getName()); + //setup highlighter for search terms checkForSearch(vreq, indiv); - // set CSS and script elements - String contextPath = ""; - if (vreq.getContextPath().length()>1) { - contextPath = vreq.getContextPath(); - } - String css = "\n" - + " \n"; - if (customCss!=null) { - css += customCss; - } if( indiv.getURI().startsWith( vreq.getWebappDaoFactory().getDefaultNamespace() )){ - vreq.setAttribute("entityLinkedDataURL", indiv.getURI() + "/" + indiv.getLocalName() + ".rdf"); + data.put("entityLinkedDataURL", indiv.getURI() + "/" + indiv.getLocalName() + ".rdf"); } @@ -247,11 +247,11 @@ public class IndividualController extends FreemarkerHttpServlet { // String individualToRDF = "http://"+vreq.getServerName()+":"+vreq.getServerPort()+vreq.getContextPath()+"/entity?home=1&uri="+forURL(entity.getURI())+"&view=rdf.rdf"; //css += ""; - vreq.setAttribute("css",css); - vreq.setAttribute("scripts", "/templates/entity/entity_inject_head.jsp"); - RequestDispatcher rd = vreq.getRequestDispatcher( view ); - rd.forward(vreq,res); + //RequestDispatcher rd = vreq.getRequestDispatcher( view ); + //rd.forward(vreq,res); + + return data; } private void doRdf(VitroRequest vreq, HttpServletResponse res, @@ -294,8 +294,8 @@ public class IndividualController extends FreemarkerHttpServlet { } - private static Pattern LINKED_DATA_URL = Pattern.compile("^/individual/([^/]*)$"); - private static Pattern NS_PREFIX_URL = Pattern.compile("^/individual/([^/]*)/([^/]*)$"); + private static Pattern LINKED_DATA_URL = Pattern.compile("^/individualfm/([^/]*)$"); + private static Pattern NS_PREFIX_URL = Pattern.compile("^/individualfm/([^/]*)/([^/]*)$"); /** Gets the entity id from the request. @@ -408,7 +408,7 @@ public class IndividualController extends FreemarkerHttpServlet { } - private static Pattern URI_PATTERN = Pattern.compile("^/individual/([^/]*)$"); + private static Pattern URI_PATTERN = Pattern.compile("^/individualfm/([^/]*)$"); //Redirect if the request is for http://hostname/individual/localname // if accept is nothing or text/html redirect to ??? // if accept is some RDF thing redirect to the URL for RDF @@ -433,16 +433,16 @@ public class IndividualController extends FreemarkerHttpServlet { } } - private static Pattern RDF_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.rdf$"); - private static Pattern N3_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.n3$"); - private static Pattern TTL_REQUEST = Pattern.compile("^/individual/([^/]*)/\\1.ttl$"); - private static Pattern HTML_REQUEST = Pattern.compile("^/display/([^/]*)$"); + private static Pattern RDF_REQUEST = Pattern.compile("^/individualfm/([^/]*)/\\1.rdf$"); + private static Pattern N3_REQUEST = Pattern.compile("^/individualfm/([^/]*)/\\1.n3$"); + private static Pattern TTL_REQUEST = Pattern.compile("^/individualfm/([^/]*)/\\1.ttl$"); + private static Pattern HTML_REQUEST = Pattern.compile("^/displayfm/([^/]*)$"); /** * @return null if this is not a linked data request, returns content type if it is a * linked data request. */ - private ContentType checkForLinkedDataRequest(String url, String acceptHeader) { + protected ContentType checkForLinkedDataRequest(String url, String acceptHeader) { try { //check the accept header if (acceptHeader != null) { @@ -495,15 +495,14 @@ public class IndividualController extends FreemarkerHttpServlet { // TODO Auto-generated method stub return false; } - - /** + + /** * If this entity represents a File Bytestream, get its alias URL so we can * properly serve the file contents. */ - private String getAliasUrlForBytestreamIndividual(HttpServletRequest req, Individual entity) + private String getAliasUrlForBytestreamIndividual(VitroRequest vreq, Individual entity) throws IOException { - FileInfo fileInfo = FileInfo.instanceFromBytestreamUri(new VitroRequest( - req).getWebappDaoFactory(), entity.getURI()); + FileInfo fileInfo = FileInfo.instanceFromBytestreamUri(vreq.getWebappDaoFactory(), entity.getURI()); if (fileInfo == null) { log.trace("Entity '" + entity.getURI() + "' is not a bytestream."); return null; @@ -615,7 +614,7 @@ public class IndividualController extends FreemarkerHttpServlet { out.println("

id is the id of the entity to query for. netid also works.

"); out.println(""); } - + private void doNotFound(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { VitroRequest vreq = new VitroRequest(req); @@ -702,16 +701,14 @@ public class IndividualController extends FreemarkerHttpServlet { rd.forward(req,res); } - private String forURL(String frag) - { - String result = null; - try - { - result = URLEncoder.encode(frag, "UTF-8"); + private String forURL(String frag) { + String result = null; + try { + result = URLEncoder.encode(frag, "UTF-8"); } catch (UnsupportedEncodingException ex) { throw new RuntimeException("UTF-8 not supported", ex); } - return result; + return result; } private class HelpException extends Throwable{} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java index 381ef14f7..fa0f471f7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java @@ -25,7 +25,9 @@ public abstract class Files extends BaseTemplateModel { } public void add(String path) { - list.add(getUrl(path)); + // Allow for an external url + String url = path.startsWith("http://") ? path : getUrl(path); + list.add(url); } public void add(String... paths) { diff --git a/webapp/web/templates/entity/entityBasic.jsp b/webapp/web/templates/entity/entityBasic.jsp index 409306998..7f006b7d1 100644 --- a/webapp/web/templates/entity/entityBasic.jsp +++ b/webapp/web/templates/entity/entityBasic.jsp @@ -314,4 +314,5 @@ if (VitroRequestPrep.isSelfEditing(request) || LoginFormBean.loggedIn(request, L - \ No newline at end of file + + diff --git a/webapp/web/templates/freemarker/body/individual.ftl b/webapp/web/templates/freemarker/body/individual.ftl index 841b7965f..c023ed4ca 100644 --- a/webapp/web/templates/freemarker/body/individual.ftl +++ b/webapp/web/templates/freemarker/body/individual.ftl @@ -1 +1,23 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> \ No newline at end of file +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for individual profile page --> + +

${title}

+ +test + +${stylesheets.addFromTheme("/css/entity.css", + "/css/visualization/visualization.css")} + +<#-- RY Figure out which of these scripts really need to go into the head, and which are needed at all (e.g., tinyMCE??) --> +${headScripts.add("/js/jquery.js", + "/js/jquery_plugins/getUrlParam.js", + "/js/jquery_plugins/colorAnimations.js", + "/js/propertyGroupSwitcher.js", + "/js/jquery_plugins/jquery.form.js", + "/js/tiny_mce/tiny_mce.js", + "/js/controls.js", + "http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221%22%2C%22packages%22%3A%5B%22areachart%22%2C%22imagesparkline%22%5D%7D%5D%7D", + "/js/toggle.js")} + +${scripts.add("/js/imageUpload/imageUploadUtils.js")} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/page/partials/head.ftl b/webapp/web/templates/freemarker/page/partials/head.ftl index 03e28da02..b6802a671 100644 --- a/webapp/web/templates/freemarker/page/partials/head.ftl +++ b/webapp/web/templates/freemarker/page/partials/head.ftl @@ -3,4 +3,5 @@ <#include "title.ftl"> <#include "stylesheets.ftl"> + <#include "headScripts.ftl"> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/page/partials/headScripts.ftl b/webapp/web/templates/freemarker/page/partials/headScripts.ftl new file mode 100644 index 000000000..0de318d64 --- /dev/null +++ b/webapp/web/templates/freemarker/page/partials/headScripts.ftl @@ -0,0 +1,5 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for scripts that must be loaded in the head --> + +${headScripts.tags} \ No newline at end of file diff --git a/webapp/web/templates/freemarker/page/partials/scripts.ftl b/webapp/web/templates/freemarker/page/partials/scripts.ftl index da0e5ac59..54d93d7bd 100644 --- a/webapp/web/templates/freemarker/page/partials/scripts.ftl +++ b/webapp/web/templates/freemarker/page/partials/scripts.ftl @@ -1,5 +1,7 @@ <#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> +<#-- Template for scripts loaded at the end of the body element --> + ${scripts.tags} <#include "googleAnalytics.ftl"> diff --git a/webapp/web/templates/freemarker/page/partials/stylesheets.ftl b/webapp/web/templates/freemarker/page/partials/stylesheets.ftl index 234e781f6..9a393dc10 100644 --- a/webapp/web/templates/freemarker/page/partials/stylesheets.ftl +++ b/webapp/web/templates/freemarker/page/partials/stylesheets.ftl @@ -1,5 +1,7 @@ <#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> +<#-- Template for loading stylesheets in the head --> +