From 7928b3cf52eee405a080f33a139a07e815bf354d Mon Sep 17 00:00:00 2001 From: briancaruso Date: Thu, 14 Jul 2011 21:52:26 +0000 Subject: [PATCH] Adding FileBasedProhibitedFromSearch and related files and directories. NIHVIVO-2914 --- .../search/vitroSearchProhibited.n3 | 23 ++++++++ .../beans/FileBasedProhibitedFromSearch.java | 59 +++++++++++++++++++ .../search/beans/ProhibitedFromSearch.java | 2 +- .../vitro/webapp/search/solr/SolrSetup.java | 12 +++- 4 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 webapp/ontologies/search/vitroSearchProhibited.n3 create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java diff --git a/webapp/ontologies/search/vitroSearchProhibited.n3 b/webapp/ontologies/search/vitroSearchProhibited.n3 new file mode 100644 index 000000000..4b79892ac --- /dev/null +++ b/webapp/ontologies/search/vitroSearchProhibited.n3 @@ -0,0 +1,23 @@ +# $This file is distributed under the terms of the license in /doc/license.txt$ + +@prefix owl: . +@prefix vitroDisplay: . +@prefix rdf: . +@prefix core: . +@prefix example: . + +# All instances of a class can be excluded from the search index +# by adding a vitroDisplay:excludeClass property between +# vitroDisplay:SearchIndex and the URI of the class +# that you would like to exclude. + +# All .n3 files in this directory will be used to configure +# the search exclusions. Only .n3 files will be loaded. +# +# If you would like to add classes to the +# exclusions, add a file to this directory ending in .n3 with +# N3 statements similar to this example. + +# vitroDisplay:SearchIndex +# rdf:type owl:Thing ; +# vitroDisplay:excludeClass example:classToExclude ; \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java new file mode 100644 index 000000000..dcc34c2fc --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java @@ -0,0 +1,59 @@ +package edu.cornell.mannlib.vitro.webapp.search.beans; + +import java.io.File; +import java.io.FileInputStream; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.rdf.model.ModelFactory; + +public class FileBasedProhibitedFromSearch extends ProhibitedFromSearch { + + /** + * Load all the .n3 files in dir, add them to a model and create + * a ProhibitedFromSearch based on that model + * @param URI of the search individual. + * @param dir to find N3 files in. + */ + public FileBasedProhibitedFromSearch(String uri, File dir){ + super( uri, getModelFromDir(dir)); + } + + public FileBasedProhibitedFromSearch(String URI, OntModel model) { + super(URI, model); + } + + protected static OntModel getModelFromDir( File dir){ + if( dir == null ) + throw new IllegalStateException("Must pass a File to FileBasedProhibitedFromSearch"); + if( !dir.isDirectory() ) + throw new IllegalStateException("Parameter dir to FileBasedProhibitedFromSearch " + + "must be a File object for a directory"); + if( !dir.canRead() ) + throw new IllegalStateException("Parameter dir to FileBasedProhibitedFromSearch must " + + "be a directory that is readable, check premissions on " + dir.getAbsolutePath()); + + OntModel modelOnlyForPFS = ModelFactory.createOntologyModel(); + for( File file : dir.listFiles()){ + if( file.isFile() + && file.canRead() + && file.getName() != null + && file.getName().endsWith(".n3")){ + try{ + modelOnlyForPFS.read( new FileInputStream(file), null, "N3"); + }catch( Throwable th){ + log.warn("could not load file " + + file.getAbsolutePath() + file.separator + file.getName(), th); + } + } + } + + if( modelOnlyForPFS.size() == 0 ){ + log.warn("No class exclusion statements found."); + } + + + modelOnlyForPFS.write( System.out , "N3-PP"); + + return modelOnlyForPFS; + } +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearch.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearch.java index 9b74e52a6..6e42e5986 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearch.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearch.java @@ -32,7 +32,7 @@ public class ProhibitedFromSearch implements ClassProhibitedFromSearch{ private static final String queryForProhibitedClasses = "SELECT ?prohibited WHERE{" + "?searchConfig <" + DisplayVocabulary.EXCLUDE_CLASS + "> ?prohibited . }"; - private static final Log log = LogFactory.getLog(ProhibitedFromSearch.class.getName()); + protected static final Log log = LogFactory.getLog(ProhibitedFromSearch.class.getName()); public ProhibitedFromSearch(String URI, OntModel model){ this.ProhibitedFromSearchURI = URI; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/SolrSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/SolrSetup.java index ae323c084..4fb7e8571 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/SolrSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/SolrSetup.java @@ -2,6 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.search.solr; +import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -18,6 +19,7 @@ import org.apache.solr.client.solrj.impl.XMLResponseParser; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.query.Dataset; import com.hp.hpl.jena.query.DatasetFactory; +import com.hp.hpl.jena.tdb.base.file.FileBase; import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; @@ -30,6 +32,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena; import edu.cornell.mannlib.vitro.webapp.search.IndexConstants; import edu.cornell.mannlib.vitro.webapp.search.beans.AdditionalURIsToIndex; +import edu.cornell.mannlib.vitro.webapp.search.beans.FileBasedProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.beans.IndividualProhibitedFromSearchImpl; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.indexing.AdditionalURIsForContextNodes; @@ -84,8 +87,13 @@ public class SolrSetup implements javax.servlet.ServletContextListener{ modifiers.add(new ContextNodeFields(jenaOntModel)); modifiers.add(new NameBoost()); - IndividualToSolrDocument indToSolrDoc = new IndividualToSolrDocument( - new ProhibitedFromSearch(DisplayVocabulary.SEARCH_INDEX_URI, displayOntModel), + // setup probhibited froms earch based on N3 files in the + // directory WEB-INF/ontologies/search + File dir = new File(sce.getServletContext().getRealPath("/WEB-INF/ontologies/search")); + ProhibitedFromSearch pfs = new FileBasedProhibitedFromSearch(DisplayVocabulary.SEARCH_INDEX_URI, dir); + + IndividualToSolrDocument indToSolrDoc = new IndividualToSolrDocument( + pfs, new IndividualProhibitedFromSearchImpl(context), modifiers);