|
| 1 | +package seedu.address.logic.commands.deliverable; |
| 2 | + |
| 3 | + |
| 4 | +import static java.util.Objects.requireNonNull; |
| 5 | +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_DELIVERABLE_DISPLAYED_INDEX; |
| 6 | +import static seedu.address.logic.parser.deliverable.CliSyntax.PREFIX_CONTACTS; |
| 7 | +import static seedu.address.logic.parser.deliverable.CliSyntax.PREFIX_DEADLINE; |
| 8 | +import static seedu.address.logic.parser.deliverable.CliSyntax.PREFIX_DESCRIPTION; |
| 9 | +import static seedu.address.logic.parser.deliverable.CliSyntax.PREFIX_MILESTONE; |
| 10 | +import static seedu.address.logic.parser.deliverable.CliSyntax.PREFIX_TITLE; |
| 11 | +import static seedu.address.model.deliverable.ModelDeliverable.PREDICATE_SHOW_ALL_DELIVERABLES; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +import seedu.address.commons.core.index.Index; |
| 17 | +import seedu.address.commons.util.CollectionUtil; |
| 18 | +import seedu.address.logic.commands.CommandResult; |
| 19 | +import seedu.address.logic.commands.exceptions.CommandException; |
| 20 | +import seedu.address.model.deliverable.ModelDeliverable; |
| 21 | +import seedu.address.model.deliverable.deliverable.Deadline; |
| 22 | +import seedu.address.model.deliverable.deliverable.Deliverable; |
| 23 | +import seedu.address.model.deliverable.deliverable.Milestone; |
| 24 | +import seedu.address.model.util.Contacts; |
| 25 | +import seedu.address.model.util.OptionalDescription; |
| 26 | +import seedu.address.model.util.Title; |
| 27 | + |
| 28 | +/** |
| 29 | + * Edits the details of an existing deliverable. |
| 30 | + */ |
| 31 | +public class EditCommand extends Command { |
| 32 | + |
| 33 | + public static final String COMMAND_WORD = "edit"; |
| 34 | + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the deliverable identified " |
| 35 | + + "by the index number used in the displayed deliverable list. " |
| 36 | + + "Existing values will be overwritten by the input values.\n" |
| 37 | + + "Parameters: INDEX (must be a positive integer) " |
| 38 | + + "[" + PREFIX_TITLE + "TITLE] " |
| 39 | + + "[" + PREFIX_MILESTONE + "MILESTONE] " |
| 40 | + + "[" + PREFIX_DESCRIPTION + "DESCRIPTION] " |
| 41 | + + "[" + PREFIX_DEADLINE + "DEADLINE] " |
| 42 | + + "[" + PREFIX_CONTACTS + "CONTACTS]\n" |
| 43 | + + "Example: " + COMMAND_WORD + " 1 " |
| 44 | + + PREFIX_TITLE + "Finalise beta release features " |
| 45 | + + PREFIX_DEADLINE + "2021-01-10 12:00"; |
| 46 | + |
| 47 | + |
| 48 | + public static final String MESSAGE_EDIT_DELIVERABLE_SUCCESS = "Edited Deliverable: %1$s"; |
| 49 | + public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; |
| 50 | + public static final String MESSAGE_DUPLICATE_DELIVERABLE = "This deliverable already exists."; |
| 51 | + |
| 52 | + |
| 53 | + private final Index index; |
| 54 | + private final EditDeliverableDescriptor editDeliverableDescriptor; |
| 55 | + |
| 56 | + /** |
| 57 | + * @param index of the deliverable in the filtered deliverable list to edit |
| 58 | + * @param editDeliverableDescriptor details to edit the deliverable with |
| 59 | + */ |
| 60 | + public EditCommand(Index index, EditDeliverableDescriptor editDeliverableDescriptor) { |
| 61 | + requireNonNull(index); |
| 62 | + requireNonNull(editDeliverableDescriptor); |
| 63 | + |
| 64 | + this.index = index; |
| 65 | + this.editDeliverableDescriptor = new EditDeliverableDescriptor(editDeliverableDescriptor); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public CommandResult execute(ModelDeliverable modelDeliverable) throws CommandException { |
| 70 | + requireNonNull(modelDeliverable); |
| 71 | + |
| 72 | + List<Deliverable> lastShownList = modelDeliverable.getFilteredDeliverableList(); |
| 73 | + if (index.getZeroBased() >= lastShownList.size()) { |
| 74 | + throw new CommandException(MESSAGE_INVALID_DELIVERABLE_DISPLAYED_INDEX); |
| 75 | + } |
| 76 | + |
| 77 | + Deliverable deliverableToEdit = lastShownList.get(index.getZeroBased()); |
| 78 | + Deliverable editedDeliverable = createEditedDeliverable(deliverableToEdit, editDeliverableDescriptor); |
| 79 | + |
| 80 | + if (!deliverableToEdit.isSameDeliverable(editedDeliverable) && modelDeliverable.hasDeliverable( |
| 81 | + editedDeliverable)) { |
| 82 | + throw new CommandException(MESSAGE_DUPLICATE_DELIVERABLE); |
| 83 | + } |
| 84 | + |
| 85 | + modelDeliverable.setDeliverable(deliverableToEdit, editedDeliverable); |
| 86 | + modelDeliverable.updateFilteredDeliverableList(PREDICATE_SHOW_ALL_DELIVERABLES); |
| 87 | + return new CommandResult(String.format(MESSAGE_EDIT_DELIVERABLE_SUCCESS, editedDeliverable)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates and returns a {@code Deliverable} with the details of {@code deliverableToEdit} |
| 92 | + * edited with {@code editDeliverableDescriptor}. |
| 93 | + */ |
| 94 | + private static Deliverable createEditedDeliverable( |
| 95 | + Deliverable deliverableToEdit, EditDeliverableDescriptor editDeliverableDescriptor) { |
| 96 | + assert deliverableToEdit != null; |
| 97 | + |
| 98 | + Title updatedTitle = editDeliverableDescriptor.getTitle().orElse(deliverableToEdit.getTitle()); |
| 99 | + Milestone updatedMilestone = editDeliverableDescriptor.getMilestone().orElse(deliverableToEdit.getMilestone()); |
| 100 | + |
| 101 | + // Description takes optional String |
| 102 | + OptionalDescription updatedDesc = editDeliverableDescriptor.getDescription() |
| 103 | + .orElse(deliverableToEdit.getDescription()); |
| 104 | + |
| 105 | + Deadline updatedDeadline = editDeliverableDescriptor.getDeadline().orElse(deliverableToEdit.getDeadline()); |
| 106 | + |
| 107 | + // Contacts takes optional String |
| 108 | + Contacts updatedContacts = editDeliverableDescriptor.getContacts().orElse(deliverableToEdit.getContacts()); |
| 109 | + |
| 110 | + return new Deliverable(updatedTitle, updatedMilestone, updatedDesc, updatedDeadline, updatedContacts); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean equals(Object other) { |
| 115 | + // short circuit if same object |
| 116 | + if (other == this) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + // instanceof handles nulls |
| 121 | + if (!(other instanceof seedu.address.logic.commands.meeting.EditCommand)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + // state check |
| 126 | + EditCommand e = (EditCommand) other; |
| 127 | + return index.equals(e.index) |
| 128 | + && editDeliverableDescriptor.equals(e.editDeliverableDescriptor); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Stores the details to edit the deliverable with. Each non-empty field value will replace the |
| 133 | + * corresponding field value of the deliverable. |
| 134 | + */ |
| 135 | + public static class EditDeliverableDescriptor { |
| 136 | + private Title title; |
| 137 | + private Milestone milestone; |
| 138 | + private OptionalDescription description; |
| 139 | + private Deadline deadline; |
| 140 | + private Contacts contacts; |
| 141 | + |
| 142 | + public EditDeliverableDescriptor() { |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Copy attributes from deliverable to be edited. |
| 147 | + * |
| 148 | + * @param toCopy deliverable to be edited. |
| 149 | + */ |
| 150 | + public EditDeliverableDescriptor(EditDeliverableDescriptor toCopy) { |
| 151 | + setTitle(toCopy.title); |
| 152 | + setMilestone(toCopy.milestone); |
| 153 | + setDescription(toCopy.description); |
| 154 | + setDeadline(toCopy.deadline); |
| 155 | + setContacts(toCopy.contacts); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Returns true if at least one field is edited. |
| 160 | + */ |
| 161 | + public boolean isAnyFieldEdited() { |
| 162 | + return CollectionUtil.isAnyNonNull(title, milestone, description, deadline, contacts); |
| 163 | + } |
| 164 | + |
| 165 | + public void setTitle(Title title) { |
| 166 | + this.title = title; |
| 167 | + } |
| 168 | + |
| 169 | + public Optional<Title> getTitle() { |
| 170 | + return Optional.ofNullable(this.title); |
| 171 | + } |
| 172 | + |
| 173 | + public void setMilestone(Milestone milestone) { |
| 174 | + this.milestone = milestone; |
| 175 | + } |
| 176 | + |
| 177 | + public Optional<Milestone> getMilestone() { |
| 178 | + return Optional.ofNullable(this.milestone); |
| 179 | + } |
| 180 | + |
| 181 | + public void setDescription(OptionalDescription description) { |
| 182 | + this.description = description; |
| 183 | + } |
| 184 | + |
| 185 | + public Optional<OptionalDescription> getDescription() { |
| 186 | + return Optional.ofNullable(description); |
| 187 | + } |
| 188 | + |
| 189 | + public void setDeadline(Deadline deadline) { |
| 190 | + this.deadline = deadline; |
| 191 | + } |
| 192 | + |
| 193 | + public Optional<Deadline> getDeadline() { |
| 194 | + return Optional.ofNullable(deadline); |
| 195 | + } |
| 196 | + |
| 197 | + public void setContacts(Contacts contact) { |
| 198 | + this.contacts = contact; |
| 199 | + } |
| 200 | + |
| 201 | + public Optional<Contacts> getContacts() { |
| 202 | + return Optional.ofNullable(this.contacts); |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + @Override |
| 207 | + public boolean equals(Object other) { |
| 208 | + // short circuit if same object |
| 209 | + if (other == this) { |
| 210 | + return true; |
| 211 | + } |
| 212 | + |
| 213 | + // instanceof handles nulls |
| 214 | + if (!(other instanceof EditDeliverableDescriptor)) { |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + // state check |
| 219 | + EditDeliverableDescriptor e = (EditDeliverableDescriptor) other; |
| 220 | + |
| 221 | + return getTitle().equals(e.getTitle()) |
| 222 | + && getMilestone().equals(e.getMilestone()) |
| 223 | + && getDescription().equals(e.getDescription()) |
| 224 | + && getDeadline().equals(e.getDeadline()) |
| 225 | + && getContacts().equals(e.getContacts()); |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | +} |
| 230 | + |
0 commit comments