NIHVIVO-2343 Use the specialized AJAX handlers instead of the SparqlQueryAjaxController -- fixes issues with placeholder images.

This commit is contained in:
j2blake 2011-11-10 21:44:34 +00:00
parent 2704ce3c78
commit 57c28c183f
7 changed files with 49 additions and 248 deletions

View file

@ -104,7 +104,7 @@ public abstract class UserAccountsPage extends AbstractPageHandler {
map.put("firstTimeExternal",
UrlBuilder.getUrl("/accounts/firstTimeExternal"));
map.put("accountsAjax", UrlBuilder.getUrl("/accountsAjax"));
map.put("sparqlQueryAjax", UrlBuilder.getUrl("/ajax/sparqlQuery"));
map.put("proxyAjax", UrlBuilder.getUrl("/proxiesAjax"));
map.put("defaultImageUrl", DEFAULT_IMAGE_URL);
return map;

View file

@ -37,11 +37,6 @@ public class ManageProxiesListPage extends AbstractPageHandler {
private static final String TEMPLATE_NAME = "manageProxies-list.ftl";
private static final String PROPERTY_PROFILE_TYPES = "proxy.eligibleTypeList";
private static final String DEFAULT_IMAGE_URL = UrlBuilder
.getUrl("/images/placeholders/person.thumbnail.jpg");
private final Context selectorContext;
private ProxyRelationshipSelectionCriteria criteria = ProxyRelationshipSelectionCriteria.DEFAULT_CRITERIA;
@ -80,6 +75,8 @@ public class ManageProxiesListPage extends AbstractPageHandler {
public ResponseValues showPage() {
ProxyRelationshipSelection selection = ProxyRelationshipSelector
.select(selectorContext, criteria);
log.debug("Selection: " + selection);
Map<String, Object> body = buildTemplateBodyMap(selection);
return new TemplateResponseValues(TEMPLATE_NAME, body);
}
@ -97,9 +94,6 @@ public class ManageProxiesListPage extends AbstractPageHandler {
body.put("total", selection.getTotalResultCount());
body.put("page", buildPageMap(selection));
body.put("matchingProperty", getMatchingProperty());
body.put("profileTypes", buildProfileTypesString());
body.put("formUrls", buildUrlsMap());
applyMessage(vreq, body);
@ -108,11 +102,6 @@ public class ManageProxiesListPage extends AbstractPageHandler {
return body;
}
private String buildProfileTypesString() {
return ConfigurationProperties.getBean(vreq).getProperty(
PROPERTY_PROFILE_TYPES, "http://www.w3.org/2002/07/owl#Thing");
}
private List<ProxyRelationship> wrapProxyRelationships(
ProxyRelationshipSelection selection) {
List<ProxyRelationship> wrapped = new ArrayList<ProxyRelationship>();
@ -153,8 +142,8 @@ public class ManageProxiesListPage extends AbstractPageHandler {
private ProxyItemInfo wrapProfileItem(ProxyItemInfo item) {
String imagePath = item.getImageUrl();
if (imagePath.isEmpty()) {
imagePath = ImageUtil
.getPlaceholderImagePathForIndividual(vreq, item.getUri());
imagePath = ImageUtil.getPlaceholderImagePathForIndividual(vreq,
item.getUri());
}
return new ProfileItemWrapper(item.getUri(), item.getLabel(),
@ -191,8 +180,7 @@ public class ManageProxiesListPage extends AbstractPageHandler {
map.put("list", UrlBuilder.getUrl("/manageProxies/list"));
map.put("edit", UrlBuilder.getUrl("/manageProxies/edit"));
map.put("create", UrlBuilder.getUrl("/manageProxies/create"));
map.put("sparqlQueryAjax", UrlBuilder.getUrl("/ajax/sparqlQuery"));
map.put("defaultImageUrl", DEFAULT_IMAGE_URL);
map.put("ajax", UrlBuilder.getUrl("/proxiesAjax"));
return map;
}

View file

@ -70,6 +70,7 @@ function itemElement(template, uri, label, classLabel, imageUrl, removeInfo) {
}
}
/*
* ----------------------------------------------------------------------------
* proxyAutoComplete
@ -77,8 +78,7 @@ function itemElement(template, uri, label, classLabel, imageUrl, removeInfo) {
* Attach the autocomplete funcionality that we like in proxy panels.
*
* You provide:
* parms -- a map containing the URL of the AJAX controller, the query, and
* the model selector.
* parms -- a map containing the URL and the action code needed for the AJAX call.
* excludedUris -- these URIs are always filtered out of the results.
* getProxyInfos -- a function that will return an array of itemElements
* that are already present in the list and so should be filtered out of
@ -90,8 +90,7 @@ function itemElement(template, uri, label, classLabel, imageUrl, removeInfo) {
* will accept the length of the search term and the number of results,
* and will display it in some way.
* ----------------------------------------------------------------------------
* Before executing the AJAX request, the query from the parms map will be modified,
* replacing "%term%" with the current search term.
* The AJAX request will include a "term" parameter, set to the current search term.
* ----------------------------------------------------------------------------
* The functionality includes:
* -- fetching data for the autocomplete list.
@ -137,14 +136,13 @@ function proxyAutocomplete(parms, excludedUris, getProxyInfos, addProxyInfo, rep
url: parms.url,
dataType: 'json',
data: {
model: parms.model,
query: parms.query.replace("%term%", request.term)
action: parms.action,
term: request.term
},
complete: function(xhr, status) {
var results = $.parseJSON(xhr.responseText);
var parsed = sparqlUtils.parseSparqlResults(results);
cache[request.term] = parsed;
sendResponse(request, response, filterResults(parsed));
cache[request.term] = results;
sendResponse(request, response, filterResults(results));
}
});
}
@ -154,6 +152,4 @@ function proxyAutocomplete(parms, excludedUris, getProxyInfos, addProxyInfo, rep
event.preventDefault();
event.target.value = '';
}
}

View file

@ -58,24 +58,35 @@ function proxyItemsPanel(panel, contextInfo) {
}
this.addItemData = function(selection) {
var imageUrl = contextInfo.defaultImageUrl;
if (selection.imageUrl) {
imageUrl = contextInfo.baseUrl + selection.imageUrl;
}
var classLabel = selection.classLabel ? selection.classLabel : "";
var info = new itemElement(self.templateHtml, selection.uri, selection.label, classLabel,
imageUrl, self.removeItem);
var info = new itemElement(self.templateHtml, selection.uri, selection.label,
selection.classLabel, selection.imageUrl, self.removeItem);
self.itemData.unshift(info);
self.displayItemData();
self.getAdditionalData(self, info, selection.externalAuthId)
}
this.getAdditionalData = function(parent, info, externalAuthId) {
// For the plain vanilla panel, this need not do anything. For the
// proxy panel, this will be replaced by a function that does another
// AJAX call to get the classLabel and imageUrl.
data = info
$.ajax({
url: contextInfo.ajaxUrl,
dataType: 'json',
data: {
action: contextInfo.moreInfoAction,
uri: info.uri
},
complete: function(xhr, status) {
var results = $.parseJSON(xhr.responseText);
if (results.length > 0) {
if ("classLabel" in results[0]) {
info.classLabel = results[0].classLabel;
}
if ("imageUrl" in results[0]) {
info.imageUrl = results[0].imageUrl;
}
self.displayItemData();
}
}
});
}
var parseOriginalData = function() {
@ -95,10 +106,9 @@ function proxyItemsPanel(panel, contextInfo) {
var setupAutoCompleteFields = function() {
var parms = {
query: contextInfo.query,
model: contextInfo.model,
url: contextInfo.sparqlQueryUrl
};
url: contextInfo.ajaxUrl,
action: contextInfo.basicInfoAction
}
var updateStatus = new statusFieldUpdater(searchStatusField, 3).setText;
var autocompleteInfo = new proxyAutocomplete(parms, excludedUris, getItemData, self.addItemData, updateStatus)
autoCompleteField.autocomplete(autocompleteInfo);
@ -126,152 +136,6 @@ function statusFieldUpdater(element, minLength) {
}
}
var profileQuery = ""
+ "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";
var profileMoreInfoQuery = ""
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ "PREFIX vitro: <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> \n"
+ "PREFIX vpublic: <http://vitro.mannlib.cornell.edu/ns/vitro/public#> \n"
+ "\n"
+ "SELECT DISTINCT ?classLabel ?imageUrl \n"
+ "WHERE { \n"
+ " OPTIONAL { \n"
+ " <%uri%> vitro:mostSpecificType ?type. \n"
+ " ?type rdfs:label ?classLabel \n"
+ " } \n"
+ " OPTIONAL { \n"
+ " <%uri%> vpublic:mainImage ?imageUri. \n"
+ " ?imageUri vpublic:thumbnailImage ?thumbUri. \n"
+ " ?thumbUri vpublic:downloadLocation ?thumbstreamUri. \n"
+ " ?thumbstreamUri vpublic:directDownloadUrl ?imageUrl. \n"
+ " } \n"
+ "} \n"
+ "ORDER BY ASC(?label) \n"
+ "LIMIT 25 \n";
var proxyQuery = ""
+ "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 ?externalAuthId \n"
+ "WHERE { \n"
+ " ?uri a auth:UserAccount ; \n"
+ " auth:firstName ?firstName ; \n"
+ " auth:lastName ?lastName . \n"
+ " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )"
+ " OPTIONAL { ?uri auth:externalAuthId ?externalAuthId } \n"
+ " FILTER (REGEX(?label, '^%term%', 'i')) \n"
+ "} \n"
+ "ORDER BY ASC(?lastName) ASC(?firstName) \n"
+ "LIMIT 25 \n";
var proxyMoreInfoQuery = ""
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ "PREFIX vitro: <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> \n"
+ "PREFIX p.1: <http://vitro.mannlib.cornell.edu/ns/vitro/public#> \n"
+ " \n"
+ "SELECT ?uri ?classLabel ?imageUrl \n"
+ "WHERE \n"
+ "{ \n"
+ " ?uri <%matchingProperty%> '%externalAuthId%'. \n"
+ " \n"
+ " OPTIONAL { \n"
+ " ?uri vitro:mostSpecificType ?type. \n"
+ " ?type rdfs:label ?classLabel \n"
+ " } \n"
+ " \n"
+ " OPTIONAL { \n"
+ " ?uri p.1:mainImage ?imageUri. \n"
+ " ?imageUri p.1:thumbnailImage ?thumbUri. \n"
+ " ?thumbUri p.1:downloadLocation ?thumbstreamUri. \n"
+ " ?thumbstreamUri p.1:directDownloadUrl ?imageUrl. \n"
+ " } \n"
+ "} \n"
+ "LIMIT 1 \n";
/*
* This function will allow a proxy panel to execute another query for each proxy.
*/
var getAdditionalProxyInfo = function(parent, info, externalAuthId) {
$.ajax({
url: proxyContextInfo.sparqlQueryUrl,
dataType: 'json',
data: {
query: proxyMoreInfoQuery.replace(/%matchingProperty%/g, proxyContextInfo.matchingProperty)
.replace(/%externalAuthId%/g, externalAuthId)
},
complete: function(xhr, status) {
var results = $.parseJSON(xhr.responseText);
var parsed = sparqlUtils.parseSparqlResults(results);
if (parsed.length > 0) {
if ("classLabel" in parsed[0]) {
info.classLabel = parsed[0].classLabel;
}
if ("imageUrl" in parsed[0]) {
info.imageUrl = proxyContextInfo.baseUrl + parsed[0].imageUrl;
}
parent.displayItemData();
}
}
});
}
/*
* The profileTypes context string must have one or more type URIs, separated by commas.
*/
var applyProfileTypes = function(rawQuery) {
var typeClause = '';
var types = proxyContextInfo.profileTypes.split(',');
for (var i = 0; i < types.length; i++) {
typeClause += '{ ?uri rdf:type <' + types[i].trim() + '> }';
if (i + 1 < types.length) {
typeClause += ' UNION ';
} else {
typeClause += ' .';
}
}
return rawQuery.replace(/%typesUnion%/g, typeClause);
}
/*
* This function will allow a profile panel to execute another query for each profile.
*/
var getAdditionalProfileInfo = function(parent, info) {
$.ajax({
url: proxyContextInfo.sparqlQueryUrl,
dataType: 'json',
data: {
query: profileMoreInfoQuery.replace(/%uri%/g, info.uri)
},
complete: function(xhr, status) {
var results = $.parseJSON(xhr.responseText);
var parsed = sparqlUtils.parseSparqlResults(results);
if (parsed.length > 0) {
if ("classLabel" in parsed[0]) {
info.classLabel = parsed[0].classLabel;
}
if ("imageUrl" in parsed[0]) {
info.imageUrl = proxyContextInfo.baseUrl + parsed[0].imageUrl;
}
parent.displayItemData();
}
}
});
}
/*
* Execute this when the page loads.
*/
@ -295,42 +159,25 @@ $(document).ready(function() {
return;
}
/*
* For each proxyProfilesPanel, modify the profile query to restrict it
* to the permitted types, then create a plain vanilla panel using the
* profile query against the main model.
*/
$("div[name='proxyProfilesPanel']").each(function(i) {
var query = applyProfileTypes(profileQuery);
var context = {
excludedUris: [],
baseUrl: proxyContextInfo.baseUrl,
sparqlQueryUrl: proxyContextInfo.sparqlQueryUrl,
defaultImageUrl: proxyContextInfo.defaultImageUrl,
query: query,
model: ''
ajaxUrl: proxyContextInfo.ajaxUrl,
basicInfoAction: "getAvailableProfiles",
moreInfoAction: "moreProfileInfo"
}
var pip = new proxyItemsPanel(this, context);
pip.getAdditionalData = getAdditionalProfileInfo;
this["proxyItemsPanel"] = pip;
this["proxyItemsPanel"] = new proxyItemsPanel(this, context);
});
/*
* For each proxyProxiesPanel, we start with a plain panel using the proxy
* query against the user accounts model. Then we augment it with a method
* that will fetch more info from the main model for each proxy.
*/
$("div[name='proxyProxiesPanel']").each(function(i) {
var context = {
excludedUris: [],
baseUrl: proxyContextInfo.baseUrl,
sparqlQueryUrl: proxyContextInfo.sparqlQueryUrl,
defaultImageUrl: proxyContextInfo.defaultImageUrl,
query: proxyQuery,
model: 'userAccounts'
ajaxUrl: proxyContextInfo.ajaxUrl,
basicInfoAction: "getAvailableProxies",
moreInfoAction: "moreProxyInfo"
}
var pip = new proxyItemsPanel(this, context);
pip.getAdditionalData = getAdditionalProxyInfo;
this["proxyItemsPanel"] = pip;
this["proxyItemsPanel"] = new proxyItemsPanel(this, context);
});
});

View file

@ -1,22 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
/*
* Parse the results that we got from the SparqlQueryAjaxController.
*
* The input is a complex structure from the controller. The output is an array
* of maps where each map represents a result row, populated with key-value pairs.
*/
sparqlUtils = {
parseSparqlResults: function(data) {
var parsed = [];
$.each(data.results.bindings, function() {
var row = {};
for (var i in this) {
row[i] = this[i].value;
}
parsed.push(row);
});
return parsed;
}
};

View file

@ -51,15 +51,11 @@ ${stylesheets.add('<link rel="stylesheet" href="${urls.base}/edit/forms/css/auto
<script type="text/javascript">
var proxyContextInfo = {
baseUrl: '${urls.base}',
sparqlQueryUrl: '${formUrls.sparqlQueryAjax}',
defaultImageUrl: '${formUrls.defaultImageUrl}',
matchingProperty: '${matchingProperty}',
profileTypes: '${profileTypes}'
ajaxUrl: '${formUrls.proxyAjax}'
};
</script>
${scripts.add('<script type="text/javascript" src="${urls.base}/js/account/proxyUtils.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/sparqlUtils.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/account/accountProxyCommon.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/account/accountProxyItemsPanel.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/jquery-ui/js/jquery-ui-1.8.9.custom.min.js"></script>')}

View file

@ -220,16 +220,12 @@ ${stylesheets.add('<link rel="stylesheet" href="${urls.base}/js/jquery-ui/css/sm
<script type="text/javascript">
var proxyContextInfo = {
baseUrl: '${urls.base}',
sparqlQueryUrl: '${formUrls.sparqlQueryAjax}',
defaultImageUrl: '${formUrls.defaultImageUrl}',
matchingProperty: '${matchingProperty}',
profileTypes: '${profileTypes}'
ajaxUrl: '${formUrls.ajax}'
};
</script>
${scripts.add('<script type="text/javascript" src="${urls.base}/js/jquery.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/jquery-ui/js/jquery-ui-1.8.9.custom.min.js"></script>')}
${scripts.add('<script type="text/javascript" src="${urls.base}/js/account/proxyUtils.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/sparqlUtils.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/account/accountProxyCommon.js"></script>',
'<script type="text/javascript" src="${urls.base}/js/account/accountProxyItemsPanel.js"></script>')}