VIVO-870 Move the DocumentModifiers into the configuration.

This commit is contained in:
j2blake 2015-01-16 16:53:43 -05:00
parent 2d2c5032cc
commit 20bf51d7c9
6 changed files with 132 additions and 397 deletions

View file

@ -1,145 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.searchindex.documentBuilding;
import static edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames.ALLTEXT;
import static edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames.ALLTEXTUNSTEMMED;
import static edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames.PREFERRED_TITLE;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ContextModelAccess;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchInputDocument;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.ContextModelsUser;
/**
* If there are any VCards on this Individual with Title objects, store the text
* in the Preferred Title search field, and the ALL_TEXT field.
*
* If there are any VCards on this Individual with EMail objects, store the text
* in the ALL_TEXT field.
*/
public class VIVOValuesFromVcards implements DocumentModifier, ContextModelsUser {
private static final Log log = LogFactory
.getLog(VIVOValuesFromVcards.class);
private static final String PREFERRED_TITLE_QUERY = ""
+ "prefix vcard: <http://www.w3.org/2006/vcard/ns#> \n"
+ "prefix obo: <http://purl.obolibrary.org/obo/> \n\n"
+ "SELECT ?title WHERE { \n" //
+ " ?uri obo:ARG_2000028 ?card . \n"
+ " ?card a vcard:Individual . \n"
+ " ?card vcard:hasTitle ?titleHolder . \n"
+ " ?titleHolder vcard:title ?title . \n" //
+ "}";
private static final ResultParser PREFERRED_TITLE_PARSER = new ResultParser() {
@Override
public void parse(String uri, QuerySolution solution, SearchInputDocument doc) {
String title = getLiteralValue(solution, "title");
if (StringUtils.isNotBlank(title)) {
doc.addField(PREFERRED_TITLE, title);
doc.addField(ALLTEXT, title);
doc.addField(ALLTEXTUNSTEMMED, title);
log.debug("Preferred Title for " + uri + ": '" + title + "'");
}
}
};
private static final String EMAIL_QUERY = ""
+ "prefix vcard: <http://www.w3.org/2006/vcard/ns#> \n"
+ "prefix obo: <http://purl.obolibrary.org/obo/> \n\n"
+ "SELECT ?email WHERE { \n" //
+ " ?uri obo:ARG_2000028 ?card . \n"
+ " ?card a vcard:Individual . \n"
+ " ?card vcard:hasEmail ?emailHolder . \n"
+ " ?emailHolder vcard:email ?email . \n" //
+ "}";
private static final ResultParser EMAIL_PARSER = new ResultParser() {
@Override
public void parse(String uri, QuerySolution solution,
SearchInputDocument doc) {
String email = getLiteralValue(solution, "email");
if (StringUtils.isNotBlank(email)) {
doc.addField(ALLTEXT, email);
doc.addField(ALLTEXTUNSTEMMED, email);
log.debug("Email for " + uri + ": '" + email + "'");
}
}};
private RDFService rdfService;
private boolean shutdown = false;
@Override
public void setContextModels(ContextModelAccess models) {
this.rdfService = models.getRDFService();
}
@Override
public void modifyDocument(Individual individual, SearchInputDocument doc) {
if (individual == null)
return;
processQuery(individual, PREFERRED_TITLE_QUERY, PREFERRED_TITLE_PARSER,
doc);
processQuery(individual, EMAIL_QUERY, EMAIL_PARSER, doc);
}
private void processQuery(Individual individual, String queryTemplate,
ResultParser resultParser, SearchInputDocument doc) {
String uri = "<" + individual.getURI() + "> ";
String query = queryTemplate.replaceAll("\\?uri", uri);
try {
ResultSet results = RDFServiceUtils.sparqlSelectQuery(query,
rdfService);
if (results != null) {
while (results.hasNext()) {
log.debug("Next solution");
QuerySolution solution = results.nextSolution();
resultParser.parse(uri, solution, doc);
}
}
} catch (Exception e) {
if (!shutdown) {
log.error("problem while running query '" + query + "'", e);
}
}
}
@Override
public void shutdown() {
shutdown = true;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "[]";
}
private abstract static class ResultParser {
public abstract void parse(String uri, QuerySolution solution, SearchInputDocument doc);
String getLiteralValue(QuerySolution solution, String name) {
RDFNode node = solution.get(name);
if ((node != null) && (node.isLiteral())) {
String value = node.asLiteral().getString();
if (StringUtils.isNotBlank(value)) {
return value;
}
}
return "";
}
}
}

View file

@ -1,52 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.searchindex.documentBuilding;
import java.util.ArrayList;
import java.util.List;
/**
* DocumentModifier for adding rdfs:labels of individuals related via
* a advising relationship.
*
* @author bdc34
*
*/
public class VivoISFAdvisingFields extends ContextNodeFields {
private static String VIVONS = "http://vivoweb.org/ontology/core#";
protected static final String prefix =
" prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+ " prefix core: <" + VIVONS + "> \n"
+ " prefix foaf: <http://xmlns.com/foaf/0.1/> \n"
+ " prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ " prefix obo: <http://purl.obolibrary.org/obo/> \n" ;
static List<String> queries = new ArrayList<String>();
static{
queries.add( makeQueryForPeople() );
}
public VivoISFAdvisingFields(){
super(queries);
}
/**
* This query will get all the labels for the other
* person in the relationship.
*/
private static String makeQueryForPeople(){
return prefix +
"SELECT \n" +
"(str(?rawresult) as ?result) WHERE \n" +
"{\n" +
" ?uri core:relatedBy ?rel . \n" +
" ?rel rdf:type core:AdvisingRelationship . \n" +
" ?rel core:relates ?other . \n" +
" ?other rdfs:label ?rawresult . \n" +
" FILTER( ?other != ?uri ) \n" +
"}";
}
}

