Skip to content

Commit

Permalink
Merge pull request #135 from EltonGohJH/Elton-v1.3-Refactor-Group-Com…
Browse files Browse the repository at this point in the history
…mands

Refactor: Refactor Group Commands
  • Loading branch information
Yukun99 authored Oct 22, 2021
2 parents f6e32b8 + 43d7070 commit 3638a13
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 141 deletions.
6 changes: 4 additions & 2 deletions src/main/java/seedu/notor/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_UNEXPECTED_ERROR = "Unexpected error!";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_GROUP_DISPLAYED_INDEX = "The group index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_GROUPS_LISTED_OVERVIEW = "%1$d groups listed!";
public static final String MESSAGE_GROUPS_NOT_LISTED = "Groups are not listed.";

public static final String MESSAGE_GROUPS_OR_SUBGROUP_NOT_LISTED = "Groups or SubGroups are not listed. "
+ "Please switch to Group or SubGroup view";
public static final String MESSAGE_GROUPS_NOT_LISTED = "Groups are not listed. Please switch to Group view";
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package seedu.notor.logic.commands;

import java.util.Arrays;
import java.util.List;

import seedu.notor.model.Model;
import seedu.notor.ui.WarningWindow;

/**
* Terminates the program.
* Clears general note.
*/
public class ClearNoteCommand implements Command {

// Keep this around for command description usage.
public static final String COMMAND_WORD = "clearnote";
public static final List<String> COMMAND_WORDS = Arrays.asList("clearnote", "cn");
public static final String MESSAGE_CLEAR_NOTE_SUCCESS = "Cleared general note.";
public static final String MESSAGE_CLEAR_NOTE_CANCEL = "Clearing of general note has been cancelled.";
public static final String CONFIRMATION_MESSAGE = "Do you want to proceed with clearing general note?";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/notor/logic/commands/NoteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.List;

import seedu.notor.model.Model;


Expand All @@ -10,6 +13,7 @@
*/
public class NoteCommand implements Command {
public static final String COMMAND_WORD = "note";
public static final List<String> COMMAND_WORDS = Arrays.asList("note", "n");

public static final String MESSAGE_SUCCESS = "Opened General Note.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static seedu.notor.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Arrays;
import java.util.List;

import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.commands.CommandResult;
import seedu.notor.logic.commands.person.PersonCommand;
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.logic.executors.group.GroupClearNoteExecutor;
import seedu.notor.logic.executors.group.GroupExecutor;
Expand All @@ -14,15 +16,16 @@
*/
public class GroupClearNoteCommand extends GroupCommand {
public static final String COMMAND_WORD = "clearnote";
public static final List<String> COMMAND_WORDS = Arrays.asList("clearnote", "cn");

private static final String COMMAND_DESCRIPTION =
": Clears the notes of the group identified by the index number used in the current group listing.\n";

public static final String MESSAGE_USAGE = PersonCommand.COMMAND_WORD + " INDEX " + COMMAND_WORD
public static final String MESSAGE_USAGE = GroupCommand.COMMAND_WORD + " INDEX /" + COMMAND_WORD
+ COMMAND_DESCRIPTION
+ "Parameters: none"
+ "Example: "
+ PersonCommand.COMMAND_WORD + " 1 " + COMMAND_WORD;
+ GroupCommand.COMMAND_WORD + " 1 /" + COMMAND_WORD;

private final GroupExecutor executor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

public abstract class GroupCommand extends TargetedCommand {
public static final String MESSAGE_USAGE = "Please use the help command to find out how to use the bot.";

public static final String COMMAND_WORD = "group";
public static final List<String> COMMAND_WORDS = Arrays.asList("group", "g");
protected final Index index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.List;

import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.commands.CommandResult;
import seedu.notor.logic.executors.exceptions.ExecuteException;
Expand All @@ -13,15 +16,16 @@
public class GroupDeleteCommand extends GroupCommand {

public static final String COMMAND_WORD = "delete";
public static final List<String> COMMAND_WORDS = Arrays.asList("delete", "d");

private static final String COMMAND_DESCRIPTION =
": Deletes the group identified by the index number used in the displayed group list.\n";

public static final String MESSAGE_USAGE = GroupCommand.COMMAND_WORD + " INDEX " + COMMAND_WORD
public static final String MESSAGE_USAGE = GroupCommand.COMMAND_WORD + " INDEX /" + COMMAND_WORD
+ COMMAND_DESCRIPTION
+ "Parameters: none"
+ "Example: "
+ GroupCommand.COMMAND_WORD + " 1 " + COMMAND_WORD;
+ GroupCommand.COMMAND_WORD + " 1 /" + COMMAND_WORD;

private final GroupDeleteExecutor executor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static seedu.notor.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Arrays;
import java.util.List;

import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.commands.CommandResult;
import seedu.notor.logic.commands.person.PersonCommand;
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.logic.executors.group.GroupExecutor;
import seedu.notor.logic.executors.group.GroupNoteExecutor;
Expand All @@ -14,15 +16,16 @@
*/
public class GroupNoteCommand extends GroupCommand {
public static final String COMMAND_WORD = "note";
public static final List<String> COMMAND_WORDS = Arrays.asList("note", "n");

private static final String COMMAND_DESCRIPTION =
": Edits the notes of the group identified by the index number used in the current group listing.\n";

public static final String MESSAGE_USAGE = PersonCommand.COMMAND_WORD + " INDEX " + COMMAND_WORD
public static final String MESSAGE_USAGE = GroupCommand.COMMAND_WORD + " INDEX /" + COMMAND_WORD
+ COMMAND_DESCRIPTION
+ "Parameters: none"
+ "Example: "
+ PersonCommand.COMMAND_WORD + " 1 " + COMMAND_WORD;
+ GroupCommand.COMMAND_WORD + " 1 /" + COMMAND_WORD;

private final GroupExecutor executor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.List;

import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.commands.CommandResult;
import seedu.notor.logic.executors.exceptions.ExecuteException;
Expand All @@ -12,17 +15,16 @@
* Clears the Note of the person by the given index.
*/
public class PersonClearNoteCommand extends PersonCommand {

public static final String COMMAND_WORD = "clearnote";

public static final List<String> COMMAND_WORDS = Arrays.asList("clearnote", "cn");

private static final String COMMAND_DESCRIPTION = ": Clears the note of a person in notor.\n";

public static final String MESSAGE_USAGE = PersonCommand.COMMAND_WORD + " INDEX " + COMMAND_WORD
public static final String MESSAGE_USAGE = PersonCommand.COMMAND_WORD + " INDEX /" + COMMAND_WORD
+ COMMAND_DESCRIPTION
+ "Parameters: none"
+ "Example: "
+ PersonCommand.COMMAND_WORD + " 1 " + COMMAND_WORD;
+ PersonCommand.COMMAND_WORD + " 1 /" + COMMAND_WORD;

private final PersonExecutor executor;

Expand Down Expand Up @@ -56,5 +58,4 @@ public boolean equals(Object other) {
PersonClearNoteCommand e = (PersonClearNoteCommand) other;
return executor.equals(e.executor);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.model.common.Note;
import seedu.notor.model.group.Group;
import seedu.notor.model.group.SubGroup;
import seedu.notor.model.group.SuperGroup;
import seedu.notor.ui.WarningWindow;

/**
Expand All @@ -34,23 +32,18 @@ public GroupClearNoteExecutor(Index index) {

@Override
public CommandResult execute() throws ExecuteException {
// TODO: To update after list is done.
if (true) {
SuperGroup superGroup = super.getGroup();
WarningWindow warningWindow = new WarningWindow(String.format(CONFIRMATION_MESSAGE,
superGroup.getName()));
warningWindow.show();
if (warningWindow.canContinue()) {
superGroup.setNote(Note.EMPTY_NOTE);
return new CommandResult(String.format(MESSAGE_CLEAR_GROUP_NOTE_SUCCESS, superGroup));
Group group = super.getGroup();
WarningWindow warningWindow = new WarningWindow(String.format(CONFIRMATION_MESSAGE, group));
warningWindow.show();
// TODO: To update the if true after list is done. it is suppose to check person view.
if (warningWindow.canContinue()) {
if (true) {
group.setNote(Note.EMPTY_NOTE);
return new CommandResult(String.format(MESSAGE_CLEAR_GROUP_NOTE_SUCCESS, group));
}
return new CommandResult(String.format(MESSAGE_CLEAR_GROUP_NOTE_CANCEL, superGroup));
} else if (model.isSubGroupView()) {
SubGroup subGroup = super.getSubGroup();
return new CommandResult(generateSuccessMessage(subGroup), false, true, false, subGroup);
} else {
throw new ExecuteException(Messages.MESSAGE_GROUPS_NOT_LISTED);
throw new ExecuteException(Messages.MESSAGE_GROUPS_OR_SUBGROUP_NOT_LISTED);
}
return new CommandResult(MESSAGE_CLEAR_GROUP_NOTE_CANCEL);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,40 @@
import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.commands.CommandResult;
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.model.group.Group;
import seedu.notor.model.group.SubGroup;
import seedu.notor.model.group.SuperGroup;
import seedu.notor.ui.WarningWindow;

public class GroupDeleteExecutor extends GroupExecutor {
public static final String MESSAGE_DELETE_GROUP_SUCCESS = "Deleted Group: %1$s";
public static final String MESSAGE_DELETE_GROUP_CANCEL = "Deleting of Group: %1$s has been cancelled.";
public static final String CONFIRMATION_MESSAGE = "Do you want to proceed with deleting of Group: %1$s?";


public GroupDeleteExecutor(Index index) {
super(index);
}

@Override
public CommandResult execute() throws ExecuteException {
// TODO: To update after list is done.
if (true) {
SuperGroup deletedGroup = super.getGroup();
model.deleteSuperGroup(deletedGroup);
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, deletedGroup));
} else if (model.isSubGroupView()) {
SubGroup deletedSubGroup = super.getSubGroup();
model.deleteSubGroup(deletedSubGroup);
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, deletedSubGroup));
} else {
throw new ExecuteException(Messages.MESSAGE_GROUPS_NOT_LISTED);
Group deletedGroup = super.getGroup();
WarningWindow warningWindow = new WarningWindow(String.format(CONFIRMATION_MESSAGE, deletedGroup));
warningWindow.show();
if (warningWindow.canContinue()) {
// TODO: To update the if true after list is done. it is suppose to check person view.
if (true) {
if (deletedGroup instanceof SuperGroup) {
model.deleteSuperGroup((SuperGroup) deletedGroup);
} else if (deletedGroup instanceof SubGroup) {
model.deleteSubGroup((SubGroup) deletedGroup);
} else {
throw new ExecuteException(Messages.MESSAGE_UNEXPECTED_ERROR);
}
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, deletedGroup));
}
throw new ExecuteException(Messages.MESSAGE_GROUPS_OR_SUBGROUP_NOT_LISTED);
}
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_CANCEL, deletedGroup));
}
}
17 changes: 3 additions & 14 deletions src/main/java/seedu/notor/logic/executors/group/GroupExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.executors.Executor;
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.model.group.SubGroup;
import seedu.notor.model.group.SuperGroup;
import seedu.notor.model.group.Group;

public abstract class GroupExecutor extends Executor {
protected final Index index;
Expand All @@ -16,18 +15,8 @@ public GroupExecutor(Index index) {
this.index = index;
}

protected SuperGroup getGroup() throws ExecuteException {
List<SuperGroup> lastShownList = model.getFilteredGroupList();

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

return lastShownList.get(index.getZeroBased());
}

protected SubGroup getSubGroup() throws ExecuteException {
List<SubGroup> lastShownList = model.getFilteredSubGroupList();
protected Group getGroup() throws ExecuteException {
List<? extends Group> lastShownList = model.getFilteredGroupList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new ExecuteException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.logic.executors.person.PersonNoteExecutor;
import seedu.notor.model.group.Group;
import seedu.notor.model.group.SubGroup;
import seedu.notor.model.group.SuperGroup;

/**
* Executor for a GroupNoteCommand.
Expand All @@ -31,16 +29,12 @@ public GroupNoteExecutor(Index index) {

@Override
public CommandResult execute() throws ExecuteException {
// TODO: To update after list is done.
// TODO: To update the if true after list is done. it is suppose to check person view.
if (true) {
SuperGroup superGroup = super.getGroup();
return new CommandResult(generateSuccessMessage(superGroup), false, true, false, superGroup);
} else if (model.isSubGroupView()) {
SubGroup subGroup = super.getSubGroup();
return new CommandResult(generateSuccessMessage(subGroup), false, true, false, subGroup);
} else {
throw new ExecuteException(Messages.MESSAGE_GROUPS_NOT_LISTED);
Group group = super.getGroup();
return new CommandResult(generateSuccessMessage(group), false, true, false, group);
}
throw new ExecuteException(Messages.MESSAGE_GROUPS_OR_SUBGROUP_NOT_LISTED);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package seedu.notor.logic.executors.group;

import seedu.notor.commons.core.Messages;
import seedu.notor.commons.core.index.Index;
import seedu.notor.logic.commands.CommandResult;
import seedu.notor.logic.executors.exceptions.ExecuteException;
import seedu.notor.model.exceptions.DuplicateItemException;
import seedu.notor.model.group.Group;
import seedu.notor.model.group.SubGroup;
import seedu.notor.model.group.SuperGroup;

Expand All @@ -27,14 +29,20 @@ public SubGroupCreateExecutor(Index index, SubGroup subGroup) {
@Override public CommandResult execute() throws ExecuteException {
try {
if (model.getFilteredGroupList().size() > index.getOneBased()) {
SuperGroup superGroup = model.getFilteredGroupList().get(index.getZeroBased());
superGroup.addSubGroup(subGroup);
subGroup.setParent(superGroup);
Group group = model.getFilteredGroupList().get(index.getZeroBased());
// TODO: Guard clause for if group is a SubGroup instead.
// Make it return its own type of error.
if (group instanceof SuperGroup) {
SuperGroup superGroup = (SuperGroup) group;
superGroup.addSubGroup(subGroup);
subGroup.setParent(superGroup);
return new CommandResult(String.format(MESSAGE_SUCCESS, subGroup));
}
throw new ExecuteException(Messages.MESSAGE_GROUPS_NOT_LISTED);
} else {
// TODO: stub error message, this is supposed to be for when index is out of bounds.
throw new ExecuteException("");
}
return new CommandResult(String.format(MESSAGE_SUCCESS, subGroup));
} catch (DuplicateItemException e) {
throw new ExecuteException(MESSAGE_DUPLICATE_GROUP);
}
Expand Down
Loading

0 comments on commit 3638a13

Please sign in to comment.