Skip to content

Commit 8eb1ef7

Browse files
authored
Merge pull request #57 from Maurice2n97/add-Delete-Meeting
Add storage of meetings
2 parents bf1e547 + c015ad6 commit 8eb1ef7

26 files changed

Lines changed: 569 additions & 28 deletions

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
import seedu.address.model.ModelManager;
2020
import seedu.address.model.ReadOnlyUserPrefs;
2121
import seedu.address.model.UserPrefs;
22+
import seedu.address.model.meeting.MeetingBook;
23+
import seedu.address.model.meeting.ReadOnlyMeetingBook;
2224
import seedu.address.model.person.AddressBook;
2325
import seedu.address.model.person.ReadOnlyAddressBook;
2426
import seedu.address.model.util.SampleDataUtil;
2527
import seedu.address.storage.AddressBookStorage;
2628
import seedu.address.storage.JsonAddressBookStorage;
29+
import seedu.address.storage.JsonMeetingBookStorage;
2730
import seedu.address.storage.JsonUserPrefsStorage;
31+
import seedu.address.storage.MeetingBookStorage;
2832
import seedu.address.storage.Storage;
2933
import seedu.address.storage.StorageManager;
3034
import seedu.address.storage.UserPrefsStorage;
@@ -57,7 +61,8 @@ public void init() throws Exception {
5761
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
5862
UserPrefs userPrefs = initPrefs(userPrefsStorage);
5963
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
60-
storage = new StorageManager(addressBookStorage, userPrefsStorage);
64+
MeetingBookStorage meetingBookStorage = new JsonMeetingBookStorage(userPrefs.getMeetingBookFilePath());
65+
storage = new StorageManager(addressBookStorage, meetingBookStorage, userPrefsStorage);
6166

6267
initLogging(config);
6368

@@ -75,22 +80,42 @@ public void init() throws Exception {
7580
*/
7681
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
7782
Optional<ReadOnlyAddressBook> addressBookOptional;
78-
ReadOnlyAddressBook initialData;
83+
Optional<ReadOnlyMeetingBook> meetingBookOptional;
84+
ReadOnlyAddressBook initialDataAddressBook;
85+
ReadOnlyMeetingBook initialDataMeetingBook;
86+
7987
try {
8088
addressBookOptional = storage.readAddressBook();
8189
if (!addressBookOptional.isPresent()) {
8290
logger.info("Data file not found. Will be starting with a sample AddressBook");
8391
}
84-
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
92+
initialDataAddressBook = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
8593
} catch (DataConversionException e) {
8694
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
87-
initialData = new AddressBook();
95+
initialDataAddressBook = new AddressBook();
8896
} catch (IOException e) {
8997
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
90-
initialData = new AddressBook();
98+
initialDataAddressBook = new AddressBook();
9199
}
92100

93-
return new ModelManager(initialData, userPrefs);
101+
//--============= MEETING ==================================================================================
102+
103+
try {
104+
meetingBookOptional = storage.readMeetingBook();
105+
if (!meetingBookOptional.isPresent()) {
106+
logger.info("Data file not found. Will be starting with a sample MeetingBook");
107+
}
108+
initialDataMeetingBook = meetingBookOptional.orElseGet(SampleDataUtil::getSampleMeetingBook);
109+
} catch (DataConversionException e) {
110+
logger.warning("Data file not in the correct format. Will be starting with an empty MeetingBook");
111+
initialDataMeetingBook = new MeetingBook();
112+
} catch (IOException e) {
113+
logger.warning("Problem while reading from the file. Will be starting with an empty MeetingBook");
114+
initialDataMeetingBook = new MeetingBook();
115+
}
116+
117+
118+
return new ModelManager(initialDataAddressBook, initialDataMeetingBook, userPrefs);
94119
}
95120

96121
private void initLogging(Config config) {

src/main/java/seedu/address/logic/LogicManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
4949

5050
try {
5151
storage.saveAddressBook(model.getAddressBook());
52+
storage.saveMeetingBook(model.getMeetingBook());
5253
} catch (IOException ioe) {
5354
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
5455
}

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyMeetingBook meeting
6666

6767

6868
public ModelManager() {
69-
this(new AddressBook(), new UserPrefs());
69+
this(new AddressBook(), new MeetingBook(), new UserPrefs());
7070
}
7171

7272
//=========== UserPrefs ==================================================================================
@@ -174,6 +174,8 @@ public void setMeeting(Meeting target, Meeting editedMeeting) {
174174
requireAllNonNull(target, editedMeeting);
175175
meetingBook.setMeeting(target, editedMeeting);
176176
}
177+
//TODO: Set MeetingBook file path in userPrefs? low priority feature(nice to have)
178+
177179
//=========== Filtered Person List Accessors =============================================================
178180

179181
/**

src/main/java/seedu/address/model/ReadOnlyUserPrefs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ public interface ReadOnlyUserPrefs {
1313

1414
Path getAddressBookFilePath();
1515

16+
Path getMeetingBookFilePath();
17+
1618
}

src/main/java/seedu/address/model/UserPrefs.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class UserPrefs implements ReadOnlyUserPrefs {
1515

1616
private GuiSettings guiSettings = new GuiSettings();
1717
private Path addressBookFilePath = Paths.get("data" , "addressbook.json");
18+
private Path meetingBookFilePath = Paths.get("data", "meetingbook.json");
1819

1920
/**
2021
* Creates a {@code UserPrefs} with default values.
@@ -51,10 +52,18 @@ public Path getAddressBookFilePath() {
5152
return addressBookFilePath;
5253
}
5354

55+
public Path getMeetingBookFilePath() {
56+
return meetingBookFilePath;
57+
}
58+
5459
public void setAddressBookFilePath(Path addressBookFilePath) {
5560
requireNonNull(addressBookFilePath);
5661
this.addressBookFilePath = addressBookFilePath;
5762
}
63+
public void setMeetingBookFilePath(Path meetingBookFilePath) {
64+
requireNonNull(meetingBookFilePath);
65+
this.meetingBookFilePath = meetingBookFilePath;
66+
}
5867

5968
@Override
6069
public boolean equals(Object other) {

src/main/java/seedu/address/model/util/SampleDataUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.stream.Collectors;
66

77
import seedu.address.model.group.Group;
8+
import seedu.address.model.meeting.MeetingBook;
9+
import seedu.address.model.meeting.ReadOnlyMeetingBook;
810
import seedu.address.model.person.Address;
911
import seedu.address.model.person.AddressBook;
1012
import seedu.address.model.person.Email;
@@ -57,4 +59,10 @@ public static Set<Group> getGroupSet(String... strings) {
5759
.collect(Collectors.toSet());
5860
}
5961

62+
// Change this to get sample meeting Data to be displayed.
63+
public static ReadOnlyMeetingBook getSampleMeetingBook() {
64+
MeetingBook sampleMb = new MeetingBook();
65+
return sampleMb;
66+
}
67+
6068
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package seedu.address.storage;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Path;
7+
import java.util.Optional;
8+
import java.util.logging.Logger;
9+
10+
import seedu.address.commons.core.LogsCenter;
11+
import seedu.address.commons.exceptions.DataConversionException;
12+
import seedu.address.commons.exceptions.IllegalValueException;
13+
import seedu.address.commons.util.FileUtil;
14+
import seedu.address.commons.util.JsonUtil;
15+
import seedu.address.model.meeting.MeetingBook;
16+
import seedu.address.model.meeting.ReadOnlyMeetingBook;
17+
18+
19+
/**
20+
* Represents a storage for {@link MeetingBook}.
21+
*/
22+
23+
public class JsonMeetingBookStorage implements MeetingBookStorage {
24+
25+
private static final Logger logger = LogsCenter.getLogger(JsonMeetingBookStorage.class);
26+
27+
private Path filePath;
28+
29+
public JsonMeetingBookStorage(Path filePath) {
30+
this.filePath = filePath;
31+
}
32+
33+
public Path getMeetingBookFilePath() {
34+
return filePath;
35+
}
36+
37+
@Override
38+
public Optional<ReadOnlyMeetingBook> readMeetingBook() throws DataConversionException {
39+
return readMeetingBook(filePath);
40+
}
41+
42+
/**
43+
* Similar to {@link #readMeetingBook()}.
44+
*
45+
* @param filePath location of the data. Cannot be null.
46+
* @throws DataConversionException if the file is not in the correct format.
47+
*/
48+
public Optional<ReadOnlyMeetingBook> readMeetingBook(Path filePath) throws DataConversionException {
49+
requireNonNull(filePath);
50+
Optional<JsonSerializableMeetingBook> jsonMeetingBook = JsonUtil.readJsonFile(
51+
filePath, JsonSerializableMeetingBook.class);
52+
if (!jsonMeetingBook.isPresent()) {
53+
return Optional.empty();
54+
}
55+
56+
try {
57+
return Optional.of(jsonMeetingBook.get().toModelType());
58+
} catch (IllegalValueException ive) {
59+
logger.info("Illegal values found for Meeting Book in " + filePath + ": " + ive.getMessage());
60+
throw new DataConversionException(ive);
61+
}
62+
}
63+
64+
@Override
65+
public void saveMeetingBook(ReadOnlyMeetingBook meetingBook) throws IOException {
66+
saveMeetingBook(meetingBook, filePath);
67+
}
68+
69+
/**
70+
* Similar to {@link #saveMeetingBook(ReadOnlyMeetingBook)}.
71+
*
72+
* @param filePath location of the data. Cannot be null.
73+
*/
74+
public void saveMeetingBook(ReadOnlyMeetingBook meetingBook, Path filePath) throws IOException {
75+
requireNonNull(meetingBook);
76+
requireNonNull(filePath);
77+
78+
FileUtil.createIfMissing(filePath);
79+
JsonUtil.saveJsonFile(new JsonSerializableMeetingBook(meetingBook), filePath);
80+
}
81+
}
82+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package seedu.address.storage;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import com.fasterxml.jackson.annotation.JsonCreator;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
import com.fasterxml.jackson.annotation.JsonRootName;
10+
11+
import seedu.address.commons.exceptions.IllegalValueException;
12+
import seedu.address.model.meeting.Meeting;
13+
import seedu.address.model.meeting.MeetingBook;
14+
import seedu.address.model.meeting.ReadOnlyMeetingBook;
15+
16+
/**
17+
* An Immutable MeetingBook that is serializable to JSON format.
18+
*/
19+
@JsonRootName(value = "meetingbook")
20+
class JsonSerializableMeetingBook {
21+
22+
public static final String MESSAGE_DUPLICATE_PERSON = "Meetings List contains duplicate meeting(s).";
23+
24+
private final List<JsonAdaptedMeeting> meetings = new ArrayList<>();
25+
26+
/**
27+
* Constructs a {@code JsonSerializableMeetingBook} with the given persons.
28+
*/
29+
@JsonCreator
30+
public JsonSerializableMeetingBook(@JsonProperty("meetings") List<JsonAdaptedMeeting> meeting) {
31+
this.meetings.addAll(meeting);
32+
}
33+
34+
/**
35+
* Converts a given {@code ReadOnlyMeetingBook} into this class for Jackson use.
36+
*
37+
* @param source future changes to this will not affect the created {@code JsonSerializableMeetingBook}.
38+
*/
39+
public JsonSerializableMeetingBook(ReadOnlyMeetingBook source) {
40+
meetings.addAll(source.getMeetingList().stream().map(JsonAdaptedMeeting::new).collect(Collectors.toList()));
41+
}
42+
43+
/**
44+
* Converts this address book into the model's {@code MeetingBook} object.
45+
*
46+
* @throws IllegalValueException if there were any data constraints violated.
47+
*/
48+
public MeetingBook toModelType() throws IllegalValueException {
49+
MeetingBook meetingBook = new MeetingBook();
50+
for (JsonAdaptedMeeting jsonAdaptedMeeting : meetings) {
51+
Meeting meeting = jsonAdaptedMeeting.toModelType();
52+
if (meetingBook.hasMeeting(meeting)) {
53+
throw new IllegalValueException(MESSAGE_DUPLICATE_PERSON);
54+
}
55+
meetingBook.addMeeting(meeting);
56+
}
57+
return meetingBook;
58+
}
59+
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
package seedu.address.storage;
3+
4+
import java.io.IOException;
5+
import java.nio.file.Path;
6+
import java.util.Optional;
7+
8+
import seedu.address.commons.exceptions.DataConversionException;
9+
import seedu.address.model.meeting.MeetingBook;
10+
import seedu.address.model.meeting.ReadOnlyMeetingBook;
11+
12+
13+
/**
14+
* Represents a storage for {@link MeetingBook}.
15+
*/
16+
17+
public interface MeetingBookStorage {
18+
/**
19+
* Returns the file path of the data file.
20+
*/
21+
Path getMeetingBookFilePath();
22+
23+
/**
24+
* Returns AddressBook data as a {@link ReadOnlyMeetingBook}.
25+
* Returns {@code Optional.empty()} if storage file is not found.
26+
* @throws DataConversionException if the data in storage is not in the expected format.
27+
* @throws IOException if there was any problem when reading from the storage.
28+
*/
29+
Optional<ReadOnlyMeetingBook> readMeetingBook() throws DataConversionException, IOException;
30+
31+
/**
32+
* @see #getMeetingBookFilePath()
33+
*/
34+
Optional<ReadOnlyMeetingBook> readMeetingBook(Path filePath) throws DataConversionException, IOException;
35+
36+
/**
37+
* Saves the given {@link ReadOnlyMeetingBook} to the storage.
38+
* @param meetingBook cannot be null.
39+
* @throws IOException if there was any problem writing to the file.
40+
*/
41+
void saveMeetingBook(ReadOnlyMeetingBook meetingBook) throws IOException;
42+
43+
/**
44+
* @see #saveMeetingBook(ReadOnlyMeetingBook)
45+
*/
46+
void saveMeetingBook(ReadOnlyMeetingBook meetingBook, Path filePath) throws IOException;
47+
}

src/main/java/seedu/address/storage/Storage.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import seedu.address.commons.exceptions.DataConversionException;
88
import seedu.address.model.ReadOnlyUserPrefs;
99
import seedu.address.model.UserPrefs;
10+
import seedu.address.model.meeting.ReadOnlyMeetingBook;
1011
import seedu.address.model.person.ReadOnlyAddressBook;
1112

1213
/**
1314
* API of the Storage component
1415
*/
15-
public interface Storage extends AddressBookStorage, UserPrefsStorage {
16+
public interface Storage extends AddressBookStorage, UserPrefsStorage, MeetingBookStorage {
1617

1718
@Override
1819
Optional<UserPrefs> readUserPrefs() throws DataConversionException, IOException;
@@ -29,4 +30,21 @@ public interface Storage extends AddressBookStorage, UserPrefsStorage {
2930
@Override
3031
void saveAddressBook(ReadOnlyAddressBook addressBook) throws IOException;
3132

33+
@Override
34+
Path getMeetingBookFilePath();
35+
36+
@Override
37+
Optional<ReadOnlyMeetingBook> readMeetingBook() throws DataConversionException, IOException;
38+
39+
@Override
40+
void saveMeetingBook(ReadOnlyMeetingBook meetingBook) throws IOException;
41+
42+
43+
44+
45+
46+
47+
48+
49+
3250
}

0 commit comments

Comments
 (0)