NIHVIVO-2343 Create AJAX handlers for proxy management requests.

This commit is contained in:
j2blake 2011-11-10 21:29:59 +00:00
parent 8fcc6807ab
commit 2704ce3c78
6 changed files with 466 additions and 0 deletions

View file

@ -0,0 +1,110 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ajax;
import java.io.IOException;
import java.util.HashMap;
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.ontology.OntModel;
import com.hp.hpl.jena.query.QuerySolution;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.utils.ImageUtil;
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner;
/**
* Get the basic auto-complete info for the profile selection.
*/
public class BasicProfilesGetter extends AbstractAjaxResponder {
private static final Log log = LogFactory.getLog(BasicProfilesGetter.class);
private static final String PROPERTY_PROFILE_TYPES = "proxy.eligibleTypeList";
private static final String PARAMETER_SEARCH_TERM = "term";
private static final String QUERY_BASIC_PROFILES = "" //
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" //
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" //
+ "\n" //
+ "SELECT DISTINCT ?uri ?label ?classLabel ?imageUrl \n" //
+ "WHERE { \n" //
+ " %typesUnion% \n" //
+ " ?uri rdfs:label ?label ; \n" //
+ " FILTER (REGEX(str(?label), '^%term%', 'i')) \n" //
+ "} \n" //
+ "ORDER BY ASC(?label) \n" //
+ "LIMIT 25 \n";
private final String term;
private final OntModel fullModel;
private final String placeholderImageUrl;
public BasicProfilesGetter(HttpServlet servlet, VitroRequest vreq,
HttpServletResponse resp) {
super(servlet, vreq, resp);
fullModel = vreq.getJenaOntModel();
term = getStringParameter(PARAMETER_SEARCH_TERM, "");
placeholderImageUrl = UrlBuilder.getUrl(ImageUtil
.getPlaceholderImagePathForType(VitroVocabulary.USERACCOUNT));
}
@Override
public String prepareResponse() throws IOException, JSONException {
log.debug("search term is '" + term + "'");
if (term.isEmpty()) {
return EMPTY_RESPONSE;
} else {
String queryStr = QUERY_BASIC_PROFILES.replace("%typesUnion%",
buildTypeClause()).replace("%term%", term);
JSONArray jsonArray = new SparqlQueryRunner<JSONArray>(fullModel,
new BasicProfileInfoParser()).executeQuery(queryStr);
String response = jsonArray.toString();
log.debug(response);
return response;
}
}
private String buildTypeClause() {
String typesString = ConfigurationProperties.getBean(vreq).getProperty(
PROPERTY_PROFILE_TYPES, "http://www.w3.org/2002/07/owl#Thing");
String[] types = typesString.split(",");
String typeClause = "{ ?uri rdf:type <" + types[0].trim() + "> }";
for (int i = 1; i < types.length; i++) {
typeClause += " UNION { ?uri rdf:type <" + types[i].trim() + "> }";
}
return typeClause;
}
/** Parse a query row into a map of keys and values. */
private static class BasicProfileInfoParser extends JsonArrayParser {
@Override
protected Map<String, String> parseSolutionRow(QuerySolution solution) {
Map<String, String> map = new HashMap<String, String>();
map.put("uri", solution.getResource("uri").getURI());
map.put("label", ifLiteralPresent(solution, "label", ""));
map.put("classLabel", "");
map.put("imageUrl", "");
return map;
}
}
}

View file

