forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/seedu/address/logic/commands/SelfEditCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROJECT; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.permission.Permission; | ||
import seedu.address.model.person.Person; | ||
|
||
|
||
/** | ||
* Edits the details of an existing person in the address book. | ||
* This command makes use of EditCommand's classes to perform it's work. | ||
*/ | ||
public class SelfEditCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "myself"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of yourself, the currently logged " | ||
+ "in person. Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: " | ||
+ "[" + PREFIX_PHONE + " PHONE] " | ||
+ "[" + PREFIX_EMAIL + " EMAIL] " | ||
+ "[" + PREFIX_ADDRESS + " ADDRESS] " | ||
+ "[" + PREFIX_PROJECT + " PROJECT]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_PHONE + " 91234567 " | ||
+ PREFIX_EMAIL + " johndoe@example.com"; | ||
|
||
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s"; | ||
public static final String ADMIN_EDIT_ERROR = "You're currently logged in as admin, not as an employee!\nAdmin" | ||
+ " cannot be edited. Perhaps you meant edit instead?"; | ||
|
||
private EditCommand.EditPersonDescriptor editPersonDescriptor; | ||
|
||
/** | ||
* @param editPersonDescriptor details to edit the person with | ||
*/ | ||
public SelfEditCommand(EditCommand.EditPersonDescriptor editPersonDescriptor) { | ||
requireNonNull(editPersonDescriptor); | ||
|
||
requiredPermission.addPermissions(Permission.EDIT_EMPLOYEE); | ||
this.editPersonDescriptor = new EditCommand.EditPersonDescriptor(editPersonDescriptor); | ||
} | ||
|
||
@Override | ||
public CommandResult runBody(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (model.getLoggedInUser().isAdminUser()) { | ||
throw new CommandException(ADMIN_EDIT_ERROR); | ||
} | ||
|
||
Person personToEdit = model.getLoggedInUser().getPerson(); | ||
Person editedPerson = EditCommand.createEditedPerson(personToEdit, editPersonDescriptor); | ||
|
||
model.updatePerson(personToEdit, editedPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
model.commitAddressBook(); | ||
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, editedPerson)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof SelfEditCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
SelfEditCommand e = (SelfEditCommand) other; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/seedu/address/logic/parser/SelfEditCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROJECT; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; | ||
import seedu.address.logic.commands.SelfEditCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.project.Project; | ||
|
||
/** | ||
* Parses input arguments and creates a new EditCommand object | ||
*/ | ||
public class SelfEditCommandParser implements Parser<SelfEditCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the EditCommand | ||
* and returns an EditCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SelfEditCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, | ||
PREFIX_PROJECT); | ||
|
||
EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor(); | ||
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) { | ||
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) { | ||
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) { | ||
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get())); | ||
} | ||
parseProjectsForEdit(argMultimap.getAllValues(PREFIX_PROJECT)).ifPresent(editPersonDescriptor::setProjects); | ||
|
||
if (!editPersonDescriptor.isAnyFieldEdited()) { | ||
throw new ParseException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, | ||
SelfEditCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
return new SelfEditCommand(editPersonDescriptor); | ||
} | ||
|
||
/** | ||
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty. | ||
* If {@code tags} contain only one element which is an empty string, it will be parsed into a | ||
* {@code Set<Tag>} containing zero tags. | ||
*/ | ||
private Optional<Set<Project>> parseProjectsForEdit(Collection<String> tags) throws ParseException { | ||
assert tags != null; | ||
|
||
if (tags.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags; | ||
return Optional.of(ParserUtil.parseProjects(tagSet)); | ||
} | ||
|
||
} |