Skip to content

Commit

Permalink
Add Support Commands for Import and Export
Browse files Browse the repository at this point in the history
  • Loading branch information
BillChee123 committed Oct 29, 2019
1 parent 2b14c7c commit ca97b75
Show file tree
Hide file tree
Showing 15 changed files with 605 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.weme.logic.commands.exportcommand;

import static java.util.Objects.requireNonNull;

import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.CommandResult;
import seedu.weme.model.Model;

/**
* Clears all the memes in Weme's export staging area.
*/
public class ExportClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Weme has been cleared!";


@Override
public CommandResult execute(Model model) {
requireNonNull(model);

model.clearExportList();
CommandResult result = new CommandResult(MESSAGE_SUCCESS);
model.commitWeme(result.getFeedbackToUser());

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.weme.logic.commands.importcommand;

import static java.util.Objects.requireNonNull;

import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.CommandResult;
import seedu.weme.model.Model;

/**
* Clears all the memes in Weme's import staging area.
*/
public class ImportClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Weme has been cleared!";


@Override
public CommandResult execute(Model model) {
requireNonNull(model);

model.clearImportList();
CommandResult result = new CommandResult(MESSAGE_SUCCESS);
model.commitWeme(result.getFeedbackToUser());

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public CommandResult execute(Model model) throws CommandException {

try {
model.importMemes();
model.clearImportList();
} catch (IOException e) {
throw new CommandException(e.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package seedu.weme.logic.commands.importcommand;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.weme.commons.core.Messages;
import seedu.weme.commons.core.index.Index;
import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.CommandResult;
import seedu.weme.logic.commands.exceptions.CommandException;
import seedu.weme.model.Model;
import seedu.weme.model.meme.Meme;

/**
* Deletes a meme identified using it's displayed index from the import staging area.
*/
public class ImportDeleteCommand extends Command {

public static final String COMMAND_WORD = "delete";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the meme identified by the index number used in the displayed import list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_MEME_SUCCESS = "Deleted Meme: %1$s";

private final Index targetIndex;

public ImportDeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Meme> lastShownList = model.getFilteredImportList();

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

Meme memeToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deleteImportedMeme(memeToDelete);
model.clearMemeStats(memeToDelete);

CommandResult result = new CommandResult(String.format(MESSAGE_DELETE_MEME_SUCCESS, memeToDelete));
model.commitWeme(result.getFeedbackToUser());

return result;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ImportDeleteCommand // instanceof handles nulls
&& targetIndex.equals(((ImportDeleteCommand) other).targetIndex)); // state check
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package seedu.weme.logic.commands.importcommand;

import static java.util.Objects.requireNonNull;
import static seedu.weme.logic.parser.util.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.weme.logic.parser.util.CliSyntax.PREFIX_TAG;
import static seedu.weme.model.Model.PREDICATE_SHOW_ALL_MEMES;

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

import seedu.weme.commons.core.Messages;
import seedu.weme.commons.core.index.Index;
import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.CommandResult;
import seedu.weme.logic.commands.exceptions.CommandException;
import seedu.weme.logic.commands.memecommand.MemeEditCommand;
import seedu.weme.model.Model;
import seedu.weme.model.imagePath.ImagePath;
import seedu.weme.model.meme.Description;
import seedu.weme.model.meme.Meme;
import seedu.weme.model.tag.Tag;

/**
* Edits the details of an imported meme in the import staging area.
*/
public class ImportEditCommand extends Command {

public static final String COMMAND_WORD = "edit";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the meme identified "
+ "by the index number used in the displayed meme list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_DESCRIPTION + "DESCRIPTION] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "d/A funny meme "
+ "t/funny";

public static final String MESSAGE_EDIT_MEME_SUCCESS = "Edited Meme: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_MEME = "This meme already exists in Weme.";

private final Index index;
private final MemeEditCommand.EditMemeDescriptor editMemeDescriptor;

/**
* @param index of the meme in the filtered meme list to edit
* @param editMemeDescriptor details to edit the meme with
*/
public ImportEditCommand(Index index, MemeEditCommand.EditMemeDescriptor editMemeDescriptor) {
requireNonNull(index);
requireNonNull(editMemeDescriptor);

this.index = index;
this.editMemeDescriptor = new MemeEditCommand.EditMemeDescriptor(editMemeDescriptor);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Meme> lastShownList = model.getFilteredImportList();

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

Meme memeToEdit = lastShownList.get(index.getZeroBased());
Meme editedMeme = createEditedMeme(memeToEdit, editMemeDescriptor);

if (!memeToEdit.isSameMeme(editedMeme) && model.hasMeme(editedMeme)) {
throw new CommandException(MESSAGE_DUPLICATE_MEME);
}

model.setImportedMeme(memeToEdit, editedMeme);
model.addMemeToRecord(editedMeme);
CommandResult result = new CommandResult(String.format(MESSAGE_EDIT_MEME_SUCCESS, editedMeme));
model.commitWeme(result.getFeedbackToUser());
model.updateFilteredMemeList(PREDICATE_SHOW_ALL_MEMES);

return result;
}

/**
* Creates and returns a {@code Meme} with the details of {@code memeToEdit}
* edited with {@code editMemeDescriptor}.
*/
private static Meme createEditedMeme(Meme memeToEdit, MemeEditCommand.EditMemeDescriptor editMemeDescriptor) {
assert memeToEdit != null;

ImagePath updatedPath = editMemeDescriptor.getFilePath().orElse(memeToEdit.getImagePath());
Description updatedDescription = editMemeDescriptor.getDescription().orElse(memeToEdit.getDescription());
Set<Tag> updatedTags = editMemeDescriptor.getTags().orElse(memeToEdit.getTags());

return new Meme(updatedPath, updatedDescription, updatedTags);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof ImportEditCommand)) {
return false;
}

// state check
ImportEditCommand e = (ImportEditCommand) other;
return index.equals(e.index)
&& editMemeDescriptor.equals(e.editMemeDescriptor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.weme.logic.parser.commandparser.importcommandparser;

import static seedu.weme.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.weme.commons.core.index.Index;
import seedu.weme.logic.commands.importcommand.ImportDeleteCommand;
import seedu.weme.logic.parser.Parser;
import seedu.weme.logic.parser.exceptions.ParseException;
import seedu.weme.logic.parser.util.ParserUtil;

/**
* Parses input arguments and creates a new MemeDeleteCommand object
*/
public class ImportDeleteCommandParser implements Parser<ImportDeleteCommand> {

/**
* Parses the given {@code String} of arguments in the context of the MemeDeleteCommand
* and returns a MemeDeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ImportDeleteCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new ImportDeleteCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportDeleteCommand.MESSAGE_USAGE), pe);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package seedu.weme.logic.parser.commandparser.importcommandparser;

import static java.util.Objects.requireNonNull;
import static seedu.weme.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.weme.logic.parser.util.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.weme.logic.parser.util.CliSyntax.PREFIX_TAG;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;

import seedu.weme.commons.core.index.Index;
import seedu.weme.logic.commands.importcommand.ImportEditCommand;
import seedu.weme.logic.commands.memecommand.MemeEditCommand;
import seedu.weme.logic.parser.Parser;
import seedu.weme.logic.parser.exceptions.ParseException;
import seedu.weme.logic.parser.util.ArgumentMultimap;
import seedu.weme.logic.parser.util.ArgumentTokenizer;
import seedu.weme.logic.parser.util.ParserUtil;
import seedu.weme.model.tag.Tag;

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

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

Index index;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportEditCommand.MESSAGE_USAGE), pe);
}

MemeEditCommand.EditMemeDescriptor editMemeDescriptor = new MemeEditCommand.EditMemeDescriptor();
if (argMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) {
editMemeDescriptor.setDescription(ParserUtil.parseDescription(
argMultimap.getValue(PREFIX_DESCRIPTION).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editMemeDescriptor::setTags);

if (!editMemeDescriptor.isAnyFieldEdited()) {
throw new ParseException(ImportEditCommand.MESSAGE_NOT_EDITED);
}

return new ImportEditCommand(index, editMemeDescriptor);
}

/**
* 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<Tag>> parseTagsForEdit(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.parseTags(tagSet));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.regex.Matcher;

import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.exportcommand.ExportClearCommand;
import seedu.weme.logic.commands.exportcommand.ExportCommand;
import seedu.weme.logic.commands.exportcommand.UnstageCommand;
import seedu.weme.logic.commands.generalcommand.HelpCommand;
Expand Down Expand Up @@ -41,6 +42,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ExportCommand.COMMAND_WORD:
return new ExportCommandParser().parse(arguments);

case ExportClearCommand.COMMAND_WORD:
return new ExportClearCommand();

default:
return super.parseCommand(userInput);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

import seedu.weme.logic.commands.Command;
import seedu.weme.logic.commands.generalcommand.HelpCommand;
import seedu.weme.logic.commands.importcommand.ImportClearCommand;
import seedu.weme.logic.commands.importcommand.ImportCommand;
import seedu.weme.logic.commands.importcommand.ImportDeleteCommand;
import seedu.weme.logic.commands.importcommand.ImportEditCommand;
import seedu.weme.logic.commands.importcommand.LoadCommand;
import seedu.weme.logic.parser.commandparser.importcommandparser.ImportDeleteCommandParser;
import seedu.weme.logic.parser.commandparser.importcommandparser.ImportEditCommandParser;
import seedu.weme.logic.parser.commandparser.importcommandparser.LoadCommandParser;
import seedu.weme.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -40,6 +45,15 @@ public Command parseCommand(String userInput) throws ParseException {
case ImportCommand.COMMAND_WORD:
return new ImportCommand();

case ImportClearCommand.COMMAND_WORD:
return new ImportClearCommand();

case ImportEditCommand.COMMAND_WORD:
return new ImportEditCommandParser().parse(arguments);

case ImportDeleteCommand.COMMAND_WORD:
return new ImportDeleteCommandParser().parse(arguments);

default:
return super.parseCommand(userInput);
}
Expand Down
Loading

0 comments on commit ca97b75

Please sign in to comment.