From d2d91dfe7750ddfafe4b282c0b75fe4f6a0b63b5 Mon Sep 17 00:00:00 2001 From: bdc34 Date: Thu, 16 Dec 2010 18:13:37 +0000 Subject: [PATCH] Adding service to get individual listing for class via lucene as a JSON result. --- .../vitro/webapp/controller/Controllers.java | 6 +- .../controller/EntityListController.java | 7 +- .../vitro/webapp/controller/JSONServlet.java | 109 +++++++++++++++++- 3 files changed, 116 insertions(+), 6 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/Controllers.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/Controllers.java index e7372ce6f..8c755addd 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/Controllers.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/Controllers.java @@ -94,12 +94,12 @@ public class Controllers { //public static final String TAB_ENTITIES_LIST_JSP = "templates/tab/tabEntities.jsp"; - private static List letters = null; - public static List getLetters() { + private static List letters = null; + public static List getLetters() { //there must be a better place to put this. if (Controllers.letters == null) { char c[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); - Controllers.letters = new ArrayList(c.length); + Controllers.letters = new ArrayList(c.length); for (int i = 0; i < c.length; i++) { letters.add("" + c[i]); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/EntityListController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/EntityListController.java index 24722f5d2..e3eaddd22 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/EntityListController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/EntityListController.java @@ -68,7 +68,7 @@ public class EntityListController extends VitroHttpServlet { throws IOException, ServletException { startTime = System.currentTimeMillis(); // TODO: remove try { - super.doGet(req, res); + super.doGet(req, res); VitroRequest vreq = new VitroRequest(req); Object obj = req.getAttribute("vclass"); VClass vclass=null; @@ -164,7 +164,10 @@ public class EntityListController extends VitroHttpServlet { rd.include(request,res); } - + /** + * This method is now called in a couple of places. It should be refactored + * into a DAO or similar object. + */ public static Map getResultsForVClass(String vclassURI, int page, String alpha, Portal portal, boolean isSinglePortal, IndividualDao indDao, ServletContext context) throws CorruptIndexException, IOException, ServletException{ Map rvMap = new HashMap(); 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 0378dc780..73cbd0ac1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JSONServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JSONServlet.java @@ -4,6 +4,9 @@ package edu.cornell.mannlib.vitro.webapp.controller; import java.io.IOException; import java.io.PrintWriter; +import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -22,15 +25,19 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import atg.taglib.json.util.HTTP; + import com.hp.hpl.jena.ontology.OntModel; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.controller.TabEntitiesController.PageRecord; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.EditConfiguration; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.SelectListGenerator; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.web.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel; /** * This servlet is for servicing requests for JSON objects/data. @@ -61,7 +68,107 @@ public class JSONServlet extends VitroHttpServlet { }else if( vreq.getParameter("getN3EditOptionList") != null ){ doN3EditOptionList(req,resp); return; + }else if( vreq.getParameter("getLuceneIndividualsByVClass") != null ){ + getLuceneIndividualsByVClass(req,resp); + return; } + + } + + private void getLuceneIndividualsByVClass(HttpServletRequest req, HttpServletResponse resp) { + VitroRequest vreq = new VitroRequest(req); + String errorMessage = null; + String message = null; + VClass vclass=null; + JSONObject rObj = new JSONObject(); + + try { + String vitroClassIdStr = vreq.getParameter("vclassId"); + if ( vitroClassIdStr != null && !vitroClassIdStr.isEmpty()){ + vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vitroClassIdStr); + if (vclass == null) { + log.debug("Couldn't retrieve vclass "); + throw new Exception (errorMessage = "Class " + vitroClassIdStr + " not found"); + } + }else{ + log.debug("parameter vclassId URI parameter expected "); + throw new Exception("parameter vclassId URI parameter expected "); + } + + rObj.put("vclass", + new JSONObject().put("URI",vclass.getURI()) + .put("name",vclass.getName())); + + if (vclass != null) { + String alpha = EntityListController.getAlphaParamter(vreq); + int page = EntityListController.getPageParameter(vreq); + Map map = EntityListController.getResultsForVClass( + vclass.getURI(), + page, + alpha, + vreq.getPortal(), + vreq.getWebappDaoFactory().getPortalDao().isSinglePortal(), + vreq.getWebappDaoFactory().getIndividualDao(), + getServletContext()); + + rObj.put("totalCount", map.get("totalCount")); + rObj.put("alpha", map.get("alpha")); + + List inds = (List)map.get("entities"); + List indsTm = new ArrayList(); + 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()); + jInds.put(jo); + } + rObj.put("individuals", jInds); + + JSONArray wpages = new JSONArray(); + 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){ + errorMessage = ex.getMessage(); + } + try{ + resp.setCharacterEncoding("UTF-8"); + resp.setContentType("application/json;charset=UTF-8"); + + if( errorMessage != null ){ + rObj.put("errorMessage", errorMessage); + resp.setStatus(500 /*HttpURLConnection.HTTP_SERVER_ERROR*/); + }else{ + rObj.put("errorMessage", ""); + } + Writer writer = resp.getWriter(); + writer.write(rObj.toString()); + }catch(JSONException jse){ + log.error(jse,jse); + } catch (IOException e) { + log.error(e,e); + } + return; } /** @@ -130,7 +237,7 @@ public class JSONServlet extends VitroHttpServlet { boolean more = false; int count = 0; int size = REPLY_SIZE; - /* we have a large number of items to send back so we need to stash the list in the session scope */ + /* we have a large number of items to send back so we need to stash the list in the session scope */ if( entsInVClass.size() > REPLY_SIZE){ more = true; ListIterator entsFromVclass = entsInVClass.listIterator();