Permalink
Browse files

Issue #86 - Password search now does OR queries if multiple search terms

or exact phrase matches if put in quotes.
  • Loading branch information...
joshdrummond committed Jul 9, 2015
1 parent 70a42ed commit d133399febd2438160cf1ce344261f2f4b7f9983
@@ -1,5 +1,5 @@
/*
Copyright 2008-2013 Josh Drummond
Copyright 2008-2015 Josh Drummond
This file is part of WebPasswordSafe.
@@ -52,7 +52,7 @@
*/
@Entity
@Table(name="passwords")
public class Password extends LightEntity implements Serializable
public class Password extends LightEntity implements Serializable, Comparable<Password>
{
private static final long serialVersionUID = 7174192307771387126L;
public static final int LENGTH_NAME = 100;
@@ -362,4 +362,51 @@ public Password cloneCopy()
return p;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Password))
return false;
Password other = (Password) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
@Override
public int compareTo(Password other)
{
if (getName().equals(other.getName()))
{
return getUsername().compareTo(other.getUsername());
}
else
{
return getName().compareTo(other.getName());
}
}
}
@@ -1,5 +1,5 @@
/*
Copyright 2008-2013 Josh Drummond
Copyright 2008-2015 Josh Drummond
This file is part of WebPasswordSafe.
@@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@@ -243,10 +244,50 @@ public void updatePassword(Password updatePassword)
query = Utils.safeString(query);
Date now = new Date();
User loggedInUser = getLoggedInUser();
List<Password> passwords = passwordDAO.findPasswordByFuzzySearch(query, loggedInUser, activeOnly, tags, tagMatch);
List<Password> passwords = new ArrayList<Password>();
Set<String> searchTerms = parseSearchTerms(query);
if (searchTerms.isEmpty())
{
passwords.addAll(passwordDAO.findPasswordByFuzzySearch("", loggedInUser, activeOnly, tags, tagMatch));
}
else
{
Set<Password> passwordSet = new HashSet<Password>();
for (String searchTerm : searchTerms)
{
passwordSet.addAll(passwordDAO.findPasswordByFuzzySearch(searchTerm, loggedInUser, activeOnly, tags, tagMatch));
}
passwords.addAll(passwordSet);
}
Collections.sort(passwords);
auditLogger.log(now, loggedInUser.getUsername(), ServerSessionUtil.getIP(), "search password", "query=["+query+"] activeOnly=["+activeOnly+"] tags=["+tags+"] tagMatch=["+tagMatch+"]", true, "found "+passwords.size());
return passwords;
}
private Set<String> parseSearchTerms(String query)
{
Set<String> searchTerms = new HashSet<String>();
String[] sOuter = query.split("\"");
boolean insideQuote = false;
for (String sOuterItem : sOuter)
{
if (insideQuote)
{
searchTerms.add(sOuterItem.trim());
}
else
{
String[] sInner = sOuterItem.split(" ");
for (String sInnerItem : sInner)
{
searchTerms.add(sInnerItem.trim());
}
}
insideQuote = !insideQuote;
}
searchTerms.remove("");
return searchTerms;
}
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=true)

0 comments on commit d133399

Please sign in to comment.