Skip to content

Commit

Permalink
Merge pull request IQSS#6211 from IQSS/5808-search-users-by-fullname
Browse files Browse the repository at this point in the history
5808 search users by fullname and affiliation
  • Loading branch information
kcondon authored Sep 24, 2019
2 parents 0d3df08 + 4b6c857 commit 6bebe7f
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/main/java/edu/harvard/iq/dataverse/UserServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private List<Object[]> getUserListCore(String searchTerm, String sortKey, Intege
String sharedSearchClause = "";

if (!searchTerm.isEmpty()) {
sharedSearchClause = " AND " + getSharedSearchClause();
sharedSearchClause = " AND " + getSharedSearchClause(searchTerm);
}


Expand All @@ -423,7 +423,6 @@ private List<Object[]> getUserListCore(String searchTerm, String sortKey, Intege
logger.log(Level.FINE, "getUserCount: {0}", qstr);

Query nativeQuery = em.createNativeQuery(qstr);
nativeQuery.setParameter("searchTerm", searchTerm + "%");

return nativeQuery.getResultList();

Expand All @@ -436,14 +435,25 @@ private List<Object[]> getUserListCore(String searchTerm, String sortKey, Intege
*
* @return
*/
private String getSharedSearchClause(){

private String getSharedSearchClause(String searchTerm){
String[] searchTermTokens = searchTerm.replaceAll("['\"]", "").split("[ ,][ ,]*");

String searchClause = "(";

String searchClause = " (u.useridentifier ILIKE #searchTerm";
searchClause += " OR u.firstname ILIKE #searchTerm";
searchClause += " OR u.lastname ILIKE #searchTerm";
searchClause += " OR u.email ILIKE #searchTerm)";
for (int i = 0; i < searchTermTokens.length; i++) {
if (i > 0) {
searchClause += " AND ";
}
searchClause += "(u.useridentifier ILIKE '" + searchTermTokens[i] + "%'";
searchClause += " OR u.firstname ILIKE '" + searchTermTokens[i] + "%'";
searchClause += " OR u.lastname ILIKE '" + searchTermTokens[i] + "%'";
searchClause += " OR u.affiliation ILIKE '" + searchTermTokens[i] + "%'";
searchClause += " OR u.affiliation ILIKE '% " + searchTermTokens[i] + "%'";
searchClause += " OR u.email ILIKE '" + searchTermTokens[i] + "%')";
}

return searchClause;
return searchClause + ")";
}


Expand Down Expand Up @@ -488,7 +498,7 @@ public Long getUserCount(String searchTerm) {
String sharedSearchClause = "";

if (!searchTerm.isEmpty()) {
sharedSearchClause = " AND " + getSharedSearchClause();
sharedSearchClause = " AND " + getSharedSearchClause(searchTerm);
}

String qstr = "SELECT count(u.id)";
Expand All @@ -502,7 +512,6 @@ public Long getUserCount(String searchTerm) {
qstr += ";";

Query nativeQuery = em.createNativeQuery(qstr);
nativeQuery.setParameter("searchTerm", searchTerm + "%");

return (Long)nativeQuery.getSingleResult();

Expand Down

0 comments on commit 6bebe7f

Please sign in to comment.