Skip to content

Commit

Permalink
implement cmd parameter
Browse files Browse the repository at this point in the history
the current flow is:
1. if there’s a specified parameter, search by that parameter only 2.
search by exact name 3. search all fields.
  • Loading branch information
tessav committed Nov 1, 2016
1 parent 6afee99 commit b2e734b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 19 deletions.
32 changes: 23 additions & 9 deletions src/main/java/seedu/gtd/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public class FindCommand extends Command {

//@@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,14 +35,26 @@ private String getMessageForTaskListShownSummaryIfExactPhraseNotFound(int displa

@Override
public CommandResult execute() {
model.updateFilteredTaskList(keywords, keywordSet);

if(model.getFilteredTaskList().isEmpty()) {
model.updateFilteredTaskList(keywords, "cmd");
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
2 changes: 1 addition & 1 deletion src/main/java/seedu/gtd/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ 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*/
/** Updates the filter of the filtered task list to filter by the right parameter*/
void updateFilteredTaskList(String keywords, String cmd);
}
42 changes: 34 additions & 8 deletions src/main/java/seedu/gtd/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ public UnmodifiableObservableList<ReadOnlyTask> getFilteredTaskList() {
public void updateFilteredListToShowAll() {
filteredTasks.setPredicate(null);
}

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

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

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

private void updateFilteredTaskList(Expression expression) {
Expand Down Expand Up @@ -174,15 +174,41 @@ public String toString() {
}
}

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

otherFieldsNameQualifier(String keywords, 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();
Expand Down

0 comments on commit b2e734b

Please sign in to comment.