@ -0,0 +1,107 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ajax;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
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.ontology.OntModel;
import com.hp.hpl.jena.query.QuerySolution;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
import edu.cornell.mannlib.vitro.webapp.utils.ImageUtil;
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner;
/**
* Get the basic auto-complete info for the proxy selection.
*/
public class BasicProxiesGetter extends AbstractAjaxResponder {
private static final Log log = LogFactory.getLog(BasicProxiesGetter.class);
private static final String PARAMETER_SEARCH_TERM = "term";
private static final String QUERY_BASIC_PROXIES = "" //
+ "PREFIX fn: <http://www.w3.org/2005/xpath-functions#> \n" //
+ "PREFIX auth: <http://vitro.mannlib.cornell.edu/ns/vitro/authorization#> \n" //
+ "\n" //
+ "SELECT DISTINCT ?uri ?label \n" //
+ "WHERE { \n" //
+ " ?uri a auth:UserAccount ; \n" //
+ " auth:firstName ?firstName ; \n" //
+ " auth:lastName ?lastName . \n" //
+ " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" //
+ " FILTER (REGEX(?label, '^%term%', 'i')) \n" //
+ "} \n" //
+ "ORDER BY ASC(?lastName) ASC(?firstName) \n" //
+ "LIMIT 25 \n"; //
private final String term;
private final OntModel userAccountsModel;
private final String placeholderImageUrl;
public BasicProxiesGetter(HttpServlet servlet, VitroRequest vreq,
HttpServletResponse resp) {
super(servlet, vreq, resp);
term = getStringParameter(PARAMETER_SEARCH_TERM, "");
ServletContext ctx = vreq.getSession().getServletContext();
OntModelSelector oms = ModelContext.getUnionOntModelSelector(ctx);
userAccountsModel = oms.getUserAccountsModel();
placeholderImageUrl = UrlBuilder.getUrl(ImageUtil
.getPlaceholderImagePathForType(VitroVocabulary.USERACCOUNT));
}
@Override
public String prepareResponse() throws IOException, JSONException {
log.debug("search term is '" + term + "'");
if (term.isEmpty()) {
return EMPTY_RESPONSE;
} else {
String queryStr = QUERY_BASIC_PROXIES.replace("%term%", term);
JSONArray jsonArray = new SparqlQueryRunner<JSONArray>(
userAccountsModel, new BasicProxyInfoParser(
placeholderImageUrl)).executeQuery(queryStr);
String response = jsonArray.toString();
log.debug(response);
return response;
}
}
/** Parse a query row into a map of keys and values. */
private static class BasicProxyInfoParser extends JsonArrayParser {
private final String placeholderImageUrl;
public BasicProxyInfoParser(String placeholderImageUrl) {
this.placeholderImageUrl = placeholderImageUrl;
}
@Override
protected Map<String, String> parseSolutionRow(QuerySolution solution) {
Map<String, String> map = new HashMap<String, String>();
map.put("uri", solution.getResource("uri").getURI());
map.put("label", ifLiteralPresent(solution, "label", ""));
map.put("classLabel", "");
map.put("imageUrl", placeholderImageUrl);
return map;
}
}
}

View file

@ -0,0 +1,56 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ajax;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.ManageOwnProxies;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.ManageProxies;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.VitroAjaxController;
/**
* Handle the AJAX functions that are specific to the ManageProxies pages.
*/
public class ManageProxiesAjaxController extends VitroAjaxController {
private static final Log log = LogFactory
.getLog(ManageProxiesAjaxController.class);
private static final String PARAMETER_ACTION = "action";
@Override
protected Actions requiredActions(VitroRequest vreq) {
return new Actions(new ManageOwnProxies()).or(new ManageProxies());
}
@Override
protected void doRequest(VitroRequest vreq, HttpServletResponse resp)
throws ServletException, IOException {
try {
String function = vreq.getParameter(PARAMETER_ACTION);
if ("getAvailableProxies".equals(function)) {
new BasicProxiesGetter(this, vreq, resp).processRequest();
} else if ("moreProxyInfo".equals(function)) {
new MoreProxyInfo(this, vreq, resp).processRequest();
} else if ("getAvailableProfiles".equals(function)) {
new BasicProfilesGetter(this, vreq, resp).processRequest();
} else if ("moreProfileInfo".equals(function)) {
new MoreProfileInfo(this, vreq, resp).processRequest();
} else {
log.error("Unrecognized function: '" + function + "'");
resp.getWriter().write("[]");
}
} catch (Exception e) {
log.error(e, e);
resp.getWriter().write("[]");
}
}
}

View file

