Skip to content

Commit

Permalink
Merge pull request #117 from edwinghy/Export_Function_User_Guide
Browse files Browse the repository at this point in the history
Export function
  • Loading branch information
tshradheya authored Oct 21, 2017
2 parents 52610cc + d0ebb4e commit 0e1fb5e
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ Format: `list`
Sorts and shows a list of all persons in the address book alphabetically. +
Format: `sort`

=== Exporting selected contacts : `export`

Exports selected contacts in iContacts. +
Format: `export [r/RANGE] [p/PATH]`

****
* Exports the person/s at the specified `RANGE` to a specified `PATH`.
* The range refers to any index number shown in the most recent listing.
* The range *must be a positive integer and must not be larger than the last index of the list* 1, 2, 3, 4-7, ...
* The path *must include the file name without the file extension* c:\exports\classmates
****

Examples:

* `list` +
`export r/all p/c:\exports\classmates` +
Exports all the contacts to the file *classmates.xml* in path *c:\exports*.

`export r/1-4 p/c:\exports\classmates` +
Exports the contacts from index 1 to index 4 to the file *classmates.xml* in path *c:\exports*.

`export r/1-4,6,8 p/c:\exports\classmates` +
Exports the contacts at index 1 to index 4 with index 6 and index 8 to the file *classmates.xml* in path *c:\exports*.

=== Editing a person : `edit`

Edits an existing person in the address book. +
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.io.IOException;

import seedu.address.commons.core.Messages;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.UndoRedoStack;
import seedu.address.logic.commands.exceptions.CommandException;

import seedu.address.model.Model;


/**
* Represents a command with hidden internal logic and the ability to be executed.
*/
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/seedu/address/logic/commands/ExportCommand.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.logic.commands.DisplayPictureCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.ExportCommand;
import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
Expand Down Expand Up @@ -113,6 +114,9 @@ public Command parseCommand(String userInput) throws ParseException {
case NicknameCommand.COMMAND_WORD:
return new NicknameCommandParser().parse(arguments);

case ExportCommand.COMMAND_WORD:
return new ExportCommandParser().parse(arguments);

case DisplayPictureCommand.COMMAND_WORD:
return new DisplayPictureCommandParser().parse(arguments);

Expand All @@ -123,5 +127,4 @@ public Command parseCommand(String userInput) throws ParseException {
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_BIRTHDAY = new Prefix("b/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_RANGE = new Prefix("r/");
public static final Prefix PREFIX_PATH = new Prefix("p/");
public static final Prefix PREFIX_DISPLAYPICTURE = new Prefix("dp/");
public static final Prefix PREFIX_NICKNAME = new Prefix("nk/");

}
26 changes: 26 additions & 0 deletions src/main/java/seedu/address/logic/parser/ExportCommandParser.java
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 src/test/java/seedu/address/logic/commands/ExportCommandTest.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import static seedu.address.logic.commands.CommandTestUtil.NICKNAME_DESC_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_NICKNAME_AMY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PATH;
import static seedu.address.logic.parser.CliSyntax.PREFIX_RANGE;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalPath.PATH_EXPORT;
import static seedu.address.testutil.TypicalRange.RANGE_ALL;

import java.util.Arrays;
import java.util.List;
Expand All @@ -25,6 +29,7 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.ExportCommand;
import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
Expand Down Expand Up @@ -290,4 +295,11 @@ public void parseCommand_unknownCommand_throwsParseException() throws Exception
thrown.expectMessage(MESSAGE_UNKNOWN_COMMAND);
parser.parseCommand("unknownCommand");
}

@Test
public void parseCommand_export() throws Exception {
ExportCommand command = (ExportCommand) parser.parseCommand(ExportCommand.COMMAND_WORD + " "
+ PREFIX_RANGE + RANGE_ALL + " " + PREFIX_PATH + PATH_EXPORT);
assertEquals(new ExportCommand(RANGE_ALL, PATH_EXPORT), command);
}
}
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);
}
}
9 changes: 9 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalPath.java
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";
}
11 changes: 11 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalRange.java
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";
}

0 comments on commit 0e1fb5e

Please sign in to comment.