Skip to content

Commit

Permalink
Merge pull request #174 from CS2103AUG2016-F11-C1/fix-clear-controller
Browse files Browse the repository at this point in the history
Fix ClearController
  • Loading branch information
irvinlim committed Nov 6, 2016
2 parents a8c6ef5 + 8de9d89 commit 7a432d1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
69 changes: 63 additions & 6 deletions src/main/java/seedu/todo/controllers/ClearController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.util.List;
import java.util.Map;

import seedu.todo.commons.exceptions.AmbiguousEventTypeException;
import seedu.todo.commons.exceptions.InvalidNaturalDateException;
import seedu.todo.commons.exceptions.ParseException;
import seedu.todo.commons.util.StringUtil;
import seedu.todo.controllers.concerns.CalendarItemFilter;
import seedu.todo.controllers.concerns.DateParser;
import seedu.todo.controllers.concerns.Renderer;
import seedu.todo.controllers.concerns.Tokenizer;
import seedu.todo.models.Event;
Expand All @@ -27,7 +30,17 @@ public class ClearController extends Controller {
private static final String COMMAND_KEYWORD = "clear";

private static final String MESSAGE_CLEAR_NO_ITEMS_FOUND = "No items found!";
private static final String MESSAGE_CLEAR_SUCCESS = "A total of %s deleted!\n" + "To undo, type \"undo\".";
private static final String MESSAGE_CLEAR_SUCCESS = "A total of %s %s and %s %s deleted!\n" + "To undo, type \"undo\".";
private static final String CLEAR_TEMPLATE = "clear [name \"%s\"] [from \"%s\"] [to \"%s\"] [tag \"%s\"]";
private static final String CLEAR_TASKS_TEMPLATE = "clear tasks [name \"%s\"] [\"%s\"] [from \"%s\"] [to \"%s\"] [tag \"%s\"]";
private static final String CLEAR_EVENTS_TEMPLATE = "clear events [name \"%s\"] [\"%s\"] [from \"%s\"] [to \"%s\"] [tag \"%s\"]";
private static final String NAME_FIELD = "<name>";
private static final String TASK_STATUS_FIELD = "<task status>";
private static final String EVENT_STATUS_FIELD = "<event status>";
private static final String START_TIME_FIELD = "<start>";
private static final String END_TIME_FIELD = "<end>";
private static final String TAG_FIELD = "<tag>";


private static CommandDefinition commandDefinition =
new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX, COMMAND_KEYWORD);
Expand All @@ -44,7 +57,14 @@ public void process(String input) throws ParseException {
Tokenizer.tokenize(CalendarItemFilter.getFilterTokenDefinitions(), input);

// Decide if task/event/both
boolean[] isTaskEvent = CalendarItemFilter.parseIsTaskEvent(parsedResult);
boolean[] isTaskEvent = null;
try {
isTaskEvent = CalendarItemFilter.parseIsTaskEvent(parsedResult);
} catch (AmbiguousEventTypeException e) {
renderDisambiguation(parsedResult, true, true);
return;
}

boolean filterTask = isTaskEvent[0];
boolean filterEvent = isTaskEvent[1];

Expand All @@ -58,20 +78,57 @@ public void process(String input) throws ParseException {
clearEvents = CalendarItemFilter.filterEvents(parsedResult);
}
} catch (InvalidNaturalDateException e) {
renderDisambiguation(parsedResult);
renderDisambiguation(parsedResult, filterTask, filterEvent);
return;
}

// Clear them all!
TodoListDB db = TodoListDB.getInstance();

if (clearTasks.size() == 0 && clearEvents.size() == 0) {
Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEMS_FOUND);
return;
}

db.destroyTasks(clearTasks);
db.destroyEvents(clearEvents);
db.save();

Renderer.renderIndex(db, "Done!");
String consoleMessage = String.format(MESSAGE_CLEAR_SUCCESS,
clearTasks.size(), StringUtil.pluralizer(clearTasks.size(), "task", "tasks"),
clearEvents.size(), StringUtil.pluralizer(clearEvents.size(), "event", "events"));

Renderer.renderIndex(db, consoleMessage);
}

private void renderDisambiguation(Map<String, String[]> parsedResult) {
return;
private void renderDisambiguation(Map<String, String[]> parsedResult, boolean filterTask, boolean filterEvent) {
String name = (parsedResult.get("name") == null) ? null : parsedResult.get("name")[1];
name = StringUtil.replaceEmpty(name, NAME_FIELD);

String taskStatus = (parsedResult.get("taskStatus") == null) ? null : parsedResult.get("taskStatus")[0];
taskStatus = StringUtil.replaceEmpty(taskStatus, TASK_STATUS_FIELD);

String eventStatus = (parsedResult.get("eventStatus") == null) ? null : parsedResult.get("eventStatus")[0];
eventStatus = StringUtil.replaceEmpty(eventStatus, EVENT_STATUS_FIELD);

String[] datePair = DateParser.extractDatePair(parsedResult);
String timeStartNatural = datePair[0];
String timeEndNatural = datePair[1];
timeStartNatural = StringUtil.replaceEmpty(timeStartNatural, START_TIME_FIELD);
timeEndNatural = StringUtil.replaceEmpty(timeEndNatural, END_TIME_FIELD);

String tag = (parsedResult.get("tag") == null) ? null : parsedResult.get("tag")[1];
tag = StringUtil.replaceEmpty(tag, TAG_FIELD);

String consoleCommand = null;
if ((filterTask && filterEvent) || (!filterTask && !filterEvent)) {
consoleCommand = String.format(CLEAR_TEMPLATE, name, timeStartNatural, timeEndNatural, tag);
} else if (filterTask) {
consoleCommand = String.format(CLEAR_TASKS_TEMPLATE, name, taskStatus, timeStartNatural, timeEndNatural, tag);
} else {
consoleCommand = String.format(CLEAR_EVENTS_TEMPLATE, name, eventStatus, timeStartNatural, timeEndNatural, tag);
}

Renderer.renderDisambiguation(consoleCommand, "");
}
}
9 changes: 7 additions & 2 deletions src/test/java/seedu/todo/guitests/UndoRedoCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.time.LocalDateTime;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -37,6 +38,9 @@ public UndoRedoCommandTest() {

@Before
public void resetDB() {
// Make sure there's something to clear, so that `clear` will have an undo state.
// Otherwise the testing environment may depend on the order of the running tests.
console.runCommand(commandAdd1);
console.runCommand("clear");
}

Expand All @@ -61,13 +65,14 @@ public void undo_notavailable() {
assertTaskNotVisibleAfterCmd("undo", task1);
console.runCommand("undo");
console.runCommand("undo");
console.runCommand("undo");
assertEquals(console.getConsoleTextArea(), "There is no command to undo!");
}

@Test
public void undo_multiple_notavailable() {
console.runCommand("undo 2");
assertEquals(console.getConsoleTextArea(), "We cannot undo 2 commands! At most, you can undo 1 command.");
console.runCommand("undo 3");
assertEquals(console.getConsoleTextArea(), "We cannot undo 3 commands! At most, you can undo 2 commands.");
}

@Test
Expand Down

0 comments on commit 7a432d1

Please sign in to comment.