From 8419ce184a4b167b5b2bcfd320f0790d9dcc1e6a Mon Sep 17 00:00:00 2001 From: j2blake Date: Fri, 25 Oct 2013 17:51:17 -0400 Subject: [PATCH] VIVO-396 Refactoring JsonServlet and IndividualListController VIVO-dependent code must be isolated from the Vitro classes, and these classes must be disentangled first. At least some. --- .../freemarker/IndividualListController.java | 51 ++---- .../individuallist/IndividualJsonWrapper.java | 57 ++++++ .../individuallist/IndividualListResults.java | 87 +++++++++ .../IndividualListResultsUtils.java | 148 +++++++++++++++ .../webapp/controller/json/JsonServlet.java | 169 ++---------------- .../utils/dataGetter/DataGetterUtils.java | 128 ------------- .../IndividualsForClassesDataGetter.java | 7 +- .../dataGetter/SolrIndividualsDataGetter.java | 13 +- .../webapp/utils/solr/SolrQueryUtils.java | 4 - 9 files changed, 332 insertions(+), 332 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResults.java create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java index 54b295e6d..184bda547 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/IndividualListController.java @@ -13,10 +13,7 @@ import javax.servlet.ServletContext; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.solr.client.solrj.SolrQuery; -import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; -import org.apache.solr.client.solrj.response.QueryResponse; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.VClass; @@ -25,9 +22,8 @@ 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.ResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; +import edu.cornell.mannlib.vitro.webapp.controller.individuallist.IndividualListResults; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; -import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; -import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup; import edu.cornell.mannlib.vitro.webapp.utils.solr.SolrQueryUtils; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividual; @@ -94,16 +90,15 @@ public class IndividualListController extends FreemarkerHttpServlet { String alpha = getAlphaParameter(vreq); int page = getPageParameter(vreq); - Map map = getResultsForVClass( + IndividualListResults vcResults = getResultsForVClass( vclass.getURI(), page, alpha, vreq.getWebappDaoFactory().getIndividualDao(), getServletContext()); - body.putAll(map); + body.putAll(vcResults.asFreemarkerMap()); - @SuppressWarnings("unchecked") - List inds = (List)map.get("entities"); + List inds = vcResults.getEntities(); List indsTm = new ArrayList(); if (inds != null) { for ( Individual ind : inds ) { @@ -154,13 +149,12 @@ public class IndividualListController extends FreemarkerHttpServlet { return SolrQueryUtils.getPageParameter(request); } - public static Map getResultsForVClass(String vclassURI, int page, String alpha, IndividualDao indDao, ServletContext context) + public static IndividualListResults getResultsForVClass(String vclassURI, int page, String alpha, IndividualDao indDao, ServletContext context) throws SearchException{ - Map rvMap = new HashMap(); try{ List classUris = Collections.singletonList(vclassURI); IndividualListQueryResults results = SolrQueryUtils.buildAndExecuteVClassQuery(classUris, alpha, page, INDIVIDUALS_PER_PAGE, context, indDao); - rvMap = getResultsForVClassQuery(results, page, INDIVIDUALS_PER_PAGE, alpha); + return getResultsForVClassQuery(results, page, INDIVIDUALS_PER_PAGE, alpha); } catch (SolrServerException e) { String msg = "An error occurred retrieving results for vclass query"; log.error(msg, e); @@ -168,31 +162,29 @@ public class IndividualListController extends FreemarkerHttpServlet { throw new SearchException(msg); } catch(Throwable th) { log.error("An error occurred retrieving results for vclass query", th); + return IndividualListResults.EMPTY; } - return rvMap; } - public static Map getResultsForVClassIntersections(List vclassURIs, int page, int pageSize, String alpha, IndividualDao indDao, ServletContext context) { - Map rvMap = new HashMap(); + public static IndividualListResults getResultsForVClassIntersections(List vclassURIs, int page, int pageSize, String alpha, IndividualDao indDao, ServletContext context) { try{ IndividualListQueryResults results = SolrQueryUtils.buildAndExecuteVClassQuery(vclassURIs, alpha, page, pageSize, context, indDao); - rvMap = getResultsForVClassQuery(results, page, pageSize, alpha); + return getResultsForVClassQuery(results, page, pageSize, alpha); } catch(Throwable th) { log.error("Error retrieving individuals corresponding to intersection multiple classes." + vclassURIs.toString(), th); + return IndividualListResults.EMPTY; } - return rvMap; } - public static Map getRandomResultsForVClass(String vclassURI, int page, int pageSize, IndividualDao indDao, ServletContext context) { - Map rvMap = new HashMap(); + public static IndividualListResults getRandomResultsForVClass(String vclassURI, int page, int pageSize, IndividualDao indDao, ServletContext context) { try{ List classUris = Collections.singletonList(vclassURI); IndividualListQueryResults results = SolrQueryUtils.buildAndExecuteRandomVClassQuery(classUris, page, pageSize, context, indDao); - rvMap = getResultsForVClassQuery(results, page, pageSize, ""); + return getResultsForVClassQuery(results, page, pageSize, ""); } catch(Throwable th) { log.error("An error occurred retrieving random results for vclass query", th); + return IndividualListResults.EMPTY; } - return rvMap; } //TODO: Get rid of this method and utilize SolrQueryUtils - currently appears to be referenced @@ -201,24 +193,13 @@ public class IndividualListController extends FreemarkerHttpServlet { return SolrQueryUtils.getIndividualCount(vclassUris, indDao, context); } - private static Map getResultsForVClassQuery(IndividualListQueryResults results, int page, int pageSize, String alpha) { - Map rvMap = new HashMap(); - + private static IndividualListResults getResultsForVClassQuery(IndividualListQueryResults results, int page, int pageSize, String alpha) { long hitCount = results.getHitCount(); if ( hitCount > pageSize ){ - rvMap.put("showPages", Boolean.TRUE); - List pageRecords = makePagesList(hitCount, pageSize, page); - rvMap.put("pages", pageRecords); + return new IndividualListResults(hitCount, results.getIndividuals(), alpha, true, makePagesList(hitCount, pageSize, page)); }else{ - rvMap.put("showPages", Boolean.FALSE); - rvMap.put("pages", Collections.emptyList()); + return new IndividualListResults(hitCount, results.getIndividuals(), alpha, false, Collections.emptyList()); } - - rvMap.put("alpha",alpha); - rvMap.put("totalCount", hitCount); - rvMap.put("entities", results.getIndividuals()); - - return rvMap; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java new file mode 100644 index 000000000..b96272883 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualJsonWrapper.java @@ -0,0 +1,57 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.controller.individuallist; + +import java.util.Collection; +import java.util.Map; + +import org.json.JSONException; +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.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.ObjectPropertyStatementDao; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; + +/** + * Wrap an Individual in a JSON object for display by the script. + * + * This will be overridden in VIVO so we can have more info in the display. + */ +public class IndividualJsonWrapper { + static JSONObject packageIndividualAsJson(VitroRequest vreq, Individual ind) + throws JSONException { + // need an unfiltered dao to get firstnames and lastnames + WebappDaoFactory fullWdf = vreq.getUnfilteredWebappDaoFactory(); + + // TODO -- get this VIVO property out of Vitro code! + DataProperty preferredTitleDp = (new DataProperty()); + preferredTitleDp + .setURI("http://vivoweb.org/ontology/core#preferredTitle"); + + 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", getMostSpecificTypes(ind, fullWdf)); + jo.put("preferredTitle", JsonServlet.getDataPropertyValue(ind, + preferredTitleDp, fullWdf)); + return jo; + } + + public static Collection getMostSpecificTypes( + Individual individual, WebappDaoFactory wdf) { + ObjectPropertyStatementDao opsDao = wdf.getObjectPropertyStatementDao(); + Map mostSpecificTypes = opsDao + .getMostSpecificTypesInClassgroupsForIndividual(individual + .getURI()); + return mostSpecificTypes.values(); + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResults.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResults.java new file mode 100644 index 000000000..68cbc1d09 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResults.java @@ -0,0 +1,87 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.controller.individuallist; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.PageRecord; + +/** + * These are the paged results of a query for Individuals. + * + * The criteria for the search are the index of the desired page, the number of + * results displayed on each page, and an optional initial letter to search + * against. + * + * By the time this is built, the results have already been partially processed. + * A list of PageRecord object is included, with values that the GUI can use to + * create Alphabetical links. Maybe this processing should have been done later. + * Maybe it should have been left to the GUI. + */ +public class IndividualListResults { + private static final Log log = LogFactory + .getLog(IndividualListResults.class); + + public static final IndividualListResults EMPTY = new IndividualListResults(); + + private final long totalCount; + private final List entities; + private final String alpha; + private final boolean showPages; + private final List pages; + + public IndividualListResults(long totalCount, List entities, + String alpha, boolean showPages, List pages) { + this.totalCount = totalCount; + this.entities = entities; + this.alpha = alpha; + this.showPages = showPages; + this.pages = pages; + } + + private IndividualListResults() { + this(0L, Collections. emptyList(), "", false, Collections + . emptyList()); + } + + public long getTotalCount() { + return totalCount; + } + + public String getAlpha() { + return alpha; + } + + public List getEntities() { + return entities; + } + + public List getPages() { + return pages; + } + + public boolean isShowPages() { + return showPages; + } + + /** + * Some controllers put this data directly into the Freemarker body map. + * Others wrap it in JSON. + */ + public Map asFreemarkerMap() { + Map m = new HashMap<>(); + m.put("showPages", showPages); + m.put("pages", pages); + m.put("alpha", alpha); + m.put("totalCount", totalCount); + m.put("entities", entities); + return m; + } +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java new file mode 100644 index 000000000..e06f35af9 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/individuallist/IndividualListResultsUtils.java @@ -0,0 +1,148 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.controller.individuallist; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONException; +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.controller.Controllers; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.PageRecord; + +/** + * Utility methods for procesing the paged results of a query for a list of Individuals. + * + * Right now, there is only a method to wrap the results in Json. + */ +public class IndividualListResultsUtils { + private static final Log log = LogFactory + .getLog(IndividualListResultsUtils.class); + + /** + * Process results related to VClass or vclasses. Handles both single and + * multiple vclasses being sent. + */ + public static JSONObject wrapIndividualListResultsInJson(IndividualListResults results, VitroRequest vreq, + boolean multipleVclasses) { + JSONObject rObj = new JSONObject(); + + if (log.isDebugEnabled()) { + dumpParametersFromRequest(vreq); + } + + try { + List vclasses = buildListOfRequestedVClasses(vreq); + + VClass vclass = null; + // if single vclass expected, then include vclass. + // This relates to what the expected behavior is, not size of list + if (!multipleVclasses) { + vclass = vclasses.get(0); + // currently used for ClassGroupPage + } 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? + vclass = vclasses.get(vclasses.size() - 1); + // rObj.put("vclasses", new JSONObject().put("URIs",vitroClassIdStr).put("name",vclass.getName())); + } + + rObj.put("vclass", packageVClassAsJson(vclass)); + rObj.put("totalCount", results.getTotalCount()); + rObj.put("alpha", results.getAlpha()); + rObj.put("individuals", packageIndividualsAsJson(vreq, results.getEntities())); + rObj.put("pages", packagePageRecordsAsJson(results.getPages())); + rObj.put("letters", packageLettersAsJson()); + } catch (Exception ex) { + log.error("Error occurred in processing JSON object", ex); + } + return rObj; + } + + private static List buildListOfRequestedVClasses(VitroRequest vreq) + throws Exception { + String[] vitroClassIdStr = vreq.getParameterValues("vclassId"); + if (ArrayUtils.isEmpty(vitroClassIdStr)) { + log.error("parameter vclassId URI parameter expected "); + throw new Exception("parameter vclassId URI parameter expected "); + } + + List list = new ArrayList<>(); + for (String vclassId : vitroClassIdStr) { + VClass vclass = vreq.getWebappDaoFactory().getVClassDao() + .getVClassByURI(vclassId); + if (vclass == null) { + log.error("Couldn't retrieve vclass "); + throw new Exception("Class " + vclassId + " not found"); + } + list.add(vclass); + } + return list; + } + + private static JSONObject packageVClassAsJson(VClass vclass) + throws JSONException { + JSONObject jvclass = new JSONObject(); + jvclass.put("URI", vclass.getURI()); + jvclass.put("name", vclass.getName()); + return jvclass; + } + + private static JSONArray packageLettersAsJson() throws JSONException, + UnsupportedEncodingException { + List letters = Controllers.getLetters(); + JSONArray jletters = new JSONArray(); + for (String s : letters) { + JSONObject jo = new JSONObject(); + jo.put("text", s); + jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); + jletters.put(jo); + } + return jletters; + } + + private static JSONArray packagePageRecordsAsJson(List pages) + throws JSONException { + JSONArray wpages = new JSONArray(); + 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); + } + return wpages; + } + + private static JSONArray packageIndividualsAsJson(VitroRequest vreq, + List inds) throws JSONException { + log.debug("Number of individuals returned from request: " + inds.size()); + + JSONArray jInds = new JSONArray(); + for (Individual ind : inds) { + jInds.put(IndividualJsonWrapper.packageIndividualAsJson(vreq, ind)); + } + return jInds; + } + + private static void dumpParametersFromRequest(VitroRequest vreq) { + Map pMap = vreq.getParameterMap(); + for (String name : pMap.keySet()) { + for (String value : pMap.get(name)) { + log.debug("value for " + name + ": '" + value + "'"); + } + } + } + +} 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 baf72ec82..6ed8a362e 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,11 +3,8 @@ 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; @@ -18,18 +15,15 @@ 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.controller.individuallist.IndividualListResultsUtils; +import edu.cornell.mannlib.vitro.webapp.controller.individuallist.IndividualListResults; 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; @@ -94,29 +88,28 @@ public class JsonServlet extends VitroHttpServlet { List vclassURIs = Collections.singletonList(vclassURI); VitroRequest vreq = new VitroRequest(req); - Map map = getSolrVClassIntersectionResults(vclassURIs, vreq, context); + IndividualListResults vcResults = getSolrVClassIntersectionResults(vclassURIs, vreq, context); //last parameter indicates single vclass instead of multiple vclasses - return processVclassResultsJSON(map, vreq, false); + return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, false); } public static JSONObject getSolrIndividualsByVClasses(List vclassURIs, HttpServletRequest req, ServletContext context) throws Exception { VitroRequest vreq = new VitroRequest(req); 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()); + IndividualListResults vcResults = getSolrVClassIntersectionResults(vclassURIs, vreq, context); + log.debug("Results returned from Solr for " + vclassURIs.toString() + " are of size " + vcResults.getTotalCount()); - return processVclassResultsJSON(map, vreq, true); + return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, true); } //Including version for Solr query for Vclass Intersections - private static Map getSolrVClassIntersectionResults(List vclassURIs, VitroRequest vreq, ServletContext context){ + private static IndividualListResults getSolrVClassIntersectionResults(List vclassURIs, VitroRequest vreq, ServletContext context){ log.debug("Retrieving Solr intersection results for " + vclassURIs.toString()); String alpha = IndividualListController.getAlphaParameter(vreq); int page = IndividualListController.getPageParameter(vreq); log.debug("Alpha and page parameters are " + alpha + " and " + page); - Map map = null; try { - map = IndividualListController.getResultsForVClassIntersections( + return IndividualListController.getResultsForVClassIntersections( vclassURIs, page, INDIVIDUALS_PER_PAGE, alpha, @@ -124,23 +117,10 @@ public class JsonServlet extends VitroHttpServlet { context); } catch(Exception ex) { log.error("Error in retrieval of search results for VClass " + vclassURIs.toString(), ex); + return IndividualListResults.EMPTY; } - - return map; } - // 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 = processVclassResultsJSON(map, vreq, multipleVclasses); -// return rObj; -// } - - public static Collection getMostSpecificTypes(Individual individual, WebappDaoFactory wdf) { - ObjectPropertyStatementDao opsDao = wdf.getObjectPropertyStatementDao(); - Map mostSpecificTypes = opsDao.getMostSpecificTypesInClassgroupsForIndividual(individual.getURI()); - return mostSpecificTypes.values(); - } - public static String getDataPropertyValue(Individual ind, DataProperty dp, WebappDaoFactory wdf){ String value = ind.getDataValue(dp.getURI()); if( value == null || value.isEmpty() ) @@ -152,21 +132,20 @@ public class JsonServlet extends VitroHttpServlet { public static JSONObject getRandomSolrIndividualsByVClass(String vclassURI, HttpServletRequest req, ServletContext context) throws Exception { VitroRequest vreq = new VitroRequest(req); - Map map = getRandomSolrVClassResults(vclassURI, vreq, context); + IndividualListResults vcResults = getRandomSolrVClassResults(vclassURI, vreq, context); //last parameter indicates single vclass instead of multiple vclasses - return processVclassResultsJSON(map, vreq, false); + return IndividualListResultsUtils.wrapIndividualListResultsInJson(vcResults, vreq, false); } //Including version for Random Solr query for Vclass Intersections - private static Map getRandomSolrVClassResults(String vclassURI, VitroRequest vreq, ServletContext context){ + private static IndividualListResults getRandomSolrVClassResults(String vclassURI, VitroRequest vreq, ServletContext context){ log.debug("Retrieving random Solr intersection results for " + vclassURI); int page = IndividualListController.getPageParameter(vreq); int pageSize = Integer.parseInt(vreq.getParameter("pageSize")); log.debug("page and pageSize parameters = " + page + " and " + pageSize); - Map map = null; try { - map = IndividualListController.getRandomResultsForVClass( + return IndividualListController.getRandomResultsForVClass( vclassURI, page, pageSize, @@ -174,126 +153,8 @@ public class JsonServlet extends VitroHttpServlet { context); } catch(Exception ex) { log.error("Error in retrieval of search results for VClass " + vclassURI, ex); + return IndividualListResults.EMPTY; } - - return map; } - - /** - * 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/dataGetter/DataGetterUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtils.java index 008a3870f..f8aa99ce1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/DataGetterUtils.java @@ -3,10 +3,7 @@ package edu.cornell.mannlib.vitro.webapp.utils.dataGetter; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.net.URLEncoder; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,7 +14,6 @@ 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 com.hp.hpl.jena.query.Query; @@ -35,20 +31,13 @@ import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.vocabulary.OWL; -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.DisplayVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupsForRequest; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; @@ -324,123 +313,6 @@ public class DataGetterUtils { return classGroupUri; } - /** - * 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; - } - private static final String forClassGroupURI = "<" + DisplayVocabulary.FOR_CLASSGROUP + ">"; private static final String classGroupForDataGetterQuery = diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/IndividualsForClassesDataGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/IndividualsForClassesDataGetter.java index 1788e3fa4..45c4a3eba 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/IndividualsForClassesDataGetter.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/IndividualsForClassesDataGetter.java @@ -353,14 +353,15 @@ public class IndividualsForClassesDataGetter extends DataGetterBase implements D public String getDataServiceUrl() { return UrlBuilder.getUrl("/dataservice?getRenderedSolrIndividualsByVClass=1&vclassId="); } + /** * For processig of JSONObject */ - public JSONObject convertToJSON(Map map, VitroRequest vreq) { - JSONObject rObj = DataGetterUtils.processVclassResultsJSON(map, vreq, true); + 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); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/SolrIndividualsDataGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/SolrIndividualsDataGetter.java index 278b8c645..cb5e89c67 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/SolrIndividualsDataGetter.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/dataGetter/SolrIndividualsDataGetter.java @@ -3,9 +3,7 @@ package edu.cornell.mannlib.vitro.webapp.utils.dataGetter; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -23,7 +21,6 @@ import com.hp.hpl.jena.query.QuerySolutionMap; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.shared.Lock; @@ -33,8 +30,9 @@ 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.controller.freemarker.IndividualListController.SearchException; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.controller.individuallist.IndividualListResults; import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; import edu.cornell.mannlib.vitro.webapp.utils.solr.SolrQueryUtils; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individuallist.ListedIndividual; @@ -174,16 +172,15 @@ public class SolrIndividualsDataGetter extends DataGetterBase implements DataGet try { String alpha = SolrQueryUtils.getAlphaParameter(vreq); int page = SolrQueryUtils.getPageParameter(vreq); - Map map = IndividualListController.getResultsForVClass( + IndividualListResults vcResults = IndividualListController.getResultsForVClass( vclass.getURI(), page, alpha, vreq.getWebappDaoFactory().getIndividualDao(), vreq.getSession().getServletContext()); - body.putAll(map); + body.putAll(vcResults.asFreemarkerMap()); - @SuppressWarnings("unchecked") - List inds = (List)map.get("entities"); + List inds = vcResults.getEntities(); List indsTm = new ArrayList(); if (inds != null) { for ( Individual ind : inds ) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/solr/SolrQueryUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/solr/SolrQueryUtils.java index 608ae0efc..924a5cd58 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/solr/SolrQueryUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/solr/SolrQueryUtils.java @@ -4,8 +4,6 @@ package edu.cornell.mannlib.vitro.webapp.utils.solr; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -20,9 +18,7 @@ import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; 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.IndividualListQueryResults; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.SearchException; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames; import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup;