From 89ea62af477ddee515187688e33ef29784f1a850 Mon Sep 17 00:00:00 2001 From: j2blake Date: Sun, 6 Nov 2011 15:47:13 +0000 Subject: [PATCH] NIHVIVO-2343 Implement filtering by search term. --- .../ProxyRelationshipSelector.java | 19 ++++++- .../ProxyRelationshipSelectorTest.java | 56 ++++++++++--------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java index 2601ffd73..233d2d238 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java @@ -13,6 +13,7 @@ import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import edu.cornell.mannlib.vitro.webapp.controller.AbstractPagingSelector; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionBuilder.ItemInfo; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionBuilder.Relationship; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionCriteria.ProxyRelationshipView; @@ -23,7 +24,7 @@ import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner.QueryParser; * A class which will accept a ProxyRelationshipSelectionCriteria and produce a * ProxyRelationshipSelection. */ -public class ProxyRelationshipSelector { +public class ProxyRelationshipSelector extends AbstractPagingSelector { private static final Log log = LogFactory .getLog(ProxyRelationshipSelector.class); @@ -84,11 +85,14 @@ public class ProxyRelationshipSelector { + " auth:firstName ?firstName ; \n" // + " auth:lastName ?lastName ; \n" // + " auth:proxyEditorFor ?profile . \n" // + + " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" // + + " %filterClause% \n" // + "} \n"; // private void figureTotalResultCount() { String qString = COUNT_QUERY_TEMPLATE.replace("%prefixes%", PREFIX_LINES); + qString = replaceFilterClauses(qString); int count = new SparqlQueryRunner(context.userAccountsModel, new CountQueryParser()).executeQuery(qString); @@ -97,6 +101,17 @@ public class ProxyRelationshipSelector { builder.count = count; } + private String replaceFilterClauses(String q) { + String searchTerm = criteria.getSearchTerm(); + if (searchTerm.isEmpty()) { + return q.replace("%filterClause%", ""); + } else { + String clean = escapeForRegex(searchTerm); + return q.replace("%filterClause%", + "FILTER (REGEX(str(?label), '^" + clean + "', 'i'))"); + } + } + private static final String QUERY_PROXY_BASICS = "" // + "%prefixes% \n" // + "SELECT DISTINCT ?uri ?label ?externalAuthId \n" // @@ -107,6 +122,7 @@ public class ProxyRelationshipSelector { + " auth:proxyEditorFor ?profile . \n" // + " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" // + " OPTIONAL { ?uri auth:externalAuthId ?externalAuthId } \n" // + + " %filterClause% \n" // + "} \n" // + "ORDER BY ASC(?lastName) ASC(?firstName) \n" // + "LIMIT %perPage% \n" // @@ -118,6 +134,7 @@ public class ProxyRelationshipSelector { .replace("%perPage%", String.valueOf(criteria.getRelationshipsPerPage())) .replace("%offset%", offset()); + qString = replaceFilterClauses(qString); List relationships = new SparqlQueryRunner>( context.userAccountsModel, new ProxyBasicsParser()) diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelectorTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelectorTest.java index f451af309..9656859a1 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelectorTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelectorTest.java @@ -16,6 +16,7 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.log4j.Level; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -28,6 +29,7 @@ import com.hp.hpl.jena.rdf.model.ModelFactory; import edu.cornell.mannlib.vitro.testing.AbstractTestClass; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionCriteria.ProxyRelationshipView; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelector.Context; +import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner; /** * TODO @@ -144,7 +146,6 @@ public class ProxyRelationshipSelectorTest extends AbstractTestClass { @Test public void checkAllFieldsOnFirstRelationshipByProxy() { - // setLoggerLevel(SparqlQueryRunner.class, Level.DEBUG); selectOnCriteria(1, 1, BY_PROXY, ""); log.debug("SELECTION: " + selection); assertExpectedCounts(7, counts(1, 1)); @@ -247,31 +248,36 @@ public class ProxyRelationshipSelectorTest extends AbstractTestClass { // ---------------------------------------------------------------------- // search tests + // TODO search by Profile also // ---------------------------------------------------------------------- - // - // @Test - // public void searchTermFoundInAllThreeFields() { - // selectOnCriteria(20, 1, DEFAULT_ORDERING, "", "bob"); - // assertSelectedUris(3, "user02", "user05", "user10"); - // } - // - // @Test - // public void searchTermNotFound() { - // selectOnCriteria(20, 1, DEFAULT_ORDERING, "", "bogus"); - // assertSelectedUris(0); - // } - // - // /** - // * If the special characters were allowed into the Regex, this would have - // 3 - // * matches. If they are escaped properly, it will have none. - // */ - // @Test - // public void searchTermContainsSpecialRegexCharacters() { - // selectOnCriteria(20, 1, DEFAULT_ORDERING, "", "b.b"); - // assertSelectedUris(0); - // } - // + + @Test + public void searchFirstProxy() { + selectOnCriteria(10, 1, BY_PROXY, "AA"); + assertExpectedRelations(1, RELATION_1); + } + + @Test + public void searchAccountWithNoProxy() { + selectOnCriteria(10, 1, BY_PROXY, "None"); + assertExpectedRelations(0); + } + + @Test + public void searchMultipleProxies() { + selectOnCriteria(10, 1, BY_PROXY, "No"); + assertExpectedRelations(3, RELATION_4, RELATION_5, RELATION_6); + } + + // ---------------------------------------------------------------------- + // combination tests + // ---------------------------------------------------------------------- + + @Test + public void searchPopularWithPagination() { + selectOnCriteria(2, 2, BY_PROXY, "No"); + assertExpectedRelations(3, RELATION_6); + } // ---------------------------------------------------------------------- // helper methods