forked from nus-cs2103-AY2122S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MainApp.java
238 lines (207 loc) · 9.8 KB
/
MainApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package tutoraid;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.stage.Stage;
import tutoraid.commons.core.Config;
import tutoraid.commons.core.LogsCenter;
import tutoraid.commons.core.Messages;
import tutoraid.commons.core.Version;
import tutoraid.commons.exceptions.DataConversionException;
import tutoraid.commons.util.ConfigUtil;
import tutoraid.commons.util.StringUtil;
import tutoraid.logic.Logic;
import tutoraid.logic.LogicManager;
import tutoraid.model.LessonBook;
import tutoraid.model.Model;
import tutoraid.model.ModelManager;
import tutoraid.model.ReadOnlyLessonBook;
import tutoraid.model.ReadOnlyStudentBook;
import tutoraid.model.ReadOnlyUserPrefs;
import tutoraid.model.StudentBook;
import tutoraid.model.UserPrefs;
import tutoraid.model.util.SampleDataUtil;
import tutoraid.storage.JsonTutorAidLessonStorage;
import tutoraid.storage.JsonTutorAidStudentStorage;
import tutoraid.storage.JsonUserPrefsStorage;
import tutoraid.storage.Storage;
import tutoraid.storage.StorageManager;
import tutoraid.storage.TutorAidLessonStorage;
import tutoraid.storage.TutorAidStudentStorage;
import tutoraid.storage.UserPrefsStorage;
import tutoraid.ui.Ui;
import tutoraid.ui.UiManager;
/**
* Runs the application.
*/
public class MainApp extends Application {
public static final Version VERSION = new Version(1, 4, 0, true);
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 String message = String.format("%s\n", Messages.WELCOME_MESSAGE);
@Override
public void init() throws Exception {
logger.info("==================[ Initializing StudentBook and LessonBook ]===========================");
super.init();
AppParameters appParameters = AppParameters.parse(getParameters());
config = initConfig(appParameters.getConfigPath());
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
TutorAidLessonStorage tutorAidLessonStorage = new JsonTutorAidLessonStorage(userPrefs.getLessonBookFilePath());
TutorAidStudentStorage tutorAidStudentStorage;
try {
tutorAidLessonStorage.readLessonBook(userPrefs.getLessonBookFilePath());
tutorAidStudentStorage =
new JsonTutorAidStudentStorage(userPrefs.getStudentBookFilePath(),
tutorAidLessonStorage.readLessonBook().orElseGet(SampleDataUtil::getSampleLessonBook));
} catch (DataConversionException e) {
tutorAidStudentStorage = new JsonTutorAidStudentStorage(
userPrefs.getStudentBookFilePath(),
new LessonBook());
}
storage = new StorageManager(tutorAidStudentStorage, tutorAidLessonStorage, userPrefsStorage);
initLogging(config);
model = initModelManager(storage, userPrefs);
logic = new LogicManager(model, storage);
ui = new UiManager(logic);
}
/**
* Returns a {@code ModelManager} with the data from {@code storage}'s student and lesson books
* and {@code userPrefs}. <br>
* The data from the sample student book will be used instead if {@code storage}'s student book is not found,
* or an empty student book will be used instead if errors occur when reading {@code storage}'s student book.
* This applies to the lesson book too.
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyStudentBook> studentBookOptional;
ReadOnlyStudentBook studentsInitialData;
Optional<ReadOnlyLessonBook> lessonBookOptional;
ReadOnlyLessonBook lessonsInitialData;
boolean hasError = false;
try {
lessonBookOptional = storage.readLessonBook();
if (lessonBookOptional.isEmpty()) {
logger.info("Data file not found. Will be starting with a sample LessonBook");
message += String.format("\n%s", Messages.MESSAGE_NO_LESSON_DATA);
}
lessonsInitialData = lessonBookOptional.orElseGet(SampleDataUtil::getSampleLessonBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty database.");
lessonsInitialData = new LessonBook();
message += String.format("\n%s", Messages.MESSAGE_JSON_INTEGRITY_ERROR);
hasError = true;
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty database.");
lessonsInitialData = new LessonBook();
message += String.format("\n%s", Messages.MESSAGE_JSON_INTEGRITY_ERROR);
hasError = true;
}
try {
if (hasError) {
studentsInitialData = new StudentBook();
} else {
studentBookOptional = storage.readStudentBook(lessonsInitialData);
if (studentBookOptional.isEmpty()) {
logger.info("Data file not found. Will be starting with a sample StudentBook");
message += String.format("\n%s", Messages.MESSAGE_NO_STUDENT_DATA);
}
ReadOnlyLessonBook finalLessonsInitialData = lessonsInitialData;
studentsInitialData = studentBookOptional.orElseGet(() -> SampleDataUtil.getSampleStudentBook(
finalLessonsInitialData));
}
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty database.");
message += String.format("\n%s", Messages.MESSAGE_JSON_INTEGRITY_ERROR);
studentsInitialData = new StudentBook();
lessonsInitialData = new LessonBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty database.");
message += String.format("\n%s", Messages.MESSAGE_JSON_INTEGRITY_ERROR);
studentsInitialData = new StudentBook();
lessonsInitialData = new LessonBook();
}
return new ModelManager(studentsInitialData, lessonsInitialData, userPrefs);
}
private void initLogging(Config config) {
LogsCenter.init(config);
}
/**
* Returns a {@code Config} using the file at {@code configFilePath}. <br>
* The default file path {@code Config#DEFAULT_CONFIG_FILE} will be used instead
* if {@code configFilePath} is null.
*/
protected Config initConfig(Path configFilePath) {
Config initializedConfig;
Path configFilePathUsed;
configFilePathUsed = Config.DEFAULT_CONFIG_FILE;
if (configFilePath != null) {
logger.info("Custom Config file specified " + configFilePath);
configFilePathUsed = configFilePath;
}
logger.info("Using config file : " + configFilePathUsed);
try {
Optional<Config> configOptional = ConfigUtil.readConfig(configFilePathUsed);
initializedConfig = configOptional.orElse(new Config());
} catch (DataConversionException e) {
logger.warning("Config file at " + configFilePathUsed + " is not in the correct format. "
+ "Using default config properties");
initializedConfig = new Config();
}
//Update config file in case it was missing to begin with or there are new/unused fields
try {
ConfigUtil.saveConfig(initializedConfig, configFilePathUsed);
} catch (IOException e) {
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
}
return initializedConfig;
}
/**
* Returns a {@code UserPrefs} using the file at {@code storage}'s user prefs file path,
* or a new {@code UserPrefs} with default configuration if errors occur when
* reading from the file.
*/
protected UserPrefs initPrefs(UserPrefsStorage storage) {
Path prefsFilePath = storage.getUserPrefsFilePath();
logger.info("Using prefs file : " + prefsFilePath);
UserPrefs initializedPrefs;
try {
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
initializedPrefs = prefsOptional.orElse(new UserPrefs());
} catch (DataConversionException e) {
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. "
+ "Using default user prefs");
initializedPrefs = new UserPrefs();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty StudentBook");
initializedPrefs = new UserPrefs();
}
//Update prefs file in case it was missing to begin with or there are new/unused fields
try {
storage.saveUserPrefs(initializedPrefs);
} catch (IOException e) {
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
}
return initializedPrefs;
}
@Override
public void start(Stage primaryStage) {
logger.info("Starting StudentBook and LessonBook " + MainApp.VERSION);
ui.start(primaryStage);
UiManager.printMessage(message);
}
@Override
public void stop() {
logger.info("============================ [ Stopping Address Book ] =============================");
try {
storage.saveUserPrefs(model.getUserPrefs());
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
}
}