diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java new file mode 100644 index 000000000..e63946b46 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProfilesGetter.java @@ -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: \n" // + + "PREFIX rdfs: \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(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 parseSolutionRow(QuerySolution solution) { + Map map = new HashMap(); + map.put("uri", solution.getResource("uri").getURI()); + map.put("label", ifLiteralPresent(solution, "label", "")); + map.put("classLabel", ""); + map.put("imageUrl", ""); + return map; + } + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java new file mode 100644 index 000000000..4bcc599cb --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/BasicProxiesGetter.java @@ -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: \n" // + + "PREFIX auth: \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( + 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 parseSolutionRow(QuerySolution solution) { + Map map = new HashMap(); + map.put("uri", solution.getResource("uri").getURI()); + map.put("label", ifLiteralPresent(solution, "label", "")); + map.put("classLabel", ""); + map.put("imageUrl", placeholderImageUrl); + return map; + } + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/ManageProxiesAjaxController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/ManageProxiesAjaxController.java new file mode 100644 index 000000000..4747e6bc3 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/ManageProxiesAjaxController.java @@ -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("[]"); + } + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/MoreProfileInfo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/MoreProfileInfo.java new file mode 100644 index 000000000..77eaee0ad --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/MoreProfileInfo.java @@ -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 map = new HashMap(); + 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 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); + } +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/MoreProxyInfo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/MoreProxyInfo.java new file mode 100644 index 000000000..27cab1a28 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ajax/MoreProxyInfo.java @@ -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 inds = SelfEditingConfiguration.getBean(vreq) + .getAssociatedIndividuals(indDao, user); + if (inds.isEmpty()) { + log.debug("no profile"); + return EMPTY_RESPONSE; + } + Individual profileInd = inds.get(0); + + Map map = new HashMap(); + 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 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); + } + } +} diff --git a/webapp/web/WEB-INF/web.xml b/webapp/web/WEB-INF/web.xml index 1484a5cb4..180b92069 100644 --- a/webapp/web/WEB-INF/web.xml +++ b/webapp/web/WEB-INF/web.xml @@ -666,6 +666,15 @@ /manageProxies/* + + ProxiesAjax + edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ajax.ManageProxiesAjaxController + + + ProxiesAjax + /proxiesAjax/* + + ShowAuth edu.cornell.mannlib.vitro.webapp.controller.admin.ShowAuthController