|
| 1 | +package seedu.address.logic.commands.reminder; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_CCA_ID; |
| 5 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; |
| 6 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_START_DATE; |
| 7 | +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_REMINDERS; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | + |
| 12 | +import seedu.address.commons.core.Messages; |
| 13 | +import seedu.address.commons.core.index.Index; |
| 14 | +import seedu.address.commons.util.CollectionUtil; |
| 15 | +import seedu.address.logic.commands.Command; |
| 16 | +import seedu.address.logic.commands.CommandResult; |
| 17 | +import seedu.address.logic.commands.exceptions.CommandException; |
| 18 | +import seedu.address.model.Model; |
| 19 | +import seedu.address.model.cca.Cca; |
| 20 | +import seedu.address.model.reminder.Reminder; |
| 21 | +import seedu.address.model.reminder.ReminderFrequency; |
| 22 | +import seedu.address.model.reminder.ReminderName; |
| 23 | +import seedu.address.model.reminder.ReminderOccurrence; |
| 24 | +import seedu.address.model.reminder.ReminderStartDate; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Edits the details of an existing person in the address book. |
| 29 | + */ |
| 30 | +public class ReminderEditCommand extends Command { |
| 31 | + |
| 32 | + public static final String COMMAND_WORD = "editr"; |
| 33 | + |
| 34 | + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the reminder identified " |
| 35 | + + "by the index number used in the displayed reminder list. " |
| 36 | + + "Existing values will be overwritten by the input valies.\n" |
| 37 | + + "Parameters: INDEX (must be a positive integer)" |
| 38 | + + PREFIX_CCA_ID + "CCA_ID " |
| 39 | + + PREFIX_NAME + "REMINDER_NAME " |
| 40 | + + PREFIX_START_DATE + "START_DATE " |
| 41 | + + "Example: " + COMMAND_WORD + " " |
| 42 | + + PREFIX_CCA_ID + "1 " |
| 43 | + + PREFIX_NAME + "NUSSO rehearsal " |
| 44 | + + PREFIX_START_DATE + "2021-10-31"; |
| 45 | + |
| 46 | + public static final String MESSAGE_EDIT_REMINDER_SUCCESS = "Edited Reminder: %1$s"; |
| 47 | + public static final String REMINDER_NOT_EDITED = "At least one field to edit must be provided."; |
| 48 | + public static final String MESSAGE_DUPLICATE_EDIT = "This reminder already exists in the address book."; |
| 49 | + |
| 50 | + private final Index index; |
| 51 | + private final EditReminderDescriptor editReminderDescriptor; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param index of the person in the filtered person list to edit |
| 55 | + * @param editReminderDescriptor details to edit the reminder with |
| 56 | + */ |
| 57 | + public ReminderEditCommand(Index index, EditReminderDescriptor editReminderDescriptor) { |
| 58 | + requireNonNull(index); |
| 59 | + requireNonNull(editReminderDescriptor); |
| 60 | + |
| 61 | + this.index = index; |
| 62 | + this.editReminderDescriptor = editReminderDescriptor; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public CommandResult execute(Model model) throws CommandException { |
| 67 | + requireNonNull(model); |
| 68 | + List<Reminder> lastShownList = model.getFilteredReminderList(); |
| 69 | + |
| 70 | + if (index.getZeroBased() >= lastShownList.size()) { |
| 71 | + throw new CommandException(Messages.MESSAGE_INVALID_REMINDER_DISPLAYED_INDEX); |
| 72 | + } |
| 73 | + |
| 74 | + Reminder reminderToEdit = lastShownList.get(index.getZeroBased()); |
| 75 | + if (editReminderDescriptor.getCcaIndex().isPresent()) { |
| 76 | + Index ccaIndex = editReminderDescriptor.getCcaIndex().get(); |
| 77 | + List<Cca> lastShownCcaList = model.getFilteredCcaList(); |
| 78 | + if (ccaIndex.getZeroBased() >= lastShownCcaList.size()) { |
| 79 | + throw new CommandException(Messages.MESSAGE_INVALID_CCA_DISPLAYED_INDEX); |
| 80 | + } |
| 81 | + editReminderDescriptor.setCca(lastShownCcaList.get(ccaIndex.getZeroBased())); |
| 82 | + } |
| 83 | + Reminder editedReminder = createEditedReminder(reminderToEdit, editReminderDescriptor); |
| 84 | + |
| 85 | + if (!reminderToEdit.isSameReminder(editedReminder) && model.hasReminder(editedReminder)) { |
| 86 | + throw new CommandException(MESSAGE_DUPLICATE_EDIT); |
| 87 | + } |
| 88 | + |
| 89 | + model.setReminder(reminderToEdit, editedReminder); |
| 90 | + model.updateFilteredReminderList(PREDICATE_SHOW_ALL_REMINDERS); |
| 91 | + return new CommandResult(String.format(MESSAGE_EDIT_REMINDER_SUCCESS, editedReminder)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Creates and returns a {@code Person} with the details of {@code personToEdit} |
| 96 | + * edited with {@code editPersonDescriptor}. |
| 97 | + */ |
| 98 | + private static Reminder createEditedReminder(Reminder remToEdit, EditReminderDescriptor editRemDescriptor) { |
| 99 | + assert editRemDescriptor != null; |
| 100 | + |
| 101 | + ReminderName updatedName = editRemDescriptor.getReminderName().orElse(remToEdit.getName()); |
| 102 | + ReminderStartDate updatedReminderStartDate = editRemDescriptor |
| 103 | + .getReminderStartDate().orElse(remToEdit.getStartDate()); |
| 104 | + ReminderFrequency updatedFreq = editRemDescriptor |
| 105 | + .getReminderFrequency().orElse(remToEdit.getFrequency()); |
| 106 | + ReminderOccurrence updatedReminderOccurrence = editRemDescriptor |
| 107 | + .getReminderOccurrence().orElse(remToEdit.getOccurrences()); |
| 108 | + Cca updatedCca = editRemDescriptor |
| 109 | + .getCca().orElse(remToEdit.getCca()); |
| 110 | + |
| 111 | + Reminder newReminder = new Reminder(updatedName, updatedReminderStartDate, |
| 112 | + updatedFreq, updatedReminderOccurrence); |
| 113 | + newReminder.setCca(updatedCca); |
| 114 | + return newReminder; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public boolean equals(Object other) { |
| 119 | + // short circuit if same object |
| 120 | + if (other == this) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + // instanceof handles nulls |
| 125 | + if (!(other instanceof ReminderEditCommand)) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + // state check |
| 130 | + ReminderEditCommand e = (ReminderEditCommand) other; |
| 131 | + return index.equals(e.index) |
| 132 | + && editReminderDescriptor.equals(e.editReminderDescriptor); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Stores the details to edit the person with. Each non-empty field value will replace the |
| 137 | + * corresponding field value of the person. |
| 138 | + */ |
| 139 | + public static class EditReminderDescriptor { |
| 140 | + private ReminderName reminderName; |
| 141 | + private ReminderStartDate reminderStartDate; |
| 142 | + private ReminderFrequency reminderFrequency; |
| 143 | + private ReminderOccurrence reminderOccurrence; |
| 144 | + |
| 145 | + private Index ccaIndex; |
| 146 | + private Cca cca; |
| 147 | + |
| 148 | + public EditReminderDescriptor() {} |
| 149 | + |
| 150 | + /** |
| 151 | + * Copy constructor. |
| 152 | + * A defensive copy of {@code tags} is used internally. |
| 153 | + */ |
| 154 | + public EditReminderDescriptor(EditReminderDescriptor toCopy) { |
| 155 | + setReminderName(toCopy.reminderName); |
| 156 | + setReminderStartDate(toCopy.reminderStartDate); |
| 157 | + setReminderFrequency(toCopy.reminderFrequency); |
| 158 | + setReminderOccurrence(toCopy.reminderOccurrence); |
| 159 | + setCcaIndex(toCopy.ccaIndex); |
| 160 | + setCca(toCopy.cca); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns true if at least one field is edited. |
| 165 | + */ |
| 166 | + public boolean isAnyFieldEdited() { |
| 167 | + return CollectionUtil |
| 168 | + .isAnyNonNull(reminderName, reminderStartDate, reminderFrequency, reminderOccurrence, cca); |
| 169 | + } |
| 170 | + |
| 171 | + public void setReminderName(ReminderName reminderName) { |
| 172 | + this.reminderName = reminderName; |
| 173 | + } |
| 174 | + |
| 175 | + public Optional<ReminderName> getReminderName() { |
| 176 | + return Optional.ofNullable(reminderName); |
| 177 | + } |
| 178 | + |
| 179 | + public void setReminderStartDate(ReminderStartDate reminderStartDate) { |
| 180 | + this.reminderStartDate = reminderStartDate; |
| 181 | + } |
| 182 | + |
| 183 | + public Optional<ReminderStartDate> getReminderStartDate() { |
| 184 | + return Optional.ofNullable(reminderStartDate); |
| 185 | + } |
| 186 | + |
| 187 | + public void setReminderFrequency(ReminderFrequency reminderFrequency) { |
| 188 | + this.reminderFrequency = reminderFrequency; |
| 189 | + } |
| 190 | + |
| 191 | + public Optional<ReminderFrequency> getReminderFrequency() { |
| 192 | + return Optional.ofNullable(reminderFrequency); |
| 193 | + } |
| 194 | + |
| 195 | + public void setReminderOccurrence(ReminderOccurrence reminderOccurrence) { |
| 196 | + this.reminderOccurrence = reminderOccurrence; |
| 197 | + } |
| 198 | + |
| 199 | + public Optional<ReminderOccurrence> getReminderOccurrence() { |
| 200 | + return Optional.ofNullable(reminderOccurrence); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Sets {@code ccaIndex} to this object's {@code ccaIndex}. |
| 205 | + * A defensive copy of {@code ccaIndex} is used internally. |
| 206 | + */ |
| 207 | + public void setCcaIndex(Index ccaIndex) { |
| 208 | + this.ccaIndex = ccaIndex; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException} |
| 213 | + * if modification is attempted. |
| 214 | + * Returns {@code Optional#empty()} if {@code tags} is null. |
| 215 | + */ |
| 216 | + public Optional<Index> getCcaIndex() { |
| 217 | + return Optional.ofNullable(ccaIndex); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Sets {@code cca} to this object's {@code cca}. |
| 222 | + * A defensive copy of {@code ccaIndex} is used internally. |
| 223 | + * This is done only when model is accessible. |
| 224 | + */ |
| 225 | + public void setCca(Cca cca) { |
| 226 | + this.cca = cca; |
| 227 | + } |
| 228 | + |
| 229 | + public Optional<Cca> getCca() { |
| 230 | + return Optional.ofNullable(cca); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public boolean equals(Object other) { |
| 235 | + // short circuit if same object |
| 236 | + if (other == this) { |
| 237 | + return true; |
| 238 | + } |
| 239 | + |
| 240 | + // instanceof handles nulls |
| 241 | + if (!(other instanceof EditReminderDescriptor)) { |
| 242 | + return false; |
| 243 | + } |
| 244 | + |
| 245 | + // state check |
| 246 | + EditReminderDescriptor e = (EditReminderDescriptor) other; |
| 247 | + |
| 248 | + return getReminderName().equals(e.getReminderName()) |
| 249 | + && getReminderStartDate().equals(e.getReminderStartDate()) |
| 250 | + && getReminderFrequency().equals(e.getReminderFrequency()) |
| 251 | + && getReminderOccurrence().equals(e.getReminderOccurrence()) |
| 252 | + && getCcaIndex().equals(e.getCcaIndex()) |
| 253 | + && getCca().equals(e.getCca()); |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments