From af3ca17ef1105eaf24e1cafd2c42ea2f6bc9efb6 Mon Sep 17 00:00:00 2001 From: j2blake Date: Tue, 1 May 2012 16:29:36 +0000 Subject: [PATCH] NIHVIVO-2411 Modify ClassGroupPageData.java and browseByVClass.js to use the new BROWSE short view. This means that the VIVO-specific browseByVClassPeople.js and menupage--classgroup-people.ftl can be replaced by a VIVO-specific short view. --- .../FakeApplicationOntologyService.java | 122 ++++++++++++++++-- .../utils/dataGetter/ClassGroupPageData.java | 13 +- webapp/web/js/menupage/browseByVClass.js | 58 +-------- .../shortview/view-browse-default.ftl | 37 ++++++ 4 files changed, 152 insertions(+), 78 deletions(-) create mode 100644 webapp/web/templates/freemarker/body/partials/shortview/view-browse-default.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/services/shortview/FakeApplicationOntologyService.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/services/shortview/FakeApplicationOntologyService.java index 3abb738ce..46e322645 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/services/shortview/FakeApplicationOntologyService.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/services/shortview/FakeApplicationOntologyService.java @@ -2,7 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.services.shortview; -import static edu.cornell.mannlib.vitro.webapp.services.shortview.ShortViewService.ShortViewContext.SEARCH; +import static edu.cornell.mannlib.vitro.webapp.services.shortview.ShortViewService.ShortViewContext.BROWSE; import java.util.Arrays; import java.util.HashMap; @@ -12,8 +12,23 @@ import java.util.Set; import javax.servlet.ServletContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.ontology.OntModelSpec; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.Resource; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.VClassDao; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetter; +import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter; /** * TODO @@ -21,20 +36,31 @@ import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetter; * Get rid of this when the Application Ontology is implemented. */ public class FakeApplicationOntologyService { + private static final Log log = LogFactory + .getLog(FakeApplicationOntologyService.class); + private static final String FACULTY_MEMBER_CLASS_URI = "http://vivoweb.org/ontology/core#FacultyMember"; + private static final String PEOPLE_CLASSGROUP_URI = "http://vivoweb.org/ontology#vitroClassGrouppeople"; /** * Return the template name and DataGetter instances associated with this * class and this short view context. If none, return null. */ - public TemplateAndDataGetters getShortViewProperties(String classUri, - String contextName) { -// if ((SEARCH.name().equals(contextName)) -// && (classUri.equals(FACULTY_MEMBER_CLASS_URI))) { -// return new TemplateAndDataGetters("view-search-faculty.ftl", new FakeFacultyDataGetter()); -// } else { - return null; -// } + public TemplateAndDataGetters getShortViewProperties(WebappDaoFactory wadf, + Individual individual, String classUri, String contextName) { + if ((BROWSE.name().equals(contextName)) + && (isClassInPeopleClassGroup(wadf, classUri))) { + return new TemplateAndDataGetters("view-browse-people.ftl", + new FakeVivoPeopleDataGetter(individual.getURI())); + } + // A mockup of Tammy's use case. + // if ((SEARCH.name().equals(contextName)) + // && (classUri.equals(FACULTY_MEMBER_CLASS_URI))) { + // return new TemplateAndDataGetters("view-search-faculty.ftl", new + // FakeFacultyDataGetter()); + // } else { + return null; + // } } /** The info associated with a short view. */ @@ -59,6 +85,36 @@ public class FakeApplicationOntologyService { } + private boolean isClassInPeopleClassGroup(WebappDaoFactory wadf, + String classUri) { + if (wadf == null) { + log.debug("isClassInPeopleClassGroup: WebappDaoFactory is null."); + return false; + } + + VClassDao vcDao = wadf.getVClassDao(); + if (vcDao == null) { + log.debug("isClassInPeopleClassGroup: VClassDao is null."); + return false; + } + + VClass vclass = vcDao.getVClassByURI(classUri); + if (vclass == null) { + log.debug("isClassInPeopleClassGroup: VClass is null."); + return false; + } + + String vclassGroupUri = vclass.getGroupURI(); + if (vclassGroupUri == null) { + log.debug("isClassInPeopleClassGroup: vclassGroupUri is null."); + return false; + } + + boolean isPeople = PEOPLE_CLASSGROUP_URI.equals(vclassGroupUri); + log.debug("isClassInPeopleClassGroup: isPeople = " + isPeople); + return isPeople; + } + private static class FakeFacultyDataGetter implements DataGetter { @Override public Map getData(ServletContext context, @@ -69,6 +125,52 @@ public class FakeApplicationOntologyService { map.put("extra", extras); return map; } - } + + /** + * A SPARQL query data getter that initializes itself from its own private + * "display model". The query finds a preferred title for the individual. + */ + private static class FakeVivoPeopleDataGetter extends SparqlQueryDataGetter { + private static final String QUERY_STRING = "SELECT ?uri ?pt WHERE {\n" + + " ?uri ?pt\n" + + "} LIMIT 1"; + + private static final String FAKE_VIVO_PEOPLE_DATA_GETTER_URI = "http://FakeVivoPeopleDataGetter"; + + private static OntModel fakeDisplayModel = initializeFakeDisplayModel(); + + private static OntModel initializeFakeDisplayModel() { + OntModel m = ModelFactory + .createOntologyModel(OntModelSpec.OWL_DL_MEM); + + Resource dataGetter = m + .getResource(FAKE_VIVO_PEOPLE_DATA_GETTER_URI); + Property queryProperty = m.getProperty(DisplayVocabulary.QUERY); + Property saveToVarProperty = m + .getProperty(DisplayVocabulary.SAVE_TO_VAR); + + m.add(dataGetter, queryProperty, QUERY_STRING); + m.add(dataGetter, saveToVarProperty, "extra"); + return m; + } + + private String individualUri; + + public FakeVivoPeopleDataGetter(String individualUri) { + super(fakeDisplayModel, "http://FakeVivoPeopleDataGetter"); + this.individualUri = individualUri; + } + + @Override + public Map getData(ServletContext context, + VitroRequest vreq, Map pageData) { + Map parms = new HashMap(); + parms.put("uri", new String[] { individualUri }); + + return doQuery(parms, getModel(context, vreq, null)); + } + + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/ClassGroupPageData.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/ClassGroupPageData.java index 70f2200e3..a019d52dc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/ClassGroupPageData.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/ClassGroupPageData.java @@ -12,18 +12,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONObject; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.QuerySolution; -import com.hp.hpl.jena.query.QuerySolutionMap; -import com.hp.hpl.jena.query.ResultSet; -import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.shared.Lock; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; @@ -164,7 +153,7 @@ public class ClassGroupPageData extends DataGetterBase implements DataGetter{ //Get data servuice public String getDataServiceUrl() { - return UrlBuilder.getUrl("/dataservice?getSolrIndividualsByVClass=1&vclassId="); + return UrlBuilder.getUrl("/dataservice?getRenderedSolrIndividualsByVClass=1&vclassId="); } /** diff --git a/webapp/web/js/menupage/browseByVClass.js b/webapp/web/js/menupage/browseByVClass.js index 2cafc0ff4..8021ced64 100644 --- a/webapp/web/js/menupage/browseByVClass.js +++ b/webapp/web/js/menupage/browseByVClass.js @@ -105,36 +105,8 @@ var browseByVClass = { } else { var vclassName = results.vclass.name; $.each(results.individuals, function(i, item) { - var individual, - label, - mostSpecificTypes, - uri, - profileUrl, - image, - listItem; - - individual = results.individuals[i]; - label = individual.label; - mostSpecificTypes = individual.mostSpecificTypes; - moreInfo = browseByVClass.getMoreInfo(mostSpecificTypes, vclassName); - uri = individual.URI; - profileUrl = individual.profileUrl; - if ( individual.thumbUrl ) { - image = browseByVClass.baseUrl + individual.thumbUrl; - } - // Build the content of each list item, piecing together each component - listItem = '
  • '; - if ( typeof individual.thumbUrl !== "undefined" ) { - listItem += ''+ label +'

    '; - } else { - listItem += '

    '; - } - listItem += ''+ label +'

    '; - if ( moreInfo != '' ) { - listItem += ''+ moreInfo +''; - } - listItem += '
  • '; - individualList += listItem; + var individual = results.individuals[i]; + individualList += individual.shortViewHtml; }) // Remove existing content @@ -158,32 +130,6 @@ var browseByVClass = { }); }, - // Handle mostSpecificType as array - // * remove requested class for redundancy - // * allow override by another property (passed as argument) - getMoreInfo: function(mostSpecificTypes, requestedClass, override) { - var requestedClassIndex = $.inArray(requestedClass, mostSpecificTypes); - if ( requestedClassIndex > -1 ) { - mostSpecificTypes.splice(requestedClassIndex, 1); - } - var mostSpecificTypeCount = mostSpecificTypes.length; - - if ( typeof override !== "undefined" ) { - return override; - } else { - if ( mostSpecificTypeCount > 1 ) { - var assembledList = '
      '; - $.each(mostSpecificTypes, function(i, item) { - assembledList += '
    • '+ item +'
    • '; - }) - assembledList += '
    '; - return assembledList; - } else { - return mostSpecificTypes; - } - } - }, - // getPageScroll() by quirksmode.org getPageScroll: function() { var xScroll, yScroll; diff --git a/webapp/web/templates/freemarker/body/partials/shortview/view-browse-default.ftl b/webapp/web/templates/freemarker/body/partials/shortview/view-browse-default.ftl new file mode 100644 index 000000000..a1aec83aa --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/shortview/view-browse-default.ftl @@ -0,0 +1,37 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Default individual browse view --> + +<#import "lib-properties.ftl" as p> + +
  • + +<#if (individual.thumbUrl)??> + ${individual.name} +

    + ${individual.name} +

    +<#else> +

    + ${individual.name} +

    + + +<#assign typesString> + [<#list individual.mostSpecificTypes as type><#if type != vclass>,"${type}"] + +<#assign cleanTypes = typesString?replace("[,", "[")?eval > + +<#if cleanTypes?size == 1> + ${cleanTypes[0]} +<#elseif (cleanTypes?size > 1) > + +
      + <#list cleanTypes as type> +
    • ${type}
    • + +
    +
    + +
  • +