NIHVIVO-1341 Refactoring in object property data postprocessors
This commit is contained in:
parent
87196d7152
commit
9709632f61
8 changed files with 100 additions and 100 deletions
|
@ -0,0 +1,56 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
|
||||
public abstract class BaseObjectPropertyDataPostprocessor implements
|
||||
ObjectPropertyDataPostprocessor {
|
||||
|
||||
protected ObjectPropertyTemplateModel objectPropertyTemplateModel;
|
||||
protected WebappDaoFactory wdf;
|
||||
|
||||
public BaseObjectPropertyDataPostprocessor(ObjectPropertyTemplateModel optm, WebappDaoFactory wdf) {
|
||||
this.objectPropertyTemplateModel = optm;
|
||||
this.wdf = wdf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(List<Map<String, String>> data) {
|
||||
for (Map<String, String> map : data) {
|
||||
process(map);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void process(Map<String, String> map);
|
||||
|
||||
|
||||
/* Postprocessor helper methods callable from any postprocessor */
|
||||
|
||||
protected void addName(Map<String, String> map, String nameKey, String objectKey) {
|
||||
String name = map.get(nameKey);
|
||||
if (name == null) {
|
||||
map.put(nameKey, getIndividual(map.get(objectKey)).getName());
|
||||
}
|
||||
}
|
||||
|
||||
/* This is a temporary measure to handle the fact that the current Individual.getMoniker()
|
||||
* method returns the individual's VClass if moniker is null. We want to replicate that
|
||||
* behavior here, but in future the moniker property (along with other Vitro namespace
|
||||
* properties) will be removed. In addition, this type of logic (display x if it exists, otherwise y)
|
||||
* will be moved into the display modules (Editing and Display Configuration Improvements).
|
||||
*/
|
||||
protected void addMoniker(Map<String, String> map, String monikerKey, String objectKey) {
|
||||
String moniker = map.get(monikerKey);
|
||||
if (moniker == null) {
|
||||
map.put(monikerKey, getIndividual(map.get(objectKey)).getMoniker());
|
||||
}
|
||||
}
|
||||
|
||||
protected Individual getIndividual(String uri) {
|
||||
return wdf.getIndividualDao().getIndividualByURI(uri);
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
|
||||
public abstract class BaseObjectPropertyDataPreprocessor implements
|
||||
ObjectPropertyDataPreprocessor {
|
||||
|
||||
protected ObjectPropertyTemplateModel objectPropertyTemplateModel;
|
||||
protected WebappDaoFactory wdf;
|
||||
|
||||
public BaseObjectPropertyDataPreprocessor(ObjectPropertyTemplateModel optm, WebappDaoFactory wdf) {
|
||||
this.objectPropertyTemplateModel = optm;
|
||||
this.wdf = wdf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(List<Map<String, String>> data) {
|
||||
for (Map<String, String> map : data) {
|
||||
process(map);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void process(Map<String, String> map);
|
||||
|
||||
/* Preprocessor helper methods callable from any preprocessor */
|
||||
|
||||
protected String getMoniker(String uri) {
|
||||
return getIndividual(uri).getMoniker();
|
||||
}
|
||||
|
||||
protected String getName(String uri) {
|
||||
return getIndividual(uri).getName();
|
||||
}
|
||||
|
||||
protected Individual getIndividual(String uri) {
|
||||
return wdf.getIndividualDao().getIndividualByURI(uri);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
|
||||
public class DefaultObjectPropertyDataPostprocessor extends
|
||||
BaseObjectPropertyDataPostprocessor {
|
||||
|
||||
protected String KEY_NAME = "name";
|
||||
protected String KEY_MONIKER = "moniker";
|
||||
protected String KEY_OBJECT = "object";
|
||||
|
||||
public DefaultObjectPropertyDataPostprocessor(ObjectPropertyTemplateModel optm, WebappDaoFactory wdf) {
|
||||
super(optm, wdf);
|
||||
}
|
||||
|
||||
@Override
|
||||
/* Apply processing specific to this postprocessor */
|
||||
protected void process(Map<String, String> map) {
|
||||
addName(map, KEY_NAME, KEY_OBJECT);
|
||||
addMoniker(map, KEY_MONIKER, KEY_OBJECT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
|
||||
public class DefaultObjectPropertyDataPreprocessor extends
|
||||
BaseObjectPropertyDataPreprocessor {
|
||||
|
||||
public DefaultObjectPropertyDataPreprocessor(ObjectPropertyTemplateModel optm, WebappDaoFactory wdf) {
|
||||
super(optm, wdf);
|
||||
}
|
||||
|
||||
@Override
|
||||
/* Apply preprocessing specific to this preprocessor */
|
||||
protected void process(Map<String, String> map) {
|
||||
addName(map);
|
||||
addMoniker(map);
|
||||
}
|
||||
|
||||
private void addName(Map<String, String> map) {
|
||||
String name = map.get("name");
|
||||
if (name == null) {
|
||||
map.put("name", getName(map.get("object")));
|
||||
}
|
||||
}
|
||||
/* This is a temporary measure to handle the fact that the current Individual.getMoniker()
|
||||
* method returns the individual's VClass if moniker is null. We want to replicate that
|
||||
* behavior here, but in future the moniker property (along with other Vitro namespace
|
||||
* properties) will be removed. In addition, this type of logic (display x if it exists, otherwise y)
|
||||
* will be moved into the display modules (Editing and Display Configuration Improvements).
|
||||
*/
|
||||
private void addMoniker(Map<String, String> map) {
|
||||
String moniker = map.get("moniker");
|
||||
if (moniker == null) {
|
||||
map.put("moniker", getMoniker(map.get("object")));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -6,14 +6,14 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* These preprocessors take a list of object property statement data derived from a
|
||||
* These postprocessors take a list of object property statement data derived from a
|
||||
* SPARQL query (or other source) and prepare it for insertion into the template data model.
|
||||
*
|
||||
* @author rjy7
|
||||
*
|
||||
*/
|
||||
|
||||
public interface ObjectPropertyDataPreprocessor {
|
||||
public interface ObjectPropertyDataPostprocessor {
|
||||
|
||||
public void process(List<Map<String, String>> data);
|
||||
|
|
@ -61,18 +61,18 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
}
|
||||
}
|
||||
|
||||
/** Apply preprocessing to query results to prepare for template */
|
||||
protected void preprocess(List<Map<String, String>> data, WebappDaoFactory wdf) {
|
||||
String preprocessorName = config.preprocessor;
|
||||
if (preprocessorName == null) {
|
||||
/** Apply postprocessing to query results to prepare for template */
|
||||
protected void postprocess(List<Map<String, String>> data, WebappDaoFactory wdf) {
|
||||
String postprocessorName = config.postprocessor;
|
||||
if (postprocessorName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Class<?> preprocessorClass = Class.forName(preprocessorName);
|
||||
Constructor<?> constructor = preprocessorClass.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
|
||||
ObjectPropertyDataPreprocessor preprocessor = (ObjectPropertyDataPreprocessor) constructor.newInstance(this, wdf);
|
||||
preprocessor.process(data);
|
||||
Class<?> postprocessorClass = Class.forName(postprocessorName);
|
||||
Constructor<?> constructor = postprocessorClass.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
|
||||
ObjectPropertyDataPostprocessor postprocessor = (ObjectPropertyDataPostprocessor) constructor.newInstance(this, wdf);
|
||||
postprocessor.process(data);
|
||||
} catch (Exception e) {
|
||||
log.error(e, e);
|
||||
}
|
||||
|
@ -85,12 +85,12 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
private static final String NODE_NAME_QUERY = "query";
|
||||
private static final String NODE_NAME_TEMPLATE = "template";
|
||||
private static final String NODE_NAME_COLLATION_TARGET = "collation-target";
|
||||
private static final String NODE_NAME_PREPROCESSOR = "preprocessor";
|
||||
private static final String NODE_NAME_POSTPROCESSOR = "postprocessor";
|
||||
|
||||
private String queryString;
|
||||
private String templateName;
|
||||
private String collationTarget;
|
||||
private String preprocessor;
|
||||
private String postprocessor;
|
||||
|
||||
PropertyListConfig(ObjectProperty op, WebappDaoFactory wdf) throws Exception {
|
||||
|
||||
|
@ -120,7 +120,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
|
|||
templateName = getConfigValue(doc, NODE_NAME_TEMPLATE);
|
||||
// Optional values
|
||||
collationTarget = getConfigValue(doc, NODE_NAME_COLLATION_TARGET);
|
||||
preprocessor = getConfigValue(doc, NODE_NAME_PREPROCESSOR);
|
||||
postprocessor = getConfigValue(doc, NODE_NAME_POSTPROCESSOR);
|
||||
} catch (Exception e) {
|
||||
log.error("Error processing config file " + configFilePath + " for object property " + op.getURI(), e);
|
||||
// What should we do here?
|
||||
|
|
|
@ -26,7 +26,7 @@ public class UncollatedObjectPropertyTemplateModel extends ObjectPropertyTemplat
|
|||
String subjectUri = subject.getURI();
|
||||
String propertyUri = op.getURI();
|
||||
List<Map<String, String>> statementData = opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getQueryString());
|
||||
preprocess(statementData, wdf);
|
||||
postprocess(statementData, wdf);
|
||||
statements = new ArrayList<ObjectPropertyStatementTemplateModel>(statementData.size());
|
||||
for (Map<String, String> map : statementData) {
|
||||
statements.add(new ObjectPropertyStatementTemplateModel(subjectUri, propertyUri, map));
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
<collation-target>object</collation-target>
|
||||
|
||||
<preprocessor>edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPreprocessor</preprocessor>
|
||||
<postprocessor>edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DefaultObjectPropertyDataPostprocessor</postprocessor>
|
||||
|
||||
<template>propStatement-default.ftl</template>
|
||||
</list-view-config>
|
Loading…
Add table
Reference in a new issue