|
| 1 | +package seedu.address; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.logging.Logger; |
| 7 | + |
| 8 | +import javafx.application.Application; |
| 9 | +import javafx.stage.Stage; |
| 10 | +import seedu.address.commons.core.Config; |
| 11 | +import seedu.address.commons.core.LogsCenter; |
| 12 | +import seedu.address.commons.core.Version; |
| 13 | +import seedu.address.commons.exceptions.DataConversionException; |
| 14 | +import seedu.address.commons.util.ConfigUtil; |
| 15 | +import seedu.address.commons.util.StringUtil; |
| 16 | +import seedu.address.logic.Logic; |
| 17 | +import seedu.address.logic.LogicManager; |
| 18 | +import seedu.address.model.AddressBook; |
| 19 | +import seedu.address.model.Model; |
| 20 | +import seedu.address.model.ModelManager; |
| 21 | +import seedu.address.model.ReadOnlyAddressBook; |
| 22 | +import seedu.address.model.ReadOnlyUserPrefs; |
| 23 | +import seedu.address.model.UserPrefs; |
| 24 | +import seedu.address.model.util.SampleDataUtil; |
| 25 | +import seedu.address.storage.AddressBookStorage; |
| 26 | +import seedu.address.storage.JsonAddressBookStorage; |
| 27 | +import seedu.address.storage.JsonUserPrefsStorage; |
| 28 | +import seedu.address.storage.Storage; |
| 29 | +import seedu.address.storage.StorageManager; |
| 30 | +import seedu.address.storage.UserPrefsStorage; |
| 31 | +import seedu.address.ui.NewUiManager; |
| 32 | +import seedu.address.ui.Ui; |
| 33 | + |
| 34 | +/** |
| 35 | + * Runs the application. |
| 36 | + */ |
| 37 | +public class NewMainApp extends Application { |
| 38 | + |
| 39 | + public static final Version VERSION = new Version(0, 6, 0, true); |
| 40 | + |
| 41 | + private static final Logger logger = LogsCenter.getLogger(MainApp.class); |
| 42 | + |
| 43 | + protected Ui ui; |
| 44 | + protected Logic logic; |
| 45 | + protected Storage storage; |
| 46 | + protected Model model; |
| 47 | + protected Config config; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void init() throws Exception { |
| 51 | + logger.info("=============================[ Initializing AddressBook ]==========================="); |
| 52 | + super.init(); |
| 53 | + |
| 54 | + AppParameters appParameters = AppParameters.parse(getParameters()); |
| 55 | + config = initConfig(appParameters.getConfigPath()); |
| 56 | + |
| 57 | + UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath()); |
| 58 | + UserPrefs userPrefs = initPrefs(userPrefsStorage); |
| 59 | + AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath()); |
| 60 | + storage = new StorageManager(addressBookStorage, userPrefsStorage); |
| 61 | + |
| 62 | + initLogging(config); |
| 63 | + |
| 64 | + model = initModelManager(storage, userPrefs); |
| 65 | + |
| 66 | + logic = new LogicManager(model, storage); |
| 67 | + |
| 68 | + ui = new NewUiManager(logic); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br> |
| 73 | + * The data from the sample address book will be used instead if {@code storage}'s address book is not found, |
| 74 | + * or an empty address book will be used instead if errors occur when reading {@code storage}'s address book. |
| 75 | + */ |
| 76 | + private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) { |
| 77 | + Optional<ReadOnlyAddressBook> addressBookOptional; |
| 78 | + ReadOnlyAddressBook initialData; |
| 79 | + try { |
| 80 | + addressBookOptional = storage.readAddressBook(); |
| 81 | + if (!addressBookOptional.isPresent()) { |
| 82 | + logger.info("Data file not found. Will be starting with a sample AddressBook"); |
| 83 | + } |
| 84 | + initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook); |
| 85 | + } catch (DataConversionException e) { |
| 86 | + logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook"); |
| 87 | + initialData = new AddressBook(); |
| 88 | + } catch (IOException e) { |
| 89 | + logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook"); |
| 90 | + initialData = new AddressBook(); |
| 91 | + } |
| 92 | + |
| 93 | + return new ModelManager(initialData, userPrefs); |
| 94 | + } |
| 95 | + |
| 96 | + private void initLogging(Config config) { |
| 97 | + LogsCenter.init(config); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns a {@code Config} using the file at {@code configFilePath}. <br> |
| 102 | + * The default file path {@code Config#DEFAULT_CONFIG_FILE} will be used instead |
| 103 | + * if {@code configFilePath} is null. |
| 104 | + */ |
| 105 | + protected Config initConfig(Path configFilePath) { |
| 106 | + Config initializedConfig; |
| 107 | + Path configFilePathUsed; |
| 108 | + |
| 109 | + configFilePathUsed = Config.DEFAULT_CONFIG_FILE; |
| 110 | + |
| 111 | + if (configFilePath != null) { |
| 112 | + logger.info("Custom Config file specified " + configFilePath); |
| 113 | + configFilePathUsed = configFilePath; |
| 114 | + } |
| 115 | + |
| 116 | + logger.info("Using config file : " + configFilePathUsed); |
| 117 | + |
| 118 | + try { |
| 119 | + Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed); |
| 120 | + initializedConfig = configOptional.orElse(new Config()); |
| 121 | + } catch (DataConversionException e) { |
| 122 | + logger.warning("Config file at " + configFilePathUsed + " is not in the correct format. " |
| 123 | + + "Using default config properties"); |
| 124 | + initializedConfig = new Config(); |
| 125 | + } |
| 126 | + |
| 127 | + //Update config file in case it was missing to begin with or there are new/unused fields |
| 128 | + try { |
| 129 | + ConfigUtil.saveConfig(initializedConfig, configFilePathUsed); |
| 130 | + } catch (IOException e) { |
| 131 | + logger.warning("Failed to save config file : " + StringUtil.getDetails(e)); |
| 132 | + } |
| 133 | + return initializedConfig; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns a {@code UserPrefs} using the file at {@code storage}'s user prefs file path, |
| 138 | + * or a new {@code UserPrefs} with default configuration if errors occur when |
| 139 | + * reading from the file. |
| 140 | + */ |
| 141 | + protected UserPrefs initPrefs(UserPrefsStorage storage) { |
| 142 | + Path prefsFilePath = storage.getUserPrefsFilePath(); |
| 143 | + logger.info("Using prefs file : " + prefsFilePath); |
| 144 | + |
| 145 | + UserPrefs initializedPrefs; |
| 146 | + try { |
| 147 | + Optional<UserPrefs> prefsOptional = storage.readUserPrefs(); |
| 148 | + initializedPrefs = prefsOptional.orElse(new UserPrefs()); |
| 149 | + } catch (DataConversionException e) { |
| 150 | + logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. " |
| 151 | + + "Using default user prefs"); |
| 152 | + initializedPrefs = new UserPrefs(); |
| 153 | + } catch (IOException e) { |
| 154 | + logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook"); |
| 155 | + initializedPrefs = new UserPrefs(); |
| 156 | + } |
| 157 | + |
| 158 | + //Update prefs file in case it was missing to begin with or there are new/unused fields |
| 159 | + try { |
| 160 | + storage.saveUserPrefs(initializedPrefs); |
| 161 | + } catch (IOException e) { |
| 162 | + logger.warning("Failed to save config file : " + StringUtil.getDetails(e)); |
| 163 | + } |
| 164 | + |
| 165 | + return initializedPrefs; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void start(Stage primaryStage) { |
| 170 | + logger.info("Starting AddressBook " + MainApp.VERSION); |
| 171 | + ui.start(primaryStage); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void stop() { |
| 176 | + logger.info("============================ [ Stopping Address Book ] ============================="); |
| 177 | + try { |
| 178 | + storage.saveUserPrefs(model.getUserPrefs()); |
| 179 | + } catch (IOException e) { |
| 180 | + logger.severe("Failed to save preferences " + StringUtil.getDetails(e)); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments