Skip to content

Commit 75b31ce

Browse files
committed
Merge branch 'master' into branch-notes
2 parents af3700e + 327c0e4 commit 75b31ce

12 files changed

Lines changed: 520 additions & 52 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package seedu.address.logic.commands.connections;
2+
3+
import seedu.address.commons.core.Messages;
4+
import seedu.address.commons.core.index.Index;
5+
import seedu.address.logic.commands.Command;
6+
import seedu.address.logic.commands.CommandResult;
7+
import seedu.address.logic.commands.exceptions.CommandException;
8+
import seedu.address.model.Model;
9+
import seedu.address.model.group.Group;
10+
import seedu.address.model.meeting.*;
11+
import seedu.address.model.person.Person;
12+
import seedu.address.logic.commands.meetings.EditMeetingCommand.EditMeetingDescriptor;
13+
14+
import java.util.HashSet;
15+
import java.util.List;
16+
import java.util.Set;
17+
18+
import static seedu.address.logic.parser.CliSyntax.*;
19+
20+
import static java.util.Objects.requireNonNull;
21+
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_MEETINGS;
22+
23+
public class AddPersonToMeetingConnectionCommand extends Command {
24+
public static final String COMMAND_WORD = "addptm";
25+
26+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds persons by index or by groups to a meeting. "
27+
+ "Parameters: "
28+
+ "INDEX of the meeting (must be a positive integer) "
29+
+ "[" + PREFIX_GROUP + "GROUP]..."
30+
+ "[" + PREFIX_PERSON_CONNECTION + "INDEX OF PERSON RELATED]...\n"
31+
+ "Example: " + COMMAND_WORD + " "
32+
+ "1 "
33+
+ PREFIX_GROUP + "lectures "
34+
+ PREFIX_GROUP + "SoC "
35+
+ PREFIX_PERSON_CONNECTION + "1 "
36+
+ PREFIX_PERSON_CONNECTION + "2";
37+
38+
private static String MESSAGE_SUCCESS = "Successfully add persons related to the meeting! "
39+
+ "The possible duplication of persons related is automatically removed.";
40+
private static String MESSAGE_GROUP_NOT_EXIST = "The group doesn't exist!";
41+
private final Index meetingIndex;
42+
private final Set<Group> groupsToAdd = new HashSet<>();
43+
private final Set<Index> personsIndexToAdd = new HashSet<>();
44+
/**
45+
* @param index of the meeting in the filtered meeting list to edit
46+
* @param personsIndexToAdd the set of index of persons the users want to add.
47+
* @param groupsToAdd the set of index of groups the users want to add.
48+
*/
49+
public AddPersonToMeetingConnectionCommand(Index index, Set<Index> personsIndexToAdd, Set<Group> groupsToAdd) {
50+
requireNonNull(index);
51+
meetingIndex = index;
52+
this.personsIndexToAdd.addAll(personsIndexToAdd);
53+
this.groupsToAdd.addAll(groupsToAdd);
54+
}
55+
@Override
56+
public CommandResult execute(Model model) throws CommandException {
57+
requireNonNull(model);
58+
List<Meeting> lastShownList = model.getFilteredMeetingList();
59+
60+
if (meetingIndex.getZeroBased() >= lastShownList.size()) {
61+
throw new CommandException(Messages.MESSAGE_INVALID_MEETING_DISPLAYED_INDEX);
62+
}
63+
Meeting meetingToEdit = lastShownList.get(meetingIndex.getZeroBased());
64+
Meeting meetingEdited = createEditedMeeting(meetingToEdit, new EditMeetingDescriptor());
65+
Set<Person> existedPersonsConnection = meetingToEdit.getConnectionToPerson();
66+
model.deleteAllPersonMeetingConnectionByMeeting(meetingToEdit);
67+
addConnectionsToPersons(meetingEdited, model, existedPersonsConnection);
68+
model.updateMeeting(meetingToEdit, meetingEdited);
69+
model.updateFilteredMeetingList(PREDICATE_SHOW_ALL_MEETINGS);
70+
return new CommandResult(MESSAGE_SUCCESS);
71+
}
72+
73+
/**
74+
* This method will handle the connections that the user wants to add from both the g/ and p/
75+
* Duplicate person that the user wants to build connection with this meeting will be automatically removed.
76+
*/
77+
private void addConnectionsToPersons(Meeting toAdd, Model model, Set<Person> existedPersonsConnection) throws CommandException {
78+
// Use set to ensure unique element.
79+
HashSet<Person> personsConnection = new HashSet<>();
80+
personsConnection.addAll(existedPersonsConnection);
81+
toAdd.setPersonMeetingConnection(model.getPersonMeetingConnection());
82+
if (!groupsToAdd.isEmpty()) {
83+
for (Group group : groupsToAdd) {
84+
if (model.findPersonsInGroup(group).isEmpty()) {
85+
throw new CommandException(MESSAGE_GROUP_NOT_EXIST);
86+
}
87+
}
88+
toAdd.addGroups(groupsToAdd);
89+
for (Group group : groupsToAdd) {
90+
Set<Person> personsInGroup = model.findPersonsInGroup(group);
91+
// Get the union set.
92+
personsConnection.addAll(personsInGroup);
93+
}
94+
}
95+
96+
if (personsIndexToAdd.size() != 0) {
97+
List<Person> lastShownList = model.getFilteredPersonList();
98+
// Check whether the index is out of bounds
99+
for (Index index : personsIndexToAdd) {
100+
if (index.getZeroBased() >= lastShownList.size()) {
101+
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
102+
}
103+
}
104+
// If we can pass the check, then add connection.
105+
for (Index index: personsIndexToAdd) {
106+
Person personToAddConnection = lastShownList.get(index.getZeroBased());
107+
personsConnection.add(personToAddConnection);
108+
}
109+
}
110+
111+
for (Person allPersonToAddConnection : personsConnection) {
112+
model.addPersonMeetingConnection(allPersonToAddConnection, toAdd);
113+
}
114+
}
115+
116+
/**
117+
* Creates and returns a {@code Meeting} with the details of {@code meetingToEdit}
118+
* edited with {@code editMeetingDescriptor}.
119+
*/
120+
private static Meeting createEditedMeeting(Meeting meetingToEdit, EditMeetingDescriptor editMeetingDescriptor) {
121+
assert meetingToEdit != null;
122+
123+
MeetingName updatedMeetingName = editMeetingDescriptor
124+
.getName()
125+
.orElse(meetingToEdit.getName());
126+
DateTime updatedStart = editMeetingDescriptor
127+
.getStart()
128+
.orElse(meetingToEdit.getStart());
129+
DateTime updatedTerminate = editMeetingDescriptor
130+
.getTerminate()
131+
.orElse(meetingToEdit.getTerminate());
132+
Priority updatedPriority = editMeetingDescriptor
133+
.getPriority()
134+
.orElse(meetingToEdit.getPriority());
135+
Description updatedDescription = editMeetingDescriptor
136+
.getDescription()
137+
.orElse(meetingToEdit.getDescription());
138+
Set<Group> updatedGroups = editMeetingDescriptor
139+
.getGroups()
140+
.orElse(meetingToEdit.getGroups());
141+
142+
return new Meeting(updatedMeetingName, updatedStart,
143+
updatedTerminate, updatedPriority, updatedDescription, updatedGroups);
144+
}
145+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package seedu.address.logic.commands.connections;
2+
3+
import seedu.address.commons.core.Messages;
4+
import seedu.address.commons.core.index.Index;
5+
import seedu.address.logic.commands.Command;
6+
import seedu.address.logic.commands.CommandResult;
7+
import seedu.address.logic.commands.exceptions.CommandException;
8+
import seedu.address.logic.commands.meetings.EditMeetingCommand;
9+
import seedu.address.model.Model;
10+
import seedu.address.model.group.Group;
11+
import seedu.address.model.meeting.*;
12+
import seedu.address.model.person.Person;
13+
14+
import java.util.HashSet;
15+
import java.util.List;
16+
import java.util.Set;
17+
18+
import static java.util.Objects.requireNonNull;
19+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
20+
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERSON_CONNECTION;
21+
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_MEETINGS;
22+
23+
public class DeletePersonToMeetingConnectionCommand extends Command {
24+
public static final String COMMAND_WORD = "deletepfm";
25+
26+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes persons by index or by groups from a meeting. "
27+
+ "Parameters: "
28+
+ "INDEX of the meeting (must be a positive integer) "
29+
+ "[" + PREFIX_GROUP + "GROUP]..."
30+
+ "[" + PREFIX_PERSON_CONNECTION + "INDEX OF PERSON RELATED]...\n"
31+
+ "Example: " + COMMAND_WORD + " "
32+
+ "1 "
33+
+ PREFIX_GROUP + "lectures "
34+
+ PREFIX_GROUP + "SoC "
35+
+ PREFIX_PERSON_CONNECTION + "1 "
36+
+ PREFIX_PERSON_CONNECTION + "2";
37+
38+
private static String MESSAGE_SUCCESS = "Successfully delete persons related to the meeting! ";
39+
private static String MESSAGE_GROUP_NOT_EXIST = "The group related to this meeting doesn't exist!";
40+
private static String MESSAGE_PERSON_NOT_EXIST = "The person related to this meeting doesn't exist!";
41+
private final Index meetingIndex;
42+
private final Set<Group> groupsToDelete = new HashSet<>();
43+
private final Set<Index> personsIndexToDelete = new HashSet<>();
44+
/**
45+
* @param index of the meeting in the filtered meeting list to edit
46+
* @param personsIndexToAdd the set of index of persons the users want to delete.
47+
* @param groupsToAdd the set of index of groups the users want to delete.
48+
*/
49+
public DeletePersonToMeetingConnectionCommand(Index index, Set<Index> personsIndexToAdd, Set<Group> groupsToAdd) {
50+
requireNonNull(index);
51+
meetingIndex = index;
52+
this.personsIndexToDelete.addAll(personsIndexToAdd);
53+
this.groupsToDelete.addAll(groupsToAdd);
54+
}
55+
@Override
56+
public CommandResult execute(Model model) throws CommandException {
57+
requireNonNull(model);
58+
List<Meeting> lastShownList = model.getFilteredMeetingList();
59+
60+
if (meetingIndex.getZeroBased() >= lastShownList.size()) {
61+
throw new CommandException(Messages.MESSAGE_INVALID_MEETING_DISPLAYED_INDEX);
62+
}
63+
Meeting meetingToEdit = lastShownList.get(meetingIndex.getZeroBased());
64+
Meeting meetingEdited = createEditedMeeting(meetingToEdit, new EditMeetingCommand.EditMeetingDescriptor());
65+
deleteConnectionsToPersons(meetingToEdit, meetingEdited, model);
66+
model.updateMeeting(meetingToEdit, meetingEdited);
67+
model.updateFilteredMeetingList(PREDICATE_SHOW_ALL_MEETINGS);
68+
return new CommandResult(MESSAGE_SUCCESS);
69+
}
70+
/**
71+
* This method will handle the connections that the user wants to add from both the g/ and p/
72+
* Duplicate person that the user wants to build connection with this meeting will be automatically removed.
73+
*/
74+
private void deleteConnectionsToPersons(Meeting toDelete, Meeting toAdd, Model model) throws CommandException {
75+
Set<Person> prevPersonsConnection = toDelete.getConnectionToPerson();
76+
toAdd.setPersonMeetingConnection(model.getPersonMeetingConnection());
77+
Set<Person> totalPersonsToRemove = new HashSet<>();
78+
if (!groupsToDelete.isEmpty()) {
79+
for (Group group : groupsToDelete) {
80+
if (!toDelete.getGroups().contains(group)) {
81+
throw new CommandException(MESSAGE_GROUP_NOT_EXIST);
82+
}
83+
}
84+
toAdd.deleteGroups(groupsToDelete);
85+
for (Group group : groupsToDelete) {
86+
Set<Person> personsInGroup = model.findPersonsInGroup(group);
87+
// Get the union set.
88+
totalPersonsToRemove.addAll(personsInGroup);
89+
}
90+
}
91+
92+
if (personsIndexToDelete.size() != 0) {
93+
List<Person> lastShownList = model.getFilteredPersonList();
94+
// Check whether the index is out of bounds
95+
for (Index index : personsIndexToDelete) {
96+
if (index.getZeroBased() >= lastShownList.size()) {
97+
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
98+
}
99+
}
100+
// Check whether the person the user wants to delete is in the meetings to be edited.
101+
for (Index index : personsIndexToDelete) {
102+
Person personToDeleteConnection = lastShownList.get(index.getZeroBased());
103+
if (!prevPersonsConnection.contains(personToDeleteConnection)) {
104+
throw new CommandException(MESSAGE_PERSON_NOT_EXIST);
105+
}
106+
}
107+
for (Index index : personsIndexToDelete) {
108+
Person personToAddConnection = lastShownList.get(index.getZeroBased());
109+
totalPersonsToRemove.add(personToAddConnection);
110+
}
111+
}
112+
113+
prevPersonsConnection.removeAll(totalPersonsToRemove);
114+
Set<Person> updatedPersonsConnection = prevPersonsConnection;
115+
116+
model.deleteAllPersonMeetingConnectionByMeeting(toDelete);
117+
118+
for (Person allPersonToAddConnection : updatedPersonsConnection) {
119+
model.addPersonMeetingConnection(allPersonToAddConnection, toAdd);
120+
}
121+
}
122+
123+
/**
124+
* Creates and returns a {@code Meeting} with the details of {@code meetingToEdit}
125+
* edited with {@code editMeetingDescriptor}.
126+
*/
127+
private static Meeting createEditedMeeting(Meeting meetingToEdit, EditMeetingCommand.EditMeetingDescriptor editMeetingDescriptor) {
128+
assert meetingToEdit != null;
129+
130+
MeetingName updatedMeetingName = editMeetingDescriptor
131+
.getName()
132+
.orElse(meetingToEdit.getName());
133+
DateTime updatedStart = editMeetingDescriptor
134+
.getStart()
135+
.orElse(meetingToEdit.getStart());
136+
DateTime updatedTerminate = editMeetingDescriptor
137+
.getTerminate()
138+
.orElse(meetingToEdit.getTerminate());
139+
Priority updatedPriority = editMeetingDescriptor
140+
.getPriority()
141+
.orElse(meetingToEdit.getPriority());
142+
Description updatedDescription = editMeetingDescriptor
143+
.getDescription()
144+
.orElse(meetingToEdit.getDescription());
145+
Set<Group> updatedGroups = editMeetingDescriptor
146+
.getGroups()
147+
.orElse(meetingToEdit.getGroups());
148+
149+
return new Meeting(updatedMeetingName, updatedStart,
150+
updatedTerminate, updatedPriority, updatedDescription, updatedGroups);
151+
}
152+
}

src/main/java/seedu/address/logic/commands/meetings/AddMeetingCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public AddMeetingCommand(Meeting meeting) {
5757
}
5858

5959
/**
60-
* Set the connection indices to meetings.
60+
* Set the connection indices to persons.
6161
*/
6262
public AddMeetingCommand setConnectionToPerson(Set<Index> indices) {
6363
this.connectionToPerson.addAll(indices);
6464
return this;
6565
}
6666

6767
/**
68-
* Get the connection indices to meetings.
68+
* Get the connection indices to persons.
6969
*/
7070
public Set<Index> getConnectionToPerson() {
7171
return this.connectionToPerson;
@@ -97,6 +97,7 @@ public CommandResult execute(Model model) throws CommandException {
9797
private void addConnectionsToPersons(Meeting toAdd, Model model) throws CommandException {
9898
// Use set to ensure unique element.
9999
HashSet<Person> personsConnection = new HashSet<>();
100+
toAdd.setPersonMeetingConnection(model.getPersonMeetingConnection());
100101

101102
if (!toAdd.getGroups().isEmpty()) {
102103
for (Group group : toAdd.getGroups()) {
@@ -107,7 +108,6 @@ private void addConnectionsToPersons(Meeting toAdd, Model model) throws CommandE
107108
}
108109

109110
if (getConnectionToPerson().size() != 0) {
110-
toAdd.setPersonMeetingConnection(model.getPersonMeetingConnection());
111111
List<Person> lastShownList = model.getFilteredPersonList();
112112
// Check whether the index is out of bounds
113113
for (Index index : getConnectionToPerson()) {

0 commit comments

Comments
 (0)