Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow search for all fields including tags #53

Merged
merged 2 commits into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/main/java/seedu/gtd/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ 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;
private final Set<String> keywordSet;
private Set<String> keywordSet;
private final String cmd;

public FindCommand(String keywords, Set<String> keywordSet) {
public FindCommand(String keywords, Set<String> keywordSet, String cmd) {
this.keywords = keywords;
this.keywordSet = keywordSet;
this.cmd = cmd;
}

private String getMessageForTaskListShownSummaryIfExactPhraseNotFound(int displaySize) {
Expand All @@ -33,13 +35,26 @@ private String getMessageForTaskListShownSummaryIfExactPhraseNotFound(int displa

@Override
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()));
}

System.out.println("command: " + cmd);

// search by parameter if specified
if (cmd != "nil") {
model.updateFilteredTaskList(keywords, cmd);
} else {
// search by exact name
model.updateFilteredTaskList(keywords, keywordSet);
}
if (!model.getFilteredTaskList().isEmpty()) {
return new CommandResult(getMessageForTaskListShownSummary(model.getFilteredTaskList().size()));
}

// search by keywords
model.updateFilteredTaskList(keywords, "nil");

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

return new CommandResult(getMessageForTaskListShownSummary(model.getFilteredTaskList().size()));
}
}
32 changes: 31 additions & 1 deletion src/main/java/seedu/gtd/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ private Optional<Integer> parseIndex(String command) {
* @return the prepared command
*/
private Command prepareFind(String args) {

// check if parameters are specified and pass specified field to FindCommand

String preprocessedArgs = " " + appendEnd(args.trim());
final Matcher addressMatcher = ADDRESS_TASK_DATA_ARGS_FORMAT.matcher(preprocessedArgs);
final Matcher priorityMatcher = PRIORITY_TASK_DATA_ARGS_FORMAT.matcher(preprocessedArgs);
final Matcher dueDateMatcher = DUEDATE_TASK_DATA_ARGS_FORMAT.matcher(preprocessedArgs);
final Matcher tagsMatcher = TAGS_TASK_DATA_ARGS_FORMAT.matcher(preprocessedArgs);

Set<String> defaultSet = new HashSet<String>();

if (addressMatcher.matches()) {
String addressToBeFound = addressMatcher.group("address");
return new FindCommand(addressToBeFound, defaultSet,"address");
}
if (priorityMatcher.matches()) {
String priorityToBeFound = priorityMatcher.group("priority");
return new FindCommand(priorityToBeFound, defaultSet, "priority");
}
if (dueDateMatcher.matches()) {
String dueDateToBeFound = dueDateMatcher.group("dueDate");
return new FindCommand(dueDateToBeFound, defaultSet, "dueDate");
}
if (tagsMatcher.matches()) {
String tagsToBeFound = tagsMatcher.group("tagArguments");
return new FindCommand(tagsToBeFound, defaultSet,"tagArguments");
}

// free-form search by keywords

final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
Expand All @@ -299,7 +329,7 @@ private Command prepareFind(String args) {
final Set<String> keywordSet = new HashSet<>(Arrays.asList(splitKeywords));

final String keywords = matcher.group("keywords");
return new FindCommand(keywords, keywordSet);
return new FindCommand(keywords, keywordSet, "nil");
}

/**
Expand Down
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 right parameter*/
void updateFilteredTaskList(String keywords, String cmd);
}
83 changes: 74 additions & 9 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 @@ -99,15 +102,20 @@ public UnmodifiableObservableList<ReadOnlyTask> getFilteredTaskList() {
public void updateFilteredListToShowAll() {
filteredTasks.setPredicate(null);
}

@Override
public void updateFilteredTaskList(Set<String> keywordSet) {
updateFilteredTaskList(new PredicateExpression(new NameQualifier(keywordSet)));
}

@Override
public void updateFilteredTaskList(String keywords, Set<String> keywordSet){
updateFilteredTaskList(new PredicateExpression(new orderedNameQualifier(keywords, keywordSet)));
}

@Override
public void updateFilteredTaskList(Set<String> keywordSet) {
updateFilteredTaskList(new PredicateExpression(new NameQualifier(keywordSet)));
public void updateFilteredTaskList(String keywords, String cmd) {
updateFilteredTaskList(new PredicateExpression(new otherFieldsQualifier(keywords, cmd)));
}

private void updateFilteredTaskList(Expression expression) {
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,64 @@ public String toString() {
}
}

private class otherFieldsQualifier implements Qualifier {
protected String nameKeyWords;
protected String cmd;

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

@Override
public boolean run(ReadOnlyTask task) {
if (cmd == "address") {
System.out.println("finding address..");
String address = task.getAddress().toString().toLowerCase();
boolean addressMatch = address.contains(nameKeyWords.toLowerCase());
return addressMatch;
} else if (cmd == "priority") {
System.out.println("finding priority..");
String priority = task.getPriority().toString();
boolean priorityMatch = priority.contains(nameKeyWords);
return priorityMatch;
} else if (cmd == "dueDate") {
System.out.println("finding dueDate..");
String dueDate = task.getDueDate().toString();
boolean dueDateMatch = dueDate.contains(nameKeyWords);
return dueDateMatch;
} else if (cmd == "tagArguments") {
System.out.println("finding tags.. ");
UniqueTagList tagsList = task.getTags();
boolean tagsMatch = tagsList.containSearch(nameKeyWords.toLowerCase());
return tagsMatch;
}

// cmd == "nil"

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 +243,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