Skip to content

Commit

Permalink
Issue #86 - Password search now does OR queries if multiple search terms
Browse files Browse the repository at this point in the history
or exact phrase matches if put in quotes.
  • Loading branch information
joshdrummond committed Jul 9, 2015
1 parent 70a42ed commit d133399
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
51 changes: 49 additions & 2 deletions src/main/java/net/webpasswordsafe/common/model/Password.java
@@ -1,5 +1,5 @@
/* /*
Copyright 2008-2013 Josh Drummond Copyright 2008-2015 Josh Drummond
This file is part of WebPasswordSafe. This file is part of WebPasswordSafe.
Expand Down Expand Up @@ -52,7 +52,7 @@
*/ */
@Entity @Entity
@Table(name="passwords") @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; private static final long serialVersionUID = 7174192307771387126L;
public static final int LENGTH_NAME = 100; public static final int LENGTH_NAME = 100;
Expand Down Expand Up @@ -362,4 +362,51 @@ public Password cloneCopy()
return p; 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. This file is part of WebPasswordSafe.
Expand All @@ -21,6 +21,7 @@


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -243,10 +244,50 @@ public List<Password> searchPassword(String query, boolean activeOnly, Collectio
query = Utils.safeString(query); query = Utils.safeString(query);
Date now = new Date(); Date now = new Date();
User loggedInUser = getLoggedInUser(); 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()); auditLogger.log(now, loggedInUser.getUsername(), ServerSessionUtil.getIP(), "search password", "query=["+query+"] activeOnly=["+activeOnly+"] tags=["+tags+"] tagMatch=["+tagMatch+"]", true, "found "+passwords.size());
return passwords; 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 @Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=true) @Transactional(propagation=Propagation.REQUIRED, readOnly=true)
Expand Down

0 comments on commit d133399

Please sign in to comment.