View file

@ -1,69 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.searchindex.documentBuilding;
import java.util.ArrayList;
import java.util.List;
/**
* DocumentModifier to populate Search index fields for the basic ISF relationships.
*
* This will add the all rdfs:labels of the related individuals to the search document.
*
* @author bdc34
*/
public class VivoISFBasicFields extends ContextNodeFields {
private static String VIVONS = "http://vivoweb.org/ontology/core#";
/**
* Subtypes of vivo:Relationship that get handled by this class.
*/
private static String[] RELATIONSHIP_TYPES = {
VIVONS + "Position",
VIVONS + "Authorship",
VIVONS + "Collaboration",
VIVONS + "Affiliation"
};
static List<String> queries = new ArrayList<String>();
public VivoISFBasicFields(){
super(queries);
}
protected static final String prefix =
" prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+ " prefix core: <" + VIVONS + "> \n"
+ " prefix foaf: <http://xmlns.com/foaf/0.1/> \n"
+ " prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ " prefix obo: <http://purl.obolibrary.org/obo/> \n" ;
static {
/* make a string of the RELATIONSHIP_TYPES for use in
* a SPARQL IN clause */
String types = "";
for( int i = 0 ; i < RELATIONSHIP_TYPES.length ; i++ ){
types += "<" + RELATIONSHIP_TYPES[i] + ">";
if( i != RELATIONSHIP_TYPES.length - 1 ){
types += ", ";
}
}
queries.add(
prefix +
"SELECT \n" +
"(str(?rawresult) as ?result) WHERE \n" +
"{\n" +
" ?uri core:relatedBy ?rel . \n" +
" ?rel rdf:type ?type . \n" +
" ?rel core:relates ?other . \n" +
" ?other rdfs:label ?rawresult . \n" +
" FILTER ( ?type IN ( " + types + " ) )\n" +
"}" );
}
}

View file

@ -1,55 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.searchindex.documentBuilding;
import java.util.ArrayList;
import java.util.List;
public class VivoISFEducationFields extends ContextNodeFields {
private static String VIVONS = "http://vivoweb.org/ontology/core#";
protected static final String prefix =
" prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+ " prefix core: <" + VIVONS + "> \n"
+ " prefix foaf: <http://xmlns.com/foaf/0.1/> \n"
+ " prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ " prefix obo: <http://purl.obolibrary.org/obo/> \n" ;
public VivoISFEducationFields(){
super(queries);
}
/**
* This query will get all the labels for the degree.
*/
private static String queryForDegree =
prefix +
"SELECT \n" +
"(str(?rawresult) as ?result) WHERE \n" +
"{\n" +
" ?uri core:relates ?deg . \n" +
" ?deg rdf:type core:AwardedDegree . \n" +
" ?deg rdfs:label ?rawresult . \n" +
"}";
/**
* This will get all labels for the organization.
*/
private static String queryForOrganization =
prefix +
"SELECT \n" +
"(str(?rawresult) as ?result) WHERE \n" +
"{\n" +
" ?uri core:relates ?deg . \n" +
" ?deg rdf:type core:AwardedDegree . \n" +
" ?deg core:assignedBy ?org . \n" +
" ?org rdfs:label ?rawresult . \n" +
"}";
static List<String> queries = new ArrayList<String>();
static{
queries.add( queryForDegree );
queries.add( queryForOrganization );
}
}

View file

@ -1,59 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.searchindex.documentBuilding;
import java.util.ArrayList;
import java.util.List;
/**
* This class will:
* add people's names to organization's search Documents.
* add organization names to people's search Documents.
*
* @author bdc34
*
*/
public class VivoISFMemberFields extends ContextNodeFields {
private static String VIVONS = "http://vivoweb.org/ontology/core#";
protected static final String prefix =
" prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+ " prefix core: <" + VIVONS + "> \n"
+ " prefix foaf: <http://xmlns.com/foaf/0.1/> \n"
+ " prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ " prefix obo: <http://purl.obolibrary.org/obo/> \n" ;
public VivoISFMemberFields(){
super(queries);
}
/**
* Add people's names to organization's search Documents.
*/
private static String peopleForOrganization =
prefix +
"SELECT (str(?rawresult) as ?result) WHERE {\n" +
" ?uri rdf:type foaf:Organization . \n" +
" ?role core:roleContributesTo ?uri . \n" +
" ?person obo:RO_0000053 ?role . \n" +
" ?person rdfs:label ?rawresult .\n" +
"}";
/**
* add organization names to people's search Documents.
*/
private static String organizationForPeople =
prefix +
"SELECT (str(?rawresult) as ?result) WHERE {\n" +
" ?uri rdf:type foaf:Person . \n" +
" ?uri obo:RO_0000053 / core:roleContributesTo / rdfs:label ?rawresult . \n" +
"}";
static List<String> queries = new ArrayList<String>();
static{
queries.add( peopleForOrganization );
queries.add( organizationForPeople );
}
}