VIVO-17: QR Code info now generated via an Ajax call when the icon is clicked.

This commit is contained in:
Tim Worrall 2014-10-03 11:33:26 -04:00
parent 0ab6d244f5
commit 1c118fc044
13 changed files with 439 additions and 111 deletions

View file

@ -2,20 +2,29 @@
package edu.cornell.mannlib.vitro.webapp.controller;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestAnalysisContextImpl;
import edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestAnalyzer;
import edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestInfo;
import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.DefaultObjectWrapper;
@ -25,18 +34,41 @@ public class ExportQrCodeController extends FreemarkerHttpServlet {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(ExportQrCodeController.class);
private static final String TEMPLATE_DEFAULT = "foaf-person--exportQrCode.ftl";
private static String VCARD_DATA_QUERY = ""
+ "PREFIX obo: <http://purl.obolibrary.org/obo/> \n"
+ "PREFIX vcard: <http://www.w3.org/2006/vcard/ns#> \n"
+ "SELECT DISTINCT ?firstName ?lastName ?email ?phone ?title \n"
+ "WHERE { \n"
+ " ?subject obo:ARG_2000028 ?vIndividual . \n"
+ " ?vIndividual vcard:hasName ?vName . \n"
+ " ?vName vcard:givenName ?firstName . \n"
+ " ?vName vcard:familyName ?lastName . \n"
+ " OPTIONAL { ?vIndividual vcard:hasEmail ?vEmail . \n"
+ " ?vEmail vcard:email ?email . \n"
+ " } \n"
+ " OPTIONAL { ?vIndividual vcard:hasTelephone ?vPhone . \n"
+ " ?vPhone vcard:telephone ?phone . \n"
+ " } \n"
+ " OPTIONAL { ?vIndividual vcard:hasTitle ?vTitle . \n"
+ " ?vTitle vcard:title ?title . \n"
+ " } \n"
+ "} " ;
private List<Map<String,String>> vcardData;
private Map<String, String> qrData = null;
@Override
protected ResponseValues processRequest(VitroRequest vreq) {
try {
Individual individual = getIndividualFromRequest(vreq);
qrData = generateQrData(individual, vreq);
DefaultObjectWrapper wrapper = new DefaultObjectWrapper();
wrapper.setExposureLevel(BeansWrapper.EXPOSE_SAFE);
Map<String, Object> body = new HashMap<String, Object>();
body.put("individual", wrapper.wrap(new IndividualTemplateModel(individual, vreq)));
body.put("qrData", qrData);
return new TemplateResponseValues(TEMPLATE_DEFAULT, body);
} catch (Throwable e) {
log.error(e, e);
@ -60,4 +92,71 @@ public class ExportQrCodeController extends FreemarkerHttpServlet {
}
}
private Map<String, String> generateQrData(Individual individual, VitroRequest vreq) {
try {
String firstName = "";
String lastName = "";
String preferredTitle = "";
String phoneNumber = "";
String email = "";
vcardData = getVcardData(individual, vreq);
Map<String,String> qrData = new HashMap<String,String>();
for (Map<String, String> map: vcardData) {
firstName = map.get("firstName");
lastName = map.get("lastName");
preferredTitle = map.get("title");
phoneNumber = map.get("phone");
email = map.get("email");
}
if(firstName != null && firstName.length() > 0)
qrData.put("firstName", firstName);
if(lastName != null && lastName.length() > 0)
qrData.put("lastName", lastName);
if(preferredTitle != null && preferredTitle.length() > 0)
qrData.put("preferredTitle", preferredTitle);
if(phoneNumber != null && phoneNumber.length() > 0)
qrData.put("phoneNumber", phoneNumber);
if(email != null && email.length() > 0)
qrData.put("email", email);
String tempUrl = vreq.getRequestURL().toString();
String prefix = "http://";
tempUrl = tempUrl.substring(0, tempUrl.replace(prefix, "").indexOf("/") + prefix.length());
String externalUrl = tempUrl ;
qrData.put("externalUrl", externalUrl);
String individualUri = individual.getURI();
String contextPath = vreq.getContextPath();
qrData.put("exportQrCodeUrl", contextPath + "/qrcode?uri=" + UrlBuilder.urlEncode(individualUri));
qrData.put("aboutQrCodesUrl", contextPath + "/qrcode/about");
return qrData;
} catch (Exception e) {
log.error("Failed getting QR code data", e);
return null;
}
}
private List<Map<String,String>> getVcardData(Individual individual, VitroRequest vreq) {
String queryStr = QueryUtils.subUriForQueryVar(VCARD_DATA_QUERY, "subject", individual.getURI());
log.debug("queryStr = " + queryStr);
List<Map<String,String>> vcardData = new ArrayList<Map<String,String>>();
try {
ResultSet results = QueryUtils.getQueryResults(queryStr, vreq);
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
vcardData.add(QueryUtils.querySolutionToStringValueMap(soln));
}
} catch (Exception e) {
log.error(e, e);
}
return vcardData;
}
}

