diff --git a/docs/DeveloperGuide.md b/docs/DeveloperGuide.md index 708f9f9f28b0..dcca872e6769 100644 --- a/docs/DeveloperGuide.md +++ b/docs/DeveloperGuide.md @@ -545,7 +545,7 @@ Use case ends. **MSS** 1. User requests to change storage file location -2. MESS changes storage file location and saves in that location +2. MESS changes storage file location and saves in that location/loads from that location if file already contains data **Extensions** 2a. The file path may not be valid diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 7764f0600bff..a0f3cde2906b 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -256,7 +256,9 @@ There is no need to save manually. #### Change storage location : `change-to` -Shows a list of tasks and events in the todo list.
+Changes to a new storage location and saves task manager data there
+Note: If new file already has data, then the taskmanager will load that data. + Format: `change-to NEWFILEPATH` Example: * `change-to data/taskmanager.xml` diff --git a/src/main/java/seedu/task/commons/events/model/ReloadFromNewFileEvent.java b/src/main/java/seedu/task/commons/events/model/ReloadFromNewFileEvent.java new file mode 100644 index 000000000000..b07ef9c75707 --- /dev/null +++ b/src/main/java/seedu/task/commons/events/model/ReloadFromNewFileEvent.java @@ -0,0 +1,25 @@ +//@@author A0144939R +package seedu.task.commons.events.model; + +import java.util.Optional; +import seedu.task.commons.events.BaseEvent; +import seedu.task.model.ReadOnlyTaskManager; +import seedu.task.model.TaskManager; + +/** Indicates that the user wishes to load from an existing file*/ +public class ReloadFromNewFileEvent extends BaseEvent { + + public final String filePath; + public final ReadOnlyTaskManager taskManager; + + public ReloadFromNewFileEvent(String newFilePath, Optional newTaskManager) { + this.filePath = newFilePath; + this.taskManager = newTaskManager.orElse(new TaskManager()); + } + + @Override + public String toString() { + return "The new file path is "+filePath; + } + +} \ No newline at end of file diff --git a/src/main/java/seedu/task/commons/events/storage/FilePathChangedEvent.java b/src/main/java/seedu/task/commons/events/storage/FilePathChangedEvent.java index 8ec0203c5b0b..3e7af95f8bd2 100644 --- a/src/main/java/seedu/task/commons/events/storage/FilePathChangedEvent.java +++ b/src/main/java/seedu/task/commons/events/storage/FilePathChangedEvent.java @@ -2,6 +2,7 @@ package seedu.task.commons.events.storage; import seedu.task.commons.events.BaseEvent; + import seedu.task.model.ReadOnlyTaskManager; /** Indicates that the user has specified a new file path*/ diff --git a/src/main/java/seedu/task/model/ModelManager.java b/src/main/java/seedu/task/model/ModelManager.java index 7d3db20c8604..0a5e4d9802a7 100644 --- a/src/main/java/seedu/task/model/ModelManager.java +++ b/src/main/java/seedu/task/model/ModelManager.java @@ -3,23 +3,33 @@ import javafx.collections.transformation.FilteredList; import javafx.collections.transformation.SortedList; import seedu.task.commons.core.ComponentManager; +import seedu.task.commons.core.Config; import seedu.task.commons.core.EventsCenter; import seedu.task.commons.core.LogsCenter; import seedu.task.commons.core.UnmodifiableObservableList; +import seedu.task.commons.events.model.ReloadFromNewFileEvent; import seedu.task.commons.events.model.TaskManagerChangedEvent; +import seedu.task.commons.events.storage.ConfigFilePathChangedEvent; +import seedu.task.commons.events.storage.FilePathChangedEvent; +import seedu.task.commons.exceptions.DataConversionException; import seedu.task.commons.events.ui.JumpToListRequestEvent; import seedu.task.commons.logic.CommandKeys.Commands; +import seedu.task.commons.util.ConfigUtil; import seedu.task.commons.util.StringUtil; import seedu.task.model.tag.Tag; import seedu.task.model.task.DateTime; import seedu.task.model.task.ReadOnlyTask; import seedu.task.model.task.Task; import seedu.task.model.task.UniqueTaskList; +import seedu.task.storage.Storage; +import java.io.IOException; import java.util.HashMap; import java.util.Set; import java.util.logging.Logger; +import com.google.common.eventbus.Subscribe; + /** * Represents the in-memory model of the task list data. All changes to any * model should be synchronized. @@ -349,4 +359,11 @@ public boolean run(ReadOnlyTask task) { return (task.getComplete() == this.isCompleted); } } + + //@@author A0144939R + @Subscribe + public void handleReloadFromNewFileEvent(ReloadFromNewFileEvent event) throws DataConversionException { + logger.info(LogsCenter.getEventHandlingLogMessage(event, "Load from new file path requested")); + resetData(event.taskManager); + } } diff --git a/src/main/java/seedu/task/storage/StorageManager.java b/src/main/java/seedu/task/storage/StorageManager.java index 803a1544c730..b237a1ea300a 100644 --- a/src/main/java/seedu/task/storage/StorageManager.java +++ b/src/main/java/seedu/task/storage/StorageManager.java @@ -5,16 +5,20 @@ import seedu.task.commons.core.ComponentManager; import seedu.task.commons.core.Config; import seedu.task.commons.core.LogsCenter; +import seedu.task.commons.events.model.ReloadFromNewFileEvent; import seedu.task.commons.events.model.TaskManagerChangedEvent; import seedu.task.commons.events.storage.ConfigFilePathChangedEvent; import seedu.task.commons.events.storage.DataSavingExceptionEvent; import seedu.task.commons.events.storage.FilePathChangedEvent; import seedu.task.commons.exceptions.DataConversionException; import seedu.task.commons.util.ConfigUtil; +import seedu.task.model.Model; import seedu.task.model.ReadOnlyTaskManager; import seedu.task.model.UserPrefs; +import java.io.File; import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; import java.util.Optional; import java.util.logging.Logger; @@ -107,17 +111,51 @@ public void handleTaskManagerChangedEvent(TaskManagerChangedEvent event) { //@@author A0144939R @Subscribe public void handleFilePathChangedEvent(FilePathChangedEvent event) throws DataConversionException { - //ReadOnlyTaskManager taskManager + logger.info(LogsCenter.getEventHandlingLogMessage(event, "File path change requested, attempting to update file path")); + String newFilePath = event.newFilePath; + String oldFilePath = getTaskManagerFilePath(); try { - logger.info(LogsCenter.getEventHandlingLogMessage(event, "File path change requested, updating file path")); - setTaskManagerFilePath(event.newFilePath); + //save data saveTaskManager(event.taskManager); + + //change file path + setTaskManagerFilePath(newFilePath); + + if(!isFileAlreadyPresent(newFilePath)) { + //save to new location + saveTaskManager(event.taskManager); + } else { + //load from pre existing file + Optional newTaskManager = readTaskManager(newFilePath); + raise(new ReloadFromNewFileEvent(newFilePath, newTaskManager)); + } + config.setTaskManagerFilePath(event.newFilePath); ConfigUtil.saveConfig(config, config.getFilePath()); raise(new ConfigFilePathChangedEvent(event.newFilePath)); - } catch (IOException e) { - e.printStackTrace(); + + } catch (IOException e) { + + logger.info(LogsCenter.getEventHandlingLogMessage(event, "Error occured on saving/loading")); + raise(new DataSavingExceptionEvent(e)); + //clean up + setTaskManagerFilePath(oldFilePath); + + } catch(DataConversionException e) { + + logger.info(LogsCenter.getEventHandlingLogMessage(event, "Error occured on loading from new file")); + //clean up + setTaskManagerFilePath(oldFilePath); } } - + + /** + * Returns true if the file already exists + * @param filePath + * @return boolean value indicating whether file already exists + */ + private boolean isFileAlreadyPresent(String filePath) { + File newFile = new File(filePath); + return newFile.exists() && !newFile.isDirectory(); + } }