Moving away from BrowseWidget and just adding browse variables to HomePageController NIHVIVO-1674
This commit is contained in:
parent
563d1b8651
commit
6f02a04a8b
3 changed files with 262 additions and 13 deletions
|
@ -13,7 +13,9 @@ import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetter;
|
||||||
|
|
||||||
public class HomePageController extends FreemarkerHttpServlet {
|
public class HomePageController extends FreemarkerHttpServlet {
|
||||||
|
|
||||||
|
@ -30,10 +32,24 @@ public class HomePageController extends FreemarkerHttpServlet {
|
||||||
List<VClassGroup> vClassGroups = vcgc.getGroups(vreq.getPortalId());
|
List<VClassGroup> vClassGroups = vcgc.getGroups(vreq.getPortalId());
|
||||||
body.put("vClassGroups", vClassGroups);
|
body.put("vClassGroups", vClassGroups);
|
||||||
|
|
||||||
|
PageDataGetter dataGetter =
|
||||||
|
PageController.getPageDataGetterMap(getServletContext())
|
||||||
|
.get(DisplayVocabulary.HOME_PAGE_TYPE);
|
||||||
|
if( dataGetter != null ){
|
||||||
|
String uriOfPageInDisplayModel = "not defined";
|
||||||
|
Map<String, Object> pageData =
|
||||||
|
dataGetter.getData(getServletContext(), vreq,
|
||||||
|
uriOfPageInDisplayModel, body,
|
||||||
|
DisplayVocabulary.HOME_PAGE_TYPE);
|
||||||
|
if(pageData != null)
|
||||||
|
body.putAll(pageData);
|
||||||
|
}
|
||||||
|
|
||||||
// Add home page data to body here
|
// Add home page data to body here
|
||||||
return new TemplateResponseValues(BODY_TEMPLATE, body);
|
return new TemplateResponseValues(BODY_TEMPLATE, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getTitle(String siteName, VitroRequest vreq) {
|
protected String getTitle(String siteName, VitroRequest vreq) {
|
||||||
return siteName;
|
return siteName;
|
||||||
|
|
|
@ -20,6 +20,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.BrowseDataGetter;
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetter;
|
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetter;
|
||||||
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.ClassGroupPageData;
|
import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.ClassGroupPageData;
|
||||||
|
|
||||||
|
@ -38,18 +39,7 @@ public class PageController extends FreemarkerHttpServlet{
|
||||||
protected final static String DEFAULT_BODY_TEMPLATE = "menupage.ftl";
|
protected final static String DEFAULT_BODY_TEMPLATE = "menupage.ftl";
|
||||||
|
|
||||||
protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap";
|
protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap";
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init() throws ServletException {
|
|
||||||
super.init();
|
|
||||||
getServletContext().setAttribute(DATA_GETTER_MAP, new HashMap<String,PageDataGetter>());
|
|
||||||
|
|
||||||
/* register all page data getters with the PageController servlet.
|
|
||||||
* There should be a better way of doing this. */
|
|
||||||
ClassGroupPageData cgpd = new ClassGroupPageData();
|
|
||||||
getPageDataGetterMap(getServletContext()).put(cgpd.getType(), cgpd);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ResponseValues processRequest(VitroRequest vreq) {
|
protected ResponseValues processRequest(VitroRequest vreq) {
|
||||||
try {
|
try {
|
||||||
|
@ -168,11 +158,24 @@ public class PageController extends FreemarkerHttpServlet{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String,PageDataGetter> getPageDataGetterMap(ServletContext sc){
|
public static Map<String,PageDataGetter> getPageDataGetterMap(ServletContext sc){
|
||||||
|
setupDataGetters(sc);
|
||||||
return (Map<String,PageDataGetter>)sc.getAttribute(DATA_GETTER_MAP);
|
return (Map<String,PageDataGetter>)sc.getAttribute(DATA_GETTER_MAP);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putPageUri(HttpServletRequest req, String pageUri){
|
public static void putPageUri(HttpServletRequest req, String pageUri){
|
||||||
req.setAttribute("pageURI", pageUri);
|
req.setAttribute("pageURI", pageUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setupDataGetters(ServletContext context ){
|
||||||
|
if( context != null && context.getAttribute(DATA_GETTER_MAP) == null ){
|
||||||
|
context.setAttribute(DATA_GETTER_MAP, new HashMap<String,PageDataGetter>());
|
||||||
|
|
||||||
|
/* register all page data getters with the PageController servlet.
|
||||||
|
* There should be a better way of doing this. */
|
||||||
|
ClassGroupPageData cgpd = new ClassGroupPageData();
|
||||||
|
getPageDataGetterMap(context).put(cgpd.getType(), cgpd);
|
||||||
|
BrowseDataGetter bdg = new BrowseDataGetter();
|
||||||
|
getPageDataGetterMap(context).put(bdg.getType(), bdg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,230 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
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.JSONServlet;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.utils.JSONtoFmModel;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateModel;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassTemplateModel;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ListedIndividualTemplateModel;
|
||||||
|
|
||||||
|
public class BrowseDataGetter implements PageDataGetter {
|
||||||
|
final static Log log = LogFactory.getLog(BrowseDataGetter.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getData(ServletContext context,
|
||||||
|
VitroRequest vreq, String pageUri, Map<String, Object> page,
|
||||||
|
String type) {
|
||||||
|
try{
|
||||||
|
Map params = vreq.getParameterMap();
|
||||||
|
Mode mode = getMode( vreq, params );
|
||||||
|
switch( mode ){
|
||||||
|
case VCLASS_ALPHA:
|
||||||
|
return doClassAlphaDisplay(params,vreq,context);
|
||||||
|
case CLASS_GROUP:
|
||||||
|
return doClassGroupDisplay(params, vreq, context);
|
||||||
|
case VCLASS:
|
||||||
|
return doClassDisplay(params, vreq, context);
|
||||||
|
case ALL_CLASS_GROUPS:
|
||||||
|
return doAllClassGroupsDisplay( params, page, vreq, context);
|
||||||
|
default:
|
||||||
|
return doAllClassGroupsDisplay( params, page, vreq, context);
|
||||||
|
}
|
||||||
|
}catch(Throwable th){
|
||||||
|
log.error(th,th);
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return DisplayVocabulary.HOME_PAGE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Map<String, Object> doClassAlphaDisplay( Map params, VitroRequest request, ServletContext context) throws Exception {
|
||||||
|
Map<String,Object> body = new HashMap<String,Object>();
|
||||||
|
body.putAll(getCommonValues(context));
|
||||||
|
body.putAll(getClassAlphaValues(params,request,context));
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String,Object> getClassAlphaValues( Map params, VitroRequest request, ServletContext context) throws Exception{
|
||||||
|
Map<String,Object> map= new HashMap<String,Object>();
|
||||||
|
|
||||||
|
String classUri = getParam(Mode.VCLASS, request, params);
|
||||||
|
VitroRequest vreq = new VitroRequest(request);
|
||||||
|
VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri);
|
||||||
|
map.put("class", new VClassTemplateModel(vclass));
|
||||||
|
|
||||||
|
JSONObject vclassRes = JSONServlet.getLuceneIndividualsByVClass(vclass.getURI(), request, context);
|
||||||
|
map.put("totalCount", JSONtoFmModel.convertJSONObjectToMap( (String) vclassRes.get("totalCount") ));
|
||||||
|
map.put("alpha", JSONtoFmModel.convertJSONObjectToMap( (String) vclassRes.get("alpha") ));
|
||||||
|
map.put("individuals", JSONtoFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("individuals") ));
|
||||||
|
map.put("pages", JSONtoFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("pages") ));
|
||||||
|
map.put("letters", JSONtoFmModel.convertJSONArrayToList( (JSONArray) vclassRes.get("letters") ));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String,Object> getCommonValues( ServletContext context){
|
||||||
|
//doesn't do anything yet
|
||||||
|
Map<String,Object> values = new HashMap<String,Object>();
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, Object> doAllClassGroupsDisplay( Map params, Map<String, Object> page, VitroRequest request, ServletContext context) {
|
||||||
|
Map<String,Object> body = new HashMap<String,Object>();
|
||||||
|
body.putAll(getCommonValues(context));
|
||||||
|
body.putAll(getAllClassGroupData(request, params, page, context));
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a list of all VClassGroups with vclasses with individual counts.
|
||||||
|
* @param params2
|
||||||
|
*/
|
||||||
|
protected Map<String,Object> getAllClassGroupData(VitroRequest request, Map params, Map<String, Object> page, ServletContext context){
|
||||||
|
Map<String,Object> map = new HashMap<String,Object>();
|
||||||
|
|
||||||
|
if( !page.containsKey("vClassGroups")){
|
||||||
|
VClassGroupCache vcgc = VClassGroupCache.getVClassGroupCache( context );
|
||||||
|
List<VClassGroup> vClassGroups = vcgc.getGroups(request.getPortalId());
|
||||||
|
map.put("vClassGroups", vClassGroups);
|
||||||
|
}
|
||||||
|
// VitroRequest vreq = new VitroRequest(request);
|
||||||
|
//
|
||||||
|
// VClassGroupCache vcgc = VClassGroupCache.getVClassGroupCache(context);
|
||||||
|
// List<VClassGroup> cgList = vcgc.getGroups(vreq.getPortalId());
|
||||||
|
//
|
||||||
|
// LinkedList<VClassGroupTemplateModel> cgtmList = new LinkedList<VClassGroupTemplateModel>();
|
||||||
|
// for( VClassGroup classGroup : cgList){
|
||||||
|
// cgtmList.add( new VClassGroupTemplateModel( classGroup ));
|
||||||
|
// }
|
||||||
|
// map.put("vclassGroupList",cgtmList);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, Object> doClassDisplay( Map params,
|
||||||
|
VitroRequest request, ServletContext context) {
|
||||||
|
Map<String,Object> body = new HashMap<String,Object>();
|
||||||
|
|
||||||
|
body.putAll(getCommonValues(context));
|
||||||
|
body.putAll(getClassData(request,params,context));
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> getClassData(VitroRequest request, Map params, ServletContext context) {
|
||||||
|
Map<String,Object> map = new HashMap<String,Object>();
|
||||||
|
|
||||||
|
map.putAll(getClassGroupData(request, params,context));
|
||||||
|
|
||||||
|
String classUri = getParam(Mode.VCLASS, request, params);
|
||||||
|
VitroRequest vreq = new VitroRequest(request);
|
||||||
|
VClass vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri);
|
||||||
|
map.put("class", new VClassTemplateModel(vclass));
|
||||||
|
|
||||||
|
List<Individual> inds = vreq.getWebappDaoFactory().getIndividualDao()
|
||||||
|
.getIndividualsByVClass(vclass);
|
||||||
|
|
||||||
|
List<ListedIndividualTemplateModel> tInds = new ArrayList<ListedIndividualTemplateModel>(inds.size());
|
||||||
|
for( Individual ind : inds){
|
||||||
|
tInds.add(new ListedIndividualTemplateModel(ind, vreq));
|
||||||
|
}
|
||||||
|
map.put("individualsInClass", tInds);
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, Object> doClassGroupDisplay(Map params, VitroRequest request, ServletContext context) {
|
||||||
|
Map<String,Object> body = new HashMap<String,Object>();
|
||||||
|
body.putAll(getCommonValues(context));
|
||||||
|
body.putAll( getClassGroupData(request,params, context));
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Map<String, Object> getClassGroupData(VitroRequest request, Map params, ServletContext context) {
|
||||||
|
Map<String,Object> map = new HashMap<String,Object>();
|
||||||
|
|
||||||
|
String vcgUri = getParam(Mode.CLASS_GROUP, request, params);
|
||||||
|
VitroRequest vreq = new VitroRequest(request);
|
||||||
|
//VClassGroup vcg = vreq.getWebappDaoFactory().getVClassGroupDao().getGroupByURI(vcgUri);
|
||||||
|
|
||||||
|
VClassGroupCache vcgc = VClassGroupCache.getVClassGroupCache(context);
|
||||||
|
VClassGroup vcg = vcgc.getGroup(vreq.getPortalId(), vcgUri);
|
||||||
|
|
||||||
|
//vreq.getWebappDaoFactory().getVClassDao().addVClassesToGroup(vcg, false, true);
|
||||||
|
ArrayList<VClassTemplateModel> classes = new ArrayList<VClassTemplateModel>(vcg.size());
|
||||||
|
for( VClass vc : vcg){
|
||||||
|
classes.add(new VClassTemplateModel(vc));
|
||||||
|
}
|
||||||
|
map.put("classes", classes);
|
||||||
|
|
||||||
|
map.put("classGroup", new VClassGroupTemplateModel(vcg));
|
||||||
|
map.put("classGroupName", vcg.getPublicName());
|
||||||
|
map.put("classGroupUri", vcg.getURI());
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum Mode{
|
||||||
|
VCLASS_ALPHA("vclassAlpha"),
|
||||||
|
VCLASS("vclassUri"),
|
||||||
|
CLASS_GROUP("classgroupUri"),
|
||||||
|
ALL_CLASS_GROUPS("all");
|
||||||
|
String param;
|
||||||
|
Mode(String param){
|
||||||
|
this.param = param;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final static Mode DEFAULT_MODE = Mode.ALL_CLASS_GROUPS;
|
||||||
|
|
||||||
|
protected Mode getMode(VitroRequest request, Map<String, Object> params){
|
||||||
|
for( Mode mode : Mode.values()){
|
||||||
|
String queryParam = request.getParameter( mode.param );
|
||||||
|
if( queryParam != null && !queryParam.isEmpty() ){
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
Object obj = params.get( mode.param );
|
||||||
|
String param = obj != null ? obj.toString():null;
|
||||||
|
if( param != null && !param.isEmpty() ){
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return DEFAULT_MODE;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getParam(Mode mode, VitroRequest request, Map params){
|
||||||
|
if( request.getParameter(mode.param) != null )
|
||||||
|
return request.getParameter(mode.param);
|
||||||
|
if( params.get(mode.param) != null )
|
||||||
|
return params.get(mode.param).toString();
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue