NIHVIVO-1510 Added error-checking of collation target in list view config file against sparql select terms.

This commit is contained in:
rjy7 2010-12-14 16:24:50 +00:00
parent 544a0b8e4d
commit 1dd5885514
5 changed files with 35 additions and 8 deletions

View file

@ -517,8 +517,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
write(sw, response);
}
protected void write(StringWriter sw, HttpServletResponse response) {
protected void write(StringWriter sw, HttpServletResponse response) {
try {
PrintWriter out = response.getWriter();
out.print(sw);

View file

@ -2,9 +2,12 @@
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -24,13 +27,19 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
CollatedObjectPropertyTemplateModel(ObjectProperty op, Individual subject, WebappDaoFactory wdf) throws Exception {
super(op, subject, wdf);
String collationTargetError = getCollationTargetError();
if ( ! collationTargetError.isEmpty()) {
String errorMessage = "Collation target error for collated object property " + getName() + ": " +
collationTargetError + " " +
"Creating uncollated property list instead.";
throw new Exception(errorMessage);
}
/* RY Temporarily throw an error because collation hasn't been implemented yet. We'll then use an uncollated one instead.
* In final version, throw an error if config doesn't contain collation-target element. We'll use an uncollated one instead.
*/
// RY Temporarily throw an error because collation hasn't been implemented yet.
boolean error = true;
if (error) {
throw new Exception("No collation target specified for collated object property " + op.getLabel());
throw new Exception("No collation target specified for collated object property " + getName());
}
ObjectPropertyStatementDao opDao = wdf.getObjectPropertyStatementDao();
@ -49,6 +58,25 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
}
private String getCollationTargetError() {
String errorMessage = null;
String collationTarget = getCollationTarget();
// Make sure the collation target is not null or empty.
if (collationTarget == null || collationTarget.trim().isEmpty()) {
errorMessage = "No collation target specified.";
} else {
// Make sure the collation target is one of the select terms in the query.
String queryString = getQueryString();
String selectClause = queryString.substring(0, queryString.indexOf("{"));
Pattern collationTargetPattern = Pattern.compile("\\b\\\\?" + collationTarget + "\\b");
Matcher matcher = collationTargetPattern.matcher(selectClause);
if (! matcher.find()) {
errorMessage = "Invalid collation target.";
}
}
return errorMessage;
}
private List<VClass> getDirectVClasses(String key, List<Map<String, Object>> data) {
return null;
}

View file

@ -49,6 +49,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
try {
return new CollatedObjectPropertyTemplateModel(op, subject, wdf);
} catch (Exception e) {
log.error(e, e);
return new UncollatedObjectPropertyTemplateModel(op, subject, wdf);
}
} else {

View file

@ -20,7 +20,6 @@ public abstract class PropertyTemplateModel extends BaseTemplateModel {
protected Property property; // needed to get the edit links
PropertyTemplateModel(Property property) {
this.name = property.getLabel();
// Do in subclass constructor. The label has not been set on the property, and getting the
// label differs between object and data properties.
// this.name = property.getLabel();

View file

@ -2,7 +2,7 @@
<!-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<view-config>
<query>
SELECT ?object {
SELECT ?object {
?subject ?property ?object .
}
</query>