From d61709730ddac0df7d2b81a2bdf2c12a92a336f8 Mon Sep 17 00:00:00 2001 From: j2blake Date: Sat, 14 Jun 2014 11:04:59 -0400 Subject: [PATCH] VIVO 767 Listen for changes to VCards and re-index. --- .../search/indexing/AdditionalUriFinders.java | 1 + .../indexing/AdditionalUrisForVCards.java | 122 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUrisForVCards.java diff --git a/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUriFinders.java b/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUriFinders.java index 727c545a..41bcf522 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUriFinders.java +++ b/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUriFinders.java @@ -23,6 +23,7 @@ public class AdditionalUriFinders { uriFinders.add(new AdditionalURIsForContextNodes(rdfService)); uriFinders.add(new AdditionalURIsForTypeStatements()); uriFinders.add(new URIsForClassGroupChange(indDao)); + uriFinders.add(new AdditionalUrisForVCards(rdfService)); return uriFinders; } diff --git a/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUrisForVCards.java b/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUrisForVCards.java new file mode 100644 index 00000000..c3ac4c31 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/search/indexing/AdditionalUrisForVCards.java @@ -0,0 +1,122 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.search.indexing; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +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.QuerySolutionMap; +import com.hp.hpl.jena.query.ResultSet; +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.rdf.model.Statement; + +import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.search.beans.StatementToURIsToUpdate; + +/** + * If the property of a VCard object is changed, we should re-index the owner of + * that VCard. + */ +public class AdditionalUrisForVCards implements StatementToURIsToUpdate { + private static final Log log = LogFactory + .getLog(AdditionalUrisForVCards.class); + + private final String QUERY_FOR_RELATED = "" // + + "prefix vcard: \n" + + "prefix obo: \n" + + "SELECT ?uri WHERE { \n" + + " ?subject a vcard:Identification . \n " + + " ?contactInfo ?p ?subject . \n " + + " ?uri obo:ARG_2000028 ?contactInfo . \n " // + + "}"; + + private final RDFService rdfService; + + public AdditionalUrisForVCards(RDFService rdfService) { + this.rdfService = rdfService; + } + + @Override + public void startIndexing() { + // Nothing to set up. + } + + @Override + public List findAdditionalURIsToIndex(Statement stmt) { + if (stmt == null) { + return Collections.emptyList(); + } + + RDFNode sNode = stmt.getSubject(); + if (sNode == null) { + log.warn("subject of modified statement was null."); + return Collections.emptyList(); + } + + if (!sNode.isURIResource()) { + return Collections.emptyList(); + } + + Resource subject = sNode.asResource(); + String uri = subject.getURI(); + if (uri == null) { + log.warn("subject of modified statement had a null URI."); + return Collections.emptyList(); + } + + return ownersOfVCard(uri); + } + + /** + * If the subject of the statement is a vcard:Identification, then we also + * want to index the owner of the vcard:ContactInfo that references this + * vcard:Identification. + * + * vcard:Identification is a superclass of vcard:Name, vcard:Email, + * vcard:Telephone, vcard:Address, and vcard:URL. + * + * @see https://wiki.duraspace.org/display/VIVO/VCard+usage+diagram + */ + private List ownersOfVCard(String subjectUri) { + List additionalUris = new ArrayList<>(); + + QuerySolutionMap initialBinding = new QuerySolutionMap(); + Resource subjectResource = ResourceFactory.createResource(subjectUri); + initialBinding.add("subject", subjectResource); + + ResultSet results = QueryUtils.getQueryResults(QUERY_FOR_RELATED, + initialBinding, rdfService); + + while (results.hasNext()) { + QuerySolution soln = results.nextSolution(); + RDFNode node = soln.get("uri"); + if (node != null) { + if (node.isURIResource()) { + additionalUris.add(node.asResource().getURI()); + } else { + log.warn("value from query for 'uri'" + + " was not a URIResource, it was " + node); + } + } else { + log.warn("value for query for 'uri' was null"); + } + } + + return additionalUris; + } + + @Override + public void endIndxing() { + // Nothing to tear down. + } + +}