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

Show a warning message if database is corrupted #146

Merged
merged 13 commits into from
Mar 28, 2024
58 changes: 43 additions & 15 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.UserPrefs;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.JsonAddressBookStorage;
Expand All @@ -32,34 +34,39 @@
*/
public class MainApp extends Application {

public static final Version VERSION = new Version(0, 2, 2, true);
public static final Version VERSION = new Version(1, 3, 0, true);

Check warning on line 37 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L37

Added line #L37 was not covered by tests

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

protected Ui ui;
protected Logic logic;
protected Storage storage;
protected Model model;
protected Config config;
private Ui ui;
private Storage storage;
private Model model;

private String initialWelcomeMessage;

@Override
public void init() throws Exception {
logger.info("=============================[ Initializing AddressBook ]===========================");
logger.info("=============================[ Initializing AssetBook ]===========================");

Check warning on line 49 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L49

Added line #L49 was not covered by tests
super.init();

AppParameters appParameters = AppParameters.parse(getParameters());
config = initConfig(appParameters.getConfigPath());
Config config = initConfig(appParameters.getConfigPath());

Check warning on line 53 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L53

Added line #L53 was not covered by tests
initLogging(config);

// initialize user preferences
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);

// initialize storage
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);
ReadOnlyAddressBook initialAddressBook = initAddressBook(storage);

Check warning on line 63 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L63

Added line #L63 was not covered by tests

model = new ModelManager(storage.readInitialAddressBook(), userPrefs);

logic = new LogicManager(model, storage);
// initialize model and logic
model = new ModelManager(initialAddressBook, userPrefs);
Logic logic = new LogicManager(model, storage);

Check warning on line 67 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L66-L67

Added lines #L66 - L67 were not covered by tests

// initialize ui
ui = new UiManager(logic);
}

