Skip to content

Commit

Permalink
Add functions to save and load manually
Browse files Browse the repository at this point in the history
  • Loading branch information
JJtan2002 committed Oct 18, 2024
1 parent 38da93a commit b66a590
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void init() throws Exception {

UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
AddressBookStorage addressBookStorage =
new JsonAddressBookStorage(userPrefs.getAddressBookFilePath(), userPrefs.getManualSaveFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);

model = initModelManager(storage, userPrefs);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/UserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class UserPrefs implements ReadOnlyUserPrefs {

private GuiSettings guiSettings = new GuiSettings();
private Path addressBookFilePath = Paths.get("data" , "addressbook.json");
private Path manualSaveFilePath = Paths.get("data", "save.json");

/**
* Creates a {@code UserPrefs} with default values.
Expand Down Expand Up @@ -56,6 +57,15 @@ public void setAddressBookFilePath(Path addressBookFilePath) {
this.addressBookFilePath = addressBookFilePath;
}

public Path getManualSaveFilePath() {
return manualSaveFilePath;
}

public void setManualSaveFilePath(Path manualSaveFilePath) {
requireNonNull(manualSaveFilePath);
this.manualSaveFilePath = manualSaveFilePath;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/model/VersionedAddressBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.model;

import java.util.ArrayList;
import java.util.List;

/**
* {@code AddressBook} that tracks its state across command executions
*/
public class VersionedAddressBook extends AddressBook {
private final List<ReadOnlyAddressBook> addressBookStates;
private int currentStatePointer;

/**
* Constructs a {@code VersionedAddressBook} with the initial state of the address book.
* The address book will start with the specified initial state and the current state pointer
* will be set to the initial state.
*
* @param initialState The initial state of the address book to be used as the starting point.
* Cannot be null.
*/
public VersionedAddressBook(ReadOnlyAddressBook initialState) {
super(initialState);

addressBookStates = new ArrayList<>();
addressBookStates.add(new AddressBook(initialState));
currentStatePointer = 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public interface AddressBookStorage {
*/
void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException;

Path getManualSaveFilePath();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ public class JsonAddressBookStorage implements AddressBookStorage {
private static final Logger logger = LogsCenter.getLogger(JsonAddressBookStorage.class);

private Path filePath;
private Path manualSaveFilePath;

public JsonAddressBookStorage(Path filePath) {
public JsonAddressBookStorage(Path filePath, Path manualSaveFilePath) {
this.filePath = filePath;
this.manualSaveFilePath = manualSaveFilePath;
}

public Path getAddressBookFilePath() {
return filePath;
}

public Path getManualSaveFilePath() {
return manualSaveFilePath;
}

@Override
public Optional<ReadOnlyAddressBook> readAddressBook() throws DataLoadingException {
return readAddressBook(filePath);
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/seedu/address/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class StorageManager implements Storage {
private AddressBookStorage addressBookStorage;
private UserPrefsStorage userPrefsStorage;

// Path for the manual save/load file
private Path manualSaveFilePath;

/**
* Creates a {@code StorageManager} with the given {@code AddressBookStorage} and {@code UserPrefStorage}.
*/
Expand Down Expand Up @@ -75,4 +78,38 @@ public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) thro
addressBookStorage.saveAddressBook(addressBook, filePath);
}

// ================ Manual Save/Load Methods ==============================

public Path getManualSaveFilePath() {
return addressBookStorage.getManualSaveFilePath();
}

/**
* Manually saves the address book to the specified file path.
*
* @param addressBook The address book data to save.
* @throws IOException If there is an error saving the file.
*/
public void saveAddressBookManually(ReadOnlyAddressBook addressBook) throws IOException {
if (manualSaveFilePath == null) {
throw new IOException("Manual save file path is not set.");
}
logger.fine("Attempting to manually write to file: " + manualSaveFilePath);
addressBookStorage.saveAddressBook(addressBook, manualSaveFilePath);
}

/**
* Manually loads the address book from the specified file path.
*
* @return An optional containing the address book if successfully loaded, otherwise an empty optional.
* @throws DataLoadingException If there is an error loading the file.
*/
public Optional<ReadOnlyAddressBook> loadAddressBookManually() throws Exception {
if (manualSaveFilePath == null) {
throw new Exception("Manual load file path is not set.");
}
logger.fine("Attempting to manually read from file: " + manualSaveFilePath);
return addressBookStorage.readAddressBook(manualSaveFilePath);
}

}
5 changes: 3 additions & 2 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class LogicManagerTest {
@BeforeEach
public void setUp() {
JsonAddressBookStorage addressBookStorage =
new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json"));
new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json"),
temporaryFolder.resolve("save.json"));
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.resolve("userPrefs.json"));
StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage);
logic = new LogicManager(model, storage);
Expand Down Expand Up @@ -150,7 +151,7 @@ private void assertCommandFailureForExceptionFromStorage(IOException e, String e
Path prefPath = temporaryFolder.resolve("ExceptionUserPrefs.json");

// Inject LogicManager with an AddressBookStorage that throws the IOException e when saving
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath) {
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath, temporaryFolder.resolve("save.json")) {
@Override
public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void readAddressBook_nullFilePath_throwsNullPointerException() {
}

private java.util.Optional<ReadOnlyAddressBook> readAddressBook(String filePath) throws Exception {
return new JsonAddressBookStorage(Paths.get(filePath)).readAddressBook(addToTestDataPathIfNotNull(filePath));
return new JsonAddressBookStorage(Paths.get(filePath), Paths.get("save.json")).readAddressBook(addToTestDataPathIfNotNull(filePath));
}

private Path addToTestDataPathIfNotNull(String prefsFileInTestDataFolder) {
Expand Down Expand Up @@ -64,7 +64,7 @@ public void readAddressBook_invalidAndValidPersonAddressBook_throwDataLoadingExc
public void readAndSaveAddressBook_allInOrder_success() throws Exception {
Path filePath = testFolder.resolve("TempAddressBook.json");
AddressBook original = getTypicalAddressBook();
JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath);
JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath, Paths.get("save.json"));

// Save in new file and read back
jsonAddressBookStorage.saveAddressBook(original, filePath);
Expand Down Expand Up @@ -96,7 +96,7 @@ public void saveAddressBook_nullAddressBook_throwsNullPointerException() {
*/
private void saveAddressBook(ReadOnlyAddressBook addressBook, String filePath) {
try {
new JsonAddressBookStorage(Paths.get(filePath))
new JsonAddressBookStorage(Paths.get(filePath), Paths.get("save.json"))
.saveAddressBook(addressBook, addToTestDataPathIfNotNull(filePath));
} catch (IOException ioe) {
throw new AssertionError("There should not be an error writing to the file.", ioe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class StorageManagerTest {

@BeforeEach
public void setUp() {
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab"));
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab"), getTempFilePath("save"));
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(getTempFilePath("prefs"));
storageManager = new StorageManager(addressBookStorage, userPrefsStorage);
}
Expand Down

0 comments on commit b66a590

Please sign in to comment.