From 68e5ebefc236823ada851c9f40a0b894d6f5e2f6 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Wed, 22 Sep 2010 18:06:57 +0000 Subject: [PATCH] NIHVIVO-650 Some reorganization of template model classes. Fix for link list display on individual profile page. --- .../freemarker/FreemarkerHttpServlet.java | 1 - .../freemarker/IndividualController.java | 112 ++++++++---------- .../freemarker/IndividualListController.java | 8 +- .../controller/freemarker/UrlBuilder.java | 5 + .../FreemarkerPagedSearchController.java | 6 +- .../web/templatemodels/BaseTemplateModel.java | 9 -- .../IndividualTemplateModel.java | 66 +++++++++-- .../web/templatemodels/menu/MainMenu.java | 2 +- .../freemarker/body/individual/individual.ftl | 19 +-- .../freemarker/body/individualList.ftl | 4 +- .../individual/individual-adminPanel.ftl | 2 +- 11 files changed, 125 insertions(+), 109 deletions(-) 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 f7bfced2f..e7839f942 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 @@ -81,7 +81,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { try { VitroRequest vreq = new VitroRequest(request); - BaseTemplateModel.setVitroRequest(vreq); Configuration config = getConfig(vreq); vreq.setAttribute("freemarkerConfig", config); 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 016273c0d..fd7470493 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 @@ -76,10 +76,7 @@ public class IndividualController extends FreemarkerHttpServlet { @Override protected ResponseValues processRequest(VitroRequest vreq) { try { - - HttpSession session = vreq.getSession(); - - cleanUpSession(session); + cleanUpSession(vreq); // get URL without hostname or servlet context String url = vreq.getRequestURI().substring(vreq.getContextPath().length()); @@ -114,11 +111,13 @@ public class IndividualController extends FreemarkerHttpServlet { Map body = new HashMap(); - int securityLevel = getSecurityLevel(session); - UrlBuilder urlBuilder = new UrlBuilder(vreq.getPortal()); - body.put("editStatus", getEditingData(vreq, securityLevel, individual, urlBuilder)); - body.put("title", individual.getName()); - body.putAll(getIndividualData(vreq, individual)); + body.put("editStatus", getEditingData(vreq)); + + body.put("title", individual.getName()); + + body.put("relatedSubject", getRelatedSubject(vreq)); + + body.put("individual", getIndividualTemplateModel(vreq, individual)); return new TemplateResponseValues(TEMPLATE_INDIVIDUAL, body); @@ -128,8 +127,9 @@ public class IndividualController extends FreemarkerHttpServlet { } } - private void cleanUpSession(HttpSession session) { + private void cleanUpSession(VitroRequest vreq) { // Session cleanup: anytime we are at an entity page we shouldn't have an editing config or submission + HttpSession session = vreq.getSession(); session.removeAttribute("editjson"); EditConfiguration.clearAllConfigsInSession(session); EditSubmission.clearAllEditSubmissionsInSession(session); @@ -149,27 +149,31 @@ public class IndividualController extends FreemarkerHttpServlet { } - private Map getEditingData(VitroRequest vreq, int securityLevel, Individual individual, UrlBuilder urlBuilder) { - // Set values related to access privileges + // Set template values related to access privileges + // RY We may want to define an EditingIndividualTemplateModel class, with methods like getAdminPanel() and + // getEditLinks(property). The constructor would take an individual and a loginFormBean object, both of which + // are needed to generate property edit links. Another idea is to subclass IndividualTemplateModel with + // EditableIndividualTemplateModel, and define editing-related methods there. However, that means in the + // template we will have expressions like individual.adminPanel or individual.editingLinks(property), + // which might seem opaque to template authors. + private Map getEditingData(VitroRequest vreq) { + + int securityLevel = getSecurityLevel(vreq.getSession()); + Map editingData = new HashMap(); editingData.put("showEditLinks", VitroRequestPrep.isSelfEditing(vreq) || securityLevel >= LoginFormBean.NON_EDITOR); boolean showAdminPanel = securityLevel >= LoginFormBean.EDITOR; editingData.put("showAdminPanel", showAdminPanel); - if (showAdminPanel) { - - editingData.put("editingUrl", urlBuilder.getPortalUrl("/entityEdit", "uri", individual.getURI())); - } - return editingData; - + return editingData; } - private Map getIndividualData(VitroRequest vreq, Individual individual) throws ServletException, IOException { - Map map = new HashMap(); - - IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao(); + private Map getRelatedSubject(VitroRequest vreq) { + Map map = null; + + IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao(); ObjectPropertyDao opDao = vreq.getWebappDaoFactory().getObjectPropertyDao(); // Check if a "relatedSubjectUri" parameter has been supplied, and, @@ -178,22 +182,30 @@ public class IndividualController extends FreemarkerHttpServlet { // be displayed in the context of their relationship to another. String relatedSubjectUri = vreq.getParameter("relatedSubjectUri"); if (relatedSubjectUri != null) { - Individual relatedSubjectInd = iwDao.getIndividualByURI(relatedSubjectUri); - if (relatedSubjectInd != null) { - Map relatedSubject = new HashMap(); - relatedSubject.put("name", relatedSubjectInd.getName()); - relatedSubject.put("url", (new IndividualTemplateModel(relatedSubjectInd)).getProfileUrl()); + Individual relatedSubjectInd = iwDao.getIndividualByURI(relatedSubjectUri); + if (relatedSubjectInd != null) { + map = new HashMap(); + map.put("name", relatedSubjectInd.getName()); + map.put("url", (new IndividualTemplateModel(relatedSubjectInd, vreq)).getProfileUrl()); String relatingPredicateUri = vreq.getParameter("relatingPredicateUri"); if (relatingPredicateUri != null) { - ObjectProperty relatingPredicateProp = opDao.getObjectPropertyByURI(relatingPredicateUri); - if (relatingPredicateProp != null) { - relatedSubject.put("relatingPredicateDomainPublic", relatingPredicateProp.getDomainPublic()); - } + ObjectProperty relatingPredicateProp = opDao.getObjectPropertyByURI(relatingPredicateUri); + if (relatingPredicateProp != null) { + map.put("relatingPredicateDomainPublic", relatingPredicateProp.getDomainPublic()); + } } - map.put("relatedSubject", relatedSubject); - } + } } - + return map; + } + + private IndividualTemplateModel getIndividualTemplateModel(VitroRequest vreq, Individual individual) + throws ServletException, IOException { + + IndividualDao iwDao = vreq.getWebappDaoFactory().getIndividualDao(); + + int securityLevel = getSecurityLevel(vreq.getSession()); + individual.setKeywords(iwDao.getKeywordsForIndividualByMode(individual.getURI(),"visible")); individual.sortForDisplay(); @@ -237,18 +249,11 @@ public class IndividualController extends FreemarkerHttpServlet { // 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 // } - - map.put("individual", new IndividualTemplateModel(individual)); //setup highlighter for search terms //checkForSearch(vreq, individual); - - if( individual.getURI().startsWith( vreq.getWebappDaoFactory().getDefaultNamespace() )){ - map.put("entityLinkedDataURL", individual.getURI() + "/" + individual.getLocalName() + ".rdf"); - } - - return map; + return new IndividualTemplateModel(individual, vreq); } private ResponseValues doRdf(VitroRequest vreq, Individual individual, @@ -266,15 +271,6 @@ public class IndividualController extends FreemarkerHttpServlet { return new RdfResponseValues(rdfFormat, newModel); } - private void doRedirect(HttpServletRequest req, HttpServletResponse res, - String redirectURL) { - // It seems like there must be a better way to do this - String hn = req.getHeader("Host"); - res.setHeader("Location", res.encodeURL( "http://" + hn + req.getContextPath() + redirectURL )); - res.setStatus(res.SC_SEE_OTHER); - } - - private static Pattern LINKED_DATA_URL = Pattern.compile("^/individualfm/([^/]*)$"); private static Pattern NS_PREFIX_URL = Pattern.compile("^/individualfm/([^/]*)/([^/]*)$"); @@ -666,17 +662,5 @@ public class IndividualController extends FreemarkerHttpServlet { return new TemplateResponseValues(Template.TITLED_ERROR_MESSAGE.toString(), body, HttpServletResponse.SC_NOT_FOUND); } - - 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; - } - - private class HelpException extends Throwable{} - private class EntityNotFoundException extends Throwable{} + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java index 1dfe7943a..b1d932eb4 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java @@ -64,16 +64,13 @@ public class IndividualListController extends FreemarkerHttpServlet { if (vclass != null) { // Create list of individual view objects List individualList = vreq.getWebappDaoFactory().getIndividualDao().getIndividualsByVClass(vclass); - List individuals = new ArrayList(individualList.size()); if (individualList == null) { // RY Is this really an error? log.error("individuals list is null"); message = "No individuals to display."; } else { - for (Individual i: individualList) { - individuals.add(new IndividualTemplateModel(i)); - } + body.put("individuals", IndividualTemplateModel.getIndividualTemplateModelList(individualList, vreq)); } // Set title and subtitle. Title will be retrieved later in getTitle(). @@ -85,9 +82,8 @@ public class IndividualListController extends FreemarkerHttpServlet { title = classGroup.getPublicName(); body.put("subtitle", vclass.getName()); } - body.put("title", title); + body.put("title", title); - body.put("individuals", individuals); } } catch (HelpException help){ diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java index 767dbd239..bf50f1e3c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/UrlBuilder.java @@ -29,6 +29,7 @@ public class UrlBuilder { BROWSE("/browse"), CONTACT("/contact"), INDIVIDUAL("/individual"), + INDIVIDUAL_EDIT("/entityEdit"), INDIVIDUAL_LIST("/individuallist"), LOGIN("/siteAdmin"), LOGOUT("/login_process.jsp"), @@ -145,6 +146,10 @@ public class UrlBuilder { public String getPortalUrl(Route route, ParamMap params) { return getPortalUrl(route.path(), params); } + + public String getPortalUrl(Route route, String...params) { + return getPortalUrl(route.path(), params); + } public static class ParamMap extends HashMap { private static final long serialVersionUID = 1L; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java index 190af6a61..380da7cfe 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java @@ -265,11 +265,7 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple new SimpleLuceneHighlighter(query,analyzer) ); // Convert search result individuals to template model objects - List individuals = new ArrayList(beans.size()); - for (Individual i : beans) { - individuals.add(new IndividualTemplateModel(i)); - } - body.put("individuals", individuals); + body.put("individuals", IndividualTemplateModel.getIndividualTemplateModelList(beans, vreq)); body.put("querytext", qtxt); body.put("title", qtxt+" - "+portal.getAppName()+" Search Results" ); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java index cdb54d50d..5d8c617e2 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java @@ -19,7 +19,6 @@ public abstract class BaseTemplateModel { private static final Log log = LogFactory.getLog(BaseTemplateModel.class); protected static ServletContext servletContext = null; - protected static VitroRequest vreq = null; // Wrap UrlBuilder method so templates can call ${item.url} public String getUrl(String path) { @@ -44,14 +43,6 @@ public abstract class BaseTemplateModel { servletContext = context; } - public static VitroRequest getVitroRequest() { - return vreq; - } - - public static void setVitroRequest(VitroRequest vrequest) { - vreq = vrequest; - } - public String dump() { return toString(); // fallback when subclass doesn't define a class-specific dump() } 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 740b696e0..ff9c51b33 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 @@ -12,9 +12,10 @@ import org.openrdf.model.impl.URIImpl; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Link; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route; -import edu.cornell.mannlib.vitro.webapp.utils.StringUtils; import edu.cornell.mannlib.vitro.webapp.web.ViewFinder; import edu.cornell.mannlib.vitro.webapp.web.ViewFinder.ClassView; @@ -24,10 +25,22 @@ public class IndividualTemplateModel extends BaseTemplateModel { private static final String PATH = Route.INDIVIDUAL.path(); - private Individual individual; + protected Individual individual; + protected VitroRequest vreq; + protected UrlBuilder urlBuilder; - public IndividualTemplateModel(Individual individual) { + // private PropertyListTemplateModel propertyList; + + // RY The IndividualTemplateModel object needs access to the request object. + // The only other template model that does is MainMenu. We could provide an + // interface for RequestAware template models, but they still wouldn't share any code. + // If they both derive from a common RequestAwareTemplateModel class, we might be + // locking ourselves in too tightly to that class hierarchy. + public IndividualTemplateModel(Individual individual, VitroRequest vreq) { this.individual = individual; + this.vreq = vreq; + // Needed for getting portal-sensitive urls. Remove if multi-portal support is removed. + this.urlBuilder = new UrlBuilder(vreq.getPortal()); } /* These methods perform some manipulation of the data returned by the Individual methods */ @@ -60,15 +73,6 @@ public class IndividualTemplateModel extends BaseTemplateModel { return isPerson() ? getUrl(Route.VISUALIZATION.path(), "uri", getUri()) : null; } - // RY We should not have references to a specific ontology in the vitro code! - // Figure out how to move this out of here. - // We could subclass IndividualTemplateModel in the VIVO code and add the isPerson() - // and getVisualizationUrl() methods there, but we still need to know whether to - // instantiate the IndividualTemplateModel or the VivoIndividualTemplateModel class. - public boolean isPerson() { - return individual.isVClass("http://xmlns.com/foaf/0.1/Person"); - } - public String getImageUrl() { String imageUrl = individual.getImageUrl(); return imageUrl == null ? null : getUrl(imageUrl); @@ -78,7 +82,26 @@ public class IndividualTemplateModel extends BaseTemplateModel { String thumbUrl = individual.getThumbUrl(); return thumbUrl == null ? null : getUrl(thumbUrl); } - + + public String getLinkedDataUrl() { + String defaultNamespace = vreq.getWebappDaoFactory().getDefaultNamespace(); + String uri = getUri(); + return uri.startsWith(defaultNamespace) ? uri + "/" + getLocalName() + ".rdf" : null; + } + + public String getEditUrl() { + return urlBuilder.getPortalUrl(Route.INDIVIDUAL_EDIT, "uri", getUri()); + } + + // RY We should not have references to a specific ontology in the vitro code! + // Figure out how to move this out of here. + // We could subclass IndividualTemplateModel in the VIVO code and add the isPerson() + // and getVisualizationUrl() methods there, but we still need to know whether to + // instantiate the IndividualTemplateModel or the VivoIndividualTemplateModel class. + public boolean isPerson() { + return individual.isVClass("http://xmlns.com/foaf/0.1/Person"); + } + public String getSearchView() { return getView(ClassView.SEARCH); } @@ -149,4 +172,21 @@ public class IndividualTemplateModel extends BaseTemplateModel { return individual.getKeywords(); } + public String getKeywordString() { + // Since this is a display method, the implementation should be moved out of IndividualImpl to here. + return individual.getKeywordString(); + } + + public String getLocalName() { + return individual.getLocalName(); + } + + public static List getIndividualTemplateModelList(List individuals, VitroRequest vreq) { + List models = new ArrayList(individuals.size()); + for (Individual individual : individuals) { + models.add(new IndividualTemplateModel(individual, vreq)); + } + return models; + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/menu/MainMenu.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/menu/MainMenu.java index ef5660e18..0e2d931fb 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/menu/MainMenu.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/menu/MainMenu.java @@ -15,7 +15,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; public class MainMenu extends Menu { private static final long serialVersionUID = 1L; - private static final Log log = LogFactory.getLog(MainMenu.class.getName()); + private static final Log log = LogFactory.getLog(MainMenu.class); protected VitroRequest vreq; diff --git a/webapp/web/templates/freemarker/body/individual/individual.ftl b/webapp/web/templates/freemarker/body/individual/individual.ftl index 6ebd034ad..01d0882e9 100644 --- a/webapp/web/templates/freemarker/body/individual/individual.ftl +++ b/webapp/web/templates/freemarker/body/individual/individual.ftl @@ -59,18 +59,23 @@ <#if individual.links?has_content> - + + + <#-- Keywords --> + <#if individual.keywords?has_content> +

Keywords: ${individual.keywordString}

+ diff --git a/webapp/web/templates/freemarker/body/individualList.ftl b/webapp/web/templates/freemarker/body/individualList.ftl index 67a172a38..3539db3c7 100644 --- a/webapp/web/templates/freemarker/body/individualList.ftl +++ b/webapp/web/templates/freemarker/body/individualList.ftl @@ -25,9 +25,9 @@ ${individual.name}
    <@l.firstLastList> - <#if individual.moniker??>
  • ${individual.moniker}
  • , + <#if individual.moniker??>
  • ${individual.moniker}
  • <#list individual.links as link> -
  • ${link.anchor}
  • , +
  • ${link.anchor}
diff --git a/webapp/web/templates/freemarker/body/partials/individual/individual-adminPanel.ftl b/webapp/web/templates/freemarker/body/partials/individual/individual-adminPanel.ftl index 9a91dcae7..ec1e60357 100644 --- a/webapp/web/templates/freemarker/body/partials/individual/individual-adminPanel.ftl +++ b/webapp/web/templates/freemarker/body/partials/individual/individual-adminPanel.ftl @@ -5,7 +5,7 @@

Admin Panel

- edit this individual + edit this individual

Resource URI: ${individual.uri}

\ No newline at end of file