View file

@ -0,0 +1,43 @@
/* $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 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.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.VitroAjaxController;
/**
* Handle the AJAX functions that are specific to the "new" home page sections, at
* this point just the mapping of geographic locations.
*/
public class QrCodeAjaxController extends VitroAjaxController {
private static final Log log = LogFactory
.getLog(QrCodeAjaxController.class);
private static final String PARAMETER_ACTION = "action";
@Override
protected void doRequest(VitroRequest vreq, HttpServletResponse resp)
throws ServletException, IOException {
try {
String function = vreq.getParameter(PARAMETER_ACTION);
if ("getQrCodeDetails".equals(function)) {
new QrCodeDetails(this, vreq, resp).processRequest();
}
else {
resp.getWriter().write("[]");
}
} catch (Exception e) {
log.error(e, e);
resp.getWriter().write("[]");
}
}
}

View file

@ -0,0 +1,162 @@
/* $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.lang.Integer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.ajax.VitroAjaxController;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestAnalysisContextImpl;
import edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestAnalyzer;
import edu.cornell.mannlib.vitro.webapp.controller.individual.IndividualRequestInfo;
import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
public class QrCodeDetails extends AbstractAjaxResponder {
private static final Log log = LogFactory.getLog(QrCodeDetails.class.getName());
private List<Map<String,String>> vcardData;
private static String VCARD_DATA_QUERY = ""
+ "PREFIX obo: <http://purl.obolibrary.org/obo/> \n"
+ "PREFIX vcard: <http://www.w3.org/2006/vcard/ns#> \n"
+ "SELECT DISTINCT ?firstName ?lastName ?email ?phone ?title \n"
+ "WHERE { \n"
+ " ?subject obo:ARG_2000028 ?vIndividual . \n"
+ " ?vIndividual vcard:hasName ?vName . \n"
+ " ?vName vcard:givenName ?firstName . \n"
+ " ?vName vcard:familyName ?lastName . \n"
+ " OPTIONAL { ?vIndividual vcard:hasEmail ?vEmail . \n"
+ " ?vEmail vcard:email ?email . \n"
+ " } \n"
+ " OPTIONAL { ?vIndividual vcard:hasTelephone ?vPhone . \n"
+ " ?vPhone vcard:telephone ?phone . \n"
+ " } \n"
+ " OPTIONAL { ?vIndividual vcard:hasTitle ?vTitle . \n"
+ " ?vTitle vcard:title ?title . \n"
+ " } \n"
+ "} " ;
public QrCodeDetails(HttpServlet parent, VitroRequest vreq,
HttpServletResponse resp) {
super(parent, vreq, resp);
}
@Override
public String prepareResponse() throws IOException, JSONException {
try {
Individual individual = getIndividualFromRequest(vreq);
String firstName = "";
String lastName = "";
String preferredTitle = "";
String phoneNumber = "";
String email = "";
String response = "[";
vcardData = getVcardData(individual, vreq);
for (Map<String, String> map: vcardData) {
firstName = map.get("firstName");
lastName = map.get("lastName");
preferredTitle = map.get("title");
phoneNumber = map.get("phone");
email = map.get("email");
}
/*
String tempUrl = vreq.getRequestURL().toString();
String prefix = "http://";
tempUrl = tempUrl.substring(0, tempUrl.replace(prefix, "").indexOf("/") + prefix.length());
String profileUrl = UrlBuilder.getIndividualProfileUrl(individual, vreq);
String externalUrl = tempUrl + profileUrl;
*/
if (firstName != null && firstName.length() > 0) {
response += "{\"firstName\": \"" + firstName + "\"},";
}
else {
response += "{\"firstName\": \"\"},";
}
if (lastName != null && lastName.length() > 0) {
response += "{\"lastName\": \"" + lastName + "\"},";
}
else {
response += "{\"lastName\": \"\"},";
}
if (preferredTitle != null && preferredTitle.length() > 0) {
response += "{\"preferredTitle\": \"" + preferredTitle + "\"},";
}
else {
response += "{\"preferredTitle\": \"\"},";
}
if (phoneNumber != null && phoneNumber.length() > 0) {
response += "{\"phoneNumber\": \"\"},";
}
else {
response += "{\"phoneNumber\": \"\"},";
}
if (email != null && email.length() > 0) {
response += "{\"email\": \"" + email + "\"},";
}
else {
response += "{\"email\": \"\"},";
}
response += " ]";
response = response.replace(", ]"," ]");
log.debug(response);
return response;
} catch (Exception e) {
log.error("Could not retrieve vCard information", e);
return EMPTY_RESPONSE;
}
}
private List<Map<String,String>> getVcardData(Individual individual, VitroRequest vreq) {
String queryStr = QueryUtils.subUriForQueryVar(VCARD_DATA_QUERY, "subject", individual.getURI());
log.debug("queryStr = " + queryStr);
List<Map<String,String>> vcardData = new ArrayList<Map<String,String>>();
try {
ResultSet results = QueryUtils.getQueryResults(queryStr, vreq);
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
vcardData.add(QueryUtils.querySolutionToStringValueMap(soln));
}
} catch (Exception e) {
log.error(e, e);
}
return vcardData;
}
private Individual getIndividualFromRequest(VitroRequest vreq) {
IndividualRequestInfo requestInfo = new IndividualRequestAnalyzer(vreq,
new IndividualRequestAnalysisContextImpl(vreq)).analyze();
return requestInfo.getIndividual();
}
}

View file

@ -39,101 +39,11 @@ public class IndividualTemplateModel extends BaseIndividualTemplateModel {
private static final String ORGANIZATION_CLASS = FOAF + "Organization";
private static final String BASE_VISUALIZATION_URL =
UrlBuilder.getUrl(Route.VISUALIZATION_SHORT.path());
private static String VCARD_DATA_QUERY = ""
+ "PREFIX obo: <http://purl.obolibrary.org/obo/> \n"
+ "PREFIX vcard: <http://www.w3.org/2006/vcard/ns#> \n"
+ "SELECT DISTINCT ?firstName ?lastName ?email ?phone ?title \n"
+ "WHERE { \n"
+ " ?subject obo:ARG_2000028 ?vIndividual . \n"
+ " ?vIndividual vcard:hasName ?vName . \n"
+ " ?vName vcard:givenName ?firstName . \n"
+ " ?vName vcard:familyName ?lastName . \n"
+ " OPTIONAL { ?vIndividual vcard:hasEmail ?vEmail . \n"
+ " ?vEmail vcard:email ?email . \n"
+ " } \n"
+ " OPTIONAL { ?vIndividual vcard:hasTelephone ?vPhone . \n"
+ " ?vPhone vcard:telephone ?phone . \n"
+ " } \n"
+ " OPTIONAL { ?vIndividual vcard:hasTitle ?vTitle . \n"
+ " ?vTitle vcard:title ?title . \n"
+ " } \n"
+ "} " ;
private List<Map<String,String>> vcardData;
private Map<String, String> qrData = null;
public IndividualTemplateModel(Individual individual, VitroRequest vreq) {
super(individual, vreq);
}
private Map<String, String> generateQrData() {
try {
String firstName = "";
String lastName = "";
String preferredTitle = "";
String phoneNumber = "";
String email = "";
vcardData = getVcardData(individual, vreq);
Map<String,String> qrData = new HashMap<String,String>();
for (Map<String, String> map: vcardData) {
firstName = map.get("firstName");
lastName = map.get("lastName");
preferredTitle = map.get("title");
phoneNumber = map.get("phone");
email = map.get("email");
}
if(firstName != null && firstName.length() > 0)
qrData.put("firstName", firstName);
if(lastName != null && lastName.length() > 0)
qrData.put("lastName", lastName);
if(preferredTitle != null && preferredTitle.length() > 0)
qrData.put("preferredTitle", preferredTitle);
if(phoneNumber != null && phoneNumber.length() > 0)
qrData.put("phoneNumber", phoneNumber);
if(email != null && email.length() > 0)
qrData.put("email", email);
String tempUrl = vreq.getRequestURL().toString();
String prefix = "http://";
tempUrl = tempUrl.substring(0, tempUrl.replace(prefix, "").indexOf("/") + prefix.length());
String profileUrl = getProfileUrl();
String externalUrl = tempUrl + profileUrl;
qrData.put("externalUrl", externalUrl);
String individualUri = individual.getURI();
String contextPath = vreq.getContextPath();
qrData.put("exportQrCodeUrl", contextPath + "/qrcode?uri=" + UrlBuilder.urlEncode(individualUri));
qrData.put("aboutQrCodesUrl", contextPath + "/qrcode/about");
return qrData;
} catch (Exception e) {
log.error("Failed getting QR code data", e);
return null;
}
}
private List<Map<String,String>> getVcardData(Individual individual, VitroRequest vreq) {
String queryStr = QueryUtils.subUriForQueryVar(VCARD_DATA_QUERY, "subject", individual.getURI());
log.debug("queryStr = " + queryStr);
List<Map<String,String>> vcardData = new ArrayList<Map<String,String>>();
try {
ResultSet results = QueryUtils.getQueryResults(queryStr, vreq);
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
vcardData.add(QueryUtils.querySolutionToStringValueMap(soln));
}
} catch (Exception e) {
log.error(e, e);
}
return vcardData;
}
private String getVisUrl(String visPath) {
String visUrl;
boolean isUsingDefaultNameSpace = UrlBuilder.isUriInDefaultNamespace(
@ -192,10 +102,4 @@ public class IndividualTemplateModel extends BaseIndividualTemplateModel {
return getVisUrl(url);
}
public Map<String, String> qrData() {
if(qrData == null)
qrData = generateQrData();
return qrData;
}
}