NIHVIVO-2762 Fix jsp errors resulting from a jsp variable inserted into a query string in Java code, where it does not get evaluated. Augment the method getActivityTypeQuery() to handle classgroup-based select lists.

This commit is contained in:
ryounes 2011-06-23 20:32:25 +00:00
parent b4a0813eb6
commit fff37871bc

View file

@ -48,6 +48,7 @@
<%@ page import="edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty"%> <%@ page import="edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty"%>
<%@ page import="edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils"%> <%@ page import="edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils"%>
<%@ page import="edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode"%> <%@ page import="edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode"%>
<%@ page import="edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils"%>
<%@ page import="org.apache.commons.lang.StringUtils" %> <%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="org.apache.commons.logging.Log" %> <%@ page import="org.apache.commons.logging.Log" %>
@ -79,16 +80,16 @@ public static Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.webapp.jsp.
<%-- OPTIONAL PARAMETERS --%> <%-- OPTIONAL PARAMETERS --%>
<c:set var="typeSelectorLabel" scope="request"> <%-- label for type selector field --%> <c:set var="typeSelectorLabel" scope="request"> <%-- label for type selector field --%>
${! empty param.typeSelectorLabel ? param.typeSelectorLabel : param.roleDescriptor} ${! empty param.typeSelectorLabel ? param.typeSelectorLabel : param.roleDescriptor }
</c:set> </c:set>
<c:set var="buttonText" scope="request"> <c:set var="buttonText" scope="request">
${! empty param.buttonText ? param.buttonText : param.roleDescriptor} ${! empty param.buttonText ? param.buttonText : param.roleDescriptor }
</c:set> </c:set>
<c:set var="roleToActivityPredicate" scope="request"> <c:set var="roleToActivityPredicate" scope="request">
${! empty param.roleToActivityPredicate ? param.roleToActivityPredicate : "http://vivoweb.org/ontology/core#roleIn"} ${! empty param.roleToActivityPredicate ? param.roleToActivityPredicate : "http://vivoweb.org/ontology/core#roleIn" }
</c:set> </c:set>
<c:set var="activityToRolePredicate"> <c:set var="activityToRolePredicate">
${! empty param.activityToRolePredicate ? param.activityToRolePredicate : "http://vivoweb.org/ontology/core#relatedRole"} ${! empty param.activityToRolePredicate ? param.activityToRolePredicate : "http://vivoweb.org/ontology/core#relatedRole" }
</c:set> </c:set>
<c:set var="numDateFields">${! empty param.numDateFields ? param.numDateFields : 2 }</c:set> <c:set var="numDateFields">${! empty param.numDateFields ? param.numDateFields : 2 }</c:set>
<c:set var="showRoleLabelField"> <c:set var="showRoleLabelField">
@ -96,6 +97,7 @@ public static Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.webapp.jsp.
</c:set> </c:set>
<% <%
VitroRequest vreq = new VitroRequest(request); VitroRequest vreq = new VitroRequest(request);
WebappDaoFactory wdf = vreq.getWebappDaoFactory(); WebappDaoFactory wdf = vreq.getWebappDaoFactory();
//vreq.setAttribute("defaultNamespace", ""); //empty string triggers default new URI behavior //vreq.setAttribute("defaultNamespace", ""); //empty string triggers default new URI behavior
@ -151,10 +153,10 @@ public static Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.webapp.jsp.
} }
%> %>
<c:set var="vivoOnt" value="http://vivoweb.org/ontology" /> <c:set var="vivoCore" value="http://vivoweb.org/ontology/core#" />
<c:set var="vivoCore" value="${vivoOnt}/core#" />
<c:set var="rdfs" value="<%= VitroVocabulary.RDFS %>" /> <c:set var="rdfs" value="<%= VitroVocabulary.RDFS %>" />
<c:set var="type" value="<%= VitroVocabulary.RDF_TYPE %>" /> <c:set var="type" value="<%= VitroVocabulary.RDF_TYPE %>" />
<c:set var="vitro" value="<%= VitroVocabulary.vitroURI %>" />
<c:set var="label" value="${rdfs}label" /> <c:set var="label" value="${rdfs}label" />
<c:set var="defaultNamespace" value=""/> <%--blank triggers default URI generation behavior --%> <c:set var="defaultNamespace" value=""/> <%--blank triggers default URI generation behavior --%>
@ -242,10 +244,22 @@ public static Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.webapp.jsp.
<% <%
request.setAttribute("typeQuery", getActivityTypeQuery(vreq)); request.setAttribute("typeQuery", getActivityTypeQuery(vreq));
log.debug("TYPE QUERY: " + vreq.getAttribute("typeQuery"));
%> %>
<v:jsonset var="activityTypeQuery">${typeQuery}</v:jsonset> <v:jsonset var="activityTypeQuery">${typeQuery}</v:jsonset>
<v:jsonset var="existingIntervalNodeQuery" > <%--
<v:jsonset var="activityTypeQuery">
PREFIX core: <${vivoCore}>
PREFIX vitro: <${vitro}>
SELECT ?existingActivityType WHERE {
?role <${roleToActivityPredicate}> ?existingActivity .
?existingActivity vitro:mostSpecificType ?existingActivityType .
}
</v:jsonset>
--%>
<v:jsonset var="existingIntervalNodeQuery" >
SELECT ?existingIntervalNode WHERE { SELECT ?existingIntervalNode WHERE {
?role <${roleToInterval}> ?existingIntervalNode . ?role <${roleToInterval}> ?existingIntervalNode .
?existingIntervalNode <${type}> <${intervalType}> . } ?existingIntervalNode <${type}> <${intervalType}> . }
@ -554,59 +568,104 @@ request.setAttribute("typeQuery", getActivityTypeQuery(vreq));
<%! <%!
private static final String VIVO_CORE = "http://vivoweb.org/ontology/core#"; private static final String VIVO_CORE = "http://vivoweb.org/ontology/core#";
private static final String DEFAULT_ACTIVITY_TYPE_QUERY =
private static final String DEFAULT_ACTIVITY_TYPE_QUERY =
"PREFIX core: <" + VIVO_CORE + ">\n" + "PREFIX core: <" + VIVO_CORE + ">\n" +
"PREFIX vitro: <" + VitroVocabulary.vitroURI + "> \n" +
"SELECT ?existingActivityType WHERE { \n" + "SELECT ?existingActivityType WHERE { \n" +
"?role <${roleToActivityPredicate}> ?existingActivity . \n" + " ?role ?predicate ?existingActivity . \n" +
"?existingActivity a ?existingActivityType . \n" + " ?existingActivity vitro:mostSpecificType ?existingActivityType . \n" +
"}"; "}";
// The activity type query results must be limited to the values in the activity type select element.
// Sometimes the query returns a superclass such as owl:Thing instead. private static final String SUBCLASS_ACTIVITY_TYPE_QUERY =
"PREFIX core: <" + VIVO_CORE + ">\n" +
"PREFIX rdfs: <" + VitroVocabulary.RDFS + ">\n" +
"PREFIX vitro: <" + VitroVocabulary.vitroURI + "> \n" +
"SELECT ?existingActivityType WHERE {\n" +
" ?role ?predicate ?existingActivity . \n" +
" ?existingActivity vitro:mostSpecificType ?existingActivityType . \n" +
" ?existingActivityType rdfs:subClassOf ?objectClassUri . \n" +
"}";
private static final String CLASSGROUP_ACTIVITY_TYPE_QUERY =
"PREFIX core: <" + VIVO_CORE + ">\n" +
"PREFIX vitro: <" + VitroVocabulary.vitroURI + "> \n" +
"SELECT ?existingActivityType WHERE { \n" +
" ?role ?predicate ?existingActivity . \n" +
" ?existingActivity vitro:mostSpecificType ?existingActivityType . \n" +
" ?existingActivityType vitro:inClassGroup ?classgroup . \n" +
"}";
/*
* The activity type query results must be limited to the values in the activity type select element.
* Sometimes the query returns a superclass such as owl:Thing instead.
* Make use of vitro:mostSpecificType so that, for example, an individual is both a
* core:InvitedTalk and a core:Presentation, core:InvitedTalk is selected.
* vitro:mostSpecificType alone may not suffice, since it does not guarantee that the value returned
* is in the select list.
* We could still have problems if the value from the select list is not a vitro:mostSpecificType,
* but that is unlikely.
*/
private String getActivityTypeQuery(VitroRequest vreq) { private String getActivityTypeQuery(VitroRequest vreq) {
String activityTypeQuery = null; String activityTypeQuery = null;
// Select options are subclasses of a specified class String optionsType = vreq.getParameter("roleActivityType_optionsType");
String objectClassUri = vreq.getParameter("roleActivityType_objectClassUri");
if (StringUtils.isNotBlank(objectClassUri)) { // Note that this value is overloaded to specify either object class uri or classgroup uri
log.debug("objectClassUri = " + objectClassUri); String objectClassUri = vreq.getParameter("roleActivityType_objectClassUri");
activityTypeQuery =
"PREFIX core: <" + VIVO_CORE + ">\n" + if (StringUtils.isNotBlank(objectClassUri)) {
"PREFIX rdfs: <" + VitroVocabulary.RDFS + ">\n" + log.debug("objectClassUri = " + objectClassUri);
"SELECT ?existingActivityType WHERE {\n" +
"?role <${roleToActivityPredicate}> ?existingActivity . \n" + if ("VCLASSGROUP".equals(optionsType)) {
"?existingActivity a ?existingActivityType . \n" + activityTypeQuery = CLASSGROUP_ACTIVITY_TYPE_QUERY;
"?existingActivityType rdfs:subClassOf <" + objectClassUri + "> . \n" + activityTypeQuery = QueryUtils.subUriForQueryVar(activityTypeQuery, "classgroup", objectClassUri);
"}";
} else { } else if ("CHILD_VCLASSES".equals(optionsType)) {
String optionsType = vreq.getParameter("roleActivityType_optionsType"); activityTypeQuery = SUBCLASS_ACTIVITY_TYPE_QUERY;
// Select options are hardcoded activityTypeQuery = QueryUtils.subUriForQueryVar(activityTypeQuery, "objectClassUri", objectClassUri);
if ("HARDCODED_LITERALS".equals(optionsType)) {
String typeLiteralOptions = vreq.getParameter("roleActivityType_literalOptions"); } else {
if (StringUtils.isNotBlank(typeLiteralOptions)) { activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY;
try { }
JSONObject json = new JSONObject("{values: [" + typeLiteralOptions + "]}");
Set<String> typeUris = new HashSet<String>(); // Select options are hardcoded
JSONArray values = json.getJSONArray("values"); } else if ("HARDCODED_LITERALS".equals(optionsType)) {
int valueCount = values.length();
for (int i = 0; i < valueCount; i++) { String typeLiteralOptions = vreq.getParameter("roleActivityType_literalOptions");
JSONArray option = values.getJSONArray(i); if (StringUtils.isNotBlank(typeLiteralOptions)) {
String uri = option.getString(0); try {
if (StringUtils.isNotBlank(uri)) { JSONObject json = new JSONObject("{values: [" + typeLiteralOptions + "]}");
typeUris.add("(?existingActivityType = <" + uri + ">)"); Set<String> typeUris = new HashSet<String>();
} JSONArray values = json.getJSONArray("values");
} int valueCount = values.length();
String typeFilters = "FILTER (" + StringUtils.join(typeUris, "||") + ")"; for (int i = 0; i < valueCount; i++) {
activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY.replace("}", "") + typeFilters + "}"; JSONArray option = values.getJSONArray(i);
} catch (JSONException e) { String uri = option.getString(0);
activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY; if (StringUtils.isNotBlank(uri)) {
} typeUris.add("(?existingActivityType = <" + uri + ">)");
} }
}
String typeFilters = "FILTER (" + StringUtils.join(typeUris, "||") + ")";
activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY.replaceAll("}$", "") + typeFilters + "}";
} catch (JSONException e) {
activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY;
}
} else { } else {
activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY; activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY;
} }
} else {
activityTypeQuery = DEFAULT_ACTIVITY_TYPE_QUERY;
} }
String roleToActivityPredicate = (String) vreq.getAttribute("roleToActivityPredicate");
activityTypeQuery = QueryUtils.subUriForQueryVar(activityTypeQuery, "predicate", roleToActivityPredicate);
log.debug("Activity type query: " + activityTypeQuery); log.debug("Activity type query: " + activityTypeQuery);
return activityTypeQuery; return activityTypeQuery;
} }
%> %>