From 1d9e1fb2ae6cac910e0f0544d6a049230f8bcff1 Mon Sep 17 00:00:00 2001 From: hjkhjk54 Date: Mon, 18 Jul 2011 20:16:28 +0000 Subject: [PATCH] Updating class counts for internal class menu pages (so counts retrieved based on what you see on page). Also reverted some log info statements in json servlet back to debug level. --- .../vitro/webapp/controller/JsonServlet.java | 16 +++--- .../freemarker/IndividualListController.java | 56 +++++++++++++------ .../utils/pageDataGetter/DataGetterUtils.java | 11 ++++ .../IndividualsForClassesDataGetter.java | 47 +++++++++++++++- 4 files changed, 102 insertions(+), 28 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JsonServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JsonServlet.java index 086ee81bd..e0ce72697 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JsonServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JsonServlet.java @@ -84,7 +84,7 @@ public class JsonServlet extends VitroHttpServlet { getVClassesForVClassGroup(req,resp); return; } else if( vreq.getParameter("getSolrIndividualsByVClasses") != null ){ - log.info("AJAX request to retrieve individuals by vclasses"); + log.debug("AJAX request to retrieve individuals by vclasses"); getSolrIndividualsByVClasses(req,resp); return; } else if( vreq.getParameter("getDataForPage") != null ){ @@ -197,18 +197,18 @@ public class JsonServlet extends VitroHttpServlet { // Accepts multiple vclasses and returns individuals which correspond to the intersection of those classes (i.e. have all those types) private void getSolrIndividualsByVClasses( HttpServletRequest req, HttpServletResponse resp ){ - log.info("Executing retrieval of individuals by vclasses"); + log.debug("Executing retrieval of individuals by vclasses"); String errorMessage = null; JSONObject rObj = null; try{ VitroRequest vreq = new VitroRequest(req); VClass vclass=null; - log.info("Retrieving solr individuals by vclasses"); + log.debug("Retrieving solr individuals by vclasses"); // Could have multiple vclass ids sent in String[] vitroClassIdStr = vreq.getParameterValues("vclassId"); if ( vitroClassIdStr != null && vitroClassIdStr.length > 0){ for(String vclassId: vitroClassIdStr) { - log.info("Iterating throug vclasses, using VClass " + vclassId); + log.debug("Iterating throug vclasses, using VClass " + vclassId); vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassId); if (vclass == null) { log.error("Couldn't retrieve vclass "); @@ -250,19 +250,19 @@ public class JsonServlet extends VitroHttpServlet { public static JSONObject getSolrIndividualsByVClasses(List vclassURIs, HttpServletRequest req, ServletContext context) throws Exception { VitroRequest vreq = new VitroRequest(req); - log.info("Retrieve solr results for vclasses" + vclassURIs.toString()); + log.debug("Retrieve solr results for vclasses" + vclassURIs.toString()); Map map = getSolrVClassIntersectionResults(vclassURIs, vreq, context); - log.info("Results returned from Solr for " + vclassURIs.toString() + " are of size " + map.size()); + log.debug("Results returned from Solr for " + vclassURIs.toString() + " are of size " + map.size()); JSONObject rObj = processVClassResults(map, vreq, context, true); return rObj; } //Including version for Solr query for Vclass Intersections private static Map getSolrVClassIntersectionResults(List vclassURIs, VitroRequest vreq, ServletContext context){ - log.info("Retrieving Solr intersection results for " + vclassURIs.toString()); + log.debug("Retrieving Solr intersection results for " + vclassURIs.toString()); String alpha = IndividualListController.getAlphaParameter(vreq); int page = IndividualListController.getPageParameter(vreq); - log.info("Alpha and page parameters are " + alpha + " and " + page); + log.debug("Alpha and page parameters are " + alpha + " and " + page); Map map = null; try { map = IndividualListController.getResultsForVClassIntersections( 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 e6ff39569..e36fee7f3 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 @@ -250,22 +250,25 @@ public class IndividualListController extends FreemarkerHttpServlet { List individuals = new ArrayList(INDIVIDUALS_PER_PAGE); int individualsAdded = 0; int index = (page-1) * INDIVIDUALS_PER_PAGE; - while (individualsAdded < INDIVIDUALS_PER_PAGE && index < hitCount) { - SolrDocument doc = docs.get(index); - if (doc != null) { - String uri = doc.get(VitroSearchTermNames.URI).toString(); - Individual individual = indDao.getIndividualByURI( uri ); - if (individual != null) { - individualsAdded++; - individuals.add(individual); - log.debug("Adding individual " + uri + " to individual list display"); - } else { - log.debug("No existing individual for search document with uri = " + uri); - } - } - index++; + if(docs.size() > 0) { + while (individualsAdded < INDIVIDUALS_PER_PAGE && index < hitCount) { + SolrDocument doc = docs.get(index); + if (doc != null) { + String uri = doc.get(VitroSearchTermNames.URI).toString(); + Individual individual = indDao.getIndividualByURI( uri ); + if (individual != null) { + individualsAdded++; + individuals.add(individual); + log.debug("Adding individual " + uri + " to individual list display"); + } else { + log.debug("No existing individual for search document with uri = " + uri); + } + } + index++; + } + } else { + log.debug("Docs size is 0"); } - // Test index < hitCount ensures that there are still some docs left if ( hitCount > INDIVIDUALS_PER_PAGE && index < hitCount ){ rvMap.put("showPages", Boolean.TRUE); @@ -284,7 +287,24 @@ public class IndividualListController extends FreemarkerHttpServlet { return rvMap; } - private static SolrQuery getQuery(List vclassUris, String alpha){ + //Get count of individuals without actually getting the results + public static long getIndividualCount(List vclassUris, IndividualDao indDao, ServletContext context) { + SolrQuery query = getQuery(vclassUris, null, 0); + try { + Map rvMap = getResultsForVClassQuery(query, 1, null, indDao, context); + Long count = (Long) rvMap.get("totalCount"); + return count.longValue(); + } catch(Exception ex) { + log.error("An error occured in retrieving individual count", ex); + } + return 0; + } + + private static SolrQuery getQuery(List vclassUris, String alpha) { + return getQuery(vclassUris, alpha, INDIVIDUAL_LIST_CONTROLLER_MAX_RESULTS); + } + + private static SolrQuery getQuery(List vclassUris, String alpha, int numberRows){ String queryText = ""; @@ -307,13 +327,13 @@ public class IndividualListController extends FreemarkerHttpServlet { SolrQuery query = new SolrQuery(queryText); log.debug("Query text is " + queryText); - + // Get all available results from index rather than just those for the current page. // Otherwise, if there are a large number of non-existent individuals in the search // index, the current page of results might not retrieve any existing individuals, // and nothing gets returned. query.setStart(0) - .setRows(INDIVIDUAL_LIST_CONTROLLER_MAX_RESULTS) + .setRows(numberRows) // Need a single-valued field for sorting .setSortField(VitroSearchTermNames.NAME_LOWERCASE_SINGLE_VALUED, SolrQuery.ORDER.asc); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java index fb6a495c9..f240f05b0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java @@ -26,6 +26,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.Controllers; import edu.cornell.mannlib.vitro.webapp.controller.JsonServlet; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.PageRecord; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; @@ -153,6 +154,14 @@ public class DataGetterUtils { } } + /** + * 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. */ @@ -297,4 +306,6 @@ public class DataGetterUtils { return map; } + + } \ 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 index 1be5483ea..48781f013 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/IndividualsForClassesDataGetter.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/IndividualsForClassesDataGetter.java @@ -19,6 +19,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; @@ -56,9 +57,52 @@ public class IndividualsForClassesDataGetter implements PageDataGetter{ HashMap data, List classes, List restrictClasses ) { processClassesForDisplay(context, data, classes); processRestrictionClasses(vreq, context, data, restrictClasses); + processIntersections(vreq, context, data); } - private void processClassesForDisplay(ServletContext context, HashMap data, List classes) { + //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) { + List newVClassList = new ArrayList(); + //Iterate through vclasses and get updated counts + 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 = new VClass(v.getURI()); + copyVClass.setEntityCount(oldCount); + 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); + } + } + + //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 = DataGetterUtils.getIndividualCountForIntersection(vreq, context, classUris); + return new Long(count).intValue(); + + } + + private void processClassesForDisplay(ServletContext context, HashMap data, List classes) { VClassGroup classesGroup = new VClassGroup(); classesGroup.setURI("displayClasses"); @@ -70,7 +114,6 @@ public class IndividualsForClassesDataGetter implements PageDataGetter{ //Retrieve vclass from cache to get the count VClass vclass = vcgc.getCachedVClass(classUri); if(vclass != null) { - log.debug("VClass does exist for " + classUri + " and entity count is " + vclass.getEntityCount()); vClasses.add(vclass); } else {