NIHVIVO-1341 Refactoring in object property data postprocessors

This commit is contained in:
rjy7 2010-12-20 01:41:42 +00:00
parent 87196d7152
commit 9709632f61
8 changed files with 100 additions and 100 deletions

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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")));
}
}
}

View file

@ -6,14 +6,14 @@ import java.util.List;
import java.util.Map; 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. * SPARQL query (or other source) and prepare it for insertion into the template data model.
* *
* @author rjy7 * @author rjy7
* *
*/ */
public interface ObjectPropertyDataPreprocessor { public interface ObjectPropertyDataPostprocessor {
public void process(List<Map<String, String>> data); public void process(List<Map<String, String>> data);

View file

@ -61,18 +61,18 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
} }
} }
/** Apply preprocessing to query results to prepare for template */ /** Apply postprocessing to query results to prepare for template */
protected void preprocess(List<Map<String, String>> data, WebappDaoFactory wdf) { protected void postprocess(List<Map<String, String>> data, WebappDaoFactory wdf) {
String preprocessorName = config.preprocessor; String postprocessorName = config.postprocessor;
if (preprocessorName == null) { if (postprocessorName == null) {
return; return;
} }
try { try {
Class<?> preprocessorClass = Class.forName(preprocessorName); Class<?> postprocessorClass = Class.forName(postprocessorName);
Constructor<?> constructor = preprocessorClass.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class); Constructor<?> constructor = postprocessorClass.getConstructor(ObjectPropertyTemplateModel.class, WebappDaoFactory.class);
ObjectPropertyDataPreprocessor preprocessor = (ObjectPropertyDataPreprocessor) constructor.newInstance(this, wdf); ObjectPropertyDataPostprocessor postprocessor = (ObjectPropertyDataPostprocessor) constructor.newInstance(this, wdf);
preprocessor.process(data); postprocessor.process(data);
} catch (Exception e) { } catch (Exception e) {
log.error(e, 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_QUERY = "query";
private static final String NODE_NAME_TEMPLATE = "template"; private static final String NODE_NAME_TEMPLATE = "template";
private static final String NODE_NAME_COLLATION_TARGET = "collation-target"; 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 queryString;
private String templateName; private String templateName;
private String collationTarget; private String collationTarget;
private String preprocessor; private String postprocessor;
PropertyListConfig(ObjectProperty op, WebappDaoFactory wdf) throws Exception { PropertyListConfig(ObjectProperty op, WebappDaoFactory wdf) throws Exception {
@ -120,7 +120,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
templateName = getConfigValue(doc, NODE_NAME_TEMPLATE); templateName = getConfigValue(doc, NODE_NAME_TEMPLATE);
// Optional values // Optional values
collationTarget = getConfigValue(doc, NODE_NAME_COLLATION_TARGET); collationTarget = getConfigValue(doc, NODE_NAME_COLLATION_TARGET);
preprocessor = getConfigValue(doc, NODE_NAME_PREPROCESSOR); postprocessor = getConfigValue(doc, NODE_NAME_POSTPROCESSOR);
} 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?

View file

@ -26,7 +26,7 @@ public class UncollatedObjectPropertyTemplateModel extends ObjectPropertyTemplat
String subjectUri = subject.getURI(); String subjectUri = subject.getURI();
String propertyUri = op.getURI(); String propertyUri = op.getURI();
List<Map<String, String>> statementData = opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getQueryString()); List<Map<String, String>> statementData = opDao.getObjectPropertyStatementsForIndividualByProperty(subjectUri, propertyUri, getQueryString());
preprocess(statementData, wdf); postprocess(statementData, wdf);
statements = new ArrayList<ObjectPropertyStatementTemplateModel>(statementData.size()); statements = new ArrayList<ObjectPropertyStatementTemplateModel>(statementData.size());
for (Map<String, String> map : statementData) { for (Map<String, String> map : statementData) {
statements.add(new ObjectPropertyStatementTemplateModel(subjectUri, propertyUri, map)); statements.add(new ObjectPropertyStatementTemplateModel(subjectUri, propertyUri, map));

View file

@ -14,7 +14,7 @@
<collation-target>object</collation-target> <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> <template>propStatement-default.ftl</template>
</list-view-config> </list-view-config>