NIHVIVO-1341, NIHVIVO-1335 Handle various error conditions in list view config (blank query, blank template name, template doesn't exist)

This commit is contained in:
rjy7 2010-12-21 00:00:21 +00:00
parent fd15870f7e
commit 555fd93962
10 changed files with 94 additions and 54 deletions

View file

@ -56,5 +56,5 @@ public interface ObjectPropertyDao extends PropertyDao {
public List<ObjectProperty> getObjectPropertyList(String subjectUri); public List<ObjectProperty> getObjectPropertyList(String subjectUri);
public String getCustomListConfigFilename(ObjectProperty objectProperty); public String getCustomListConfigFileName(ObjectProperty objectProperty);
} }

View file

@ -216,7 +216,7 @@ class ObjectPropertyDaoFiltering extends BaseFiltering implements ObjectProperty
} }
@Override @Override
public String getCustomListConfigFilename(ObjectProperty objectProperty) { public String getCustomListConfigFileName(ObjectProperty objectProperty) {
return innerObjectPropertyDao.getCustomListConfigFilename(objectProperty); return innerObjectPropertyDao.getCustomListConfigFileName(objectProperty);
} }
} }

View file

@ -76,7 +76,7 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
"PREFIX display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#>" + "PREFIX display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#>" +
"SELECT ?property ?filename WHERE { \n" + "SELECT ?property ?filename WHERE { \n" +
" ?property display:listViewConfigFile ?filename . \n" + " ?property display:listViewConfigFile ?filename . \n" +
"} LIMIT 1"; "}";
static protected Query listViewConfigFileQuery; static protected Query listViewConfigFileQuery;
static { static {
@ -875,7 +875,7 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp
} }
@Override @Override
public String getCustomListConfigFilename(ObjectProperty op) { public String getCustomListConfigFileName(ObjectProperty op) {
if (customListViewConfigFileMap == null) { if (customListViewConfigFileMap == null) {
customListViewConfigFileMap = new HashMap<ObjectProperty, String>(); customListViewConfigFileMap = new HashMap<ObjectProperty, String>();
OntModel displayModel = getOntModelSelector().getDisplayModel(); OntModel displayModel = getOntModelSelector().getDisplayModel();

View file

@ -2,7 +2,6 @@
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual; package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -15,6 +14,7 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -23,10 +23,9 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
private static final Log log = LogFactory.getLog(CollatedObjectPropertyTemplateModel.class); private static final Log log = LogFactory.getLog(CollatedObjectPropertyTemplateModel.class);
private Map<String, List<ObjectPropertyStatementTemplateModel>> subclasses; private Map<String, List<ObjectPropertyStatementTemplateModel>> subclasses;
//private List<SubclassList> subclassList;
CollatedObjectPropertyTemplateModel(ObjectProperty op, Individual subject, WebappDaoFactory wdf) throws Exception { CollatedObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq) throws Exception {
super(op, subject, wdf); super(op, subject, vreq);
/* Change the approach to collation: /* Change the approach to collation:
* Custom views can get the subclasses in the query. Must use a term ?subclass - throw error if not. * Custom views can get the subclasses in the query. Must use a term ?subclass - throw error if not.
@ -36,13 +35,6 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
* We can also use these for custom views. Throw error if property is collated but there's no subclass term * We can also use these for custom views. Throw error if property is collated but there's no subclass term
* in the query. (The reverse is okay - uncollated property with a subclass term in the query. * in the query. (The reverse is okay - uncollated property with a subclass term in the query.
*/ */
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. // RY Temporarily throw an error because collation hasn't been implemented yet.
boolean error = true; boolean error = true;
@ -50,6 +42,7 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
throw new Exception("No collation target specified for collated object property " + getName()); throw new Exception("No collation target specified for collated object property " + getName());
} }
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
ObjectPropertyStatementDao opDao = wdf.getObjectPropertyStatementDao(); ObjectPropertyStatementDao opDao = wdf.getObjectPropertyStatementDao();
String subjectUri = subject.getURI(); String subjectUri = subject.getURI();
String propertyUri = op.getURI(); String propertyUri = op.getURI();

View file

@ -11,6 +11,7 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao; import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -21,12 +22,12 @@ public class DataPropertyTemplateModel extends PropertyTemplateModel {
private static final String TYPE = "data"; private static final String TYPE = "data";
private List<DataPropertyStatementTemplateModel> statements; private List<DataPropertyStatementTemplateModel> statements;
DataPropertyTemplateModel(DataProperty dp, Individual subject, WebappDaoFactory wdf) { DataPropertyTemplateModel(DataProperty dp, Individual subject, VitroRequest vreq) {
super(dp); super(dp);
setName(dp.getPublicName()); setName(dp.getPublicName());
// Get the data property statements via a sparql query // Get the data property statements via a sparql query
DataPropertyStatementDao dpDao = wdf.getDataPropertyStatementDao(); DataPropertyStatementDao dpDao = vreq.getWebappDaoFactory().getDataPropertyStatementDao();
List<DataPropertyStatement> dpStatements = dpDao.getDataPropertyStatementsForIndividualByProperty(subject, dp); List<DataPropertyStatement> dpStatements = dpDao.getDataPropertyStatementsForIndividualByProperty(subject, dp);
statements = new ArrayList<DataPropertyStatementTemplateModel>(dpStatements.size()); statements = new ArrayList<DataPropertyStatementTemplateModel>(dpStatements.size());
for (DataPropertyStatement dps : dpStatements) { for (DataPropertyStatement dps : dpStatements) {

View file

@ -3,6 +3,7 @@
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual; package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -10,6 +11,7 @@ import java.util.Map;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -18,8 +20,11 @@ import org.w3c.dom.NodeList;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel { public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel {
@ -28,13 +33,13 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
private PropertyListConfig config; private PropertyListConfig config;
ObjectPropertyTemplateModel(ObjectProperty op, Individual subject, WebappDaoFactory wdf) { ObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq) {
super(op); super(op);
setName(op.getDomainPublic()); setName(op.getDomainPublic());
// Get the config for this object property // Get the config for this object property
try { try {
config = new PropertyListConfig(op, wdf); config = new PropertyListConfig(op, vreq);
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); log.error(e, e);
} }
@ -48,16 +53,16 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
return config.collationTarget; return config.collationTarget;
} }
protected static ObjectPropertyTemplateModel getObjectPropertyTemplateModel(ObjectProperty op, Individual subject, WebappDaoFactory wdf) { protected static ObjectPropertyTemplateModel getObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq) {
if (op.getCollateBySubclass()) { if (op.getCollateBySubclass()) {
try { try {
return new CollatedObjectPropertyTemplateModel(op, subject, wdf); return new CollatedObjectPropertyTemplateModel(op, subject, vreq);
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); log.error(e, e);
return new UncollatedObjectPropertyTemplateModel(op, subject, wdf); return new UncollatedObjectPropertyTemplateModel(op, subject, vreq);
} }
} else { } else {
return new UncollatedObjectPropertyTemplateModel(op, subject, wdf); return new UncollatedObjectPropertyTemplateModel(op, subject, vreq);
} }
} }
@ -94,47 +99,86 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
private String postprocessor; private String postprocessor;
private String editObject; private String editObject;
PropertyListConfig(ObjectProperty op, WebappDaoFactory wdf) throws Exception { PropertyListConfig(ObjectProperty op, VitroRequest vreq) throws Exception {
// Get the custom config filename // Get the custom config filename
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
ObjectPropertyDao opDao = wdf.getObjectPropertyDao(); ObjectPropertyDao opDao = wdf.getObjectPropertyDao();
String filename = opDao.getCustomListConfigFilename(op); String configFileName = opDao.getCustomListConfigFileName(op);
if (filename == null) { // no custom config; use default config if (configFileName == null) { // no custom config; use default config
filename = DEFAULT_CONFIG_FILE; configFileName = DEFAULT_CONFIG_FILE;
} }
log.debug("Using custom list view config file " + filename + " for object property " + op.getURI()); log.debug("Using list view config file " + configFileName + " for object property " + op.getURI());
String configFilePath = getConfigFilePath(filename); String configFilePath = getConfigFilePath(configFileName);
try { try {
File config = new File(configFilePath); File config = new File(configFilePath);
if (configFilePath != DEFAULT_CONFIG_FILE && ! config.exists()) { if (configFileName != DEFAULT_CONFIG_FILE && ! config.exists()) {
log.warn("Can't find config file " + configFilePath + " for object property " + op.getURI() + "\n" + log.warn("Can't find config file " + configFilePath + " for object property " + op.getURI() + "\n" +
". Using default config file instead."); ". Using default config file instead.");
configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE); configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE);
// Should we test for the existence of the default, and throw an error if it doesn't exist? // Should we test for the existence of the default, and throw an error if it doesn't exist?
} }
setValuesFromConfigFile(configFilePath);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(configFilePath);
// Required values
queryString = getConfigValue(doc, NODE_NAME_QUERY);
templateName = getConfigValue(doc, NODE_NAME_TEMPLATE);
// Optional values
collationTarget = getConfigValue(doc, NODE_NAME_COLLATION_TARGET);
postprocessor = getConfigValue(doc, NODE_NAME_POSTPROCESSOR);
editObject = getConfigValue(doc, NODE_NAME_EDIT_OBJECT);
} catch (Exception e) { } catch (Exception e) {
log.error("Error processing config file " + configFilePath + " for object property " + op.getURI(), e); log.error("Error processing config file " + configFilePath + " for object property " + op.getURI(), e);
// What should we do here? // What should we do here?
} }
if (queryString == null) { if ( ! configFileName.equals(DEFAULT_CONFIG_FILE) ) {
throw new Exception("Invalid custom view configuration: query string not defined."); String invalidConfigMessage = checkForInvalidConfig(vreq);
if ( StringUtils.isNotEmpty(invalidConfigMessage) ) {
log.warn("Invalid list view config for object property " + op.getURI() +
" in " + configFilePath + ":\n" +
invalidConfigMessage + " Using default config instead.");
configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE);
setValuesFromConfigFile(configFilePath);
}
} }
if (templateName == null) { }
throw new Exception("Invalid custom view configuration: template name not defined.");
private String checkForInvalidConfig(VitroRequest vreq) {
String invalidConfigMessage = null;
if ( StringUtils.isBlank(queryString)) {
invalidConfigMessage = "Missing query specification.";
} else if ( StringUtils.isBlank(templateName)) {
invalidConfigMessage = "Missing template specification.";
} else {
Configuration fmConfig = (Configuration) vreq.getAttribute("freemarkerConfig");
TemplateLoader tl = fmConfig.getTemplateLoader();
try {
if ( tl.findTemplateSource(templateName) == null ) {
invalidConfigMessage = "Specified template " + templateName + " does not exist.";
}
} catch (IOException e) {
log.error("Error finding template " + templateName, e);
}
} }
return invalidConfigMessage;
}
private void setValuesFromConfigFile(String configFilePath) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
Document doc = db.parse(configFilePath);
// Required values
queryString = getConfigValue(doc, NODE_NAME_QUERY);
templateName = getConfigValue(doc, NODE_NAME_TEMPLATE);
editObject = getConfigValue(doc, NODE_NAME_EDIT_OBJECT);
// Optional values
collationTarget = getConfigValue(doc, NODE_NAME_COLLATION_TARGET);
postprocessor = getConfigValue(doc, NODE_NAME_POSTPROCESSOR);
} catch (Exception e) {
log.error("Error processing config file " + configFilePath, e);
// What should we do here?
}
} }
private String getConfigValue(Document doc, String nodeName) { private String getConfigValue(Document doc, String nodeName) {

View file

@ -13,6 +13,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.Property; import edu.cornell.mannlib.vitro.webapp.beans.Property;
import edu.cornell.mannlib.vitro.webapp.beans.PropertyGroup; import edu.cornell.mannlib.vitro.webapp.beans.PropertyGroup;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
@ -23,7 +24,7 @@ public class PropertyGroupTemplateModel extends BaseTemplateModel {
private String name; private String name;
private List<PropertyTemplateModel> properties; private List<PropertyTemplateModel> properties;
PropertyGroupTemplateModel(WebappDaoFactory wdf, PropertyGroup group, Individual subject) { PropertyGroupTemplateModel(VitroRequest vreq, PropertyGroup group, Individual subject) {
this.name = group.getName(); this.name = group.getName();
List<Property> propertyList = group.getPropertyList(); List<Property> propertyList = group.getPropertyList();
@ -31,9 +32,9 @@ public class PropertyGroupTemplateModel extends BaseTemplateModel {
for (Property p : propertyList) { for (Property p : propertyList) {
if (p instanceof ObjectProperty) { if (p instanceof ObjectProperty) {
ObjectProperty op = (ObjectProperty)p; ObjectProperty op = (ObjectProperty)p;
properties.add(ObjectPropertyTemplateModel.getObjectPropertyTemplateModel(op, subject, wdf)); properties.add(ObjectPropertyTemplateModel.getObjectPropertyTemplateModel(op, subject, vreq));
} else { } else {
properties.add(new DataPropertyTemplateModel((DataProperty)p, subject, wdf)); properties.add(new DataPropertyTemplateModel((DataProperty)p, subject, vreq));
} }
} }
} }

View file

@ -102,7 +102,7 @@ public class PropertyListBuilder {
// Build the template data model from the groupList // Build the template data model from the groupList
List<PropertyGroupTemplateModel> groups = new ArrayList<PropertyGroupTemplateModel>(groupList.size()); List<PropertyGroupTemplateModel> groups = new ArrayList<PropertyGroupTemplateModel>(groupList.size());
for (PropertyGroup pg : groupList) { for (PropertyGroup pg : groupList) {
groups.add(new PropertyGroupTemplateModel(wdf, pg, subject)); groups.add(new PropertyGroupTemplateModel(vreq, pg, subject));
} }
return groups; return groups;

View file

@ -11,6 +11,7 @@ import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -20,8 +21,9 @@ public class UncollatedObjectPropertyTemplateModel extends ObjectPropertyTemplat
private List<ObjectPropertyStatementTemplateModel> statements; private List<ObjectPropertyStatementTemplateModel> statements;
UncollatedObjectPropertyTemplateModel(ObjectProperty op, Individual subject, WebappDaoFactory wdf) { UncollatedObjectPropertyTemplateModel(ObjectProperty op, Individual subject, VitroRequest vreq) {
super(op, subject, wdf); super(op, subject, vreq);
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
ObjectPropertyStatementDao opDao = wdf.getObjectPropertyStatementDao(); ObjectPropertyStatementDao opDao = wdf.getObjectPropertyStatementDao();
String subjectUri = subject.getURI(); String subjectUri = subject.getURI();
String propertyUri = op.getURI(); String propertyUri = op.getURI();

View file

@ -3,4 +3,3 @@
<#-- FreeMarker test cases --> <#-- FreeMarker test cases -->
<h2>${title}</h2> <h2>${title}</h2>