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

Modify change to #109

Merged
merged 10 commits into from
Nov 7, 2016
2 changes: 1 addition & 1 deletion docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ There is no need to save manually.
<!-- @@author A0144939R -->

#### Change storage location : `change-to`
Shows a list of tasks and events in the todo list.<br>
Changes to a new storage location and saves task manager data there<br>
Note: If new file already has data, then the taskmanager will load that data.

Format: `change-to NEWFILEPATH`
Example:
* `change-to data/taskmanager.xml`
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ReadOnlyTaskManager> newTaskManager) {
this.filePath = newFilePath;
this.taskManager = newTaskManager.orElse(new TaskManager());
}

@Override
public String toString() {
return "The new file path is "+filePath;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/seedu/task/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
50 changes: 44 additions & 6 deletions src/main/java/seedu/task/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ReadOnlyTaskManager> 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();
}
}