NIHVIVO-3628 convert PropertyListConfig and InvalidConfigurationException from inner classes on ObjectPropertyTemplateModel to top-level classes. Combine InvalidConfigurationException with InvalidConfigFileException. Adjust tests accordingly.
This commit is contained in:
parent
6a4228f099
commit
11d32685cf
8 changed files with 313 additions and 269 deletions
|
@ -71,7 +71,7 @@ public class CustomListViewConfigFile {
|
|||
// Might be empty but will not be null.
|
||||
private final String postprocessorName;
|
||||
|
||||
public CustomListViewConfigFile(Reader reader) throws InvalidConfigFileException {
|
||||
public CustomListViewConfigFile(Reader reader) throws InvalidConfigurationException {
|
||||
Document doc = parseDocument(reader);
|
||||
selectQueryElement = parseSelectQuery(doc);
|
||||
constructQueries = parseConstructQueries(doc);
|
||||
|
@ -80,9 +80,9 @@ public class CustomListViewConfigFile {
|
|||
}
|
||||
|
||||
private Document parseDocument(Reader reader)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
if (reader == null) {
|
||||
throw new InvalidConfigFileException("Config file reader is null.");
|
||||
throw new InvalidConfigurationException("Config file reader is null.");
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -91,18 +91,18 @@ public class CustomListViewConfigFile {
|
|||
Document doc = db.parse(new InputSource(reader));
|
||||
return doc;
|
||||
} catch (ParserConfigurationException e) {
|
||||
throw new InvalidConfigFileException("Problem with XML parser.", e);
|
||||
throw new InvalidConfigurationException("Problem with XML parser.", e);
|
||||
} catch (SAXException e) {
|
||||
throw new InvalidConfigFileException(
|
||||
throw new InvalidConfigurationException(
|
||||
"Config file is not valid XML: " + e.getMessage(), e);
|
||||
} catch (IOException e) {
|
||||
throw new InvalidConfigFileException("Unable to read config file.",
|
||||
throw new InvalidConfigurationException("Unable to read config file.",
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
private Element parseSelectQuery(Document doc)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
Element element = getExactlyOneElement(doc, TAG_SELECT);
|
||||
elementMustNotBeEmpty(element);
|
||||
return element;
|
||||
|
@ -120,14 +120,14 @@ public class CustomListViewConfigFile {
|
|||
}
|
||||
|
||||
private String parseTemplateName(Document doc)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
Element element = getExactlyOneElement(doc, TAG_TEMPLATE);
|
||||
elementMustNotBeEmpty(element);
|
||||
return element.getTextContent();
|
||||
}
|
||||
|
||||
private String parsePostprocessorName(Document doc)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
Element element = getZeroOrOneElement(doc, TAG_POSTPROCESSOR);
|
||||
if (element == null) {
|
||||
return "";
|
||||
|
@ -148,40 +148,40 @@ public class CustomListViewConfigFile {
|
|||
}
|
||||
|
||||
private Element getExactlyOneElement(Document doc, String tagName)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
List<Element> elements = getElements(doc, tagName);
|
||||
|
||||
if (elements.size() == 1) {
|
||||
return elements.get(0);
|
||||
} else if (elements.isEmpty()) {
|
||||
throw new InvalidConfigFileException("Config file must contain a "
|
||||
throw new InvalidConfigurationException("Config file must contain a "
|
||||
+ tagName + " element");
|
||||
} else {
|
||||
throw new InvalidConfigFileException(
|
||||
throw new InvalidConfigurationException(
|
||||
"Config file may not contain more than one " + tagName
|
||||
+ " element");
|
||||
}
|
||||
}
|
||||
|
||||
private Element getZeroOrOneElement(Document doc, String tagName)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
List<Element> elements = getElements(doc, tagName);
|
||||
if (elements.size() == 1) {
|
||||
return elements.get(0);
|
||||
} else if (elements.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
throw new InvalidConfigFileException(
|
||||
throw new InvalidConfigurationException(
|
||||
"Config file may not contain more than one " + tagName
|
||||
+ " element");
|
||||
}
|
||||
}
|
||||
|
||||
private void elementMustNotBeEmpty(Element element)
|
||||
throws InvalidConfigFileException {
|
||||
throws InvalidConfigurationException {
|
||||
String contents = element.getTextContent();
|
||||
if (contents.trim().isEmpty()) {
|
||||
throw new InvalidConfigFileException("In a config file, the <"
|
||||
throw new InvalidConfigurationException("In a config file, the <"
|
||||
+ element.getTagName() + "> element must not be empty.");
|
||||
}
|
||||
}
|
||||
|
@ -249,13 +249,4 @@ public class CustomListViewConfigFile {
|
|||
}
|
||||
}
|
||||
|
||||
public static class InvalidConfigFileException extends Exception {
|
||||
public InvalidConfigFileException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public InvalidConfigFileException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview;
|
||||
|
||||
/**
|
||||
* Indicates that there is a problem with the custom configuration. Perhaps the
|
||||
* file can't be found, can't be parsed, or the information it contains is
|
||||
* erroneous.
|
||||
*/
|
||||
public class InvalidConfigurationException extends Exception {
|
||||
|
||||
public InvalidConfigurationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public InvalidConfigurationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public InvalidConfigurationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public InvalidConfigurationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,213 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.CollatedObjectPropertyTemplateModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostProcessor;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ObjectPropertyDataPostProcessor;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ObjectPropertyTemplateModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ObjectPropertyTemplateModel.ConfigError;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
public class PropertyListConfig {
|
||||
private static final Log log = LogFactory.getLog(PropertyListConfig.class);
|
||||
|
||||
|
||||
private static final String CONFIG_FILE_PATH = "/config/";
|
||||
private static final String DEFAULT_CONFIG_FILE_NAME = "listViewConfig-default.xml";
|
||||
|
||||
/* NB The default post-processor is not the same as the post-processor for the default view. The latter
|
||||
* actually defines its own post-processor, whereas the default post-processor is used for custom views
|
||||
* that don't define a post-processor, to ensure that the standard post-processing applies.
|
||||
*
|
||||
* edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostProcessor
|
||||
*/
|
||||
|
||||
// TODO Lump these together into the PropertyListConfigContext
|
||||
private final ObjectPropertyTemplateModel optm;
|
||||
private final VitroRequest vreq;
|
||||
|
||||
private boolean isDefaultConfig;
|
||||
private Set<String> constructQueries;
|
||||
private String selectQuery;
|
||||
private String templateName;
|
||||
private ObjectPropertyDataPostProcessor postprocessor; // never null
|
||||
|
||||
public PropertyListConfig(ObjectPropertyTemplateModel optm, VitroRequest vreq, ObjectProperty op, boolean editing)
|
||||
throws InvalidConfigurationException {
|
||||
|
||||
this.optm = optm;
|
||||
this.vreq = vreq;
|
||||
WebappDaoFactory wadf = vreq.getWebappDaoFactory();
|
||||
|
||||
// Get the custom config filename
|
||||
String configFileName = wadf.getObjectPropertyDao().getCustomListViewConfigFileName(op);
|
||||
if (configFileName == null) { // no custom config; use default config
|
||||
configFileName = DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
log.debug("Using list view config file " + configFileName + " for object property " + op.getURI());
|
||||
|
||||
String configFilePath = getConfigFilePath(configFileName);
|
||||
|
||||
try {
|
||||
File config = new File(configFilePath);
|
||||
if ( ! isDefaultConfig(configFileName) && ! config.exists() ) {
|
||||
log.warn("Can't find config file " + configFilePath + " for object property " + op.getURI() + "\n" +
|
||||
". Using default config file instead.");
|
||||
configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE_NAME);
|
||||
// Should we test for the existence of the default, and throw an error if it doesn't exist?
|
||||
}
|
||||
setValuesFromConfigFile(configFilePath, wadf, editing);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing config file " + configFilePath + " for object property " + op.getURI(), e);
|
||||
// What should we do here?
|
||||
}
|
||||
|
||||
if ( ! isDefaultConfig(configFileName) ) {
|
||||
ConfigError configError = checkConfiguration();
|
||||
if ( configError != null ) { // the configuration contains an error
|
||||
// If this is a collated property, throw an error: this results in creating an
|
||||
// UncollatedPropertyTemplateModel instead.
|
||||
if (optm instanceof CollatedObjectPropertyTemplateModel) {
|
||||
throw new InvalidConfigurationException(configError.getMessage());
|
||||
}
|
||||
// Otherwise, switch to the default config
|
||||
log.warn("Invalid list view config for object property " + op.getURI() +
|
||||
" in " + configFilePath + ":\n" +
|
||||
configError + " Using default config instead.");
|
||||
configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE_NAME);
|
||||
setValuesFromConfigFile(configFilePath, wadf, editing);
|
||||
}
|
||||
}
|
||||
|
||||
isDefaultConfig = isDefaultConfig(configFileName);
|
||||
}
|
||||
|
||||
private boolean isDefaultConfig(String configFileName) {
|
||||
return configFileName.equals(DEFAULT_CONFIG_FILE_NAME);
|
||||
}
|
||||
|
||||
private ConfigError checkConfiguration() {
|
||||
|
||||
ConfigError error = optm.checkQuery(selectQuery);
|
||||
if (error != null) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(selectQuery)) {
|
||||
return ConfigError.NO_SELECT_QUERY;
|
||||
}
|
||||
|
||||
if ( StringUtils.isBlank(templateName)) {
|
||||
return ConfigError.NO_TEMPLATE;
|
||||
}
|
||||
|
||||
Configuration fmConfig = (Configuration) vreq.getAttribute("freemarkerConfig");
|
||||
TemplateLoader tl = fmConfig.getTemplateLoader();
|
||||
try {
|
||||
if ( tl.findTemplateSource(templateName) == null ) {
|
||||
return ConfigError.TEMPLATE_NOT_FOUND;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error finding template " + templateName, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setValuesFromConfigFile(String configFilePath, WebappDaoFactory wdf,
|
||||
boolean editing) {
|
||||
try {
|
||||
FileReader reader = new FileReader(configFilePath);
|
||||
CustomListViewConfigFile configFileContents = new CustomListViewConfigFile(reader);
|
||||
|
||||
boolean collated = optm instanceof CollatedObjectPropertyTemplateModel;
|
||||
|
||||
selectQuery = configFileContents.getSelectQuery(collated, editing);
|
||||
templateName = configFileContents.getTemplateName();
|
||||
constructQueries = configFileContents.getConstructQueries();
|
||||
|
||||
String postprocessorName = configFileContents.getPostprocessorName();
|
||||
postprocessor = getPostProcessor(postprocessorName, optm, wdf, configFilePath);
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing config file " + configFilePath, e);
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectPropertyDataPostProcessor getPostProcessor(
|
||||
String className,
|
||||
ObjectPropertyTemplateModel optm,
|
||||
WebappDaoFactory wdf, String configFilePath) {
|
||||
try {
|
||||
if (StringUtils.isBlank(className)) {
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
}
|
||||
|
||||
Class<?> clazz = Class.forName(className);
|
||||
Constructor<?> constructor = clazz.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
|
||||
return (ObjectPropertyDataPostProcessor) constructor.newInstance(optm, wdf);
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': can't load postprocessor class '" + className
|
||||
+ "'. " + "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
} catch (NoSuchMethodException e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': postprocessor class '" + className
|
||||
+ "' does not have a constructor that takes "
|
||||
+ "ObjectPropertyTemplateModel and WebappDaoFactory. "
|
||||
+ "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
} catch (ClassCastException e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': postprocessor class '" + className + "' does "
|
||||
+ "not implement ObjectPropertyDataPostProcessor. "
|
||||
+ "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
} catch (Exception e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': can't create postprocessor instance of class '"
|
||||
+ className + "'. " + "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
}
|
||||
}
|
||||
|
||||
private String getConfigFilePath(String filename) {
|
||||
return vreq.getSession().getServletContext().getRealPath(CONFIG_FILE_PATH + filename);
|
||||
}
|
||||
|
||||
public String getSelectQuery() {
|
||||
return this.selectQuery;
|
||||
}
|
||||
|
||||
public Set<String> getConstructQueries() {
|
||||
return this.constructQueries;
|
||||
}
|
||||
|
||||
public String getTemplateName() {
|
||||
return this.templateName;
|
||||
}
|
||||
|
||||
public boolean isDefaultListView() {
|
||||
return this.isDefaultConfig;
|
||||
}
|
||||
|
||||
public ObjectPropertyDataPostProcessor getPostprocessor() {
|
||||
return this.postprocessor;
|
||||
}
|
||||
}
|
|
@ -21,6 +21,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException;
|
||||
|
||||
public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateModel {
|
||||
|
||||
|
@ -77,7 +78,7 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
|
|||
return subclasses.isEmpty();
|
||||
}
|
||||
|
||||
protected ConfigError checkQuery(String queryString) {
|
||||
public ConfigError checkQuery(String queryString) {
|
||||
|
||||
if (StringUtils.isBlank(queryString)) {
|
||||
return ConfigError.NO_SELECT_QUERY;
|
||||
|
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -31,10 +27,8 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMa
|
|||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.CustomListViewConfigFile;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import freemarker.template.Configuration;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.PropertyListConfig;
|
||||
|
||||
public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel {
|
||||
|
||||
|
@ -59,7 +53,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
// ?subject ?property ?\w+
|
||||
Pattern.compile("\\?" + KEY_SUBJECT + "\\s+\\?" + KEY_PROPERTY + "\\s+\\?(\\w+)");
|
||||
|
||||
protected static enum ConfigError {
|
||||
public static enum ConfigError {
|
||||
NO_SELECT_QUERY("Missing select query specification"),
|
||||
NO_SUBCLASS_SELECT("Query does not select a subclass variable"),
|
||||
NO_SUBCLASS_ORDER_BY("Query does not sort first by subclass variable"),
|
||||
|
@ -93,7 +87,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
|
||||
// Get the config for this object property
|
||||
try {
|
||||
config = new PropertyListConfig(op, editing);
|
||||
config = new PropertyListConfig(this, vreq, op, editing);
|
||||
} catch (InvalidConfigurationException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
|
@ -146,7 +140,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
return Route.OBJECT_PROPERTY_EDIT;
|
||||
}
|
||||
|
||||
protected ConfigError checkQuery(String queryString) {
|
||||
public ConfigError checkQuery(String queryString) {
|
||||
if (StringUtils.isBlank(queryString)) {
|
||||
return ConfigError.NO_SELECT_QUERY;
|
||||
}
|
||||
|
@ -154,19 +148,19 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
}
|
||||
|
||||
private String getSelectQuery() {
|
||||
return config.selectQuery;
|
||||
return config.getSelectQuery();
|
||||
}
|
||||
|
||||
private Set<String> getConstructQueries() {
|
||||
return config.constructQueries;
|
||||
return config.getConstructQueries();
|
||||
}
|
||||
|
||||
protected String getTemplateName() {
|
||||
return config.templateName;
|
||||
return config.getTemplateName();
|
||||
}
|
||||
|
||||
protected boolean hasDefaultListView() {
|
||||
return config.isDefaultConfig;
|
||||
return config.isDefaultListView();
|
||||
}
|
||||
|
||||
protected static String getImageUploadUrl(String subjectUri, String action) {
|
||||
|
@ -227,14 +221,8 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
logData(data);
|
||||
}
|
||||
|
||||
ObjectPropertyDataPostProcessor postprocessor = config.postprocessor;
|
||||
if (postprocessor == null) {
|
||||
log.debug("No postprocessor for property " + getUri());
|
||||
return;
|
||||
} else {
|
||||
log.debug("Using postprocessor " + postprocessor.getClass().getName() + " for property " + getUri());
|
||||
}
|
||||
|
||||
ObjectPropertyDataPostProcessor postprocessor = config.getPostprocessor();
|
||||
log.debug("Using postprocessor " + postprocessor.getClass().getName() + " for property " + getUri());
|
||||
postprocessor.process(data);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
|
@ -331,174 +319,6 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
return objectKey;
|
||||
}
|
||||
|
||||
private class PropertyListConfig {
|
||||
|
||||
private static final String CONFIG_FILE_PATH = "/config/";
|
||||
private static final String DEFAULT_CONFIG_FILE_NAME = "listViewConfig-default.xml";
|
||||
|
||||
/* NB The default post-processor is not the same as the post-processor for the default view. The latter
|
||||
* actually defines its own post-processor, whereas the default post-processor is used for custom views
|
||||
* that don't define a post-processor, to ensure that the standard post-processing applies.
|
||||
*
|
||||
* edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostProcessor
|
||||
*/
|
||||
|
||||
private boolean isDefaultConfig;
|
||||
private Set<String> constructQueries;
|
||||
private String selectQuery;
|
||||
private String templateName;
|
||||
private ObjectPropertyDataPostProcessor postprocessor = null;
|
||||
|
||||
PropertyListConfig(ObjectProperty op, boolean editing)
|
||||
throws InvalidConfigurationException {
|
||||
|
||||
// Get the custom config filename
|
||||
String configFileName = vreq.getWebappDaoFactory().getObjectPropertyDao().getCustomListViewConfigFileName(op);
|
||||
if (configFileName == null) { // no custom config; use default config
|
||||
configFileName = DEFAULT_CONFIG_FILE_NAME;
|
||||
}
|
||||
log.debug("Using list view config file " + configFileName + " for object property " + op.getURI());
|
||||
|
||||
String configFilePath = getConfigFilePath(configFileName);
|
||||
|
||||
try {
|
||||
File config = new File(configFilePath);
|
||||
if ( ! isDefaultConfig(configFileName) && ! config.exists() ) {
|
||||
log.warn("Can't find config file " + configFilePath + " for object property " + op.getURI() + "\n" +
|
||||
". Using default config file instead.");
|
||||
configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE_NAME);
|
||||
// Should we test for the existence of the default, and throw an error if it doesn't exist?
|
||||
}
|
||||
setValuesFromConfigFile(configFilePath, vreq.getWebappDaoFactory(), editing);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing config file " + configFilePath + " for object property " + op.getURI(), e);
|
||||
// What should we do here?
|
||||
}
|
||||
|
||||
if ( ! isDefaultConfig(configFileName) ) {
|
||||
ConfigError configError = checkConfiguration();
|
||||
if ( configError != null ) { // the configuration contains an error
|
||||
// If this is a collated property, throw an error: this results in creating an
|
||||
// UncollatedPropertyTemplateModel instead.
|
||||
if (ObjectPropertyTemplateModel.this instanceof CollatedObjectPropertyTemplateModel) {
|
||||
throw new InvalidConfigurationException(configError.getMessage());
|
||||
}
|
||||
// Otherwise, switch to the default config
|
||||
log.warn("Invalid list view config for object property " + op.getURI() +
|
||||
" in " + configFilePath + ":\n" +
|
||||
configError + " Using default config instead.");
|
||||
configFilePath = getConfigFilePath(DEFAULT_CONFIG_FILE_NAME);
|
||||
setValuesFromConfigFile(configFilePath, vreq.getWebappDaoFactory(), editing);
|
||||
}
|
||||
}
|
||||
|
||||
isDefaultConfig = isDefaultConfig(configFileName);
|
||||
}
|
||||
|
||||
private boolean isDefaultConfig(String configFileName) {
|
||||
return configFileName.equals(DEFAULT_CONFIG_FILE_NAME);
|
||||
}
|
||||
|
||||
private ConfigError checkConfiguration() {
|
||||
|
||||
ConfigError error = ObjectPropertyTemplateModel.this.checkQuery(selectQuery);
|
||||
if (error != null) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(selectQuery)) {
|
||||
return ConfigError.NO_SELECT_QUERY;
|
||||
}
|
||||
|
||||
if ( StringUtils.isBlank(templateName)) {
|
||||
return ConfigError.NO_TEMPLATE;
|
||||
}
|
||||
|
||||
Configuration fmConfig = (Configuration) vreq.getAttribute("freemarkerConfig");
|
||||
TemplateLoader tl = fmConfig.getTemplateLoader();
|
||||
try {
|
||||
if ( tl.findTemplateSource(templateName) == null ) {
|
||||
return ConfigError.TEMPLATE_NOT_FOUND;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error finding template " + templateName, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setValuesFromConfigFile(String configFilePath, WebappDaoFactory wdf,
|
||||
boolean editing) {
|
||||
try {
|
||||
FileReader reader = new FileReader(configFilePath);
|
||||
CustomListViewConfigFile configFileContents = new CustomListViewConfigFile(reader);
|
||||
|
||||
boolean collated = ObjectPropertyTemplateModel.this instanceof CollatedObjectPropertyTemplateModel;
|
||||
|
||||
selectQuery = configFileContents.getSelectQuery(collated, editing);
|
||||
templateName = configFileContents.getTemplateName();
|
||||
constructQueries = configFileContents.getConstructQueries();
|
||||
|
||||
String postprocessorName = configFileContents.getPostprocessorName();
|
||||
postprocessor = getPostProcessor(postprocessorName, ObjectPropertyTemplateModel.this, wdf, configFilePath);
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing config file " + configFilePath, e);
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectPropertyDataPostProcessor getPostProcessor(
|
||||
String className,
|
||||
ObjectPropertyTemplateModel optm,
|
||||
WebappDaoFactory wdf, String configFilePath) {
|
||||
try {
|
||||
if (StringUtils.isBlank(className)) {
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
}
|
||||
|
||||
Class<?> clazz = Class.forName(className);
|
||||
Constructor<?> constructor = clazz.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
|
||||
return (ObjectPropertyDataPostProcessor) constructor.newInstance(optm, wdf);
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': can't load postprocessor class '" + className
|
||||
+ "'. " + "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
} catch (NoSuchMethodException e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': postprocessor class '" + className
|
||||
+ "' does not have a constructor that takes "
|
||||
+ "ObjectPropertyTemplateModel and WebappDaoFactory. "
|
||||
+ "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
} catch (ClassCastException e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': postprocessor class '" + className + "' does "
|
||||
+ "not implement ObjectPropertyDataPostProcessor. "
|
||||
+ "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
} catch (Exception e) {
|
||||
log.warn("Error processing config file '" + configFilePath
|
||||
+ "': can't create postprocessor instance of class '"
|
||||
+ className + "'. " + "Using default postprocessor.", e);
|
||||
return new DefaultObjectPropertyDataPostProcessor(optm, wdf);
|
||||
}
|
||||
}
|
||||
|
||||
private String getConfigFilePath(String filename) {
|
||||
return servletContext.getRealPath(CONFIG_FILE_PATH + filename);
|
||||
}
|
||||
}
|
||||
|
||||
protected class InvalidConfigurationException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected InvalidConfigurationException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
|
||||
/* Template properties */
|
||||
|
||||
public String getType() {
|
||||
|
@ -506,7 +326,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
}
|
||||
|
||||
public String getTemplate() {
|
||||
return config.templateName;
|
||||
return getTemplateName();
|
||||
}
|
||||
|
||||
public abstract boolean isCollatedBySubclass();
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.customlistview.InvalidConfigurationException;
|
||||
|
||||
public class UncollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateModel {
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue