NIHVIVO-2343 Implement filtering by search term.

This commit is contained in:
j2blake 2011-11-06 15:47:13 +00:00
parent 4422a995f9
commit 89ea62af47
2 changed files with 49 additions and 26 deletions

View file

@ -13,6 +13,7 @@ import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet; 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.ItemInfo;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionBuilder.Relationship; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionBuilder.Relationship;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionCriteria.ProxyRelationshipView; 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 * A class which will accept a ProxyRelationshipSelectionCriteria and produce a
* ProxyRelationshipSelection. * ProxyRelationshipSelection.
*/ */
public class ProxyRelationshipSelector { public class ProxyRelationshipSelector extends AbstractPagingSelector {
private static final Log log = LogFactory private static final Log log = LogFactory
.getLog(ProxyRelationshipSelector.class); .getLog(ProxyRelationshipSelector.class);
@ -84,11 +85,14 @@ public class ProxyRelationshipSelector {
+ " auth:firstName ?firstName ; \n" // + " auth:firstName ?firstName ; \n" //
+ " auth:lastName ?lastName ; \n" // + " auth:lastName ?lastName ; \n" //
+ " auth:proxyEditorFor ?profile . \n" // + " auth:proxyEditorFor ?profile . \n" //
+ " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" //
+ " %filterClause% \n" //
+ "} \n"; // + "} \n"; //
private void figureTotalResultCount() { private void figureTotalResultCount() {
String qString = COUNT_QUERY_TEMPLATE.replace("%prefixes%", String qString = COUNT_QUERY_TEMPLATE.replace("%prefixes%",
PREFIX_LINES); PREFIX_LINES);
qString = replaceFilterClauses(qString);
int count = new SparqlQueryRunner<Integer>(context.userAccountsModel, int count = new SparqlQueryRunner<Integer>(context.userAccountsModel,
new CountQueryParser()).executeQuery(qString); new CountQueryParser()).executeQuery(qString);
@ -97,6 +101,17 @@ public class ProxyRelationshipSelector {
builder.count = count; 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 = "" // private static final String QUERY_PROXY_BASICS = "" //
+ "%prefixes% \n" // + "%prefixes% \n" //
+ "SELECT DISTINCT ?uri ?label ?externalAuthId \n" // + "SELECT DISTINCT ?uri ?label ?externalAuthId \n" //
@ -107,6 +122,7 @@ public class ProxyRelationshipSelector {
+ " auth:proxyEditorFor ?profile . \n" // + " auth:proxyEditorFor ?profile . \n" //
+ " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" // + " LET ( ?label := fn:concat(?lastName, ', ', ?firstName) )" //
+ " OPTIONAL { ?uri auth:externalAuthId ?externalAuthId } \n" // + " OPTIONAL { ?uri auth:externalAuthId ?externalAuthId } \n" //
+ " %filterClause% \n" //
+ "} \n" // + "} \n" //
+ "ORDER BY ASC(?lastName) ASC(?firstName) \n" // + "ORDER BY ASC(?lastName) ASC(?firstName) \n" //
+ "LIMIT %perPage% \n" // + "LIMIT %perPage% \n" //
@ -118,6 +134,7 @@ public class ProxyRelationshipSelector {
.replace("%perPage%", .replace("%perPage%",
String.valueOf(criteria.getRelationshipsPerPage())) String.valueOf(criteria.getRelationshipsPerPage()))
.replace("%offset%", offset()); .replace("%offset%", offset());
qString = replaceFilterClauses(qString);
List<Relationship> relationships = new SparqlQueryRunner<List<Relationship>>( List<Relationship> relationships = new SparqlQueryRunner<List<Relationship>>(
context.userAccountsModel, new ProxyBasicsParser()) context.userAccountsModel, new ProxyBasicsParser())

View file

@ -16,6 +16,7 @@ import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; 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.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionCriteria.ProxyRelationshipView; 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.controller.accounts.manageproxies.ProxyRelationshipSelector.Context;
import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner;
/** /**
* TODO * TODO
@ -144,7 +146,6 @@ public class ProxyRelationshipSelectorTest extends AbstractTestClass {
@Test @Test
public void checkAllFieldsOnFirstRelationshipByProxy() { public void checkAllFieldsOnFirstRelationshipByProxy() {
// setLoggerLevel(SparqlQueryRunner.class, Level.DEBUG);
selectOnCriteria(1, 1, BY_PROXY, ""); selectOnCriteria(1, 1, BY_PROXY, "");
log.debug("SELECTION: " + selection); log.debug("SELECTION: " + selection);
assertExpectedCounts(7, counts(1, 1)); assertExpectedCounts(7, counts(1, 1));
@ -247,31 +248,36 @@ public class ProxyRelationshipSelectorTest extends AbstractTestClass {
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// search tests // search tests
// TODO search by Profile also
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
//
// @Test @Test
// public void searchTermFoundInAllThreeFields() { public void searchFirstProxy() {
// selectOnCriteria(20, 1, DEFAULT_ORDERING, "", "bob"); selectOnCriteria(10, 1, BY_PROXY, "AA");
// assertSelectedUris(3, "user02", "user05", "user10"); assertExpectedRelations(1, RELATION_1);
// } }
//
// @Test @Test
// public void searchTermNotFound() { public void searchAccountWithNoProxy() {
// selectOnCriteria(20, 1, DEFAULT_ORDERING, "", "bogus"); selectOnCriteria(10, 1, BY_PROXY, "None");
// assertSelectedUris(0); assertExpectedRelations(0);
// } }
//
// /** @Test
// * If the special characters were allowed into the Regex, this would have public void searchMultipleProxies() {
// 3 selectOnCriteria(10, 1, BY_PROXY, "No");
// * matches. If they are escaped properly, it will have none. assertExpectedRelations(3, RELATION_4, RELATION_5, RELATION_6);
// */ }
// @Test
// public void searchTermContainsSpecialRegexCharacters() { // ----------------------------------------------------------------------
// selectOnCriteria(20, 1, DEFAULT_ORDERING, "", "b.b"); // combination tests
// assertSelectedUris(0); // ----------------------------------------------------------------------
// }
// @Test
public void searchPopularWithPagination() {
selectOnCriteria(2, 2, BY_PROXY, "No");
assertExpectedRelations(3, RELATION_6);
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// helper methods // helper methods