|
| 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 | +} |
0 commit comments