forked from se-edu/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 109
/
StorageManager.java
90 lines (70 loc) · 3.11 KB
/
StorageManager.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
package seedu.address.storage;
import com.google.common.eventbus.Subscribe;
import seedu.address.commons.core.ComponentManager;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.UserPrefs;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger;
/**
* Manages storage of AddressBook data in local storage.
*/
public class StorageManager extends ComponentManager implements Storage {
private static final Logger logger = LogsCenter.getLogger(StorageManager.class);
private AddressBookStorage addressBookStorage;
private UserPrefsStorage userPrefsStorage;
public StorageManager(AddressBookStorage addressBookStorage, UserPrefsStorage userPrefsStorage) {
super();
this.addressBookStorage = addressBookStorage;
this.userPrefsStorage = userPrefsStorage;
}
public StorageManager(String addressBookFilePath, String userPrefsFilePath) {
this(new XmlAddressBookStorage(addressBookFilePath), new JsonUserPrefsStorage(userPrefsFilePath));
}
// ================ UserPrefs methods ==============================
@Override
public Optional<UserPrefs> readUserPrefs() throws DataConversionException, IOException {
return userPrefsStorage.readUserPrefs();
}
@Override
public void saveUserPrefs(UserPrefs userPrefs) throws IOException {
userPrefsStorage.saveUserPrefs(userPrefs);
}
// ================ AddressBook methods ==============================
@Override
public String getAddressBookFilePath() {
return addressBookStorage.getAddressBookFilePath();
}
@Override
public Optional<ReadOnlyAddressBook> readAddressBook() throws DataConversionException, IOException {
return readAddressBook(addressBookStorage.getAddressBookFilePath());
}
@Override
public Optional<ReadOnlyAddressBook> readAddressBook(String filePath) throws DataConversionException, IOException {
logger.fine("Attempting to read data from file: " + filePath);
return addressBookStorage.readAddressBook(filePath);
}
@Override
public void saveAddressBook(ReadOnlyAddressBook addressBook) throws IOException {
saveAddressBook(addressBook, addressBookStorage.getAddressBookFilePath());
}
@Override
public void saveAddressBook(ReadOnlyAddressBook addressBook, String filePath) throws IOException {
logger.fine("Attempting to write to data file: " + filePath);
addressBookStorage.saveAddressBook(addressBook, filePath);
}
@Override
@Subscribe
public void handleAddressBookChangedEvent(AddressBookChangedEvent event) {
logger.info(LogsCenter.getEventHandlingLogMessage(event, "Local data changed, saving to file"));
try {
saveAddressBook(event.data);
} catch (IOException e) {
raise(new DataSavingExceptionEvent(e));
}
}
}