diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/IndividualProhibitedFromSearch.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/IndividualProhibitedFromSearch.java new file mode 100644 index 000000000..090319fc5 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/IndividualProhibitedFromSearch.java @@ -0,0 +1,67 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.search.beans; + +import javax.servlet.ServletContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.shared.Lock; +import com.hp.hpl.jena.vocabulary.OWL; +import com.hp.hpl.jena.vocabulary.RDF; + +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; + +public class IndividualProhibitedFromSearch { + + protected OntModel fullModel; + + protected static Log log = LogFactory.getLog(IndividualProhibitedFromSearch.class); + + + public IndividualProhibitedFromSearch( ServletContext context ){ + this.fullModel = ModelContext.getUnionOntModelSelector(context).getFullModel(); + } + + public boolean isIndividualProhibited(String uri){ + if( uri == null || uri.isEmpty() ) + return true; + + boolean prohibited = false; + try { + fullModel.getLock().enterCriticalSection(Lock.READ); + Query query = makeAskQueryForUri( uri ); + prohibited = QueryExecutionFactory.create( query, fullModel).execAsk(); + } finally { + fullModel.getLock().leaveCriticalSection(); + } + if( prohibited ) + log.debug("prohibited " + uri); + + return prohibited; + } + + private Query makeAskQueryForUri( String uri ){ + String queryString = + "PREFIX fn: \n" + + "ASK { \n" + + " <"+uri+"> <" + RDF.type.getURI() + "> ?type . \n" + + " FILTER ( \n" + + " ( fn:starts-with( str(?type), \"" + VitroVocabulary.vitroURI + "\" ) \n" + + " && \n"+ + " ! fn:starts-with( str(?type), \"" + VitroVocabulary.vitroURI + "Flag\" ) ) || \n" + + " fn:starts-with( str(?type), \"" + VitroVocabulary.PUBLIC + "\" ) || \n" + + " str(?type) = \"" + OWL.ObjectProperty.getURI() + "\" || \n" + + " str(?type) = \"" + OWL.DatatypeProperty.getURI() + "\" || \n" + + " str(?type) = \"" + OWL.AnnotationProperty.getURI() + "\" \n" + + " )\n" + + "}" ; + return QueryFactory.create( queryString ); + } +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/Entity2LuceneDoc.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/Entity2LuceneDoc.java index 059d4c5c7..d188cbff0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/Entity2LuceneDoc.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/Entity2LuceneDoc.java @@ -2,9 +2,6 @@ package edu.cornell.mannlib.vitro.webapp.search.lucene; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -24,9 +21,9 @@ import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.search.IndexingException; +import edu.cornell.mannlib.vitro.webapp.search.beans.IndividualProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.docbuilder.Obj2DocIface; -import edu.cornell.mannlib.vitro.webapp.utils.FlagMathUtils; /** * This class expect that Entities passed to it will have @@ -88,7 +85,17 @@ public class Entity2LuceneDoc implements Obj2DocIface{ public static VitroLuceneTermNames term = new VitroLuceneTermNames(); private static String entClassName = Individual.class.getName(); - private ProhibitedFromSearch classesProhibitedFromSearch = null; + + private ProhibitedFromSearch classesProhibitedFromSearch; + + private IndividualProhibitedFromSearch individualProhibited; + + public Entity2LuceneDoc( + ProhibitedFromSearch classesProhibitedFromSearch, + IndividualProhibitedFromSearch individualProhibited){ + this.classesProhibitedFromSearch = classesProhibitedFromSearch; + this.individualProhibited = individualProhibited; + } public boolean canTranslate(Object obj) { return (obj != null && obj instanceof Individual); @@ -118,6 +125,11 @@ public class Entity2LuceneDoc implements Obj2DocIface{ return null; } + //filter out class groups, owl:ObjectProperties etc. + if( individualProhibited.isIndividualProhibited( id ) ){ + return null; + } + /* Types and ClassGroup */ boolean prohibited = false; List vclasses = ent.getVClasses(false); @@ -130,12 +142,7 @@ public class Entity2LuceneDoc implements Obj2DocIface{ } else if ( clz.getURI().startsWith( OWL.NS ) ){ log.debug("not indexing " + id + " because of type " + clz.getURI()); return null; - }else if( clz.getURI().startsWith( VitroVocabulary.vitroURI ) - || clz.getURI().startsWith( VitroVocabulary.VITRO_PUBLIC ) - || clz.getURI().startsWith( VitroVocabulary.PSEUDO_BNODE_NS) ){ - log.debug("not indexing " + id + " because of type " + clz.getURI()); - return null; - }else{ + }else{ if( !prohibited && classesProhibitedFromSearch.isClassProhibited(clz.getURI()) ) prohibited = true; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetup.java index 3ef35259c..3154abd44 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetup.java @@ -33,6 +33,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilterUtils; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; import edu.cornell.mannlib.vitro.webapp.dao.jena.SearchReindexingListener; +import edu.cornell.mannlib.vitro.webapp.search.beans.IndividualProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.beans.ObjectSourceIface; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder; @@ -99,11 +100,13 @@ public class LuceneSetup implements javax.servlet.ServletContextListener { // This will attempt to create a new directory and empty index if there is none. LuceneIndexer indexer = new LuceneIndexer(getBaseIndexDirName(),liveIndexDir, null, getAnalyzer()); context.setAttribute(ANALYZER, getAnalyzer()); - Entity2LuceneDoc translator = new Entity2LuceneDoc(); + OntModel displayOntModel = (OntModel) sce.getServletContext().getAttribute("displayOntModel"); - translator.setClassesProhibitedFromSearch( - new ProhibitedFromSearch(DisplayVocabulary.PRIMARY_LUCENE_INDEX_URI, displayOntModel)); + Entity2LuceneDoc translator = new Entity2LuceneDoc( + new ProhibitedFromSearch(DisplayVocabulary.PRIMARY_LUCENE_INDEX_URI, displayOntModel), + new IndividualProhibitedFromSearch(context) ); indexer.addObj2Doc(translator); + context.setAttribute(LuceneIndexer.class.getName(), indexer); indexer.setLuceneIndexFactory(lif); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetupCJK.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetupCJK.java index c6e840ea0..13c406603 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetupCJK.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/lucene/LuceneSetupCJK.java @@ -21,12 +21,15 @@ import com.hp.hpl.jena.ontology.OntModel; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilterUtils; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; import edu.cornell.mannlib.vitro.webapp.dao.jena.SearchReindexingListener; +import edu.cornell.mannlib.vitro.webapp.search.beans.IndividualProhibitedFromSearch; +import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder; /** @@ -82,8 +85,14 @@ public class LuceneSetupCJK implements javax.servlet.ServletContextListener { //here we want to put the LuceneIndex object into the application scope LuceneIndexer indexer = new LuceneIndexer(indexDir, liveIndexDir, null, getAnalyzer()); - context.setAttribute(LuceneSetup.ANALYZER, getAnalyzer()); - indexer.addObj2Doc(new Entity2LuceneDoc()); + context.setAttribute(LuceneSetup.ANALYZER, getAnalyzer()); + + OntModel displayOntModel = (OntModel) sce.getServletContext().getAttribute("displayOntModel"); + Entity2LuceneDoc translator = new Entity2LuceneDoc( + new ProhibitedFromSearch(DisplayVocabulary.PRIMARY_LUCENE_INDEX_URI, displayOntModel), + new IndividualProhibitedFromSearch(context) ); + indexer.addObj2Doc(translator); + indexer.setLuceneIndexFactory(lif); //This is where the builder gets the list of places to try to