Skip to content

Commit

Permalink
Implement tasks unassign and contacts unassign (#102)
Browse files Browse the repository at this point in the history
* Implement tasks unassign and contacts unassign

* Fix lint
  • Loading branch information
taneliang authored and JayPeeTeeDee committed Oct 31, 2018
1 parent 6cb8f6f commit c31f4c3
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package seedu.address.logic.commands.contacts;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.events.ui.JumpToPersonListRequestEvent;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonId;
import seedu.address.model.task.Task;
import seedu.address.model.task.TaskId;

/**
* Unassigns a contact from a task. Both contact and task are identified by the index number used in the displayed
* person and task list respectively.
*/
public class UnassignCommand extends Command {

public static final String COMMAND_WORD = "unassign";

public static final String MESSAGE_USAGE = getCommandFormat(COMMAND_WORD)
+ ": Unassigns a contact to a task. Both contact and task are identified by the index number used in the "
+ "displayed person and task list respectively.\n"
+ "Parameters: "
+ PREFIX_CONTACT_ID + "CONTACT_INDEX "
+ PREFIX_TASK_ID + "TASK_INDEX\n"
+ "Example: " + getCommandFormat(COMMAND_WORD) + " "
+ PREFIX_CONTACT_ID + "2 "
+ PREFIX_TASK_ID + "4";

public static final String MESSAGE_ASSIGN_PERSON_SUCCESS = "Unassigned Person %1$s from Task %2$s";

private final Index targetContactIndex;
private final Index targetTaskIndex;

public UnassignCommand(Index targetContactIndex, Index targetTaskIndex) {
this.targetContactIndex = targetContactIndex;
this.targetTaskIndex = targetTaskIndex;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

List<Person> filteredPersonList = model.getFilteredPersonList();
if (targetContactIndex.getZeroBased() >= filteredPersonList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

List<Task> filteredTaskList = model.getFilteredTaskList();
if (targetTaskIndex.getZeroBased() >= filteredTaskList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Person personToEdit = filteredPersonList.get(targetContactIndex.getZeroBased());
Task taskToUnassign = filteredTaskList.get(targetTaskIndex.getZeroBased());

Set<TaskId> updatedTaskIds = new HashSet<>(personToEdit.getTaskIds());
updatedTaskIds.remove(taskToUnassign.getId());
Person editedPerson = new Person(personToEdit.getId(), personToEdit.getName(), personToEdit.getPhone(),
personToEdit.getEmail(), personToEdit.getAddress(), personToEdit.getTags(), updatedTaskIds);

Set<PersonId> updatedPersonIds = new HashSet<>(taskToUnassign.getPersonIds());
updatedPersonIds.remove(personToEdit.getId());
Task editedTask = new Task(taskToUnassign.getId(), taskToUnassign.getName(), taskToUnassign.getStartDateTime(),
taskToUnassign.getEndDateTime(), taskToUnassign.getTags(), updatedPersonIds);

model.updatePerson(personToEdit, editedPerson);
model.updateTask(taskToUnassign, editedTask);
model.commitAddressBook();

EventsCenter.getInstance().post(new JumpToPersonListRequestEvent(targetContactIndex));
return new CommandResult(String.format(MESSAGE_ASSIGN_PERSON_SUCCESS,
targetContactIndex.getOneBased(), targetTaskIndex.getOneBased()));

}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof UnassignCommand // instanceof handles nulls
&& targetContactIndex.equals(((UnassignCommand) other).targetContactIndex) // state checks
&& targetTaskIndex.equals(((UnassignCommand) other).targetTaskIndex)); // state checks
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package seedu.address.logic.commands.tasks;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.events.ui.JumpToPersonListRequestEvent;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonId;
import seedu.address.model.task.Task;
import seedu.address.model.task.TaskId;

/**
* Unassigns a task from a contact. Both contact and task are identified by the index number used in the displayed
* person and task list respectively.
*/
public class UnassignCommand extends Command {

public static final String COMMAND_WORD = "unassign";

public static final String MESSAGE_USAGE = getCommandFormat(COMMAND_WORD)
+ ": Unassigns a task from a contact. Both contact and task are identified by the index number used in the "
+ "displayed person and task list respectively.\n"
+ "Parameters: "
+ PREFIX_CONTACT_ID + "CONTACT_INDEX "
+ PREFIX_TASK_ID + "TASK_INDEX\n"
+ "Example: " + getCommandFormat(COMMAND_WORD) + " "
+ PREFIX_CONTACT_ID + "2 "
+ PREFIX_TASK_ID + "4";

public static final String MESSAGE_ASSIGN_TASK_SUCCESS = "Unassigned Task %1$s from Person %2$s";

private final Index targetContactIndex;
private final Index targetTaskIndex;

public UnassignCommand(Index targetContactIndex, Index targetTaskIndex) {
this.targetContactIndex = targetContactIndex;
this.targetTaskIndex = targetTaskIndex;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

List<Person> filteredPersonList = model.getFilteredPersonList();
if (targetContactIndex.getZeroBased() >= filteredPersonList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

List<Task> filteredTaskList = model.getFilteredTaskList();
if (targetTaskIndex.getZeroBased() >= filteredTaskList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Person personToEdit = filteredPersonList.get(targetContactIndex.getZeroBased());
Task taskToUnassign = filteredTaskList.get(targetTaskIndex.getZeroBased());

Set<TaskId> updatedTaskIds = new HashSet<>(personToEdit.getTaskIds());
updatedTaskIds.remove(taskToUnassign.getId());
Person editedPerson = new Person(personToEdit.getId(), personToEdit.getName(), personToEdit.getPhone(),
personToEdit.getEmail(), personToEdit.getAddress(), personToEdit.getTags(), updatedTaskIds);

Set<PersonId> updatedPersonIds = new HashSet<>(taskToUnassign.getPersonIds());
updatedPersonIds.remove(personToEdit.getId());
Task editedTask = new Task(taskToUnassign.getId(), taskToUnassign.getName(), taskToUnassign.getStartDateTime(),
taskToUnassign.getEndDateTime(), taskToUnassign.getTags(), updatedPersonIds);

model.updatePerson(personToEdit, editedPerson);
model.updateTask(taskToUnassign, editedTask);
model.commitAddressBook();

EventsCenter.getInstance().post(new JumpToPersonListRequestEvent(targetContactIndex));
return new CommandResult(String.format(MESSAGE_ASSIGN_TASK_SUCCESS,
targetTaskIndex.getOneBased(), targetContactIndex.getOneBased()));

}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof UnassignCommand // instanceof handles nulls
&& targetContactIndex.equals(((UnassignCommand) other).targetContactIndex) // state checks
&& targetTaskIndex.equals(((UnassignCommand) other).targetTaskIndex)); // state checks
}
}

5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/parser/ContactsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import seedu.address.logic.commands.contacts.FindCommand;
import seedu.address.logic.commands.contacts.ListCommand;
import seedu.address.logic.commands.contacts.SelectCommand;
import seedu.address.logic.commands.contacts.UnassignCommand;
import seedu.address.logic.parser.contacts.AddCommandParser;
import seedu.address.logic.parser.contacts.AssignCommandParser;
import seedu.address.logic.parser.contacts.DeleteCommandParser;
import seedu.address.logic.parser.contacts.EditCommandParser;
import seedu.address.logic.parser.contacts.FindCommandParser;
import seedu.address.logic.parser.contacts.SelectCommandParser;
import seedu.address.logic.parser.contacts.UnassignCommandParser;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -78,6 +80,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AssignCommand.COMMAND_WORD:
return new AssignCommandParser().parse(arguments);

case UnassignCommand.COMMAND_WORD:
return new UnassignCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/parser/TasksParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import seedu.address.logic.commands.tasks.EditCommand;
import seedu.address.logic.commands.tasks.FindCommand;
import seedu.address.logic.commands.tasks.ListCommand;
import seedu.address.logic.commands.tasks.UnassignCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.tasks.AddCommandParser;
import seedu.address.logic.parser.tasks.AssignCommandParser;
import seedu.address.logic.parser.tasks.DeleteCommandParser;
import seedu.address.logic.parser.tasks.EditCommandParser;
import seedu.address.logic.parser.tasks.FindCommandParser;
import seedu.address.logic.parser.tasks.UnassignCommandParser;

/**
* Parses user input.
Expand Down Expand Up @@ -69,6 +71,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AssignCommand.COMMAND_WORD:
return new AssignCommandParser().parse(arguments);

case UnassignCommand.COMMAND_WORD:
return new UnassignCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.parser.contacts;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID;

import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.contacts.UnassignCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new UnassignCommand object
*/
public class UnassignCommandParser implements Parser<UnassignCommand> {

/**
* Parses the given {@code String} of arguments in the context of the UnassignCommand
* and returns an UnassignCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public UnassignCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_CONTACT_ID, PREFIX_TASK_ID);

if (!arePrefixesPresent(argMultimap, PREFIX_CONTACT_ID, PREFIX_TASK_ID)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnassignCommand.MESSAGE_USAGE),
true);
}

Index contactIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT_ID).get());
Index taskIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_TASK_ID).get());
return new UnassignCommand(contactIndex, taskIndex);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.parser.tasks;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_CONTACT_ID;
import static seedu.address.logic.parser.contacts.CliSyntax.PREFIX_TASK_ID;

import java.util.stream.Stream;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.tasks.UnassignCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new UnassignCommand object
*/
public class UnassignCommandParser implements Parser<UnassignCommand> {

/**
* Parses the given {@code String} of arguments in the context of the UnassignCommand
* and returns an UnassignCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public UnassignCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_CONTACT_ID, PREFIX_TASK_ID);

if (!arePrefixesPresent(argMultimap, PREFIX_CONTACT_ID, PREFIX_TASK_ID)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, UnassignCommand.MESSAGE_USAGE),
true);
}

Index contactIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_CONTACT_ID).get());
Index taskIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_TASK_ID).get());
return new UnassignCommand(contactIndex, taskIndex);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}

0 comments on commit c31f4c3

Please sign in to comment.