From f5161e14b72836958f8b029c27e1f42728163628 Mon Sep 17 00:00:00 2001 From: Brian Caruso Date: Mon, 5 Aug 2013 12:41:15 -0400 Subject: [PATCH] Removing PageDataGetters and its related servlets and tests. VIVO-218 --- .../controller/json/GetDataForPage.java | 39 -- .../webapp/controller/json/JsonServlet.java | 145 +++++++- .../pageDataGetter/BrowseDataGetter.java | 234 ------------ .../pageDataGetter/ClassGroupPageData.java | 155 -------- .../IndividualsForClassesDataGetter.java | 316 ---------------- .../utils/pageDataGetter/PageDataGetter.java | 27 -- .../pageDataGetter/PageDataGetterUtils.java | 339 ------------------ .../utils/dataGetter/DataGetterUtilsTest.java | 11 +- .../dataGetter/resources/dataGetterTest.n3 | 11 +- .../PageDataGetterUtilsTest.java | 68 ---- .../resources/pageDataGetter.n3 | 30 -- 11 files changed, 137 insertions(+), 1238 deletions(-) delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/GetDataForPage.java delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/BrowseDataGetter.java delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ClassGroupPageData.java delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/IndividualsForClassesDataGetter.java delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetter.java delete mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtils.java delete mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtilsTest.java delete mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/resources/pageDataGetter.n3 diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/GetDataForPage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/GetDataForPage.java deleted file mode 100644 index 632a0a611..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/GetDataForPage.java +++ /dev/null @@ -1,39 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.controller.json; - -import java.util.Map; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; - -import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; -import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetterUtils; - -/** - * Gets data based on data getter for page uri and returns in the form of Json objects - */ -public class GetDataForPage extends JsonObjectProducer { - private static final Log log = LogFactory.getLog(GetDataForPage.class); - - protected GetDataForPage(VitroRequest vreq) { - super(vreq); - } - - @Override - protected JSONObject process() throws Exception { - JSONObject rObj = null; - String pageUri = vreq.getParameter("pageUri"); - if(pageUri != null && !pageUri.isEmpty()) { - Map data = PageDataGetterUtils.getDataForPage(pageUri, vreq, ctx); - //Convert to json version based on type of page - if(data != null) { - //Convert to json version based on type of page - rObj = PageDataGetterUtils.covertDataToJSONForPage(pageUri, data, vreq, ctx); - } - } - return rObj; - } - -} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java index 2fbd9d09f..3f516e109 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/json/JsonServlet.java @@ -3,8 +3,11 @@ package edu.cornell.mannlib.vitro.webapp.controller.json; import java.io.IOException; +import java.net.URLEncoder; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Enumeration; import java.util.List; import java.util.Map; @@ -15,17 +18,21 @@ import javax.servlet.http.HttpServletResponse; 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.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.controller.Controllers; import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; 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.IndividualListController.PageRecord; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.utils.log.LogUtils; -import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.PageDataGetterUtils; /** * This servlet is for servicing requests for JSON objects/data. @@ -69,7 +76,9 @@ public class JsonServlet extends VitroHttpServlet { log.debug("AJAX request to retrieve individuals by vclasses"); new GetSolrIndividualsByVClasses(vreq).process(resp); } else if( vreq.getParameter("getDataForPage") != null ){ - new GetDataForPage(vreq).process(resp); + throw new IllegalArgumentException("The call invoked deprecated classes " + + "and the parameter for this call appeared nowhere in the code base, " + + "so it was removed in Aug 5th 2013."); }else if( vreq.getParameter("getRenderedSolrIndividualsByVClass") != null ){ new GetRenderedSolrIndividualsByVClass(vreq).process(resp); }else if( vreq.getParameter("getRandomSolrIndividualsByVClass") != null ){ @@ -85,7 +94,7 @@ public class JsonServlet extends VitroHttpServlet { Map map = getSolrVClassIntersectionResults(vclassURIs, vreq, context); //last parameter indicates single vclass instead of multiple vclasses - return processVClassResults(map, vreq, context, false); + return processVclassResultsJSON(map, vreq, false); } public static JSONObject getSolrIndividualsByVClasses(List vclassURIs, HttpServletRequest req, ServletContext context) throws Exception { @@ -93,8 +102,8 @@ public class JsonServlet extends VitroHttpServlet { log.debug("Retrieve solr results for vclasses" + vclassURIs.toString()); Map map = getSolrVClassIntersectionResults(vclassURIs, vreq, context); log.debug("Results returned from Solr for " + vclassURIs.toString() + " are of size " + map.size()); - JSONObject rObj = processVClassResults(map, vreq, context, true); - return rObj; + + return processVclassResultsJSON(map, vreq, true); } //Including version for Solr query for Vclass Intersections @@ -119,10 +128,10 @@ public class JsonServlet extends VitroHttpServlet { } // Map given to process method includes the actual individuals returned from the search - public static JSONObject processVClassResults(Map map, VitroRequest vreq, ServletContext context, boolean multipleVclasses) throws Exception{ - JSONObject rObj = PageDataGetterUtils.processVclassResultsJSON(map, vreq, multipleVclasses); - return rObj; - } +// public static JSONObject processVClassResults(Map map, VitroRequest vreq, ServletContext context, boolean multipleVclasses) throws Exception{ +// JSONObject rObj = processVclassResultsJSON(map, vreq, multipleVclasses); +// return rObj; +// } public static Collection getMostSpecificTypes(Individual individual, WebappDaoFactory wdf) { ObjectPropertyStatementDao opsDao = wdf.getObjectPropertyStatementDao(); @@ -143,7 +152,7 @@ public class JsonServlet extends VitroHttpServlet { Map map = getRandomSolrVClassResults(vclassURI, vreq, context); //last parameter indicates single vclass instead of multiple vclasses - return processVClassResults(map, vreq, context, false); + return processVclassResultsJSON(map, vreq, false); } //Including version for Random Solr query for Vclass Intersections @@ -169,4 +178,120 @@ public class JsonServlet extends VitroHttpServlet { } + /** + * Process results related to VClass or vclasses. Handles both single and multiple vclasses being sent. + */ + public static JSONObject processVclassResultsJSON(Map map, VitroRequest vreq, boolean multipleVclasses) { + JSONObject rObj = new JSONObject(); + VClass vclass=null; + + try { + + // Properties from ontologies used by VIVO - should not be in vitro + DataProperty fNameDp = (new DataProperty()); + fNameDp.setURI("http://xmlns.com/foaf/0.1/firstName"); + DataProperty lNameDp = (new DataProperty()); + lNameDp.setURI("http://xmlns.com/foaf/0.1/lastName"); + DataProperty preferredTitleDp = (new DataProperty()); + preferredTitleDp.setURI("http://vivoweb.org/ontology/core#preferredTitle"); + + if( log.isDebugEnabled() ){ + @SuppressWarnings("unchecked") + Enumeration e = vreq.getParameterNames(); + while(e.hasMoreElements()){ + String name = e.nextElement(); + log.debug("parameter: " + name); + for( String value : vreq.getParameterValues(name) ){ + log.debug("value for " + name + ": '" + value + "'"); + } + } + } + + //need an unfiltered dao to get firstnames and lastnames + WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory(); + + String[] vitroClassIdStr = vreq.getParameterValues("vclassId"); + if ( vitroClassIdStr != null && vitroClassIdStr.length > 0){ + for(String vclassId: vitroClassIdStr) { + vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassId); + if (vclass == null) { + log.error("Couldn't retrieve vclass "); + throw new Exception ("Class " + vclassId + " not found"); + } + } + }else{ + log.error("parameter vclassId URI parameter expected "); + throw new Exception("parameter vclassId URI parameter expected "); + } + List vclassIds = Arrays.asList(vitroClassIdStr); + //if single vclass expected, then include vclass. This relates to what the expected behavior is, not size of list + if(!multipleVclasses) { + //currently used for ClassGroupPage + rObj.put("vclass", + new JSONObject().put("URI",vclass.getURI()) + .put("name",vclass.getName())); + } else { + //For now, utilize very last VClass (assume that that is the one to be employed) + //TODO: Find more general way of dealing with this + //put multiple ones in? + if(vclassIds.size() > 0) { + int numberVClasses = vclassIds.size(); + vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassIds.get(numberVClasses - 1)); + rObj.put("vclass", new JSONObject().put("URI",vclass.getURI()) + .put("name",vclass.getName())); + } + // rObj.put("vclasses", new JSONObject().put("URIs",vitroClassIdStr) + // .put("name",vclass.getName())); + } + if (vclass != null) { + + rObj.put("totalCount", map.get("totalCount")); + rObj.put("alpha", map.get("alpha")); + + List inds = (List)map.get("entities"); + log.debug("Number of individuals returned from request: " + inds.size()); + JSONArray jInds = new JSONArray(); + for(Individual ind : inds ){ + JSONObject jo = new JSONObject(); + jo.put("URI", ind.getURI()); + jo.put("label",ind.getRdfsLabel()); + jo.put("name",ind.getName()); + jo.put("thumbUrl", ind.getThumbUrl()); + jo.put("imageUrl", ind.getImageUrl()); + jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq)); + + jo.put("mostSpecificTypes", JsonServlet.getMostSpecificTypes(ind,fullWdf)); + jo.put("preferredTitle", JsonServlet.getDataPropertyValue(ind, preferredTitleDp, fullWdf)); + + jInds.put(jo); + } + rObj.put("individuals", jInds); + + JSONArray wpages = new JSONArray(); + //Made sure that PageRecord here is SolrIndividualListController not IndividualListController + List pages = (List)map.get("pages"); + for( PageRecord pr: pages ){ + JSONObject p = new JSONObject(); + p.put("text", pr.text); + p.put("param", pr.param); + p.put("index", pr.index); + wpages.put( p ); + } + rObj.put("pages",wpages); + + JSONArray jletters = new JSONArray(); + List letters = Controllers.getLetters(); + for( String s : letters){ + JSONObject jo = new JSONObject(); + jo.put("text", s); + jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); + jletters.put( jo ); + } + rObj.put("letters", jletters); + } + } catch(Exception ex) { + log.error("Error occurred in processing JSON object", ex); + } + return rObj; + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/BrowseDataGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/BrowseDataGetter.java deleted file mode 100644 index 7fbd63ada..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/BrowseDataGetter.java +++ /dev/null @@ -1,234 +0,0 @@ -/* $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.LinkedList; -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.VitroRequest; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; -import edu.cornell.mannlib.vitro.webapp.controller.json.JsonServlet; -import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupsForRequest; -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.individuallist.ListedIndividual; - -public class BrowseDataGetter implements PageDataGetter { - final static Log log = LogFactory.getLog(BrowseDataGetter.class); - - @Override - public Map getData(ServletContext context, - VitroRequest vreq, String pageUri, Map page) { - 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; - } - - //Get data servuice - @Override - public String getDataServiceUrl() { - return UrlBuilder.getUrl("/dataservice?getSolrIndividualsByVClass=1&vclassId="); - } - private Map doClassAlphaDisplay( Map params, VitroRequest request, ServletContext context) throws Exception { - Map body = new HashMap(); - body.putAll(getCommonValues(context, request)); - body.putAll(getClassAlphaValues(params,request,context)); - return body; - } - - private Map getClassAlphaValues( Map params, VitroRequest request, ServletContext context) throws Exception{ - Map map= new HashMap(); - - 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.getSolrIndividualsByVClass(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 getCommonValues( ServletContext context, VitroRequest vreq){ - Map values = new HashMap(); - - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); - List cgList = vcgc.getGroups(); - LinkedList cgtmList = new LinkedList(); - for( VClassGroup classGroup : cgList){ - cgtmList.add( new VClassGroupTemplateModel( classGroup )); - } - values.put("vClassGroups",cgtmList); - - return values; - } - - protected Map doAllClassGroupsDisplay( Map params, Map page, VitroRequest request, ServletContext context) { - Map body = new HashMap(); - body.putAll(getCommonValues(context,request)); - body.putAll(getAllClassGroupData(request, params, page, context)); - - return body; - } - - /** - * Gets a list of all VClassGroups with vclasses with individual counts. - * @param params2 - */ - protected Map getAllClassGroupData(VitroRequest request, Map params, Map page, ServletContext context){ - Map map = new HashMap(); - return map; - } - - protected Map doClassDisplay( Map params, - VitroRequest request, ServletContext context) { - Map body = new HashMap(); - - body.putAll(getCommonValues(context,request)); - body.putAll(getClassData(request,params,context)); - - return body; - } - - private Map getClassData(VitroRequest request, Map params, ServletContext context) { - Map map = new HashMap(); - - 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 inds = vreq.getWebappDaoFactory().getIndividualDao() - .getIndividualsByVClass(vclass); - - List tInds = new ArrayList(inds.size()); - for( Individual ind : inds){ - tInds.add(new ListedIndividual(ind, vreq)); - } - map.put("individualsInClass", tInds); - - return map; - } - - protected Map doClassGroupDisplay(Map params, VitroRequest request, ServletContext context) { - Map body = new HashMap(); - body.putAll(getCommonValues(context,request)); - body.putAll( getClassGroupData(request,params, context)); - - return body; - } - - protected Map getClassGroupData(VitroRequest request, Map params, ServletContext context) { - Map map = new HashMap(); - - String vcgUri = getParam(Mode.CLASS_GROUP, request, params); - VitroRequest vreq = new VitroRequest(request); - - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(request); - VClassGroup vcg = vcgc.getGroup(vcgUri); - - ArrayList classes = new ArrayList(vcg.size()); - for( VClass vc : vcg){ - classes.add(new VClassTemplateModel(vc)); - } - map.put("classes", classes); - - map.put("classGroup", new VClassGroupTemplateModel(vcg)); - - 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 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; - } - - public static 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; - } - - /** - * For processig of JSONObject - */ - public JSONObject convertToJSON(Map dataMap, VitroRequest vreq) { - JSONObject rObj = null; - return rObj; - } - - -} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ClassGroupPageData.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ClassGroupPageData.java deleted file mode 100644 index 0e68dd4b9..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ClassGroupPageData.java +++ /dev/null @@ -1,155 +0,0 @@ -/* $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.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.JSONObject; - -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.UrlBuilder; -import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupsForRequest; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; -import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateModel; - -/** - * This will pass these variables to the template: - * classGroupUri: uri of the classgroup associated with this page. - * vClassGroup: a data structure that is the classgroup associated with this page. - */ -public class ClassGroupPageData implements PageDataGetter{ - private static final Log log = LogFactory.getLog(ClassGroupPageData.class); - - @Override - public Map getData(ServletContext context, VitroRequest vreq, String pageUri, Map page ){ - HashMap data = new HashMap(); - String classGroupUri = vreq.getWebappDaoFactory().getPageDao().getClassGroupPage(pageUri); - data.put("classGroupUri", classGroupUri); - - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); - List vcgList = vcgc.getGroups(); - VClassGroup group = null; - for( VClassGroup vcg : vcgList){ - if( vcg.getURI() != null && vcg.getURI().equals(classGroupUri)){ - group = vcg; - break; - } - } - if( classGroupUri != null && !classGroupUri.isEmpty() && group == null ){ - /*This could be for two reasons: one is that the classgroup doesn't exist - * The other is that there are no individuals in any of the classgroup's classes */ - group = vreq.getWebappDaoFactory().getVClassGroupDao().getGroupByURI(classGroupUri); - if( group != null ){ - List vcgFullList = vreq.getWebappDaoFactory().getVClassGroupDao() - .getPublicGroupsWithVClasses(false, true, false); - for( VClassGroup vcg : vcgFullList ){ - if( classGroupUri.equals(vcg.getURI()) ){ - group = vcg; - break; - } - } - if( group == null ){ - log.error("Cannot get classgroup '" + classGroupUri + "' for page '" + pageUri + "'"); - }else{ - setAllClassCountsToZero(group); - } - }else{ - log.error("classgroup " + classGroupUri + " does not exist in the system"); - } - - } - log.debug("Retrieved class group " + group.getURI() + " and returning to template"); - //if debug enabled, print out the number of entities within each class in the class gorup - if(log.isDebugEnabled()){ - List groupClasses = group.getVitroClassList(); - for(VClass v: groupClasses) { - log.debug("Class " + v.getName() + " - " + v.getURI() + " has " + v.getEntityCount() + " entities"); - } - } - data.put("vClassGroup", group); //may put null - - //This page level data getters tries to set its own template, - // not all of the data getters need to do this. - data.put("bodyTemplate", "page-classgroup.ftl"); - - //Also add data service url - //Hardcoding for now, need a more dynamic way of doing this - data.put("dataServiceUrlIndividualsByVClass", this.getDataServiceUrl()); - return data; - } - - public static VClassGroupTemplateModel getClassGroup(String classGroupUri, ServletContext context, VitroRequest vreq){ - - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); - List vcgList = vcgc.getGroups(); - VClassGroup group = null; - for( VClassGroup vcg : vcgList){ - if( vcg.getURI() != null && vcg.getURI().equals(classGroupUri)){ - group = vcg; - break; - } - } - - if( classGroupUri != null && !classGroupUri.isEmpty() && group == null ){ - /*This could be for two reasons: one is that the classgroup doesn't exist - * The other is that there are no individuals in any of the classgroup's classes */ - group = vreq.getWebappDaoFactory().getVClassGroupDao().getGroupByURI(classGroupUri); - if( group != null ){ - List vcgFullList = vreq.getWebappDaoFactory().getVClassGroupDao() - .getPublicGroupsWithVClasses(false, true, false); - for( VClassGroup vcg : vcgFullList ){ - if( classGroupUri.equals(vcg.getURI()) ){ - group = vcg; - break; - } - } - if( group == null ){ - log.error("Cannot get classgroup '" + classGroupUri + "'"); - return null; - }else{ - setAllClassCountsToZero(group); - } - }else{ - log.error("classgroup " + classGroupUri + " does not exist in the system"); - return null; - } - } - - return new VClassGroupTemplateModel(group); - } - - @Override - public String getType(){ - return PageDataGetterUtils.generateDataGetterTypeURI(ClassGroupPageData.class.getName()); - } - - //Get data servuice - @Override - public String getDataServiceUrl() { - return UrlBuilder.getUrl("/dataservice?getSolrIndividualsByVClass=1&vclassId="); - } - - - /** - * For processing of JSONObject - */ - //Currently empty, TODO: Review requirements - @Override - public JSONObject convertToJSON(Map dataMap, VitroRequest vreq) { - JSONObject rObj = null; - return rObj; - } - protected static void setAllClassCountsToZero(VClassGroup vcg){ - for(VClass vc : vcg){ - vc.setEntityCount(0); - } - } -} \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/IndividualsForClassesDataGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/IndividualsForClassesDataGetter.java deleted file mode 100644 index 902f8ca9d..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/IndividualsForClassesDataGetter.java +++ /dev/null @@ -1,316 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; - -import java.net.URLEncoder; -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.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; - -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.UrlBuilder; -import edu.cornell.mannlib.vitro.webapp.dao.PageDao; -import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupsForRequest; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; -import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateModel; - -/** - * This will pass these variables to the template: - * classGroupUri: uri of the classgroup associated with this page. - * vClassGroup: a data structure that is the classgroup associated with this page. - */ -public class IndividualsForClassesDataGetter implements PageDataGetter{ - private static final Log log = LogFactory.getLog(IndividualsForClassesDataGetter.class); - protected static String restrictClassesTemplateName = null; - @Override - public Map getData(ServletContext context, VitroRequest vreq, String pageUri, Map page ){ - this.setTemplateName(); - HashMap data = new HashMap(); - //This is the old technique of getting class intersections - PageDao pageDao = vreq.getWebappDaoFactory().getPageDao(); - Map classIntersectionsMap = getClassIntersectionsMap(pageDao, pageUri); - - try{ - List classes = retrieveClasses(context, classIntersectionsMap); - List restrictClasses = retrieveRestrictClasses(context, classIntersectionsMap); - log.debug("Retrieving classes for " + classes.toString() + " and restricting by " + restrictClasses.toString()); - processClassesAndRestrictions(vreq, context, data, classes, restrictClasses); - //Also add data service url - //Hardcoding for now, need a more dynamic way of doing this - data.put("dataServiceUrlIndividualsByVClass", this.getDataServiceUrl()); - //this is the class group associated with the data getter utilized for display on menu editing, not the custom one created - data.put("classGroupUri", pageDao.getClassGroupPage(pageUri)); - } catch(Exception ex) { - log.error("An error occurred retrieving Vclass Intersection individuals", ex); - } - - return data; - } - - protected void setTemplateName() { - this.restrictClassesTemplateName = "restricted"; - } - - protected Map getClassIntersectionsMap(PageDao pageDao, - String pageUri) { - // TODO Auto-generated method stub - return pageDao.getClassesAndRestrictionsForPage(pageUri); - } - - protected List retrieveClasses( - ServletContext context, Map classIntersectionsMap) { - List restrictClasses = (List) classIntersectionsMap.get("classes"); - return restrictClasses; - } - - protected List retrieveRestrictClasses( - ServletContext context, Map classIntersectionsMap) { - List restrictClasses = (List) classIntersectionsMap.get("restrictClasses"); - return restrictClasses; - } - - protected void processClassesAndRestrictions(VitroRequest vreq, ServletContext context, - HashMap data, List classes, List restrictClasses ) { - processClassesForDisplay(vreq, context, data, classes); - processRestrictionClasses(vreq, context, data, restrictClasses); - processIntersections(vreq, context, data); - - } - - - - //At this point, data specifices whether or not intersections included - private void processIntersections(VitroRequest vreq, - ServletContext context, HashMap data) { - VClassGroup classesGroup = (VClassGroup) data.get("vClassGroup"); - List vclassList = classesGroup.getVitroClassList(); - List restrictClasses = (List) data.get("restrictVClasses"); - //if there are restrict classes, then update counts - if(restrictClasses.size() > 0) { - log.debug("Restriction classes exist"); - List newVClassList = new ArrayList(); - //Iterate through vclasses and get updated counts, iterated and saved in same order as initially included - for(VClass v: vclassList) { - int oldCount = v.getEntityCount(); - //Making a copy so as to ensure we don't touch the values in the cache - VClass copyVClass = makeCopyVClass(v); - int count = retrieveCount(vreq, context, v, restrictClasses); - if(oldCount != count) { - log.debug("Old count was " + v.getEntityCount() + " and New count for " + v.getURI() + " is " + count); - copyVClass.setEntityCount(count); - } - newVClassList.add(copyVClass); - } - classesGroup.setVitroClassList(newVClassList); - //TODO: Do we need to do this again or will this already be reset? - data.put("vClassGroup", classesGroup); - } - } - - private VClass makeCopyVClass(VClass v) { - VClass copyVClass = new VClass(v.getURI()); - copyVClass.setLocalName(copyVClass.getLocalName()); - copyVClass.setDisplayRank(v.getDisplayRank()); - copyVClass.setName(v.getName()); - copyVClass.setNamespace(v.getNamespace()); - copyVClass.setEntityCount(v.getEntityCount()); - return copyVClass; - } - - //update class count based on restrict classes - private int retrieveCount(VitroRequest vreq, ServletContext context, VClass v, List restrictClasses) { - //Execute solr query that returns only count of individuals - log.debug("Entity count is " + v.getEntityCount()); - List classUris = new ArrayList(); - classUris.add(v.getURI()); - for(VClass r: restrictClasses) { - classUris.add(r.getURI()); - } - long count = PageDataGetterUtils.getIndividualCountForIntersection(vreq, context, classUris); - return new Long(count).intValue(); - - } - - private void processClassesForDisplay(VitroRequest vreq, ServletContext context, HashMap data, List classes) { - VClassGroup classesGroup = new VClassGroup(); - classesGroup.setURI("displayClasses"); - log.debug("Processing classes that will be displayed"); - List vClasses = new ArrayList(); - - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); - for(String classUri: classes) { - //Retrieve vclass from cache to get the count - VClass vclass = vcgc.getCachedVClass(classUri); - //if not found in cache, possibly due to not being in any class group - if(vclass == null) { - vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(classUri); - } - if(vclass != null) { - log.debug("VClass does exist for " + classUri + " and entity count is " + vclass.getEntityCount()); - vClasses.add(vclass); - } else { - log.debug("Vclass " + classUri + " does not exist in the cache"); - log.error("Error occurred, vclass does not exist for this uri " + classUri); - //Throw exception here - } - } - //Sort these classes - Collections.sort(vClasses); - log.debug("Sorting complete for V Classes"); - classesGroup.setVitroClassList(vClasses); - log.debug("Returning vitro class list in data for template"); - //Set vclass group - data.put("vClassGroup", classesGroup); - } - - private void processRestrictionClasses(VitroRequest vreq, ServletContext context, - HashMap data, List restrictClasses) { - try { - VClassGroup restrictClassesGroup = new VClassGroup(); - restrictClassesGroup.setURI("restrictClasses"); - - List restrictVClasses = new ArrayList(); - - List urlEncodedRestrictClasses = new ArrayList(); - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); - - if(restrictClasses.size() > 0) { - //classes for restriction are not displayed so don't need to include their class individual counts - for(String restrictClassUri: restrictClasses) { - //Also uses cache to remain consistent with process classes and also allow - //vclasses to be returned even if switched to display model, although - //uris used within display model editing and not vclass objects - VClass vclass = vcgc.getCachedVClass(restrictClassUri); - //if not found in cache, possibly due to not being in any class group - if(vclass == null) { - vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(restrictClassUri); - } - if(vclass != null) { - log.debug("Found restrict class and adding to list " + restrictClassUri); - restrictVClasses.add(vclass); - } else { - log.error("Error occurred, vclass does not exist for this uri " + restrictClassUri); - } - //Assuming utf-8? - urlEncodedRestrictClasses.add(URLEncoder.encode(restrictClassUri, "UTF-8")); - } - - restrictClassesGroup.setVitroClassList(restrictVClasses); - restrictClassesGroup.setIndividualCount(restrictVClasses.size()); - } else { - - } - String[] restrictClassesArray = new String[urlEncodedRestrictClasses.size()]; - restrictClassesArray = urlEncodedRestrictClasses.toArray(restrictClassesArray); - - //In case just want uris - log.debug("Variable name for including restriction classes " + getRestrictClassesTemplateName()); - data.put(getRestrictClassesTemplateName(), StringUtils.join(restrictClassesArray, ",")); - data.put("restrictVClasses", restrictVClasses); - //not sure if this is useful - data.put("restrictVClassGroup", restrictClassesGroup); - } catch(Exception ex) { - log.error("An error occurred in processing restriction classes ", ex); - } - } - - public static VClassGroupTemplateModel getClassGroup(String classGroupUri, ServletContext context, VitroRequest vreq){ - - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(vreq); - List vcgList = vcgc.getGroups(); - VClassGroup group = null; - for( VClassGroup vcg : vcgList){ - if( vcg.getURI() != null && vcg.getURI().equals(classGroupUri)){ - group = vcg; - break; - } - } - - if( classGroupUri != null && !classGroupUri.isEmpty() && group == null ){ - /*This could be for two reasons: one is that the classgroup doesn't exist - * The other is that there are no individuals in any of the classgroup's classes */ - group = vreq.getWebappDaoFactory().getVClassGroupDao().getGroupByURI(classGroupUri); - if( group != null ){ - List vcgFullList = vreq.getWebappDaoFactory().getVClassGroupDao() - .getPublicGroupsWithVClasses(false, true, false); - for( VClassGroup vcg : vcgFullList ){ - if( classGroupUri.equals(vcg.getURI()) ){ - group = vcg; - break; - } - } - if( group == null ){ - log.error("Cannot get classgroup '" + classGroupUri + "'"); - return null; - }else{ - setAllClassCountsToZero(group); - } - }else{ - log.error("classgroup " + classGroupUri + " does not exist in the system"); - return null; - } - } - - return new VClassGroupTemplateModel(group); - } - - @Override - public String getType(){ - return PageDataGetterUtils.generateDataGetterTypeURI(IndividualsForClassesDataGetter.class.getName()); - } - - //Get data servuice - @Override - public String getDataServiceUrl() { - return UrlBuilder.getUrl("/dataservice?getSolrIndividualsByVClasses=1&vclassId="); - } - /** - * For processig of JSONObject - */ - @Override - public JSONObject convertToJSON(Map map, VitroRequest vreq) { - JSONObject rObj = PageDataGetterUtils.processVclassResultsJSON(map, vreq, true); - return rObj; - } - - protected static void setAllClassCountsToZero(VClassGroup vcg){ - for(VClass vc : vcg){ - vc.setEntityCount(0); - } - } - - protected static String getAlphaParameter(VitroRequest request){ - return request.getParameter("alpha"); - } - - protected static int getPageParameter(VitroRequest request) { - String pageStr = request.getParameter("page"); - if( pageStr != null ){ - try{ - return Integer.parseInt(pageStr); - }catch(NumberFormatException nfe){ - log.debug("could not parse page parameter"); - return 1; - } - }else{ - return 1; - } - } - - //Get template parameter - private static String getRestrictClassesTemplateName() { - return restrictClassesTemplateName; - - } -} \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetter.java deleted file mode 100644 index 724ed0353..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetter.java +++ /dev/null @@ -1,27 +0,0 @@ -/* $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.Map; - -import javax.servlet.ServletContext; - -import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; - -import org.json.JSONObject; - -public interface PageDataGetter{ - Map getData(ServletContext contect, VitroRequest vreq, String pageUri, Map page ); - - /** Gets the type that this class applies to */ - //This has been changed to return the class name for data getter used in menu management - String getType(); - - //get data service url based on data getter requirements - //Get data servuice - String getDataServiceUrl(); - - /**Convert data to JSONObject based on what's required for the data processing**/ - JSONObject convertToJSON(Map map, VitroRequest vreq); -} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtils.java deleted file mode 100644 index 94dd8d378..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtils.java +++ /dev/null @@ -1,339 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; - -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.lang.StringUtils; -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.DataProperty; -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.Controllers; -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.IndividualListController.PageRecord; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; -import edu.cornell.mannlib.vitro.webapp.controller.json.JsonServlet; -import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupsForRequest; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; - -public class PageDataGetterUtils { - protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap"; - private static final Log log = LogFactory.getLog(PageDataGetterUtils.class); - - public static Map getDataForPage(String pageUri, VitroRequest vreq, ServletContext context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { - //Based on page type get the appropriate data getter - Map page = vreq.getWebappDaoFactory().getPageDao().getPage(pageUri); - - Map data = new HashMap(); - List dataGetters = getPageDataGetterObjects(vreq, pageUri); - for(PageDataGetter getter: dataGetters) { - try{ - Map moreData = null; - moreData = getAdditionalData(pageUri, getter.getType(), page, vreq, getter, context); - if( moreData != null) - data.putAll(moreData); - }catch(Throwable th){ - log.error(th,th); - } - } - return data; - } - - /** - * - * Convert data to JSON for page uri based on type and related datagetters - * TODO: How to handle different data getters? Will this replace json fields or add to them? - * @throws ClassNotFoundException - * @throws IllegalAccessException - * @throws InstantiationException - */ - public static JSONObject covertDataToJSONForPage(String pageUri, Map data, VitroRequest vreq, ServletContext context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { - //Get PageDataGetter types associated with pageUri - JSONObject rObj = null; - List dataGetters = getPageDataGetterObjects(vreq, pageUri); - for(PageDataGetter getter: dataGetters) { - JSONObject typeObj = null; - try{ - typeObj = getter.convertToJSON(data, vreq); - if( typeObj != null) { - //Copy over everything from this type Obj to - //TODO: Review how to handle duplicate keys, etc. - if(rObj != null) { - //For now, just nests as separate entry - rObj.put(getter.getType(), typeObj); - } else { - rObj = typeObj; - } - } - - } catch(Throwable th){ - log.error(th,th); - } - } - return rObj; - } - - public static Map getAdditionalData( - String pageUri, String dataGetterName, Map page, VitroRequest vreq, PageDataGetter getter, ServletContext context) { - if(dataGetterName == null || dataGetterName.isEmpty()) - return Collections.emptyMap(); - - - if( getter != null ){ - try{ - log.debug("Retrieve data for this data getter for " + pageUri); - return getter.getData(context, vreq, pageUri, page); - }catch(Throwable th){ - log.error(th,th); - return Collections.emptyMap(); - } - } else { - return Collections.emptyMap(); - } - } - - /*** - * For the page, get the actual Data Getters to be employed. - * @throws ClassNotFoundException - * @throws IllegalAccessException - * @throws InstantiationException - */ - public static List getPageDataGetterObjects(VitroRequest vreq, String pageUri) throws InstantiationException, IllegalAccessException, ClassNotFoundException { - List dataGetterObjects = new ArrayList(); - - List dataGetterClassNames = vreq.getWebappDaoFactory().getPageDao().getDataGetterClass(pageUri); - if( dataGetterClassNames == null ) - return Collections.emptyList(); - - for(String dgClassName: dataGetterClassNames) { - String className = getClassNameFromUri(dgClassName); - Class clz = Class.forName(className); - - if( PageDataGetter.class.isAssignableFrom(clz)){ - PageDataGetter pg = (PageDataGetter) clz.newInstance(); - dataGetterObjects.add(pg); - }// else skip if class does not implement PageDataGetter - } - - return dataGetterObjects; - } - - //Class URIs returned include "java:" and to instantiate object need to remove java: portion - public static String getClassNameFromUri(String dataGetterClassUri) { - if( !StringUtils.isEmpty(dataGetterClassUri) && dataGetterClassUri.contains("java:")) { - String[] splitArray = dataGetterClassUri.split("java:"); - if(splitArray.length > 1) { - return splitArray[1]; - } - } - return dataGetterClassUri; - } - - /** - * Get Individual count for Solr query for intersection of multiple classes - */ - public static long getIndividualCountForIntersection(VitroRequest vreq, ServletContext context, List classUris) { - return IndividualListController.getIndividualCount(classUris, vreq.getWebappDaoFactory().getIndividualDao(), context); - } - - - /** - * Process results related to VClass or vclasses. Handles both single and multiple vclasses being sent. - */ - public static JSONObject processVclassResultsJSON(Map map, VitroRequest vreq, boolean multipleVclasses) { - JSONObject rObj = new JSONObject(); - VClass vclass=null; - - try { - - // Properties from ontologies used by VIVO - should not be in vitro - DataProperty fNameDp = (new DataProperty()); - fNameDp.setURI("http://xmlns.com/foaf/0.1/firstName"); - DataProperty lNameDp = (new DataProperty()); - lNameDp.setURI("http://xmlns.com/foaf/0.1/lastName"); - DataProperty preferredTitleDp = (new DataProperty()); - preferredTitleDp.setURI("http://vivoweb.org/ontology/core#preferredTitle"); - - if( log.isDebugEnabled() ){ - @SuppressWarnings("unchecked") - Enumeration e = vreq.getParameterNames(); - while(e.hasMoreElements()){ - String name = e.nextElement(); - log.debug("parameter: " + name); - for( String value : vreq.getParameterValues(name) ){ - log.debug("value for " + name + ": '" + value + "'"); - } - } - } - - //need an unfiltered dao to get firstnames and lastnames - WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory(); - - String[] vitroClassIdStr = vreq.getParameterValues("vclassId"); - if ( vitroClassIdStr != null && vitroClassIdStr.length > 0){ - for(String vclassId: vitroClassIdStr) { - vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassId); - if (vclass == null) { - log.error("Couldn't retrieve vclass "); - throw new Exception ("Class " + vclassId + " not found"); - } - } - }else{ - log.error("parameter vclassId URI parameter expected "); - throw new Exception("parameter vclassId URI parameter expected "); - } - List vclassIds = Arrays.asList(vitroClassIdStr); - //if single vclass expected, then include vclass. This relates to what the expected behavior is, not size of list - if(!multipleVclasses) { - //currently used for ClassGroupPage - rObj.put("vclass", - new JSONObject().put("URI",vclass.getURI()) - .put("name",vclass.getName())); - } else { - //For now, utilize very last VClass (assume that that is the one to be employed) - //TODO: Find more general way of dealing with this - //put multiple ones in? - if(vclassIds.size() > 0) { - int numberVClasses = vclassIds.size(); - vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassIds.get(numberVClasses - 1)); - rObj.put("vclass", new JSONObject().put("URI",vclass.getURI()) - .put("name",vclass.getName())); - } - // rObj.put("vclasses", new JSONObject().put("URIs",vitroClassIdStr) - // .put("name",vclass.getName())); - } - if (vclass != null) { - - rObj.put("totalCount", map.get("totalCount")); - rObj.put("alpha", map.get("alpha")); - - List inds = (List)map.get("entities"); - log.debug("Number of individuals returned from request: " + inds.size()); - JSONArray jInds = new JSONArray(); - for(Individual ind : inds ){ - JSONObject jo = new JSONObject(); - jo.put("URI", ind.getURI()); - jo.put("label",ind.getRdfsLabel()); - jo.put("name",ind.getName()); - jo.put("thumbUrl", ind.getThumbUrl()); - jo.put("imageUrl", ind.getImageUrl()); - jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq)); - - jo.put("mostSpecificTypes", JsonServlet.getMostSpecificTypes(ind,fullWdf)); - jo.put("preferredTitle", JsonServlet.getDataPropertyValue(ind, preferredTitleDp, fullWdf)); - - jInds.put(jo); - } - rObj.put("individuals", jInds); - - JSONArray wpages = new JSONArray(); - //Made sure that PageRecord here is SolrIndividualListController not IndividualListController - List pages = (List)map.get("pages"); - for( PageRecord pr: pages ){ - JSONObject p = new JSONObject(); - p.put("text", pr.text); - p.put("param", pr.param); - p.put("index", pr.index); - wpages.put( p ); - } - rObj.put("pages",wpages); - - JSONArray jletters = new JSONArray(); - List letters = Controllers.getLetters(); - for( String s : letters){ - JSONObject jo = new JSONObject(); - jo.put("text", s); - jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); - jletters.put( jo ); - } - rObj.put("letters", jletters); - } - } catch(Exception ex) { - log.error("Error occurred in processing JSON object", ex); - } - return rObj; - } - - /* - * Copied from JSONServlet as expect this to be related to VitroClassGroup - */ - public static JSONObject processVClassGroupJSON(VitroRequest vreq, ServletContext context, VClassGroup vcg) { - JSONObject map = new JSONObject(); - try { - ArrayList classes = new ArrayList(vcg.size()); - for( VClass vc : vcg){ - JSONObject vcObj = new JSONObject(); - vcObj.put("name", vc.getName()); - vcObj.put("URI", vc.getURI()); - vcObj.put("entityCount", vc.getEntityCount()); - classes.add(vcObj); - } - map.put("classes", classes); - map.put("classGroupName", vcg.getPublicName()); - map.put("classGroupUri", vcg.getURI()); - - } catch(Exception ex) { - log.error("Error occurred in processing VClass group ", ex); - } - return map; - } - - - //Get All VClass Groups information - //Used within menu management and processing - //TODO: Check if more appropriate location possible - public static List> getClassGroups(HttpServletRequest req) { - //Wanted this to be - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(req); - List vcgList = vcgc.getGroups(); - //For now encoding as hashmap with label and URI as trying to retrieve class group - //results in errors for some reason - List> classGroups = new ArrayList>(); - for(VClassGroup vcg: vcgList) { - HashMap hs = new HashMap(); - hs.put("publicName", vcg.getPublicName()); - hs.put("URI", vcg.getURI()); - classGroups.add(hs); - } - return classGroups; - } - - //Return data getter type to be employed in display model - public static String generateDataGetterTypeURI(String dataGetterClassName) { - return "java:" + dataGetterClassName; - } - - //TODO: Check whether this needs to be put here or elsewhere, as this is data getter specific - //with respect to class groups - //Need to use VClassGroupCache to retrieve class group information - this is the information returned from "for class group" - public static void getClassGroupForDataGetter(HttpServletRequest req, Map pageData, Map templateData) { - //Get the class group from VClassGroup, this is the same as the class group for the class group page data getter - //and the associated class group (not custom) for individuals datagetter - String classGroupUri = (String) pageData.get("classGroupUri"); - VClassGroupsForRequest vcgc = VClassGroupCache.getVClassGroups(req); - VClassGroup group = vcgc.getGroup(classGroupUri); - - templateData.put("classGroup", group); - templateData.put("associatedPage", group.getPublicName()); - templateData.put("associatedPageURI", group.getURI()); - } - -} \ No newline at end of file diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtilsTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtilsTest.java index 3c03fd856..a788e179e 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtilsTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtilsTest.java @@ -67,15 +67,6 @@ public class DataGetterUtilsTest extends AbstractTestClass{ } - @Test - public void testNonPageDataGetter() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException{ - DataGetter dg = DataGetterUtils.dataGetterForURI(vreq, displayModel,dataGetterX); - Assert.assertNull(dg); - - List dgList = - DataGetterUtils.getDataGettersForPage(vreq, displayModel, pageX); - Assert.assertNotNull(dgList); - Assert.assertTrue("List should be, it was not", dgList.size() == 0); - } + } diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/resources/dataGetterTest.n3 b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/resources/dataGetterTest.n3 index 4e1f95ad0..4c9ecc56c 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/resources/dataGetterTest.n3 +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/resources/dataGetterTest.n3 @@ -20,14 +20,5 @@ display:query1data display:saveToVar "people" . -### test of what happens with a PageDataGetter instead of a DataGetter ### - -display:pageX - a display:Page ; - display:title "A PageDataGetter, not a DataGetter" ; - display:urlMapping "/query2" ; - display:hasDataGetter display:pageDataGetterX . - -display:pageDataGetterX - a . + \ No newline at end of file diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtilsTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtilsTest.java deleted file mode 100644 index a6745102c..000000000 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/PageDataGetterUtilsTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; - -import java.io.InputStream; -import java.util.List; - -import org.apache.log4j.Level; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import stubs.javax.servlet.http.HttpServletRequestStub; - -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.impl.RDFDefaultErrorHandler; - -import edu.cornell.mannlib.vitro.testing.AbstractTestClass; -import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; -import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; -import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector; -import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena; - -public class PageDataGetterUtilsTest extends AbstractTestClass{ - OntModel displayModel; - WebappDaoFactory wdf; - - String pageURI = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#pageX"; - String pageURI_2 = "http://vitro.mannlib.cornell.edu/ontologies/display/1.1#SPARQLPage"; - - @Before - public void setUp() throws Exception { - // Suppress error logging. - setLoggerLevel(RDFDefaultErrorHandler.class, Level.OFF); - - OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM); - InputStream in = PageDataGetterUtilsTest.class.getResourceAsStream("resources/pageDataGetter.n3"); - model.read(in,"","N3"); - displayModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,model); - - SimpleOntModelSelector sos = new SimpleOntModelSelector( ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM)); - sos.setDisplayModel(displayModel); - - wdf = new WebappDaoFactoryJena(sos); - } - - @Test - public void testGetPageDataGetterObjects() throws Exception{ - VitroRequest vreq = new VitroRequest( new HttpServletRequestStub() ); - ModelAccess.on(vreq).setWebappDaoFactory(wdf); - - List pdgList = PageDataGetterUtils.getPageDataGetterObjects(vreq, pageURI); - Assert.assertNotNull(pdgList); - Assert.assertTrue("should have one PageDataGetter", pdgList.size() == 1); - } - - @Test - public void testGetNonPageDataGetterObjects() throws Exception{ - VitroRequest vreq = new VitroRequest( new HttpServletRequestStub() ); - ModelAccess.on(vreq).setWebappDaoFactory(wdf); - - List pdgList = PageDataGetterUtils.getPageDataGetterObjects(vreq, pageURI_2); - Assert.assertNotNull(pdgList); - Assert.assertTrue("should have no PageDataGetters", pdgList.size() == 0); - } -} diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/resources/pageDataGetter.n3 b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/resources/pageDataGetter.n3 deleted file mode 100644 index f3ec2f4c0..000000000 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/resources/pageDataGetter.n3 +++ /dev/null @@ -1,30 +0,0 @@ -# $This file is distributed under the terms of the license in /doc/license.txt$ - -@prefix owl: . -@prefix display: . -@prefix rdf: . -@prefix core: . -@prefix xsd: . - -### This file is for the test PageDataGetterUtilsTest.java - -display:SPARQLPage - a display:Page ; - display:title "TestQuery" ; - display:urlMapping "/query1" ; - display:hasDataGetter display:query1data . - -display:query1data - a ; - display:query "SELECT * WHERE { ?uri a } " ; - display:saveToVar "people" . - -display:pageX - a display:Page ; - display:title "A PageDataGetter, not a DataGetter" ; - display:urlMapping "/query2" ; - display:hasDataGetter display:pageDataGetterX . - -display:pageDataGetterX - a . - \ No newline at end of file