forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support Commands for Import and Export
- Loading branch information
1 parent
2b14c7c
commit ca97b75
Showing
15 changed files
with
605 additions
and
2 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/weme/logic/commands/exportcommand/ExportClearCommand.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,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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/weme/logic/commands/importcommand/ImportClearCommand.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,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; | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/main/java/seedu/weme/logic/commands/importcommand/ImportDeleteCommand.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,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 | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
src/main/java/seedu/weme/logic/commands/importcommand/ImportEditCommand.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,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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../seedu/weme/logic/parser/commandparser/importcommandparser/ImportDeleteCommandParser.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,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); | ||
} | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...va/seedu/weme/logic/parser/commandparser/importcommandparser/ImportEditCommandParser.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,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)); | ||
} | ||
} |
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
Oops, something went wrong.