forked from nus-cs2103-AY1718S1/addressbook-level4-old
-
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.
Merge pull request #117 from edwinghy/Export_Function_User_Guide
Export function
- Loading branch information
Showing
11 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
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
136 changes: 136 additions & 0 deletions
136
src/main/java/seedu/address/logic/commands/ExportCommand.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,136 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PATH; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_RANGE; | ||
|
||
import java.io.IOException; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.AddressBook; | ||
import seedu.address.model.person.ReadOnlyPerson; | ||
import seedu.address.model.person.exceptions.DuplicatePersonException; | ||
import seedu.address.storage.AddressBookStorage; | ||
import seedu.address.storage.XmlAddressBookStorage; | ||
|
||
/** | ||
* Export selected person/s by the index number or range in the last person listing. | ||
*/ | ||
public class ExportCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "export"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Export selected person/s\n" | ||
+ "by the index number or range in the last person listing\n" | ||
+ "Parameters: " + PREFIX_RANGE + "[RANGE] " | ||
+ PREFIX_PATH + "[PATH]\n" | ||
+ "Example 1: " + COMMAND_WORD + " " + PREFIX_RANGE + "all " + PREFIX_PATH + "C:\\Exports\n" | ||
+ "Example 2: " + COMMAND_WORD + " " + PREFIX_RANGE + "1 " + PREFIX_PATH + "C:\\Exports\n" | ||
+ "Example 3: " + COMMAND_WORD + " " + PREFIX_RANGE + "1,2 " + PREFIX_PATH + "C:\\Exports\n" | ||
+ "Example 4: " + COMMAND_WORD + " " + PREFIX_RANGE + "1-5 " + PREFIX_PATH + "C:\\Exports"; | ||
|
||
public static final String MESSAGE_ARGUMENTS = "Range: %1$s, Path: %2$s"; | ||
|
||
public static final String MESSAGE_EXPORT_FAIL = "Export Failed"; | ||
public static final String MESSAGE_EXPORT_SUCCESS = "Export Successful"; | ||
|
||
private final String range; | ||
private final String path; | ||
private AddressBook exportBook; | ||
|
||
public ExportCommand(String range, String path) { | ||
requireNonNull(range); | ||
requireNonNull(path); | ||
|
||
this.range = range; | ||
this.path = path; | ||
exportBook = new AddressBook(); | ||
} | ||
|
||
@Override | ||
public CommandResult execute() throws CommandException { | ||
|
||
String[] multipleRange = getRangeFromInput(); | ||
|
||
if (multipleRange[0].equals("all")) { | ||
exportAll(); | ||
} else { | ||
for (int i = 0; i < multipleRange.length; i++) { | ||
if (multipleRange[i].contains("-")) { | ||
String[] rangeToExport = multipleRange[i].split("-"); | ||
exportRange(Integer.parseInt(rangeToExport[0]), Integer.parseInt(rangeToExport[1])); | ||
} else { | ||
exportSpecific(Integer.parseInt(multipleRange[i])); | ||
} | ||
} | ||
} | ||
|
||
try { | ||
AddressBookStorage storage = new XmlAddressBookStorage(path + ".xml"); | ||
storage.saveAddressBook(exportBook); | ||
} catch (IOException ioe) { | ||
return new CommandResult(MESSAGE_EXPORT_FAIL); | ||
} | ||
return new CommandResult(MESSAGE_EXPORT_SUCCESS); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (!(other instanceof ExportCommand)) { | ||
return false; | ||
} | ||
|
||
ExportCommand e = (ExportCommand) other; | ||
return range.equals(e.range) && path.equals(e.path); | ||
} | ||
|
||
/** | ||
*Export all contacts from last shown list | ||
*/ | ||
private void exportAll() { | ||
List<ReadOnlyPerson> lastShownList = model.getFilteredPersonList(); | ||
try { | ||
exportBook.setPersons(lastShownList); | ||
} catch (DuplicatePersonException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
*Export a specific contact from last shown list | ||
*/ | ||
private void exportSpecific(int index) { | ||
List<ReadOnlyPerson> lastShownList = model.getFilteredPersonList(); | ||
try { | ||
exportBook.addPerson(lastShownList.get(index - 1)); | ||
} catch (DuplicatePersonException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
*Export a range of contacts from last shown list | ||
*/ | ||
private void exportRange(int start, int end) { | ||
List<ReadOnlyPerson> lastShownList = model.getFilteredPersonList(); | ||
try { | ||
for (int i = start - 1; i <= end - 1; i++) { | ||
exportBook.addPerson(lastShownList.get(i)); | ||
} | ||
} catch (DuplicatePersonException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private String[] getRangeFromInput() { | ||
String[] splitStringComma = this.range.split(","); | ||
|
||
return splitStringComma; | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/seedu/address/logic/parser/ExportCommandParser.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,26 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PATH; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_RANGE; | ||
|
||
import seedu.address.logic.commands.ExportCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new ExportCommand object | ||
*/ | ||
public class ExportCommandParser implements Parser<ExportCommand> { | ||
@Override | ||
public ExportCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultiMap = ArgumentTokenizer.tokenize(args, PREFIX_RANGE, PREFIX_PATH); | ||
|
||
String range = argMultiMap.getValue(PREFIX_RANGE).orElse(""); | ||
|
||
String path = argMultiMap.getValue(PREFIX_PATH).orElse(""); | ||
|
||
return new ExportCommand(range, path); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/seedu/address/logic/commands/ExportCommandTest.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,54 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import static seedu.address.testutil.TypicalPath.PATH_CONTACT; | ||
import static seedu.address.testutil.TypicalPath.PATH_EXPORT; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
import static seedu.address.testutil.TypicalRange.RANGE_1; | ||
import static seedu.address.testutil.TypicalRange.RANGE_ALL; | ||
|
||
import org.junit.Test; | ||
|
||
import seedu.address.logic.CommandHistory; | ||
import seedu.address.logic.UndoRedoStack; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
|
||
public class ExportCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void equals() { | ||
final ExportCommand standardCommand = new ExportCommand(RANGE_ALL, PATH_EXPORT); | ||
|
||
// same values -> returns true | ||
ExportCommand commandWithSameValues = new ExportCommand(RANGE_ALL, PATH_EXPORT); | ||
assertTrue(standardCommand.equals(commandWithSameValues)); | ||
|
||
// same object -> returns true | ||
assertTrue(standardCommand.equals(standardCommand)); | ||
|
||
// null -> returns false | ||
assertFalse(standardCommand.equals(null)); | ||
|
||
// different types -> returns false | ||
assertFalse(standardCommand.equals(new ClearCommand())); | ||
|
||
// different range -> returns false | ||
assertFalse(standardCommand.equals(new ExportCommand(RANGE_1, PATH_EXPORT))); | ||
|
||
// different remarks -> returns false | ||
assertFalse(standardCommand.equals(new ExportCommand(RANGE_ALL, PATH_CONTACT))); | ||
} | ||
|
||
private ExportCommand prepareCommand(String range, String path) { | ||
ExportCommand exportCommand = new ExportCommand(range, path); | ||
exportCommand.setData(model, new CommandHistory(), new UndoRedoStack()); | ||
return exportCommand; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/test/java/seedu/address/logic/parser/ExportCommandParserTest.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,24 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PATH; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_RANGE; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
|
||
import static seedu.address.testutil.TypicalPath.PATH_EXPORT; | ||
import static seedu.address.testutil.TypicalRange.RANGE_ALL; | ||
|
||
import org.junit.Test; | ||
|
||
import seedu.address.logic.commands.ExportCommand; | ||
|
||
public class ExportCommandParserTest { | ||
private ExportCommandParser parser = new ExportCommandParser(); | ||
|
||
@Test | ||
public void parse_indexSpecified_failure() throws Exception { | ||
|
||
String userInput = " " + PREFIX_RANGE + RANGE_ALL + " " + PREFIX_PATH + PATH_EXPORT; | ||
ExportCommand expectedCommand = new ExportCommand(RANGE_ALL, PATH_EXPORT); | ||
assertParseSuccess(parser, userInput, expectedCommand); | ||
} | ||
} |
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,9 @@ | ||
package seedu.address.testutil; | ||
|
||
/** | ||
* A utility class containing a list of {@code Index} objects to be used in tests. | ||
*/ | ||
public class TypicalPath { | ||
public static final String PATH_EXPORT = "C:\\Exports\\test"; | ||
public static final String PATH_CONTACT = "C:\\Contacts\\test"; | ||
} |
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,11 @@ | ||
package seedu.address.testutil; | ||
|
||
/** | ||
* A utility class containing a list of {@code Index} objects to be used in tests. | ||
*/ | ||
public class TypicalRange { | ||
public static final String RANGE_ALL = "all"; | ||
public static final String RANGE_1 = "1"; | ||
public static final String RANGE_1_AND_3 = "1,3"; | ||
public static final String RANGE_1_TO_3 = "1-3"; | ||
} |