From 17bf5b46de55a113325ae21b2e71e27c80034052 Mon Sep 17 00:00:00 2001 From: louietyj Date: Sun, 6 Nov 2016 03:29:43 +0800 Subject: [PATCH 1/5] Remove Yaocong --- .../todo/controllers/ClearController.java | 394 ------------------ 1 file changed, 394 deletions(-) diff --git a/src/main/java/seedu/todo/controllers/ClearController.java b/src/main/java/seedu/todo/controllers/ClearController.java index bd09ef2b4d58..326453e1e474 100644 --- a/src/main/java/seedu/todo/controllers/ClearController.java +++ b/src/main/java/seedu/todo/controllers/ClearController.java @@ -1,20 +1,8 @@ package seedu.todo.controllers; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; import java.util.Map; -import com.joestelmach.natty.DateGroup; -import com.joestelmach.natty.Parser; - import seedu.todo.commons.exceptions.ParseException; -import seedu.todo.commons.util.DateUtil; -import seedu.todo.commons.util.StringUtil; -import seedu.todo.controllers.concerns.Renderer; import seedu.todo.controllers.concerns.Tokenizer; import seedu.todo.models.TodoListDB; @@ -33,15 +21,6 @@ public class ClearController implements Controller { private static final String MESSAGE_CLEAR_NO_ITEM_FOUND = "No item found!"; private static final String MESSAGE_CLEAR_SUCCESS = "A total of %s deleted!\n" + "To undo, type \"undo\"."; - //Use by array access - private static final int KEYWORD = 0; - private static final int RESULT = 1; - private static final int MAXIMUM_SIZE = 2; - //Use by accessing date value - private static final int INDEX_DATE_ON = 0; - private static final int INDEX_DATE_FROM = 1; - private static final int INDEX_DATE_TO = 2; - private static CommandDefinition commandDefinition = new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX); @@ -54,381 +33,8 @@ public float inputConfidence(String input) { return (input.toLowerCase().startsWith(COMMAND_WORD)) ? 1 : 0; } - - /** - * Get the token definitions for use with tokenizer.
- * This method exists primarily because Java does not support HashMap - * literals... - * - * @return tokenDefinitions - */ - private static Map getTokenDefinitions() { - Map tokenDefinitions = new HashMap(); - tokenDefinitions.put("default", new String[] {"clear"}); - tokenDefinitions.put("eventType", new String[] { "event", "events", "task", "tasks" }); - tokenDefinitions.put("time", new String[] { "at", "by", "on", "before", "time" }); - tokenDefinitions.put("timeFrom", new String[] { "from" }); - tokenDefinitions.put("timeTo", new String[] { "to", "until" }); - return tokenDefinitions; - } - @Override public void process(String input) throws ParseException { - TodoListDB db = TodoListDB.getInstance(); - - Map parsedResult; - parsedResult = Tokenizer.tokenize(getTokenDefinitions(), input); - - String[] parsedDates = parseDates(parsedResult); - // Task or event specified? - boolean deleteAll = parseDeleteAllType(parsedResult); - - // Task or event? - boolean isTask = true; //default - - // task or event keyword is been found - if (!deleteAll) { - isTask = parseIsTask(parsedResult); - } - - //no dates provided and input is exactly the same as COMMAND_WORD - if (parsedDates == null && parseExactClearCommand(parsedResult) && deleteAll) { - destroyAll(db); - return; - } else { - //invalid date provide by user with no date keywords parsed by natty - if (deleteAll && !parseExactClearCommand(parsedResult) && parsedDates == null) { //no item type and date provided - LocalDateTime date = parseDateWithNoKeyword(parsedResult); - if (date == null) { - displayErrorMessage(input, parsedDates, deleteAll, isTask); - return; - } - } - } - - //parsing of dates with keywords with natty - LocalDateTime dateOn = parseDateWithNoKeyword(parsedResult); - LocalDateTime dateFrom = (LocalDateTime) null; - LocalDateTime dateTo = (LocalDateTime) null; - if (parsedDates != null) { - String naturalOn = parsedDates[INDEX_DATE_ON]; - String naturalFrom = parsedDates[INDEX_DATE_FROM]; - String naturalTo = parsedDates[INDEX_DATE_TO]; - // if all are null = no date provided - - // Parse natural date using Natty. - dateOn = naturalOn == null ? (LocalDateTime) null : parseNatural(naturalOn); - dateFrom = naturalFrom == null ? (LocalDateTime) null : parseNatural(naturalFrom); - dateTo = naturalTo == null ? (LocalDateTime) null : parseNatural(naturalTo); - } - - //invoke destroy command - destroyByDate(db, parsedDates, dateOn, dateFrom, dateTo, deleteAll, isTask, input); } - - /** ================ DESTROY TASKS/EVENTS WITH FILTERED KEYWORDS ================== **/ - - /** - * Clear all tasks and events by a single or a range of date that exist in the database. - * - * @param db - * TodoListDB object - * @param parsedDate - * null if no date entered, or natural date that user has entered - * @param dateOn, dateFrom - * null if parsing failed or Due date for Task or start date for Event - * @param dateTo - * null if parsing failed or End date for Event - * @param deleteAll - * true if no CalendarItem Type provided, false if "task" or "event" keyword found - * @param isTask - * true if "task" keyword found, false if "event" keyword found - * @param input - * the input the user have entered - */ - private void destroyByDate(TodoListDB db, String[] parsedDate, LocalDateTime dateOn, - LocalDateTime dateFrom, LocalDateTime dateTo, boolean deleteAll, - boolean isTask, String input) { - if (dateOn == null && dateFrom == null && dateTo == null && deleteAll) { - displayErrorMessage(input, parsedDate, deleteAll, isTask); - } - else if (dateOn != null) { - destroyBySelectedDate(db, dateOn, deleteAll, isTask); - return; - } else { - if (!deleteAll && parsedDate != null && dateFrom == null - && dateTo == null && dateOn == null) { //date provided is invalid - displayErrorMessage(input, parsedDate, deleteAll, isTask); - return; - } else { - if (parsedDate != null) { - if (parsedDate[INDEX_DATE_FROM] != null && parsedDate[INDEX_DATE_TO] != null - && (dateFrom == null || dateTo == null)) { - displayErrorMessage(input, parsedDate, deleteAll, isTask); - return; - } - } - destroyByRange(db, dateFrom, dateTo, deleteAll, isTask); - return; - } - } - } - - /** - * clear all tasks and events of given date range that exist in the database. - * - * @param TodoListDB - * @param dateFrom - * null if parsing failed or Due date for Task or start date for Event - * @param dateTo - * null if parsing failed or End date for Event - * @param deleteAll - * true if no CalendarItem Type provided, false if "task" or "event" keyword found - * @param isTask - * true if "task" keyword found, false if "event" keyword found - */ - private void destroyByRange(TodoListDB db, LocalDateTime dateFrom, LocalDateTime dateTo, - boolean deleteAll, boolean isTask) { - if (dateFrom == null) { - dateFrom = LocalDateTime.MIN; - } - - if (dateTo == null) { - dateTo = LocalDateTime.MAX; - } - - int numTasks = db.getTaskByRange(dateFrom, dateTo).size(); - int numEvents = db.getEventByRange(dateFrom, dateTo).size(); - - //if no tasks or events are been found - if (numTasks == 0 && numEvents == 0) { - Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEM_FOUND); - return; - } - - //if CalendarItem type not specified - if (deleteAll) { - db.destroyAllEventByRange(dateFrom, dateTo); - db.destroyAllTaskByRange(dateFrom, dateTo); - } else if (isTask) { - // no task is been found - if (numTasks == 0) { - Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEM_FOUND); - return; - } - db.destroyAllTaskByRange(dateFrom, dateTo); - numEvents = 0; - } else { - // no event is been found - if (numEvents == 0) { - Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEM_FOUND); - return; - } - db.destroyAllEventByRange(dateFrom, dateTo); - numTasks = 0; - } - - //save and render - db.save(); - Renderer.renderIndex(db, String.format(MESSAGE_CLEAR_SUCCESS, StringUtil.formatNumberOfTaskAndEventWithPuralizer(numTasks, numEvents))); - } - - - /** - * clear all tasks and events of the date that exist in the database. - * - * @param TodoListDB - * @param givenDate - * null if parsing failed or Due date for Task or start date for Event - * @param deleteAll - * true if no CalendarItem Type provided, false if "task" or "event" keyword found - * @param isTask - * true if "task" keyword found, false if "event" keyword found - */ - private void destroyBySelectedDate(TodoListDB db, LocalDateTime givenDate, boolean deleteAll, boolean isTask) { - int numTasks = db.getTaskByDate(givenDate).size(); - int numEvents = db.getEventByDate(givenDate).size(); - - // no tasks or events are been found - if (numTasks == 0 && numEvents == 0) { - Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEM_FOUND); - return; - } - - // task or event is not specified - if (deleteAll) { - db.destroyAllEventByDate(givenDate); - db.destroyAllTaskByDate(givenDate); - } else if (isTask) { //deleting task - if (numTasks == 0) { //if no task is found - Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEM_FOUND); - return; - } - db.destroyAllTaskByDate(givenDate); - numEvents = 0; - } else { //deleting events - if (numEvents == 0) { //if no event is found - Renderer.renderIndex(db, MESSAGE_CLEAR_NO_ITEM_FOUND); - return; - } - db.destroyAllEventByDate(givenDate); - numTasks = 0; - } - - //save and render - db.save(); - Renderer.renderIndex(db, String.format(MESSAGE_CLEAR_SUCCESS, StringUtil.formatNumberOfTaskAndEventWithPuralizer(numTasks, numEvents))); - } - - /** - * clear all tasks and events that exist in the database. - * - * @param TodoListDB - */ - private void destroyAll(TodoListDB db) { - int totalCalendarItems = db.getAllEvents().size() + db.getAllTasks().size(); - db.destroyAllEvent(); - db.destroyAllTask(); - db.save(); - Renderer.renderIndex(db, String.format(MESSAGE_CLEAR_SUCCESS, totalCalendarItems)); - } - - /** ================ PARSING METHODS ================== **/ - - /** - * Extracts the intended COMMAND_WORD from parsedResult. - * - * @param parsedResult - * @return true if no String provided after command word, false if some String provided after command word - */ - private boolean parseExactClearCommand(Map parsedResult) { - return parsedResult.get("default")[RESULT] == null; - } - - /** - * Extracts the date without any keyword from parsedResult. - * - * @param parsedResult - * @return LocalDatetime date if found, or null if no date found - */ - private LocalDateTime parseDateWithNoKeyword(Map parsedResult) { - if (parsedResult.get("default").length == MAXIMUM_SIZE) { // user enter more than 1 date with no keyword - if (parsedResult.get("default")[RESULT] != null) { - return parseNatural(parsedResult.get("default")[RESULT]); - } else { - return null; - } - } else { - return null; - } - } - - /** - * Parse a natural date into a LocalDateTime object. - * - * @param natural - * @return LocalDateTime object - */ - private LocalDateTime parseNatural(String natural) { - Parser parser = new Parser(); - List groups = parser.parse(natural); - Date date = null; - try { - date = groups.get(0).getDates().get(0); - } catch (IndexOutOfBoundsException e) { - System.out.println("Error!"); // TODO - return null; - } - LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); - return DateUtil.floorDate(ldt); - } - - /** - * Extracts the natural dates from parsedResult. - * - * @param parsedResult - * @return { naturalOn, naturalFrom, naturalTo } or null if no date provided - */ - private String[] parseDates(Map parsedResult) { - String naturalFrom = (String) null; - String naturalTo = (String) null; - String naturalOn = (String) null; - - if (parsedResult.get("time") == null) { - if (parsedResult.get("timeFrom") != null) { - naturalFrom = parsedResult.get("timeFrom")[RESULT]; - } - if (parsedResult.get("timeTo") != null) { - naturalTo = parsedResult.get("timeTo")[RESULT]; - } - } else { - naturalOn = parsedResult.get("time")[RESULT]; - } - - if (naturalFrom != null || naturalTo != null || naturalOn != null) { - return new String[] { naturalOn, naturalFrom, naturalTo }; - } else { - return null; - } - } - - /** - * Extracts the intended CalendarItem type specify from parsedResult. - * - * @param parsedResult - * @return true if Task or event is not specify, false if either Task or Event specify - */ - private boolean parseDeleteAllType (Map parsedResult) { - return !(parsedResult.get("eventType") != null); - } - - /** - * Extracts the intended CalendarItem type from parsedResult. - * - * @param parsedResult - * @return true if Task, false if Event - */ - private boolean parseIsTask (Map parsedResult) { - return parsedResult.get("eventType")[KEYWORD].contains("task"); - } - - /** ================ FORMATTING OF SUCCESS/ERROR MESSAGE ================== **/ - - /** - * display error message due to invalid clear command - * - * @param input - * based on user input - * @param parsedDate - * the date entered by the user - * @param deleteAll - * true if no CalendarItem type provided, isTask will be ignored - * @param isTask - * true if task keyword, false if event keyword is provided - */ - private void displayErrorMessage(String input, String[] parsedDate, boolean deleteAll, boolean isTask) { - String consoleDisplayMessage = String.format("You have entered : %s.",input); - String commandLineMessage = COMMAND_WORD; - if (!deleteAll) { - if (isTask) { - commandLineMessage = String.format("%s %s", commandLineMessage, "task"); - } else { - commandLineMessage = String.format("%s %s", commandLineMessage, "event"); - } - } - if (parsedDate != null) { - if (parsedDate[0] != null) { - commandLineMessage = String.format("%s by ", commandLineMessage); - } else if (parsedDate[1] != null && parsedDate[2] != null) { - commandLineMessage = String.format("%s from to ", commandLineMessage); - } else if (parsedDate[1] != null) { - commandLineMessage = String.format("%s from ", commandLineMessage); - } else { - commandLineMessage = String.format("%s to ", commandLineMessage); - } - } - Renderer.renderDisambiguation(commandLineMessage, consoleDisplayMessage); - } - } From 975785005526cf218a546be8e8d2212320c8a8c0 Mon Sep 17 00:00:00 2001 From: louietyj Date: Sun, 6 Nov 2016 03:53:05 +0800 Subject: [PATCH 2/5] destroyTasks and destroyEvents --- .../java/seedu/todo/models/TodoListDB.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/seedu/todo/models/TodoListDB.java b/src/main/java/seedu/todo/models/TodoListDB.java index 3d2ae7d0a262..81de8a6c1bc4 100644 --- a/src/main/java/seedu/todo/models/TodoListDB.java +++ b/src/main/java/seedu/todo/models/TodoListDB.java @@ -181,6 +181,15 @@ public boolean destroyTask(Task task) { return save(); } + /** + * Destroys all `tasks` from the DB. + * + * @param tasks Tasks to remove + */ + public void destroyTasks(List tasks) { + tasks.removeAll(tasks); + } + /** * Destroys all Task in the DB and persists the commit. * @@ -238,6 +247,15 @@ public boolean destroyEvent(Event event) { return save(); } + /** + * Destroys all `events` from the DB. + * + * @param tasks Tasks to remove + */ + public void destroyEvents(List events) { + events.removeAll(events); + } + /** * Destroys all Event in the DB and persists the commit. * From 7dbb0da0b81188b8a41bba1b479b5b2956b07e33 Mon Sep 17 00:00:00 2001 From: louietyj Date: Sun, 6 Nov 2016 04:01:46 +0800 Subject: [PATCH 3/5] Fix naming conflicts --- src/main/java/seedu/todo/models/TodoListDB.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/todo/models/TodoListDB.java b/src/main/java/seedu/todo/models/TodoListDB.java index 81de8a6c1bc4..8b277ea19e2c 100644 --- a/src/main/java/seedu/todo/models/TodoListDB.java +++ b/src/main/java/seedu/todo/models/TodoListDB.java @@ -186,8 +186,8 @@ public boolean destroyTask(Task task) { * * @param tasks Tasks to remove */ - public void destroyTasks(List tasks) { - tasks.removeAll(tasks); + public void destroyTasks(List clearTasks) { + tasks.removeAll(clearTasks); } /** @@ -252,8 +252,8 @@ public boolean destroyEvent(Event event) { * * @param tasks Tasks to remove */ - public void destroyEvents(List events) { - events.removeAll(events); + public void destroyEvents(List clearEvents) { + events.removeAll(clearEvents); } /** From 72e7f3d6cf21d2eed7959f2e8a951415152da9e6 Mon Sep 17 00:00:00 2001 From: louietyj Date: Sun, 6 Nov 2016 04:03:24 +0800 Subject: [PATCH 4/5] ClearController done, woohoo! --- .../todo/controllers/ClearController.java | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/todo/controllers/ClearController.java b/src/main/java/seedu/todo/controllers/ClearController.java index 326453e1e474..cc867aa6a532 100644 --- a/src/main/java/seedu/todo/controllers/ClearController.java +++ b/src/main/java/seedu/todo/controllers/ClearController.java @@ -1,9 +1,16 @@ package seedu.todo.controllers; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import seedu.todo.commons.exceptions.InvalidNaturalDateException; import seedu.todo.commons.exceptions.ParseException; +import seedu.todo.controllers.concerns.CalendarItemFilter; +import seedu.todo.controllers.concerns.Renderer; import seedu.todo.controllers.concerns.Tokenizer; +import seedu.todo.models.Event; +import seedu.todo.models.Task; import seedu.todo.models.TodoListDB; /** @@ -18,7 +25,7 @@ public class ClearController implements Controller { private static final String DESCRIPTION = "Clear all tasks/events or by specify date."; private static final String COMMAND_SYNTAX = "clear [task/event] [on date]"; private static final String COMMAND_WORD = "clear"; - private static final String MESSAGE_CLEAR_NO_ITEM_FOUND = "No item found!"; + 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 CommandDefinition commandDefinition = @@ -35,6 +42,39 @@ public float inputConfidence(String input) { @Override public void process(String input) throws ParseException { + // Tokenize input + Map parsedResult = + Tokenizer.tokenize(CalendarItemFilter.getFilterTokenDefinitions(), input); + // Decide if task/event/both + boolean[] isTaskEvent = CalendarItemFilter.parseIsTaskEvent(parsedResult); + boolean filterTask = isTaskEvent[0]; + boolean filterEvent = isTaskEvent[1]; + + List clearTasks = new ArrayList(); + List clearEvents = new ArrayList(); + try { + if (filterTask) { + clearTasks = CalendarItemFilter.filterTasks(parsedResult); + } + if (filterEvent) { + clearEvents = CalendarItemFilter.filterEvents(parsedResult); + } + } catch (InvalidNaturalDateException e) { + renderDisambiguation(parsedResult); + return; + } + + // Clear them all! + TodoListDB db = TodoListDB.getInstance(); + db.destroyTasks(clearTasks); + db.destroyEvents(clearEvents); + db.save(); + + Renderer.renderIndex(db, "Done!"); + } + + private void renderDisambiguation(Map parsedResult) { + return; } } From 37a4f77037e332ee7da3ff9104b05faca59d4dd1 Mon Sep 17 00:00:00 2001 From: louietyj Date: Sun, 6 Nov 2016 04:03:45 +0800 Subject: [PATCH 5/5] This now belongs to me -.- --- src/main/java/seedu/todo/controllers/ClearController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/todo/controllers/ClearController.java b/src/main/java/seedu/todo/controllers/ClearController.java index cc867aa6a532..45018f1fc2e0 100644 --- a/src/main/java/seedu/todo/controllers/ClearController.java +++ b/src/main/java/seedu/todo/controllers/ClearController.java @@ -16,7 +16,7 @@ /** * Controller to clear task/event by type or status * - * @@author Tiong YaoCong A0139922Y + * @@author A0093907W * */ public class ClearController implements Controller {