Skip to content

Commit

Permalink
allow search for all fields including tags
Browse files Browse the repository at this point in the history
A little hacky because I created another overloading method to allow
for comparisons of other fields without disrupting the current methods
and qualifiers. Do let me know if there are better ways to do this!
  • Loading branch information
tessav committed Oct 31, 2016
1 parent c51e4d5 commit 6afee99
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/main/java/seedu/gtd/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class FindCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all tasks whose names contain any of "
+ "the specified keywords (case-sensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";
+ "Example: " + COMMAND_WORD + " cs2103";

//@@author A0146130W
private final String keywords;
Expand All @@ -36,10 +36,11 @@ public CommandResult execute() {
model.updateFilteredTaskList(keywords, keywordSet);

if(model.getFilteredTaskList().isEmpty()) {
model.updateFilteredTaskList(keywordSet);
if(!model.getFilteredTaskList().isEmpty()) return new CommandResult(getMessageForTaskListShownSummaryIfExactPhraseNotFound(model.getFilteredTaskList().size()));
}

model.updateFilteredTaskList(keywords, "cmd");
if(!model.getFilteredTaskList().isEmpty()) {
return new CommandResult(getMessageForTaskListShownSummaryIfExactPhraseNotFound(model.getFilteredTaskList().size()));
}
}
return new CommandResult(getMessageForTaskListShownSummary(model.getFilteredTaskList().size()));
}
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/gtd/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ public interface Model {

/** Updates the filter of the filtered task list to filter by the given keywords*/
void updateFilteredTaskList(Set<String> keywordSet);

/** Updates the filter of the filtered task list to filter by the given keywords*/
void updateFilteredTaskList(String keywords, String cmd);
}
53 changes: 46 additions & 7 deletions src/main/java/seedu/gtd/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import seedu.gtd.commons.events.model.AddressBookChangedEvent;
import seedu.gtd.commons.util.StringUtil;
import seedu.gtd.model.task.Task;
import seedu.gtd.model.tag.Tag;
import seedu.gtd.model.tag.UniqueTagList;
import seedu.gtd.model.task.ReadOnlyTask;
import seedu.gtd.model.task.UniqueTaskList;
import seedu.gtd.model.task.UniqueTaskList.TaskNotFoundException;

import java.util.Arrays;
import java.util.Set;
import java.util.logging.Logger;

Expand Down Expand Up @@ -109,6 +112,11 @@ public void updateFilteredTaskList(String keywords, Set<String> keywordSet){
public void updateFilteredTaskList(Set<String> keywordSet) {
updateFilteredTaskList(new PredicateExpression(new NameQualifier(keywordSet)));
}

@Override
public void updateFilteredTaskList(String keywords, String cmd) {
updateFilteredTaskList(new PredicateExpression(new otherFieldsNameQualifier(keywords, cmd)));
}

private void updateFilteredTaskList(Expression expression) {
filteredTasks.setPredicate(expression::satisfies);
Expand Down Expand Up @@ -154,10 +162,10 @@ private class NameQualifier implements Qualifier {

@Override
public boolean run(ReadOnlyTask task) {
return keywordSet.stream()
.filter(keyword -> StringUtil.containsIgnoreCase(task.getName().fullName, keyword))
.findAny()
.isPresent();
return keywordSet.stream()
.filter(keyword -> StringUtil.containsIgnoreCase(task.getName().fullName, keyword))
.findAny()
.isPresent();
}

@Override
Expand All @@ -166,6 +174,38 @@ public String toString() {
}
}

private class otherFieldsNameQualifier implements Qualifier {
protected String nameKeyWords;

otherFieldsNameQualifier(String keywords, String cmd) {
this.nameKeyWords = keywords;
}

@Override
public boolean run(ReadOnlyTask task) {
String taskFullNameLowerCase = task.getName().fullName.toLowerCase();
String priority = task.getPriority().toString();
String address = task.getAddress().toString().toLowerCase();
String dueDate = task.getDueDate().toString();
UniqueTagList tagsList = task.getTags();

boolean nameMatch = taskFullNameLowerCase.contains(nameKeyWords.toLowerCase());
boolean addressMatch = address.contains(nameKeyWords.toLowerCase());
boolean priorityMatch = priority.contains(nameKeyWords);
boolean dueDateMatch = dueDate.contains(nameKeyWords);
boolean tagsMatch = tagsList.containSearch(nameKeyWords.toLowerCase());
boolean eachWordMatch = false;

String[] eachWord = nameKeyWords.split(" ");
for (String word : eachWord) {
System.out.println("each: " + word);
eachWordMatch = eachWordMatch || taskFullNameLowerCase.contains(word.toLowerCase());
}

return eachWordMatch || nameMatch || addressMatch || priorityMatch || dueDateMatch || tagsMatch;
}
}

private class orderedNameQualifier extends NameQualifier implements Qualifier {
private String nameKeyWords;

Expand All @@ -177,14 +217,13 @@ private class orderedNameQualifier extends NameQualifier implements Qualifier {
@Override
public boolean run(ReadOnlyTask task) {
String taskFullNameLowerCase = task.getName().fullName.toLowerCase();
boolean orderMatch = taskFullNameLowerCase.contains(nameKeyWords.toLowerCase());
boolean nameMatch = taskFullNameLowerCase.contains(nameKeyWords.toLowerCase());

boolean eachWordMatch = keywordSet.stream()
.filter(keyword -> StringUtil.containsIgnoreCase(task.getName().fullName, keyword))
.findAny()
.isPresent();

return eachWordMatch && orderMatch;
return eachWordMatch && nameMatch;
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/seedu/gtd/model/tag/UniqueTagList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.gtd.commons.exceptions.DuplicateDataException;
import seedu.gtd.commons.exceptions.IllegalValueException;
import seedu.gtd.commons.util.CollectionUtil;

import java.util.*;
Expand Down Expand Up @@ -105,6 +106,25 @@ public boolean contains(Tag toCheck) {
assert toCheck != null;
return internalList.contains(toCheck);
}



public boolean containSearch(String toCheck) {
assert toCheck != null;
boolean containsTag = true;
try {
String[] tagsArray = toCheck.split(" ");

for (String tag : tagsArray) {
System.out.println(tag);
Tag searchTag = new Tag(tag);
containsTag = containsTag && internalList.contains(searchTag);
}
return containsTag;
} catch (IllegalValueException e) {
return false;
}
}

/**
* Adds a Tag to the list.
Expand Down

0 comments on commit 6afee99

Please sign in to comment.