NIHVIVO-3746 Change DataGetter.getData() so it doesn't require VitroRequest. Modify implementations of DataGetter to conform.

This commit is contained in:
j2blake 2012-05-02 17:46:08 +00:00
parent a26f0c2809
commit 3250efd091
10 changed files with 87 additions and 73 deletions

View file

@ -31,7 +31,7 @@ public class HomePageController extends FreemarkerHttpServlet {
List<DataGetter> dgList = DataGetterUtils.getDataGettersForPage(vreq, vreq.getDisplayModel(), DisplayVocabulary.HOME_PAGE_URI);
for( DataGetter dg : dgList){
Map<String,Object> moreData = dg.getData(getServletContext(),vreq,body);
Map<String,Object> moreData = dg.getData(body);
if( moreData != null ){
body.putAll(moreData);
}

View file

@ -284,7 +284,7 @@ public class MenuManagementController extends FreemarkerHttpServlet {
//TODO: Change so that instantiation here occurs correctly <-- how should data getter be instantiated
DataGetter pg = DataGetterUtils.dataGetterForURI(vreq, vreq.getDisplayModel(), dataGetterURI);
//TODO: Check template data variable and what that is?
Map<String, Object> pageData = pg.getData(getServletContext(), vreq, templateData);
Map<String, Object> pageData = pg.getData(templateData);
//Map<String, Object> pageInfo = vreq.getWebappDaoFactory().getPageDao().getPage(pageURI);
SelectDataGetterUtils.processAndRetrieveData(vreq, getServletContext(), pageData, className, templateData);
} catch(Exception ex) {

View file

@ -67,7 +67,7 @@ public class PageController extends FreemarkerHttpServlet{
//executePageDataGetters( pageUri, vreq, getServletContext(), mapForTemplate );
//these should all be data getters now
executeDataGetters( pageUri, vreq, getServletContext(), mapForTemplate);
executeDataGetters( pageUri, vreq, mapForTemplate);
mapForTemplate.putAll( getPageControllerValues( pageUri, vreq, getServletContext(), mapForTemplate));
@ -75,12 +75,12 @@ public class PageController extends FreemarkerHttpServlet{
return rv;
}
private void executeDataGetters(String pageUri, VitroRequest vreq, ServletContext context, Map<String, Object> mapForTemplate)
private void executeDataGetters(String pageUri, VitroRequest vreq, Map<String, Object> mapForTemplate)
throws Exception {
List<DataGetter> dgList = DataGetterUtils.getDataGettersForPage(vreq, vreq.getDisplayModel(), pageUri);
for( DataGetter dg : dgList){
Map<String,Object> moreData = dg.getData(context,vreq,mapForTemplate);
Map<String,Object> moreData = dg.getData(mapForTemplate);
if( moreData != null ){
mapForTemplate.putAll(moreData);
}

View file

@ -46,12 +46,13 @@ public class FakeApplicationOntologyService {
* Return the template name and DataGetter instances associated with this
* class and this short view context. If none, return null.
*/
public TemplateAndDataGetters getShortViewProperties(WebappDaoFactory wadf,
public TemplateAndDataGetters getShortViewProperties(VitroRequest vreq,
Individual individual, String classUri, String contextName) {
if ((BROWSE.name().equals(contextName))
&& (isClassInPeopleClassGroup(wadf, classUri))) {
&& (isClassInPeopleClassGroup(vreq.getWebappDaoFactory(),
classUri))) {
return new TemplateAndDataGetters("view-browse-people.ftl",
new FakeVivoPeopleDataGetter(individual.getURI()));
new FakeVivoPeopleDataGetter(vreq, individual.getURI()));
}
// A mockup of Tammy's use case.
// if ((SEARCH.name().equals(contextName))
@ -117,8 +118,7 @@ public class FakeApplicationOntologyService {
private static class FakeFacultyDataGetter implements DataGetter {
@Override
public Map<String, Object> getData(ServletContext context,
VitroRequest vreq, Map<String, Object> valueMap) {
public Map<String, Object> getData(Map<String, Object> valueMap) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> extras = new HashMap<String, Object>();
extras.put("departmentName", "Department of Redundancy Department");
@ -156,19 +156,22 @@ public class FakeApplicationOntologyService {
}
private String individualUri;
private VitroRequest vreq;
private ServletContext ctx;
public FakeVivoPeopleDataGetter(String individualUri) {
super(fakeDisplayModel, "http://FakeVivoPeopleDataGetter");
public FakeVivoPeopleDataGetter(VitroRequest vreq, String individualUri) {
super(vreq, fakeDisplayModel, "http://FakeVivoPeopleDataGetter");
this.individualUri = individualUri;
this.vreq = vreq;
this.ctx = vreq.getSession().getServletContext();
}
@Override
public Map<String, Object> getData(ServletContext context,
VitroRequest vreq, Map<String, Object> pageData) {
public Map<String, Object> getData(Map<String, Object> pageData) {
Map<String, String[]> parms = new HashMap<String, String[]>();
parms.put("uri", new String[] { individualUri });
return doQuery(parms, getModel(context, vreq, null));
return doQuery(parms, getModel(ctx, vreq, null));
}
}

View file

@ -11,8 +11,6 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -80,7 +78,7 @@ public class ShortViewServiceImpl implements ShortViewService {
ShortViewContext svContext, VitroRequest vreq) {
TemplateAndDataGetters tdg = fetchTemplateAndDataGetters(individual,
svContext, vreq);
Map<String, Object> gotData = runDataGetters(tdg.getDataGetters(), vreq);
Map<String, Object> gotData = runDataGetters(tdg.getDataGetters());
return new TemplateAndSupplementalDataImpl(tdg.getTemplateName(),
gotData);
}
@ -103,9 +101,8 @@ public class ShortViewServiceImpl implements ShortViewService {
classUris.addAll(figureMostSpecificClassUris(individual));
for (String classUri : classUris) {
TemplateAndDataGetters tdg = faker.getShortViewProperties(
vreq.getWebappDaoFactory(), individual, classUri,
svContext.name());
TemplateAndDataGetters tdg = faker.getShortViewProperties(vreq,
individual, classUri, svContext.name());
if (tdg != null) {
return tdg;
}
@ -116,13 +113,10 @@ public class ShortViewServiceImpl implements ShortViewService {
}
/** Build a data map from the combined results of all data getters. */
private Map<String, Object> runDataGetters(Set<DataGetter> dataGetters,
VitroRequest vreq) {
ServletContext ctx = vreq.getSession().getServletContext();
private Map<String, Object> runDataGetters(Set<DataGetter> dataGetters) {
Map<String, Object> gotData = new HashMap<String, Object>();
for (DataGetter dg : dataGetters) {
gotData.putAll(dg.getData(ctx, vreq, EMPTY_MAP));
gotData.putAll(dg.getData(EMPTY_MAP));
}
return gotData;
}

View file

@ -34,28 +34,34 @@ import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.Listed
public class BrowseDataGetter extends DataGetterBase implements DataGetter {
final static Log log = LogFactory.getLog(BrowseDataGetter.class);
String dataGetterURI;
VitroRequest vreq;
ServletContext context;
/**
* Constructor with display model and data getter URI that will be called by reflection.
*/
public BrowseDataGetter(Model displayModel, String dataGetterURI){
this.configure(displayModel,dataGetterURI);
public BrowseDataGetter(VitroRequest vreq, Model displayModel, String dataGetterURI){
this.configure(vreq, displayModel,dataGetterURI);
}
/**
* Configure this instance based on the URI and display model.
*/
protected void configure(Model displayModel, String dataGetterURI) {
protected void configure(VitroRequest vreq, Model displayModel, String dataGetterURI) {
if( vreq == null )
throw new IllegalArgumentException("VitroRequest may not be null.");
if( displayModel == null )
throw new IllegalArgumentException("Display Model may not be null.");
if( dataGetterURI == null )
throw new IllegalArgumentException("PageUri may not be null.");
this.dataGetterURI = dataGetterURI;
this.vreq = vreq;
this.context = vreq.getSession().getServletContext();
this.dataGetterURI = dataGetterURI;
}
@Override
public Map<String, Object> getData(ServletContext context, VitroRequest vreq, Map<String, Object> pageData) {
public Map<String, Object> getData(Map<String, Object> pageData) {
try{
Map params = vreq.getParameterMap();

View file

@ -32,29 +32,36 @@ public class ClassGroupPageData extends DataGetterBase implements DataGetter{
private static final Log log = LogFactory.getLog(ClassGroupPageData.class);
String dataGetterURI;
String classGroupUri;
VitroRequest vreq;
ServletContext context;
/**
* Constructor with display model and data getter URI that will be called by reflection.
*/
public ClassGroupPageData(Model displayModel, String dataGetterURI){
this.configure(displayModel,dataGetterURI);
public ClassGroupPageData(VitroRequest vreq, Model displayModel, String dataGetterURI){
this.configure(vreq, displayModel,dataGetterURI);
}
/**
* Configure this instance based on the URI and display model.
*/
protected void configure(Model displayModel, String dataGetterURI) {
protected void configure(VitroRequest vreq, Model displayModel, String dataGetterURI) {
if( vreq == null )
throw new IllegalArgumentException("VitroRequest may not be null.");
if( displayModel == null )
throw new IllegalArgumentException("Display Model may not be null.");
if( dataGetterURI == null )
throw new IllegalArgumentException("PageUri may not be null.");
this.vreq = vreq;
this.context = vreq.getSession().getServletContext();
this.dataGetterURI = dataGetterURI;
this.classGroupUri = DataGetterUtils.getClassGroupForDataGetter(displayModel, dataGetterURI);
}
@Override
public Map<String, Object> getData(ServletContext context, VitroRequest vreq, Map<String, Object> pageData) {
public Map<String, Object> getData(Map<String, Object> pageData) {
HashMap<String, Object> data = new HashMap<String,Object>();
data.put("classGroupUri", this.classGroupUri);

View file

@ -3,16 +3,21 @@ package edu.cornell.mannlib.vitro.webapp.utils.dataGetter;
import java.util.Map;
import javax.servlet.ServletContext;
import com.hp.hpl.jena.rdf.model.Model;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
/**
* A class to get data based on configurations in RDF. These are useful for pages bug not specific to pages.
* Consider using this interface when you need data based on RDF configuration.
*
* Instances should be short-lived, and only used on a single HTTP request. The getData method accepts only
* a value map, so if stat is required from the request (or the servlet context), the request should be
* passed to the constructor.
*
* Constructors: Objects that implement this interface will be constructed by reflection with the following
* constructor signatures in the following order:
* DataGetter( VitroRequest vreq, Model displayModel, String dataGetterURI )
* DataGetter( Model displayModel, String dataGetterURI )
* DataGetter( VitroRequest vreq )
* DataGetter()
*
* The main difference between this and PageDataGetter is that these are configured not based on page URI
* but on DataGetter URI. This allows a configuration of a DataGetter to be used in multiple situations.
* The DataGetter is not passed information about what page it might be associated with.
@ -20,22 +25,18 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
* Using this interface is preferred over PageDataGetter because then the DataGetter can be associated with
* things other than pages.
*
* Constructors: Objects that implement this interface will be constructed by reflection with the following
* constructor signatures in the following order:
* DataGetter( Model displayModel, String dataGetterURI )
* DataGetter()
*
*/
public interface DataGetter {
/**
* Get data. Throwing an Exception is acceptable.
*
* @param context - servlet context to get state from
* @param vreq - request to get state from
* @param valueMap - any values already generated by data getters or the controller.
* @return data to add to valueMap. Should not be null.
*/
Map<String,Object> getData(ServletContext context, VitroRequest vreq, Map<String, Object> valueMap );
/**
* Get data. Throwing an Exception is acceptable.
*
* @param valueMap
* any values already generated by data getters or the controller.
* Might be immutable, and should not be modified within the method.
*
* @return data to add to valueMap. Might be empty, but should not be null.
*/
Map<String,Object> getData( Map<String, Object> valueMap );
}

View file

@ -5,7 +5,6 @@ package edu.cornell.mannlib.vitro.webapp.utils.dataGetter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -17,30 +16,21 @@ 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.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.shared.Lock;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.PageDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateModel;
@ -52,6 +42,8 @@ import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateMo
public class IndividualsForClassesDataGetter extends DataGetterBase implements DataGetter{
private static final Log log = LogFactory.getLog(IndividualsForClassesDataGetter.class);
protected static String restrictClassesTemplateName = null;
VitroRequest vreq;
ServletContext context;
String dataGetterURI;
String classGroupURI;
Map<String, Object> classIntersectionsMap;
@ -59,19 +51,23 @@ public class IndividualsForClassesDataGetter extends DataGetterBase implements D
/**
* Constructor with display model and data getter URI that will be called by reflection.
*/
public IndividualsForClassesDataGetter(Model displayModel, String dataGetterURI){
this.configure(displayModel,dataGetterURI);
public IndividualsForClassesDataGetter(VitroRequest vreq, Model displayModel, String dataGetterURI){
this.configure(vreq,displayModel,dataGetterURI);
}
/**
* Configure this instance based on the URI and display model.
*/
protected void configure(Model displayModel, String dataGetterURI) {
protected void configure(VitroRequest vreq, Model displayModel, String dataGetterURI) {
if( vreq == null )
throw new IllegalArgumentException("VitroRequest may not be null.");
if( displayModel == null )
throw new IllegalArgumentException("Display Model may not be null.");
if( dataGetterURI == null )
throw new IllegalArgumentException("PageUri may not be null.");
this.vreq = vreq;
this.context = vreq.getSession().getServletContext();
this.dataGetterURI = dataGetterURI;
this.classGroupURI = DataGetterUtils.getClassGroupForDataGetter(displayModel, dataGetterURI);
this.classIntersectionsMap = getClassIntersectionsMap(displayModel);
@ -122,7 +118,7 @@ public class IndividualsForClassesDataGetter extends DataGetterBase implements D
}
@Override
public Map<String, Object> getData(ServletContext context, VitroRequest vreq, Map<String, Object> pageData) {
public Map<String, Object> getData(Map<String, Object> pageData) {
this.setTemplateName();
HashMap<String, Object> data = new HashMap<String,Object>();

View file

@ -35,30 +35,37 @@ public class SparqlQueryDataGetter extends DataGetterBase implements DataGetter{
String queryText;
String saveToVar;
String modelURI;
VitroRequest vreq;
ServletContext context;
final static Log log = LogFactory.getLog(SparqlQueryDataGetter.class);
/**
* Constructor with display model and data getter URI that will be called by reflection.
*/
public SparqlQueryDataGetter(Model displayModel, String dataGetterURI){
this.configure(displayModel,dataGetterURI);
public SparqlQueryDataGetter(VitroRequest vreq, Model displayModel, String dataGetterURI){
this.configure(vreq, displayModel,dataGetterURI);
}
@Override
public Map<String, Object> getData(ServletContext context, VitroRequest vreq, Map<String, Object> pageData) {
public Map<String, Object> getData(Map<String, Object> pageData) {
return doQuery( vreq.getParameterMap(), getModel(context, vreq, modelURI));
}
/**
* Configure this instance based on the URI and display model.
*/
protected void configure(Model displayModel, String dataGetterURI) {
protected void configure(VitroRequest vreq, Model displayModel, String dataGetterURI) {
if( vreq == null )
throw new IllegalArgumentException("VitroRequest may not be null.");
if( displayModel == null )
throw new IllegalArgumentException("Display Model may not be null.");
if( dataGetterURI == null )
throw new IllegalArgumentException("PageUri may not be null.");
this.vreq = vreq;
this.context = vreq.getSession().getServletContext();
this.dataGetterURI = dataGetterURI;
QuerySolutionMap initBindings = new QuerySolutionMap();