NIHVIVO-2343 Pull the AbstractAjaxResponder class out of UserAccountsAjaxController so it can be more widely used. Augment it with an abstract JsonArrayParser class to simplify processing of SPARQL query results.

This commit is contained in:
j2blake 2011-11-10 16:08:19 +00:00
parent 45556a41c2
commit 3b4f8e11ca
4 changed files with 115 additions and 44 deletions

View file

@ -17,7 +17,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration;
import edu.cornell.mannlib.vitro.webapp.beans.UserAccount;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.admin.ajax.UserAccountsAjaxController.AjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
/**
@ -32,7 +32,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
*
* If the externalAuthId is empty, or if there is any error, say "neither".
*/
class ExternalAuthChecker extends AjaxResponder {
class ExternalAuthChecker extends AbstractAjaxResponder {
private static final Log log = LogFactory.getLog(ExternalAuthChecker.class);
private static final String PARAMETER_USER_ACCOUNT_URI = "userAccountUri";

View file

@ -28,7 +28,7 @@ import com.hp.hpl.jena.query.Syntax;
import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.admin.ajax.UserAccountsAjaxController.AjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
/**
@ -42,7 +42,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
* If the matching property is not defined, or if the search term is empty, or
* if an error occurs, return an empty result.
*/
class ProfileAutoCompleter extends AjaxResponder {
class ProfileAutoCompleter extends AbstractAjaxResponder {
private static final Log log = LogFactory
.getLog(ProfileAutoCompleter.class);

View file

@ -5,19 +5,15 @@ package edu.cornell.mannlib.vitro.webapp.controller.accounts.admin.ajax;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.ManageUserAccounts;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.VitroAjaxController;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.UserAccountsDao;
/**
* Handle the AJAX functions that are specific to the UserAccounts pages.
@ -51,40 +47,4 @@ public class UserAccountsAjaxController extends VitroAjaxController {
}
}
static abstract class AjaxResponder {
protected static final String EMPTY_RESPONSE = "[]";
protected final HttpServlet parent;
protected final VitroRequest vreq;
protected final HttpServletResponse resp;
protected final IndividualDao indDao;
protected final UserAccountsDao uaDao;
public AjaxResponder(HttpServlet parent, VitroRequest vreq,
HttpServletResponse resp) {
this.parent = parent;
this.vreq = vreq;
this.resp = resp;
this.indDao = vreq.getWebappDaoFactory().getIndividualDao();
this.uaDao = vreq.getWebappDaoFactory().getUserAccountsDao();
}
public final void processRequest() {
try {
resp.getWriter().write(prepareResponse());
} catch (Exception e) {
log.error("Problem with AJAX response", e);
}
}
protected abstract String prepareResponse() throws IOException,
JSONException;
protected String getStringParameter(String key, String defaultValue) {
String value = vreq.getParameter(key);
return (value == null) ? defaultValue : value;
}
}
}

View file

@ -0,0 +1,111 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.ajax;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import javax.servlet.http.HttpServlet;
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 com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.UserAccountsDao;
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner;
/**
* A base class for AJAX responder objects, to be instantiated and invoked by
* AJAX servlets.
*/
public abstract class AbstractAjaxResponder {
private static final Log log = LogFactory
.getLog(AbstractAjaxResponder.class);
protected static final String EMPTY_RESPONSE = "[]";
protected final HttpServlet servlet;
protected final VitroRequest vreq;
protected final HttpServletResponse resp;
protected final IndividualDao indDao;
protected final UserAccountsDao uaDao;
public AbstractAjaxResponder(HttpServlet servlet, VitroRequest vreq,
HttpServletResponse resp) {
this.servlet = servlet;
this.vreq = vreq;
this.resp = resp;
this.indDao = vreq.getWebappDaoFactory().getIndividualDao();
this.uaDao = vreq.getWebappDaoFactory().getUserAccountsDao();
}
public final void processRequest() {
try {
resp.getWriter().write(prepareResponse());
} catch (Exception e) {
log.error("Problem with AJAX response", e);
}
}
protected abstract String prepareResponse() throws IOException,
JSONException;
protected String getStringParameter(String key, String defaultValue) {
String value = vreq.getParameter(key);
return (value == null) ? defaultValue : value;
}
protected Collection<String> getStringParameters(String key) {
String[] values = vreq.getParameterValues(key);
if (values == null) {
return Collections.emptySet();
} else {
return new HashSet<String>(Arrays.asList(values));
}
}
/**
* AJAX responders can use a parser that extends this class. The parser must
* implement "parseSolutionRow()"
*/
protected abstract static class JsonArrayParser extends
SparqlQueryRunner.QueryParser<JSONArray> {
@Override
protected JSONArray defaultValue() {
return new JSONArray();
}
@Override
protected JSONArray parseResults(String queryStr, ResultSet results) {
JSONArray jsonArray = new JSONArray();
while (results.hasNext()) {
Map<String, String> map = parseSolutionRow(results.next());
if (map != null) {
jsonArray.put(map);
}
}
return jsonArray;
}
/**
* Subclasses must implement. Return a map of field names and values,
* which will become a row in the result.
*
* Or return null, and no row will be created in the result.
*/
protected abstract Map<String, String> parseSolutionRow(
QuerySolution solution);
}
}