NIHVIVO-2650 Added a field THUMBNAIL_URL that contains the location of the thumbnail of an individual.

This commit is contained in:
deepakkoni 2011-07-26 17:42:13 +00:00
parent b5f1fd70b5
commit c42e4fbcbe
4 changed files with 119 additions and 0 deletions

View file

@ -578,6 +578,9 @@
<field name="modType" type="ignored"/> <field name="modType" type="ignored"/>
<field name="JCLASS" type="ignored"/> <field name="JCLASS" type="ignored"/>
<!-- field for storing locations of thumbnails -->
<field name="THUMBNAIL_URL" type="string" indexed="false" stored="true"/>
<!-- Copy nameLowercase to sortable field. --> <!-- Copy nameLowercase to sortable field. -->
<copyField source="nameLowercase" dest="nameLowercaseSingleValued" /> <copyField source="nameLowercase" dest="nameLowercaseSingleValued" />

View file

@ -67,4 +67,7 @@ public class VitroSearchTermNames {
/** adding phonetic field **/ /** adding phonetic field **/
public static final String ALLTEXT_PHONETIC = "ALLTEXT_PHONETIC"; public static final String ALLTEXT_PHONETIC = "ALLTEXT_PHONETIC";
public static final String NAME_PHONETIC = "NAME_PHONETIC"; public static final String NAME_PHONETIC = "NAME_PHONETIC";
/** download url location for thumbnail */
public static final String THUMBNAIL_URL = "THUMBNAIL_URL";
} }

View file

@ -85,6 +85,7 @@ public class SolrSetup implements javax.servlet.ServletContextListener{
modifiers.add(new CalculateParameters(dataset)); modifiers.add(new CalculateParameters(dataset));
modifiers.add(new ContextNodeFields(jenaOntModel)); modifiers.add(new ContextNodeFields(jenaOntModel));
modifiers.add(new NameBoost()); modifiers.add(new NameBoost());
modifiers.add(new ThumbnailImageURL(jenaOntModel));
// setup probhibited froms earch based on N3 files in the // setup probhibited froms earch based on N3 files in the
// directory WEB-INF/ontologies/search // directory WEB-INF/ontologies/search

View file

@ -0,0 +1,112 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.search.solr;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.shared.Lock;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
public class ThumbnailImageURL implements DocumentModifier {
private static final String prefix = "prefix owl: <http://www.w3.org/2002/07/owl#> "
+ " prefix vitroDisplay: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> "
+ " prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
+ " prefix core: <http://vivoweb.org/ontology/core#> "
+ " prefix foaf: <http://xmlns.com/foaf/0.1/> "
+ " prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
+ " prefix localNav: <http://vitro.mannlib.cornell.edu/ns/localnav#> "
+ " prefix bibo: <http://purl.org/ontology/bibo/> ";
private static final String query = prefix +
" SELECT (str(?thumbnailLocationURL) as ?ThumbnailLocationURL) WHERE { " +
" ?uri <http://vitro.mannlib.cornell.edu/ns/vitro/public#mainImage> ?a . " +
" ?a <http://vitro.mannlib.cornell.edu/ns/vitro/public#downloadLocation> ?b . " +
" ?b <http://vitro.mannlib.cornell.edu/ns/vitro/public#directDownloadUrl> ?thumbnailLocationURL . } ";
private Model model;
private Log log = LogFactory.getLog(ThumbnailImageURL.class);
private boolean shutdown = false;
static VitroSearchTermNames term = new VitroSearchTermNames();
String fieldForThumbnailURL = term.THUMBNAIL_URL;
public ThumbnailImageURL(Model model){
this.model = model;
}
@Override
public void modifyDocument(Individual individual, SolrInputDocument doc,
StringBuffer addUri) throws SkipIndividualException {
//add a field for storing the location of thumbnail for the individual.
doc.addField(fieldForThumbnailURL, runQueryForThumbnailLocation(individual));
}
private String runQueryForThumbnailLocation(Individual individual) {
StringBuffer result = new StringBuffer();
QuerySolutionMap initialBinding = new QuerySolutionMap();
Resource uriResource = ResourceFactory.createResource(individual.getURI());
initialBinding.add("uri", uriResource);
Query sparqlQuery = QueryFactory.create(query, Syntax.syntaxARQ);
model.getLock().enterCriticalSection(Lock.READ);
try{
QueryExecution qExec = QueryExecutionFactory.create(sparqlQuery, model, initialBinding);
try{
ResultSet results = qExec.execSelect();
while(results.hasNext()){
QuerySolution soln = results.nextSolution();
Iterator<String> iter = soln.varNames() ;
while( iter.hasNext()){
String name = iter.next();
RDFNode node = soln.get( name );
if( node != null ){
result.append(" " + node.toString());
}else{
log.info(name + " is null");
}
}
}
}catch(Throwable t){
if( ! shutdown )
log.error(t,t);
} finally{
qExec.close();
}
}finally{
model.getLock().leaveCriticalSection();
}
return result.toString();
}
@Override
public void shutdown() {
shutdown = true;
}
}