@ -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.accounts.manageproxies.ajax;
import java.io.IOException;
import java.util.HashMap;
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 edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.AbstractAjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.utils.ImageUtil;
/**
* Get more information (class label and image URL) about a selected proxy.
*/
public class MoreProfileInfo extends AbstractAjaxResponder {
private static final Log log = LogFactory.getLog(MoreProfileInfo.class);
private static final String PARAMETER_PROFILE_URI = "uri";
private final ObjectPropertyStatementDao opsDao;
private final String profileUri;
public MoreProfileInfo(HttpServlet servlet, VitroRequest vreq,
HttpServletResponse resp) {
super(servlet, vreq, resp);
opsDao = vreq.getWebappDaoFactory().getObjectPropertyStatementDao();
profileUri = getStringParameter(PARAMETER_PROFILE_URI, "");
}
@Override
public String prepareResponse() throws IOException, JSONException {
log.debug("profile URI is '" + profileUri + "'");
if (profileUri.isEmpty()) {
return EMPTY_RESPONSE;
}
Individual profileInd = indDao.getIndividualByURI(profileUri);
if (profileInd == null) {
log.debug("no such individual");
return EMPTY_RESPONSE;
}
Map<String, String> map = new HashMap<String, String>();
map.put("imageUrl", getFullImageUrl(profileInd));
map.put("classLabel", getMostSpecificTypeLabel(profileInd.getURI()));
JSONArray jsonArray = new JSONArray();
jsonArray.put(map);
String response = jsonArray.toString();
log.debug("response is '" + response + "'");
return response;
}
private String getMostSpecificTypeLabel(String uri) {
Map<String, String> types = opsDao
.getMostSpecificTypesInClassgroupsForIndividual(uri);
if (types.isEmpty()) {
return "";
} else {
return types.values().iterator().next();
}
}
private String getFullImageUrl(Individual ind) {
String path = ind.getThumbUrl();
if ((path == null) || path.isEmpty()) {
path = ImageUtil.getPlaceholderImagePathForIndividual(vreq,
ind.getURI());
}
return UrlBuilder.getUrl(path);
}
}

View file

@ -0,0 +1,97 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ajax;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
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 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.ajax.AbstractAjaxResponder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
/**
* Get more information (class label and image URL) about a selected proxy.
*/
public class MoreProxyInfo extends AbstractAjaxResponder {
private static final Log log = LogFactory.getLog(MoreProxyInfo.class);
private static final String PARAMETER_PROXY_URI = "uri";
private final ObjectPropertyStatementDao opsDao;
private final String proxyUri;
public MoreProxyInfo(HttpServlet servlet, VitroRequest vreq,
HttpServletResponse resp) {
super(servlet, vreq, resp);
opsDao = vreq.getWebappDaoFactory().getObjectPropertyStatementDao();
proxyUri = getStringParameter(PARAMETER_PROXY_URI, "");
}
@Override
public String prepareResponse() throws IOException, JSONException {
log.debug("proxy URI is '" + proxyUri + "'");
if (proxyUri.isEmpty()) {
return EMPTY_RESPONSE;
}
UserAccount user = uaDao.getUserAccountByUri(proxyUri);
if (user == null) {
log.debug("no such user");
return EMPTY_RESPONSE;
}
List<Individual> inds = SelfEditingConfiguration.getBean(vreq)
.getAssociatedIndividuals(indDao, user);
if (inds.isEmpty()) {
log.debug("no profile");
return EMPTY_RESPONSE;
}
Individual profileInd = inds.get(0);
Map<String, String> map = new HashMap<String, String>();
map.put("imageUrl", getFullImageUrl(profileInd));
map.put("classLabel", getMostSpecificTypeLabel(profileInd.getURI()));
JSONArray jsonArray = new JSONArray();
jsonArray.put(map);
String response = jsonArray.toString();
log.debug("response is '" + response + "'");
return response;
}
private String getMostSpecificTypeLabel(String uri) {
Map<String, String> types = opsDao
.getMostSpecificTypesInClassgroupsForIndividual(uri);
if (types.isEmpty()) {
return "";
} else {
return types.values().iterator().next();
}
}
private String getFullImageUrl(Individual ind) {
String thumbnailUrl = ind.getThumbUrl();
if ((thumbnailUrl == null) || thumbnailUrl.isEmpty()) {
return "";
} else {
return UrlBuilder.getUrl(thumbnailUrl);
}
}
}

View file

@ -666,6 +666,15 @@
<url-pattern>/manageProxies/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ProxiesAjax</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ajax.ManageProxiesAjaxController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProxiesAjax</servlet-name>
<url-pattern>/proxiesAjax/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ShowAuth</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.admin.ShowAuthController</servlet-class>