-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathEditCommand.java
320 lines (273 loc) · 12.3 KB
/
EditCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package seedu.address.logic.commands;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FACULTY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FRAMEWORK;
import static seedu.address.logic.parser.CliSyntax.PREFIX_LANGUAGE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MAJOR;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARKS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SKILL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Email;
import seedu.address.model.person.Faculty;
import seedu.address.model.person.Major;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.remark.Remark;
import seedu.address.model.skill.Framework;
import seedu.address.model.skill.Language;
import seedu.address.model.skill.Skill;
import seedu.address.model.tag.Tag;
/**
* Edits the details of an existing person in ComputingConnection.
*/
public class EditCommand extends Command {
public static final String COMMAND_WORD = "edit";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_FACULTY + "FACULTY] "
+ "[" + PREFIX_MAJOR + "MAJOR] "
+ "[" + PREFIX_SKILL + "SKILL] "
+ "[" + PREFIX_LANGUAGE + "LANGUAGE] "
+ "[" + PREFIX_FRAMEWORK + "FRAMEWORK] "
+ "[" + PREFIX_TAG + "TAG] "
+ "[" + PREFIX_REMARKS + "REMARK]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_EMAIL + "[email protected]";
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";
private final Index index;
private final EditPersonDescriptor editPersonDescriptor;
/**
* @param index of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(index);
requireNonNull(editPersonDescriptor);
this.index = index;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, editedPerson));
}
/**
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
*/
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
assert personToEdit != null;
Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Faculty updatedFaculty = editPersonDescriptor.getFaculty().orElse(personToEdit.getFaculty());
Major updatedMajor = editPersonDescriptor.getMajor().orElse(personToEdit.getMajor());
Set<Skill> updatedSkills = editPersonDescriptor.getSkills().orElse(personToEdit.getSkills());
Set<Language> updatedLanguages = editPersonDescriptor.getLanguages().orElse(personToEdit.getLanguages());
Set<Framework> updatedFrameworks = editPersonDescriptor.getFrameworks().orElse(personToEdit.getFrameworks());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Set<Remark> updatedRemarks = editPersonDescriptor.getRemarks().orElse(personToEdit.getRemarks());
return new Person(updatedName, updatedEmail, updatedFaculty, updatedMajor,
updatedSkills, updatedLanguages, updatedFrameworks, updatedTags,
updatedRemarks, personToEdit.getInteractions());
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}
// instanceof handles nulls
if (!(other instanceof EditCommand)) {
return false;
}
// state check
EditCommand e = (EditCommand) other;
return index.equals(e.index)
&& editPersonDescriptor.equals(e.editPersonDescriptor);
}
/**
* Stores the details to edit the person with. Each non-empty field value will replace the
* corresponding field value of the person.
*/
public static class EditPersonDescriptor {
private Name name;
private Email email;
private Faculty faculty;
private Major major;
private Set<Skill> skills;
private Set<Language> languages;
private Set<Framework> frameworks;
private Set<Tag> tags;
private Set<Remark> remarks;
public EditPersonDescriptor() {}
/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setName(toCopy.name);
setEmail(toCopy.email);
setFaculty(toCopy.faculty);
setMajor(toCopy.major);
setSkills(toCopy.skills);
setLanguages(toCopy.languages);
setFrameworks(toCopy.frameworks);
setTags(toCopy.tags);
setRemarks(toCopy.remarks);
}
/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, email, faculty, major, skills,
languages, frameworks, tags, remarks);
}
public void setName(Name name) {
this.name = name;
}
public Optional<Name> getName() {
return Optional.ofNullable(name);
}
public void setEmail(Email email) {
this.email = email;
}
public Optional<Email> getEmail() {
return Optional.ofNullable(email);
}
public void setFaculty(Faculty faculty) {
this.faculty = faculty;
}
public Optional<Faculty> getFaculty() {
return Optional.ofNullable(faculty);
}
public void setMajor(Major major) {
this.major = major;
}
public Optional<Major> getMajor() {
return Optional.ofNullable(major);
}
/**
* Sets {@code skill} to this object's {@code skills}.
* A defensive copy of {@code skills} is used internally.
*/
public void setSkills(Set<Skill> skills) {
this.skills = (skills != null) ? new HashSet<>(skills) : null;
}
/**
* Returns an unmodifiable skill set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code skills} is null.
*/
public Optional<Set<Skill>> getSkills() {
return (skills != null) ? Optional.of(Collections.unmodifiableSet(skills)) : Optional.empty();
}
/**
* Sets {@code language} to this object's {@code languages}.
* A defensive copy of {@code languages} is used internally.
*/
public void setLanguages(Set<Language> languages) {
this.languages = (languages != null) ? new HashSet<>(languages) : null;
}
/**
* Returns an unmodifiable language set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code languages} is null.
*/
public Optional<Set<Language>> getLanguages() {
return (languages != null) ? Optional.of(Collections.unmodifiableSet(languages)) : Optional.empty();
}
/**
* Sets {@code framework} to this object's {@code frameworks}.
* A defensive copy of {@code frameworks} is used internally.
*/
public void setFrameworks(Set<Framework> frameworks) {
this.frameworks = (frameworks != null) ? new HashSet<>(frameworks) : null;
}
/**
* Returns an unmodifiable framework set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code frameworks} is null.
*/
public Optional<Set<Framework>> getFrameworks() {
return (frameworks != null) ? Optional.of(Collections.unmodifiableSet(frameworks)) : Optional.empty();
}
/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}
/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}
/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setRemarks(Set<Remark> remarks) {
this.remarks = (remarks != null) ? new HashSet<>(remarks) : null;
}
/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Remark>> getRemarks() {
return (remarks != null) ? Optional.of(Collections.unmodifiableSet(remarks)) : Optional.empty();
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}
// instanceof handles nulls
if (!(other instanceof EditPersonDescriptor)) {
return false;
}
// state check
EditPersonDescriptor e = (EditPersonDescriptor) other;
return getName().equals(e.getName())
&& getEmail().equals(e.getEmail())
&& getFaculty().equals(e.getFaculty())
&& getMajor().equals(e.getMajor())
&& getTags().equals(e.getTags());
}
}
}