Expand All @@ -72,7 +79,7 @@
* The default file path {@code Config#DEFAULT_CONFIG_FILE} will be used instead
* if {@code configFilePath} is null.
*/
protected Config initConfig(Path configFilePath) {
private static Config initConfig(Path configFilePath) {
Config initializedConfig;
Path configFilePathUsed;

Expand Down Expand Up @@ -111,7 +118,7 @@
* or a new {@code UserPrefs} with default configuration if errors occur when
* reading from the file.
*/
protected UserPrefs initPrefs(UserPrefsStorage storage) {
private static UserPrefs initPrefs(UserPrefsStorage storage) {
Path prefsFilePath = storage.getUserPrefsFilePath();
logger.info("Using preference file : " + prefsFilePath);

Expand All @@ -138,19 +145,40 @@
return initializedPrefs;
}

/**
* Reads the filepath stored within a {@cide Storage} object and returns a {@code ReadOnlyAddressBook}.
*
* @param storage the storage object.
* @return a read-only address book.
*/
private ReadOnlyAddressBook initAddressBook(Storage storage) {
ReadOnlyAddressBook initialAddressBook;
try {
initialAddressBook = storage.readInitialAddressBook();
initialWelcomeMessage = "Data file loaded successfully.";
} catch (DataLoadingException e) {
initialAddressBook = new AddressBook();
initialWelcomeMessage = "WARNING!! Entering a command will override the old data file.\n"
+ e.getMessage();
}
return initialAddressBook;

Check warning on line 164 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L157-L164

Added lines #L157 - L164 were not covered by tests
}

@Override
public void start(Stage primaryStage) {
logger.info("Starting AddressBook " + MainApp.VERSION);
logger.info("Starting AssetBook " + MainApp.VERSION);

Check warning on line 169 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L169

Added line #L169 was not covered by tests
ui.start(primaryStage);
ui.showMessage(initialWelcomeMessage);

Check warning on line 171 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L171

Added line #L171 was not covered by tests
}

@Override
public void stop() {
logger.info("============================ [ Stopping Address Book ] =============================");
logger.info("============================ [ Stopping Asset Book ] =============================");

Check warning on line 176 in src/main/java/seedu/address/MainApp.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/MainApp.java#L176

Added line #L176 was not covered by tests
try {
storage.saveUserPrefs(model.getUserPrefs());
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public DataLoadingException(Exception cause) {
super(cause);
}

public DataLoadingException(String message) {
super(message);
}
}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public interface Storage extends AddressBookStorage, UserPrefsStorage {
@Override
void saveAddressBook(ReadOnlyAddressBook addressBook) throws StorageException;

ReadOnlyAddressBook readInitialAddressBook();
ReadOnlyAddressBook readInitialAddressBook() throws DataLoadingException;

}
17 changes: 7 additions & 10 deletions src/main/java/seedu/address/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.exceptions.DataLoadingException;
import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.UserPrefs;
Expand All @@ -18,6 +17,7 @@
* Manages storage of AddressBook data in local storage.
*/
public class StorageManager implements Storage {

private static final Logger logger = LogsCenter.getLogger(StorageManager.class);
private AddressBookStorage addressBookStorage;
private UserPrefsStorage userPrefsStorage;
Expand Down Expand Up @@ -84,25 +84,22 @@ public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) thro
* address book.
*/
@Override
public ReadOnlyAddressBook readInitialAddressBook() {
logger.info("Using data file : " + getAddressBookFilePath());

public ReadOnlyAddressBook readInitialAddressBook() throws DataLoadingException {
Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialAddressBook;
try {
addressBookOptional = readAddressBook();
if (addressBookOptional.isEmpty()) {
logger.info("Creating a new data file " + getAddressBookFilePath()
+ " populated with a sample AddressBook.");
}
initialAddressBook = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataLoadingException e) {
logger.warning("Data file at " + getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
initialAddressBook = new AddressBook();
String errorMessage = "Data file at " + getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.";
logger.warning(errorMessage);
throw new DataLoadingException(errorMessage);
}

return initialAddressBook;
return addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
}

}
36 changes: 17 additions & 19 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

private final Logger logger = LogsCenter.getLogger(getClass());

private Stage primaryStage;
private Logic logic;
private final Stage primaryStage;
private final Logic logic;

// Independent Ui parts residing in this Ui container
private PersonListPanel personListPanel;
private ResultDisplay resultDisplay;
private HelpWindow helpWindow;
private final PersonListPanel personListPanel;
private final ResultDisplay resultDisplay = new ResultDisplay();
private final HelpWindow helpWindow = new HelpWindow();

Check warning on line 37 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L36-L37

Added lines #L36 - L37 were not covered by tests

@FXML
private StackPane commandBoxPlaceholder;
Expand All @@ -60,13 +60,16 @@
// Set dependencies
this.primaryStage = primaryStage;
this.logic = logic;
this.personListPanel = new PersonListPanel(logic.getFilteredPersonList());

Check warning on line 63 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L63

Added line #L63 was not covered by tests

// Configure the UI
setWindowDefaultSize(logic.getGuiSettings());

setAccelerators();

helpWindow = new HelpWindow();
primaryStage.show(); // This should be called before creating other UI parts

Check warning on line 70 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L70

Added line #L70 was not covered by tests

fillInnerParts();

Check warning on line 72 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L72

Added line #L72 was not covered by tests
}

public Stage getPrimaryStage() {
Expand Down Expand Up @@ -110,11 +113,9 @@
/**
* Fills up all the placeholders of this window.
*/
void fillInnerParts() {
personListPanel = new PersonListPanel(logic.getFilteredPersonList());
private void fillInnerParts() {
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());

resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());

StatusBarFooter statusBarFooter = new StatusBarFooter(logic.getAddressBookFilePath());
Expand Down Expand Up @@ -148,10 +149,6 @@
}
}

void show() {
primaryStage.show();
}

/**
* Closes the application.
*/
Expand All @@ -164,10 +161,6 @@
primaryStage.hide();
}

public PersonListPanel getPersonListPanel() {
return personListPanel;
}

/**
* Executes the command and returns the result.
*
Expand All @@ -178,10 +171,10 @@
try {
commandResult = logic.execute(commandText);
logger.info("Result: " + commandResult);
resultDisplay.setFeedbackToUser(commandResult);
showMessage(commandResult);

Check warning on line 174 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L174

Added line #L174 was not covered by tests
} catch (CommandException | ParseException | StorageException e) {
logger.info("An error occurred while executing command: " + commandText);
resultDisplay.setFeedbackToUser(e.getMessage());
showMessage(e.getMessage());

Check warning on line 177 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L177

Added line #L177 was not covered by tests
throw e;
}

Expand All @@ -193,4 +186,9 @@
}
return commandResult;
}

public void showMessage(String msg) {
resultDisplay.setFeedbackToUser(msg);
}

Check warning on line 192 in src/main/java/seedu/address/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/MainWindow.java#L191-L192

Added lines #L191 - L192 were not covered by tests

}
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface Ui {
/** Starts the UI (and the App). */
void start(Stage primaryStage);

void showMessage(String msg);

}
9 changes: 6 additions & 3 deletions src/main/java/seedu/address/ui/UiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
private static final Logger logger = LogsCenter.getLogger(UiManager.class);
private static final String ICON_APPLICATION = "/images/address_book_32.png";

private Logic logic;
private final Logic logic;
private MainWindow mainWindow;

/**
Expand All @@ -41,8 +41,6 @@

try {
mainWindow = new MainWindow(primaryStage, logic);
mainWindow.show(); //This should be called before creating other UI parts
mainWindow.fillInnerParts();

} catch (Throwable e) {
logger.severe(StringUtil.getDetails(e));
Expand Down Expand Up @@ -85,4 +83,9 @@
System.exit(1);
}

@Override
public void showMessage(String msg) {
mainWindow.showMessage(msg);
}

Check warning on line 89 in src/main/java/seedu/address/ui/UiManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/UiManager.java#L88-L89

Added lines #L88 - L89 were not covered by tests

}
10 changes: 5 additions & 5 deletions src/test/java/seedu/address/storage/StorageManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static seedu.address.model.util.SampleDataUtil.getSampleAddressBook;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

Expand Down Expand Up @@ -82,23 +83,22 @@ public void getUserPrefsFilePath() {
}

@Test
public void readInitialAddressBook_noDataFile_sampleAddressBook() {
public void readInitialAddressBook_noDataFile_sampleAddressBook() throws DataLoadingException {
addressBookStorage = new JsonAddressBookStorage(Paths.get("unavailable"));
storageManager = new StorageManager(addressBookStorage, userPrefsStorage);
ReadOnlyAddressBook retrieved = storageManager.readInitialAddressBook();
assertEquals(getSampleAddressBook(), retrieved);
}

@Test
public void readInitialAddressBook_corruptedDataFile_emptyAddressBook() {
public void readInitialAddressBook_corruptedDataFile_throwsDataLoadingException() {
storageManager = new StorageManager(addressBookStorage, userPrefsStorage) {
@Override
public Optional<ReadOnlyAddressBook> readAddressBook() throws DataLoadingException {
throw new DataLoadingException(null);
throw new DataLoadingException("");
}
};
ReadOnlyAddressBook retrieved = storageManager.readInitialAddressBook();
assertEquals(new AddressBook(), new AddressBook(retrieved));
assertThrows(DataLoadingException.class, () -> storageManager.readInitialAddressBook());
}

}