diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java index c0da4af5f..a77910da2 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCache.java @@ -16,11 +16,13 @@ import javax.servlet.ServletContextListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.hp.hpl.jena.ontology.Individual; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.rdf.listeners.StatementListener; import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.shared.Lock; +import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDF; import com.hp.hpl.jena.vocabulary.RDFS; @@ -208,6 +210,24 @@ public class VClassGroupCache { return groups; } + protected static boolean isClassNameChange(Statement stmt, OntModel jenaOntModel) { + // Check if the stmt is a rdfs:label change and that the + // subject is an owl:Class. + if( RDFS.label.equals( stmt.getPredicate() )) { + jenaOntModel.enterCriticalSection(Lock.READ); + try{ + return jenaOntModel.contains( + ResourceFactory.createStatement( + ResourceFactory.createResource(stmt.getSubject().getURI()), + RDF.type, + OWL.Class)); + }finally{ + jenaOntModel.leaveCriticalSection(); + } + }else{ + return false; + } + } /* ******************** RebuildGroupCacheThread **************** */ @@ -310,7 +330,7 @@ public class VClassGroupCache { } /* ****************** Jena Model Change Listener***************************** */ - private class VClassGroupCacheChangeListener extends StatementListener { + protected class VClassGroupCacheChangeListener extends StatementListener { public void addedStatement(Statement stmt) { checkAndDoUpdate(stmt); } @@ -332,26 +352,13 @@ public class VClassGroupCache { requestCacheUpdate(); } else if(VitroVocabulary.DISPLAY_RANK.equals(stmt.getPredicate().getURI())){ requestCacheUpdate(); - } else if( isVClassGroupNameChange(stmt) ) { - requestCacheUpdate(); - } - } - - protected boolean isVClassGroupNameChange(Statement stmt) { - // Check if the stmt is a rdfs:label change and that the - // subject is a VClassGroup. - if( RDFS.label.equals( stmt.getPredicate() )) { + } else { OntModel jenaOntModel = ModelContext.getJenaOntModel(context); - jenaOntModel.enterCriticalSection(Lock.READ); - try{ - return jenaOntModel.contains(stmt.getSubject(), RDF.type, ResourceFactory.createResource(VitroVocabulary.CLASSGROUP)); - }finally{ - jenaOntModel.leaveCriticalSection(); + if( isClassNameChange(stmt, jenaOntModel) ) { + requestCacheUpdate(); } - }else{ - return false; } - } + } } /* ******************** ServletContextListener **************** */ diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCacheTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCacheTest.java new file mode 100644 index 000000000..782461ce2 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/VClassGroupCacheTest.java @@ -0,0 +1,102 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.dao.jena; + +import java.io.StringReader; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.vocabulary.RDFS; + +public class VClassGroupCacheTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testIsVClassGroupNameChange() { + + + //protected static boolean isVClassGroupNameChange(Statement stmt, OntModel jenaOntModel) { + + String rdf = + "core:Summer \n" + + " a owl:Class ; \n" + + " rdfs:label \"Spring and toast 3\"@en-US ; \n" + + " rdfs:subClassOf owl:Thing ; \n" + + " vitro:descriptionAnnot \n" + + " \"cgaa\"^^xsd:string ; \n" + + " vitro:displayLimitAnnot \n" + + " \"-1\"^^xsd:int ; \n" + + " vitro:displayRankAnnot \n" + + " \"-1\"^^xsd:int ; \n" + + " vitro:exampleAnnot \"serefs\"^^xsd:string ; \n" + + " vitro:hiddenFromDisplayBelowRoleLevelAnnot \n" + + " ; \n" + + " vitro:inClassGroup ; \n" + + " vitro:prohibitedFromUpdateBelowRoleLevelAnnot \n" + + " ; \n" + + " vitro:shortDef \"sfsfe\"^^xsd:string ; \n" + + " owl:equivalentClass core:Summer . "; + + OntModel om = ModelFactory.createOntologyModel(); + om.read( new StringReader( prefixes + rdf) , null , "N3"); + + Statement stmt = ResourceFactory.createStatement( + ResourceFactory.createResource("http://vivoweb.org/ontology/core#Summer"), + RDFS.label, + ResourceFactory.createPlainLiteral("some old label")); + + boolean isNameChange = VClassGroupCache.isClassNameChange(stmt, om); + Assert.assertTrue("Expected it to be a name change but it wasn't.", isNameChange); + + stmt = ResourceFactory.createStatement( + ResourceFactory.createResource("http://vivoweb.org/ontology/core#bogus"), + ResourceFactory.createProperty("http://example.com/nonLabelProperty"), + ResourceFactory.createPlainLiteral("some old label")); + + boolean notNameChange = ! VClassGroupCache.isClassNameChange(stmt, om); + Assert.assertTrue("Expected it to NOT be a name change but it was.", notNameChange); + + stmt = ResourceFactory.createStatement( + ResourceFactory.createResource("http://vivoweb.org/ontology/core#bogus"), + RDFS.label, + ResourceFactory.createPlainLiteral("some old label")); + + notNameChange = ! VClassGroupCache.isClassNameChange(stmt, om); + Assert.assertTrue("Expected it to NOT be a name change but it was.", notNameChange); + } + + static final String prefixes = + "@prefix dc: . \n" + + "@prefix pvs: . \n" + + "@prefix geo: . \n" + + "@prefix foaf: . \n" + + "@prefix scires: . \n" + + "@prefix scripps: . \n" + + "@prefix dcterms: . \n" + + "@prefix rdfs: . \n" + + "@prefix swrl: . \n" + + "@prefix vitro: . \n" + + "@prefix event: . \n" + + "@prefix bibo: . \n" + + "@prefix xsd: . \n" + + "@prefix owl: . \n" + + "@prefix swrlb: . \n" + + "@prefix rdf: . \n" + + "@prefix core: . \n" + + "@prefix skos: . \n" + + "@prefix vivo: . \n" + + "@prefix dcelem: . \n" + + "@prefix ero: . \n" + + " \n" ; + + +}