Skip to content

Commit

Permalink
Merge pull request #59 from tanshiyu1999/branch-Edit
Browse files Browse the repository at this point in the history
Update Edit Function
  • Loading branch information
tanshiyu1999 authored Oct 27, 2023
2 parents 63628d9 + 14647e6 commit 6cabedd
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 29 deletions.
137 changes: 126 additions & 11 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
import seedu.address.model.contact.Email;
import seedu.address.model.contact.Id;
import seedu.address.model.contact.Name;
import seedu.address.model.contact.Organization;
import seedu.address.model.contact.Phone;
import seedu.address.model.contact.Position;
import seedu.address.model.contact.Recruiter;
import seedu.address.model.contact.Status;
import seedu.address.model.contact.Type;
import seedu.address.model.contact.Url;
import seedu.address.model.tag.Tag;

Expand All @@ -54,25 +59,53 @@ public class EditCommand extends Command {
AutocompleteDataSet.anyNumberOf(FLAG_TAG)
);

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the contact identified "
+ "by the index number used in the displayed contact list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
public static final String MESSAGE_ORGANIZATION_USAGE = "Edits an organization.\n"
+ "Parameters: INDEX/ID "
+ "[" + FLAG_NAME + " NAME] "
+ "[" + FLAG_ID + " ID] "
+ "[" + FLAG_PHONE + " PHONE] "
+ "[" + FLAG_EMAIL + " EMAIL] "
+ "[" + FLAG_URL + " URL] "
+ "[" + FLAG_ADDRESS + " ADDRESS] "
+ "[" + FLAG_STATUS + " STATUS] "
+ "[" + FLAG_POSITION + " POSITION] "
+ "[" + FLAG_TAG + " TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ FLAG_PHONE + " 91234567 "
+ FLAG_EMAIL + " [email protected]";
+ FLAG_STATUS + " Applied";

public static final String MESSAGE_RECRUITER_USAGE = "Edits a recruiter.\n"
+ "Parameters: INDEX/ID "
+ "[" + FLAG_NAME + " NAME] "
+ "[" + FLAG_ID + " ID] "
+ "[" + FLAG_PHONE + " PHONE] "
+ "[" + FLAG_EMAIL + " EMAIL] "
+ "[" + FLAG_URL + " URL] "
+ "[" + FLAG_ADDRESS + " ADDRESS] "
+ "[" + FLAG_ORGANIZATION_ID + " OID] "
+ "[" + FLAG_TAG + " TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ FLAG_PHONE + " 91234567 "
+ FLAG_EMAIL + " [email protected]";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Edits the details of the contact of the class type Organization or Recruiter,"
+ " identified by its index in the displayed contact list or its id."
+ " Note that existing values will be overwritten by the input values."
+ " The input format varies depending on the class:\n\n"
+ MESSAGE_ORGANIZATION_USAGE + "\n\n"
+ MESSAGE_RECRUITER_USAGE;

public static final String MESSAGE_EDIT_CONTACT_SUCCESS = "Edited Contact: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the address book.";
public static final String MESSAGE_INVALID_ORGANIZATION =
"The organization id you supplied does not match any organization in the address book.";

private final Index index;
private final EditContactDescriptor editContactDescriptor;
private Index index;

private Id targetId = null;
private EditContactDescriptor editContactDescriptor;

/**
* @param index of the contact in the filtered contact list to edit
Expand All @@ -86,32 +119,54 @@ public EditCommand(Index index, EditContactDescriptor editContactDescriptor) {
this.editContactDescriptor = new EditContactDescriptor(editContactDescriptor);
}

/**
* @param targetId of the contact to be editted
*/
public EditCommand(Id targetId, EditContactDescriptor editContactDescriptor) {
this.targetId = targetId;
this.editContactDescriptor = new EditContactDescriptor(editContactDescriptor);
}


@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (this.targetId != null) {
Contact contactToEdit = model.getContactById(targetId);
return getCommandResult(model, contactToEdit);
}

List<Contact> lastShownList = model.getFilteredContactList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_CONTACT_DISPLAYED_INDEX);
}

Contact contactToEdit = lastShownList.get(index.getZeroBased());
Contact editedContact = createEditedContact(contactToEdit, editContactDescriptor);
return getCommandResult(model, contactToEdit);
}


public CommandResult getCommandResult(Model model, Contact contactToEdit) throws CommandException {
Contact editedContact = createEditedContact(model, contactToEdit, editContactDescriptor);
if (!contactToEdit.isSameContact(editedContact) && model.hasContact(editedContact)) {
throw new CommandException(MESSAGE_DUPLICATE_CONTACT);
}

model.setContact(contactToEdit, editedContact);
model.updateFilteredContactList(PREDICATE_SHOW_ALL_CONTACTS);
return new CommandResult(String.format(MESSAGE_EDIT_CONTACT_SUCCESS, Messages.format(editedContact)));
}





/**
* Creates and returns a {@code Contact} with the details of {@code contactToEdit}
* edited with {@code editContactDescriptor}.
*/
private static Contact createEditedContact(Contact contactToEdit, EditContactDescriptor editContactDescriptor) {
private static Contact createEditedContact(Model model, Contact contactToEdit,
EditContactDescriptor editContactDescriptor) throws CommandException {
assert contactToEdit != null;

Name updatedName = editContactDescriptor.getName()
Expand All @@ -129,6 +184,32 @@ private static Contact createEditedContact(Contact contactToEdit, EditContactDes
Set<Tag> updatedTags = editContactDescriptor.getTags()
.orElse(contactToEdit.getTags());


if (contactToEdit.getType() == Type.ORGANIZATION) {
Status updatedStatus = editContactDescriptor.getStatus()
.orElse(((Organization) contactToEdit).getStatus().orElse(null));

Position updatedPosition = editContactDescriptor.getPosition()
.orElse(((Organization) contactToEdit).getPosition().orElse(null));
return new Organization(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl,
updatedAddress, updatedTags, updatedStatus, updatedPosition, null);
} else if (contactToEdit.getType() == Type.RECRUITER) {
Optional<Id> updatedOid = editContactDescriptor
.getOrganizationId()
.or(() -> ((Recruiter) contactToEdit).getOrganizationId());

Organization updatedOrganization = (Organization) updatedOid.map(model::getContactById)
.filter(c -> c.getType() == Type.ORGANIZATION)
.orElse(null);

if (updatedOid.isPresent() && updatedOrganization == null) {
throw new CommandException(MESSAGE_INVALID_ORGANIZATION);
}

return new Recruiter(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl,
updatedAddress, updatedTags, updatedOrganization);
}

return new Contact(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl, updatedAddress, updatedTags);
}

Expand Down Expand Up @@ -168,6 +249,10 @@ public static class EditContactDescriptor {
private Url url;
private Address address;
private Set<Tag> tags;
private Status status;
private Position position;

private Id oid;

public EditContactDescriptor() {}

Expand All @@ -183,19 +268,30 @@ public EditContactDescriptor(EditContactDescriptor toCopy) {
setUrl(toCopy.url);
setAddress(toCopy.address);
setTags(toCopy.tags);
setStatus(toCopy.status);
setPosition(toCopy.position);
setOid(toCopy.oid);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags);
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags, id, url, status, position, oid);
}

public void setName(Name name) {
this.name = name;
}

public Optional<Id> getOrganizationId() {
return Optional.ofNullable(oid);
}

public void setOid(Id id) {
this.oid = id;
}

public Optional<Name> getName() {
return Optional.ofNullable(name);
}
Expand Down Expand Up @@ -236,6 +332,22 @@ public void setAddress(Address address) {
this.address = address;
}

public void setStatus(Status status) {
this.status = status;
}

public void setPosition(Position position) {
this.position = position;
}

public Optional<Status> getStatus() {
return Optional.ofNullable(status);
}

public Optional<Position> getPosition() {
return Optional.ofNullable(position);
}

public Optional<Address> getAddress() {
return Optional.ofNullable(address);
}
Expand Down Expand Up @@ -284,6 +396,9 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("tags", tags)
.add("url", url)
.add("status", status)
.add("position", position)
.toString();
}
}
Expand Down
49 changes: 46 additions & 3 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL;
import static seedu.address.logic.parser.CliSyntax.FLAG_ID;
import static seedu.address.logic.parser.CliSyntax.FLAG_NAME;
import static seedu.address.logic.parser.CliSyntax.FLAG_ORGANIZATION_ID;
import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE;
import static seedu.address.logic.parser.CliSyntax.FLAG_POSITION;
import static seedu.address.logic.parser.CliSyntax.FLAG_STATUS;
import static seedu.address.logic.parser.CliSyntax.FLAG_TAG;
import static seedu.address.logic.parser.CliSyntax.FLAG_URL;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -17,8 +22,13 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditContactDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.contact.Id;
import seedu.address.model.tag.Tag;





