diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/grefine/GrefineMqlreadServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/grefine/GrefineMqlreadServlet.java new file mode 100644 index 000000000..8917db3de --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/grefine/GrefineMqlreadServlet.java @@ -0,0 +1,164 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.controller.grefine; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +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.config.ConfigurationProperties; +import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao; + +import com.hp.hpl.jena.rdf.model.Literal; + +/** + * This servlet is for servicing Google Refine's + * "Add columns from VIVO"'s search for individual properties. + * e.g. search for Joe Smith's email in VIVO and download to Google Refine under a new email column. + * + * @author Eliza Chan (elc2013@med.cornell.edu) + * + */ +public class GrefineMqlreadServlet extends VitroHttpServlet { + + private static final long serialVersionUID = 1L; + private static final Log log = LogFactory.getLog(GrefineMqlreadServlet.class.getName()); + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + //resp.setContentType("application/json"); + super.doPost(req, resp); + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + super.doGet(req, resp); + resp.setContentType("application/json"); + VitroRequest vreq = new VitroRequest(req); + + try { + if (vreq.getParameter("query") != null) { + + JSONObject qJson = getResult(vreq, req, resp); + log.debug("result: " + qJson.toString()); + String responseStr = (vreq.getParameter("callback") == null) ? qJson + .toString() : vreq.getParameter("callback") + "(" + + qJson.toString() + ")"; + System.out.println(responseStr); + ServletOutputStream out = resp.getOutputStream(); + out.print(responseStr); + } + + } catch (Exception ex) { + log.warn(ex, ex); + } + } + + private JSONObject getResult(VitroRequest vreq, HttpServletRequest req, + HttpServletResponse resp) throws ServletException { + + JSONObject resultAllJson = new JSONObject(); + + try { + // parse query + String query = vreq.getParameter("query"); + System.out.println("query: " + query); + + JSONObject rawJson = new JSONObject(query); + JSONArray qJsonArr = rawJson.getJSONArray("query"); + + + ArrayList subjectUriList = new ArrayList(); + Map propertyUriMap = new HashMap(); + for (int i=0; i entry : propertyUriMap.entrySet()) { + int limit = 10; // default + String propertyUri = entry.getKey(); + JSONArray propertyUriOptions = entry.getValue(); + for (int i=0; i literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, propertyUri); + // Make sure the subject has a value for this property + if (literals.size() > 0) { + int counter = 0; + JSONArray valueJsonArr = new JSONArray(); + for (Literal literal: literals) { + if (counter <= limit) { + String value = literal.getLexicalForm(); + valueJsonArr.put(value); + } + counter++; + } + subjectPropertyResultJson.put(propertyUri, valueJsonArr); + } + } + resultAllJsonArr.put(subjectPropertyResultJson); + } + resultAllJson.put("result", resultAllJsonArr); + + } catch (JSONException e) { + log.error("JSONException: " + e); + throw new ServletException("GrefineMqlreadServlet JSONException: " + e); + } + + // System.out.println(resultAllJson); + return resultAllJson; + } +}