Skip to content

Commit

Permalink
Merge 2c4582a into ae7d85e
Browse files Browse the repository at this point in the history
  • Loading branch information
QzSG authored Oct 24, 2018
2 parents ae7d85e + 2c4582a commit c7f02bc
Show file tree
Hide file tree
Showing 33 changed files with 569 additions and 162 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/commons/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public boolean equals(Object other) {

return Objects.equals(appTitle, o.appTitle)
&& Objects.equals(logLevel, o.logLevel)
&& Objects.equals(userPrefsFilePath, o.userPrefsFilePath);
&& Objects.equals(userPrefsFilePath.toAbsolutePath(), o.userPrefsFilePath.toAbsolutePath());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
package seedu.address.commons.events.model;

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.ReadOnlyAddressBook;

/** Indicates a AddressBook restore request*/
public class AddressBookLocalRestoreEvent extends BaseEvent {

public final ReadOnlyAddressBook readOnlyAddressBook;

public AddressBookLocalRestoreEvent(ReadOnlyAddressBook readOnlyAddressBook) {
this.readOnlyAddressBook = readOnlyAddressBook;
}

@Override
public String toString() {
return null;
return "Restoring local backup from " + readOnlyAddressBook.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@@author QzSG
package seedu.address.commons.events.model;

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.ReadOnlyExpenseBook;

/** Indicates a AddressBook restore request*/
public class ExpenseBookLocalRestoreEvent extends BaseEvent {

public final ReadOnlyExpenseBook readOnlyExpenseBook;

public ExpenseBookLocalRestoreEvent(ReadOnlyExpenseBook readOnlyExpenseBook) {
this.readOnlyExpenseBook = readOnlyExpenseBook;
}

@Override
public String toString() {
return "Restoring local backup from " + readOnlyExpenseBook.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@@author QzSG
package seedu.address.commons.events.model;

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.ReadOnlyExpenseBook;

/** Indicates a ExpenseBook online restore request*/
public class ExpenseBookOnlineRestoreEvent extends BaseEvent {

public final ReadOnlyExpenseBook data;

public ExpenseBookOnlineRestoreEvent(ReadOnlyExpenseBook data) {
this.data = data;
}
@Override
public String toString() {
return "Restoring online backup";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.address.commons.events.model;

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.UserPrefs;

/** Indicates the AddressBook in the model has changed*/
public class UserPrefsChangedEvent extends BaseEvent {

public final UserPrefs data;

public UserPrefsChangedEvent(UserPrefs data) {
this.data = data;
}

@Override
public String toString() {
return "User Prefs Update\n" + data.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package seedu.address.commons.events.storage;

import java.nio.file.Path;

import seedu.address.commons.events.BaseEvent;

//@@author QzSG

/** Indicates a request for local restore*/
public class LocalRestoreEvent extends BaseEvent {

public final Path addressBookPath;
public final Path expenseBookPath;

public LocalRestoreEvent(Path addressBookPath, Path expenseBookPath) {
this.addressBookPath = addressBookPath;
this.expenseBookPath = expenseBookPath;
}

@Override
public String toString() {
return "Restoring local backup";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyExpenseBook;
import seedu.address.storage.OnlineStorage;

//@@author QzSG
/** Indicates a request for online backup*/
public class OnlineBackupEvent extends BaseEvent {

public final OnlineStorage.OnlineStorageType target;
public final ReadOnlyAddressBook data;
public final String fileName;
public final OnlineStorage.Type target;
public final ReadOnlyAddressBook addressData;
public final ReadOnlyExpenseBook expenseData;
public final Optional<String> authToken;

public OnlineBackupEvent(OnlineStorage.OnlineStorageType target, ReadOnlyAddressBook data,
String fileName, Optional<String> authToken) {
public OnlineBackupEvent(OnlineStorage.Type target, ReadOnlyAddressBook addressData,
ReadOnlyExpenseBook expenseData, Optional<String> authToken) {
this.target = target;
this.data = data;
this.fileName = fileName;
this.addressData = addressData;
this.expenseData = expenseData;
this.authToken = authToken;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.address.commons.events.storage;

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.UserPrefs;
import seedu.address.storage.OnlineStorage;

//@@author QzSG

/** Indicates a request for online backup*/
public class OnlineBackupSuccessResultEvent extends BaseEvent {

public final OnlineStorage.Type target;
public final UserPrefs.TargetBook targetBook;
public final String ref;

public OnlineBackupSuccessResultEvent(OnlineStorage.Type target, UserPrefs.TargetBook targetBook, String ref) {
this.target = target;
this.targetBook = targetBook;
this.ref = ref;
}

@Override
public String toString() {
return "Signaling user preference update with success reference from specific online service";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import java.util.Optional;

import seedu.address.commons.events.BaseEvent;
import seedu.address.model.UserPrefs;
import seedu.address.storage.OnlineStorage;

//@@author QzSG

/** Indicates a request for online backup*/
public class OnlineRestoreEvent extends BaseEvent {

public final OnlineStorage.OnlineStorageType target;
public final OnlineStorage.Type target;
public final UserPrefs.TargetBook targetBook;
public final String ref;
public final Optional<String> authToken;

public OnlineRestoreEvent(OnlineStorage.OnlineStorageType target, String ref, Optional<String> authToken) {
public OnlineRestoreEvent(OnlineStorage.Type target, UserPrefs.TargetBook targetBook,
String ref, Optional<String> authToken) {
this.target = target;
this.targetBook = targetBook;
this.ref = ref;
this.authToken = authToken;
}
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/seedu/address/logic/commands/BackupCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyExpenseBook;
import seedu.address.storage.OnlineStorage;

//@@author QzSG
Expand All @@ -30,17 +31,17 @@ public class BackupCommand extends Command {

private Optional<Path> backupPath;
private boolean isLocal = true;
private OnlineStorage.OnlineStorageType target;
private OnlineStorage.Type target;
private Optional<String> authToken;

/**
* Creates a BackupCommand to backup data to storage
*/
public BackupCommand(Optional<Path> backupPath, boolean isLocal,
Optional<OnlineStorage.OnlineStorageType> target, Optional<String> authToken) {
Optional<OnlineStorage.Type> target, Optional<String> authToken) {
this.backupPath = backupPath;
this.isLocal = isLocal;
this.target = target.orElse(OnlineStorage.OnlineStorageType.GITHUB);
this.target = target.orElse(OnlineStorage.Type.GITHUB);
this.authToken = authToken;

}
Expand All @@ -49,10 +50,12 @@ public BackupCommand(Optional<Path> backupPath, boolean isLocal,
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
if (isLocal) {
model.backupAddressBookLocal(retrievePath(model));
//model.backupAddressBookLocal(retrievePath(model));
model.backupAddressBookLocal(retrieveAddressBookPath(model));
model.backupExpenseBookLocal(retrieveExpenseBookPath(model));
return new CommandResult(String.format(MESSAGE_SUCCESS, retrievePath(model).toString()));
} else {
onlineBackupTask(model.getAddressBook());
onlineBackupTask(model.getAddressBook(), model.getExpenseBook());
return new CommandResult(String.format(MESSAGE_SUCCESS, "GitHub Gists"));
}

Expand All @@ -62,6 +65,14 @@ private Path retrievePath(Model model) {
return backupPath.orElse(model.getUserPrefs().getAddressBookBackupFilePath());
}

private Path retrieveAddressBookPath(Model model) {
return model.getUserPrefs().getAddressBookBackupFilePath();
}

private Path retrieveExpenseBookPath(Model model) {
return model.getUserPrefs().getExpenseBookBackupFilePath();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand All @@ -73,7 +84,8 @@ public boolean equals(Object other) {
* Raises event to start online backup
* @param addressBook
*/
private void onlineBackupTask(ReadOnlyAddressBook addressBook) {
EventsCenter.getInstance().post(new OnlineBackupEvent(target, addressBook, "AddressBook.bak", authToken));
private void onlineBackupTask(ReadOnlyAddressBook addressBook, ReadOnlyExpenseBook expenseBook) {
EventsCenter.getInstance().post(
new OnlineBackupEvent(target, addressBook, expenseBook, authToken));
}
}
56 changes: 37 additions & 19 deletions src/main/java/seedu/address/logic/commands/RestoreCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import java.util.Optional;

import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.events.storage.LocalRestoreEvent;
import seedu.address.commons.events.storage.OnlineRestoreEvent;
import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.UserPrefs;
import seedu.address.storage.OnlineStorage;

/**
Expand All @@ -20,27 +22,29 @@ public class RestoreCommand extends Command {
public static final String COMMAND_WORD = "restore";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Restore student planner data from location specified "
+ "(restores from default backup data path if not provided)\n"
+ ": Restore student planner addressData from location specified "
+ "(restores from default backup addressData path if not provided)\n"
+ "Parameters: [github authToken] OR [PATH] (must be a writable path)\n"
+ "Example: " + COMMAND_WORD + " data\\addressbook.bak OR\n"
+ "Example: " + COMMAND_WORD + " addressData\\addressbook.bak OR\n"
+ "Example: " + COMMAND_WORD + " github my_personal_access_token";

public static final String MESSAGE_SUCCESS = "Restoring Backup from %s";

public static final String MESSAGE_FAILURE = "Please perform an online backup using %s first or set relevant"
+ " settings in user prefs";
public static final String MESSAGE_INVALID = "Invalid online service provided";
private Optional<Path> backupPath;
private boolean isLocal = true;
private OnlineStorage.OnlineStorageType target;
private OnlineStorage.Type target;
private Optional<String> authToken;

/**
* Creates a RestoreCommand to backup data to storage
* Creates a RestoreCommand to backup addressData to storage
*/
public RestoreCommand(Optional<Path> backupPath, boolean isLocal,
Optional<OnlineStorage.OnlineStorageType> target, Optional<String> authToken) {
Optional<OnlineStorage.Type> target, Optional<String> authToken) {
this.backupPath = backupPath;
this.isLocal = isLocal;
this.target = target.orElse(OnlineStorage.OnlineStorageType.GITHUB);
this.target = target.orElse(OnlineStorage.Type.GITHUB);
this.authToken = authToken;

}
Expand All @@ -49,11 +53,23 @@ public RestoreCommand(Optional<Path> backupPath, boolean isLocal,
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
if (isLocal) {
model.restoreAddressBookLocal(retrievePath(model));
return new CommandResult(String.format(MESSAGE_SUCCESS, retrievePath(model).toString()));
EventsCenter.getInstance().post(new LocalRestoreEvent(
retrieveAddressBookPath(model), retrieveExpenseBookPath(model)));
return new CommandResult(String.format(MESSAGE_SUCCESS, retrievePath(model).getParent().toString()));
} else {
onlineRestoreTask();
return new CommandResult(String.format(MESSAGE_SUCCESS, "GitHub Gists"));
if (target == OnlineStorage.Type.GITHUB) {
String gistId = model.getUserPrefs().getAddressBookGistId();
if (gistId == null) {
return new CommandResult(String.format(MESSAGE_FAILURE, ": backup github [personal_access_token]"));
}
EventsCenter.getInstance().post(new OnlineRestoreEvent(target, UserPrefs.TargetBook.AddressBook,
model.getUserPrefs().getAddressBookGistId(), authToken));
EventsCenter.getInstance().post(new OnlineRestoreEvent(target, UserPrefs.TargetBook.ExpenseBook,
model.getUserPrefs().getExpenseBookGistId(), authToken));
return new CommandResult(String.format(MESSAGE_SUCCESS, "GitHub Gists"));
} else {
return new CommandResult(MESSAGE_INVALID);
}
}

}
Expand All @@ -62,17 +78,19 @@ private Path retrievePath(Model model) {
return backupPath.orElse(model.getUserPrefs().getAddressBookBackupFilePath());
}

private Path retrieveAddressBookPath(Model model) {
return model.getUserPrefs().getAddressBookBackupFilePath();
}

private Path retrieveExpenseBookPath(Model model) {
return model.getUserPrefs().getExpenseBookBackupFilePath();
}


@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof RestoreCommand // instanceof handles nulls
&& backupPath.equals(((RestoreCommand) other).backupPath));
}

/**
* Raises event to start online restore
*/
private void onlineRestoreTask() {
EventsCenter.getInstance().post(new OnlineRestoreEvent(target, "53b262c0c41a18747dd3978941901057", authToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ public BackupCommand parse(String args) throws ParseException {
*/
private BackupCommand parseArguments(String args) throws ParseException {
List<String> argumentList = Arrays.asList(args.split(" ", 0));
/* temp removal of custom backup location
if (argumentList.size() == 1) {
return new BackupCommand(ParserUtil.parsePath(argumentList.get(0)), true,
Optional.empty(), Optional.empty());
}
}*/
if (argumentList.size() == 2 && argumentList.get(0).toLowerCase().equals("github")) {
return new BackupCommand(Optional.empty(), false,
Optional.ofNullable(OnlineStorage.OnlineStorageType.GITHUB),
Optional.ofNullable(OnlineStorage.Type.GITHUB),
Optional.ofNullable(argumentList.get(1)));
}
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, BackupCommand.MESSAGE_USAGE));
Expand Down
Loading

0 comments on commit c7f02bc

Please sign in to comment.