NIHVIVO-1193 listener to update externally-linked namespaces

This commit is contained in:
bjl23 2011-01-03 18:15:58 +00:00
parent f03b69f309
commit a5bd1b9faf

View file

@ -6,10 +6,13 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.listeners.StatementListener;
import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.NodeIterator; import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.dao.ApplicationDao; import edu.cornell.mannlib.vitro.webapp.dao.ApplicationDao;
@ -17,12 +20,16 @@ import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
public class ApplicationDaoJena extends JenaBaseDao implements ApplicationDao { public class ApplicationDaoJena extends JenaBaseDao implements ApplicationDao {
Property LINKED_NAMESPACE_PROP = ResourceFactory.createProperty(
VitroVocabulary.DISPLAY + "linkedNamespace");
Integer portalCount = null; Integer portalCount = null;
List<String> externallyLinkedNamespaces = null; List<String> externallyLinkedNamespaces = null;
public ApplicationDaoJena(WebappDaoFactoryJena wadf) { public ApplicationDaoJena(WebappDaoFactoryJena wadf) {
super(wadf); super(wadf);
getOntModelSelector().getDisplayModel().register(
new ExternalNamespacesChangeListener());
} }
@ -44,13 +51,17 @@ public class ApplicationDaoJena extends JenaBaseDao implements ApplicationDao {
return (getFlag2ValueMap().isEmpty()) ? false : true; return (getFlag2ValueMap().isEmpty()) ? false : true;
} }
private static final boolean CLEAR_CACHE = true;
public synchronized List<String> getExternallyLinkedNamespaces() {
return getExternallyLinkedNamespaces(!CLEAR_CACHE);
}
public List<String> getExternallyLinkedNamespaces() { public synchronized List<String> getExternallyLinkedNamespaces(boolean clearCache) {
if (externallyLinkedNamespaces == null) { if (clearCache || externallyLinkedNamespaces == null) {
externallyLinkedNamespaces = new ArrayList<String>(); externallyLinkedNamespaces = new ArrayList<String>();
OntModel ontModel = getOntModelSelector().getDisplayModel(); OntModel ontModel = getOntModelSelector().getDisplayModel();
Property linkedNamespaceProp = ontModel.getProperty(VitroVocabulary.DISPLAY + "linkedNamespace"); NodeIterator nodes = ontModel.listObjectsOfProperty(LINKED_NAMESPACE_PROP);
NodeIterator nodes = ontModel.listObjectsOfProperty(linkedNamespaceProp);
while (nodes.hasNext()) { while (nodes.hasNext()) {
RDFNode node = nodes.next(); RDFNode node = nodes.next();
if (node.isLiteral()) { if (node.isLiteral()) {
@ -68,4 +79,29 @@ public class ApplicationDaoJena extends JenaBaseDao implements ApplicationDao {
return externallyLinkedNamespaces; return externallyLinkedNamespaces;
} }
private class ExternalNamespacesChangeListener extends StatementListener {
@Override
public void addedStatement(Statement stmt) {
process(stmt);
}
@Override
public void removedStatement(Statement stmt) {
process(stmt);
}
//We could also listen for end-of-edit events,
//but there should be so few of these statments that
//it won't be very expensive to run this method multiple
//times when the model is updated.
private void process(Statement stmt) {
if (stmt.getPredicate().equals(LINKED_NAMESPACE_PROP)) {
getExternallyLinkedNamespaces(CLEAR_CACHE);
}
}
}
} }