/**
* Parses input arguments and creates a new EditCommand object
*/
Expand All @@ -36,14 +46,23 @@ public EditCommand parse(String args) throws ParseException {
EditCommand.AUTOCOMPLETE_SUPPLIER.getAllPossibleFlags().toArray(Flag[]::new));

Index index;
Id targetId;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
String preambleStr = argMultimap.getPreamble();
if (preambleStr.matches("^[A-Za-z].*")) {
targetId = ParserUtil.parseId(preambleStr);
index = null;
} else {
index = ParserUtil.parseIndex(preambleStr);
targetId = null;
}
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

argMultimap.verifyNoDuplicateFlagsFor(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS);
argMultimap.verifyNoDuplicateFlagsFor(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL,
FLAG_ADDRESS, FLAG_URL, FLAG_ID, FLAG_STATUS, FLAG_POSITION, FLAG_ORGANIZATION_ID);

EditContactDescriptor editContactDescriptor = new EditContactDescriptor();

Expand All @@ -59,15 +78,39 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(FLAG_ADDRESS).isPresent()) {
editContactDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get()));
}
if (argMultimap.getValue(FLAG_URL).isPresent()) {
editContactDescriptor.setUrl(ParserUtil.parseUrl(argMultimap.getValue(FLAG_URL).get()));
}
if (argMultimap.getValue(FLAG_ID).isPresent()) {
editContactDescriptor.setId(ParserUtil.parseId(argMultimap.getValue(FLAG_ID).get()));
}
if (argMultimap.getValue(FLAG_STATUS).isPresent()) {
editContactDescriptor.setStatus(ParserUtil.parseStatus(argMultimap.getValue(FLAG_STATUS).get()));
}
if (argMultimap.getValue(FLAG_POSITION).isPresent()) {
editContactDescriptor.setPosition(ParserUtil.parsePosition(argMultimap.getValue(FLAG_POSITION).get()));
}
if (argMultimap.getValue(FLAG_ORGANIZATION_ID).isPresent()) {
editContactDescriptor.setOid(ParserUtil.parseId(argMultimap.getValue(FLAG_ORGANIZATION_ID).get()));
}
parseTagsForEdit(argMultimap.getAllValues(FLAG_TAG)).ifPresent(editContactDescriptor::setTags);

if (!editContactDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
}

return new EditCommand(index, editContactDescriptor);
if (targetId == null) {
return new EditCommand(index, editContactDescriptor);

} else {
return new EditCommand(targetId, editContactDescriptor);

}

}



/**
* 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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/contact/Id.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Id {
* The first character of the id must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[a-zA-Z0-9]([_\\-]?[a-zA-Z0-9])*";
public static final String VALIDATION_REGEX = "[a-zA-Z](([_\\-]?[a-zA-Z0-9])*)";

public final String value;

Expand All @@ -36,7 +36,7 @@ public Id(String id) {
* Constructs an autogenerated {@code Id}.
*/
public Id() {
value = UUID.randomUUID().toString();
value = "i-" + UUID.randomUUID().toString();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/contact/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public Organization(
super(name, id, phone, email, url, address, tags);
this.status = Optional.ofNullable(status);
this.position = Optional.ofNullable(position);
this.rids.addAll(rids);
// Todo: Likely to deprecate rids completely
// this.rids.addAll(rids);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"persons": [ {
"name": "Valid Person",
"id": "18dfa74c-2229-4f4f-99a5-336c25e6783e",
"id": "a8dfa74c-2229-4f4f-99a5-336c25e6783e",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
}, {
"name": "Person With Invalid Phone Field",
"id" : "3a6e0af8-5092-47c0-baf7-18d6dc535823",
"id" : "ba6e0af8-5092-47c0-baf7-18d6dc535823",
"phone": "948asdf2424",
"email": "[email protected]",
"address": "4th street"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"persons": [ {
"name": "Person with invalid name field: Ha!ns Mu@ster",
"id" : "3a6e0af8-5092-47c0-baf7-18d6dc535823",
"id" : "ca6e0af8-5092-47c0-baf7-18d6dc535823",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
Expand Down
Loading

0 comments on commit 6cabedd

Please sign in to comment.