From 88ebdf16b218664d01cd58dc9c1f1bae20e25299 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 21 Mar 2019 21:22:42 +0800 Subject: [PATCH 01/59] change parser implementation of export command --- .../address/logic/commands/ExportCommand.java | 18 +++++++------ .../logic/parser/ExportCommandParser.java | 25 +++++++++++++++++++ .../address/logic/parser/ParserUtil.java | 24 ++++++++++++++---- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ExportCommand.java b/src/main/java/seedu/address/logic/commands/ExportCommand.java index 0befd3949edb..4efa5fccb178 100644 --- a/src/main/java/seedu/address/logic/commands/ExportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExportCommand.java @@ -4,6 +4,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_FOLDERNAME; import java.io.IOException; +import java.util.List; import java.util.Set; import seedu.address.logic.CommandHistory; @@ -14,19 +15,17 @@ import seedu.address.storage.csvmanager.CsvFile; /** - * Exports single or multiple card folders into a .json file. Users must specify file name to export card folders to. + * Exports single or multiple card folders into a .csv file. */ public class ExportCommand extends Command { public static final String COMMAND_WORD = "export"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": exports single or multiple card folders into" - + "a .csv file. " - + "Users must include the .csv extension.\n" - + "Parameters: " - + PREFIX_FOLDERNAME + "CARD_FOLDER_NAME [MORE CARD_FOLDER_NAMES]..." - + PREFIX_FILENAME + "Filename.csv\n" - + "Example: " + COMMAND_WORD + "f/Human_anatomy f/Bone_structure n/myfilename.csv"; + + "their respective .csv files." + + "Parameters: INDEX (Index specifies the card folder index to export) \n" + + "Can specify more than one card folder index to export" + + "Example: " + COMMAND_WORD + "1 3 5 7"; public static final String MESSAGE_SUCCESS = "Successfully exported card folders to: $1%s"; @@ -36,12 +35,17 @@ public class ExportCommand extends Command { private Set cardFolders; private CsvFile filename; + private List cardFolderIndexes; public ExportCommand(Set cardFolders, CsvFile filename) { this.cardFolders = cardFolders; this.filename = filename; } + public ExportCommand(List cardFolderIndexes) { + this.cardFolderIndexes = cardFolderIndexes; + } + @Override public CommandResult execute(Model model, CommandHistory history) throws CommandException { // check whether model contains the card folders desired. Catch exception thrown diff --git a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java index 927776afe84a..ffb741cf3d9a 100644 --- a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java @@ -4,6 +4,8 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_FILENAME; import static seedu.address.logic.parser.CliSyntax.PREFIX_FOLDERNAME; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import seedu.address.logic.commands.ExportCommand; @@ -18,6 +20,7 @@ public class ExportCommandParser implements Parser { @Override public ExportCommand parse(String userInput) throws ParseException { + /* ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize( userInput, PREFIX_FILENAME, PREFIX_FOLDERNAME); if (!ParserUtil.arePrefixesPresent(argMultimap, PREFIX_FOLDERNAME, PREFIX_FILENAME) @@ -26,6 +29,28 @@ public ExportCommand parse(String userInput) throws ParseException { } Set folderNames = ParserUtil.parseFolders(argMultimap.getAllValues(PREFIX_FOLDERNAME)); CsvFile filename = ParserUtil.parseFileName(argMultimap.getValue(PREFIX_FILENAME).get()); + */ + + // check if array elements contain all numbers + try { + List folderIndexes = ParserUtil.parseFolderIndex(userInput); + return new ExportCommand() + } catch (ParseException e) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE), e); + } + + String[] array = userInput.split(" "); + + + + List indexList = new ArrayList<>(); + + for (String element : array) { + indexList.add(Integer.parseInt(element)); + } + + + return new ExportCommand(folderNames, filename); } } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 8b1e7ec4322a..19e7d15683d4 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -2,15 +2,13 @@ import static java.util.Objects.requireNonNull; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; import java.util.stream.Stream; import seedu.address.commons.core.index.Index; import seedu.address.commons.util.StringUtil; +import seedu.address.logic.commands.ExportCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.card.Answer; import seedu.address.model.card.Question; @@ -24,6 +22,7 @@ public class ParserUtil { public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer."; + public static final String MESSAGE_NOT_AN_INTEGER = "Cannot parse string to integer"; /** * Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be @@ -110,6 +109,21 @@ public static Set parseFolders(Collection folderNames) return cardFolderExports; } + public static Integer stringToInt(String element) throws NumberFormatException { + return Integer.parseInt(element); + } + + + public static List parseFolderIndex(String folderIndexes) throws ParseException { + List indexList = new ArrayList<>(); + folderIndexes = folderIndexes.trim(); + try { + return Arrays.stream(folderIndexes.split(" ")).map(ParserUtil::stringToInt).collect(Collectors.toList()); + } catch (NumberFormatException exception) { + throw new ParseException(MESSAGE_NOT_AN_INTEGER); + } + } + /** * Parses a {@Code String folderName} into a {@code CardFolderExport}. * Trims any leading and trailing white space for folder name. From 0c1f9b90bf510057fe6bdfc2983199f479e53f1b Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 21 Mar 2019 21:26:29 +0800 Subject: [PATCH 02/59] reimplement export card folder for model --- .../address/logic/parser/ExportCommandParser.java | 13 ++----------- src/main/java/seedu/address/model/Model.java | 2 ++ src/main/java/seedu/address/model/ModelManager.java | 5 +++++ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java index ffb741cf3d9a..bcbb46c5a2ee 100644 --- a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java @@ -34,23 +34,14 @@ public ExportCommand parse(String userInput) throws ParseException { // check if array elements contain all numbers try { List folderIndexes = ParserUtil.parseFolderIndex(userInput); - return new ExportCommand() + return new ExportCommand(folderIndexes); } catch (ParseException e) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE), e); } - String[] array = userInput.split(" "); - List indexList = new ArrayList<>(); - - for (String element : array) { - indexList.add(Integer.parseInt(element)); - } - - - - return new ExportCommand(folderNames, filename); + // return new ExportCommand(folderNames, filename); } } diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 80fcb304deba..625829db4e24 100755 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -229,6 +229,8 @@ public interface Model extends Observable { void exportCardFolders(Set cardFolderExports, CsvFile csvFile) throws IOException; + void exportCardFolders(List cardFolderExports) throws IOException; + void importCardFolders(CsvFile csvFile) throws IOException; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 9bc2043e67b9..eb8485375c3c 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -437,6 +437,11 @@ public void exportCardFolders(Set cardFolderExports, CsvFile c csvManager.writeFoldersToCsv(cardFolderObject, csvFile); } + @Override + public void exportCardFolders(List cardFolderExports) throws IOException { + + } + @Override public void importCardFolders(CsvFile csvFile) throws IOException { From b280cdd42dba51100c8e2098bf4e7d9c0e9262a1 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 21 Mar 2019 21:39:14 +0800 Subject: [PATCH 03/59] change return card folder value --- .../model/CardFolderNotFoundException.java | 5 +++++ .../java/seedu/address/model/ModelManager.java | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/CardFolderNotFoundException.java b/src/main/java/seedu/address/model/CardFolderNotFoundException.java index c345698e2e47..05c3ef33eb00 100644 --- a/src/main/java/seedu/address/model/CardFolderNotFoundException.java +++ b/src/main/java/seedu/address/model/CardFolderNotFoundException.java @@ -8,4 +8,9 @@ public class CardFolderNotFoundException extends RuntimeException { public CardFolderNotFoundException(String message) { super(message); } + + public CardFolderNotFoundException() { + super(); + } + } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index eb8485375c3c..252d6d859120 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -13,6 +13,7 @@ import java.util.Set; import java.util.function.Predicate; import java.util.logging.Logger; +import java.util.stream.Collectors; import javafx.beans.InvalidationListener; import javafx.beans.property.ReadOnlyProperty; @@ -23,6 +24,7 @@ import javafx.collections.transformation.FilteredList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; +import seedu.address.commons.core.index.Index; import seedu.address.commons.util.InvalidationListenerManager; import seedu.address.model.card.Answer; import seedu.address.model.card.Card; @@ -429,6 +431,20 @@ public boolean equals(Object obj) { && Objects.equals(selectedCard.get(), other.selectedCard.get()); } + private List returnValidCardFolders(List cardFolderExports) { + List readOnlyCardFolders = new ArrayList<>(); + List indexList = cardFolderExports.stream().map(Index::fromOneBased).collect(Collectors.toList()); + for (Index index : indexList) { + try { + ReadOnlyCardFolder cardFolder = filteredFoldersList.get(index.getZeroBased()); + readOnlyCardFolders.add(cardFolder); + } catch (IndexOutOfBoundsException e) { + throw new CardFolderNotFoundException(); + } + } + return readOnlyCardFolders; + } + //=========== Export / Import card folders ======================================================================== @Override @@ -439,7 +455,7 @@ public void exportCardFolders(Set cardFolderExports, CsvFile c @Override public void exportCardFolders(List cardFolderExports) throws IOException { - + List cardFolders = returnValidCardFolders(cardFolderExports); } @Override From b1c2f85721283ce0c152be200a1c0bebb1192cb7 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 21 Mar 2019 21:50:59 +0800 Subject: [PATCH 04/59] change write to csv implementation --- .../address/logic/commands/ExportCommand.java | 4 ++-- .../seedu/address/model/ModelManager.java | 1 + .../storage/csvmanager/CsvCommands.java | 2 ++ .../storage/csvmanager/CsvManager.java | 23 +++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ExportCommand.java b/src/main/java/seedu/address/logic/commands/ExportCommand.java index 4efa5fccb178..f2f1b027b8d4 100644 --- a/src/main/java/seedu/address/logic/commands/ExportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExportCommand.java @@ -29,7 +29,7 @@ public class ExportCommand extends Command { public static final String MESSAGE_SUCCESS = "Successfully exported card folders to: $1%s"; - public static final String MESSAGE_MISSING_CARD_FOLDERS = "Could not find the specified folder: "; + public static final String MESSAGE_MISSING_CARD_FOLDERS = "Could not find the specified folder indexes: "; public static final String MESSAGE_FILE_OPS_FAILURE = "Could not export to specified file"; @@ -50,7 +50,7 @@ public ExportCommand(List cardFolderIndexes) { public CommandResult execute(Model model, CommandHistory history) throws CommandException { // check whether model contains the card folders desired. Catch exception thrown try { - model.exportCardFolders(cardFolders, filename); + model.exportCardFolders(cardFolderIndexes); } catch (CardFolderNotFoundException e) { throw new CommandException(MESSAGE_MISSING_CARD_FOLDERS + e.getMessage()); } catch (IOException e) { diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 252d6d859120..9670dd7458cb 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -456,6 +456,7 @@ public void exportCardFolders(Set cardFolderExports, CsvFile c @Override public void exportCardFolders(List cardFolderExports) throws IOException { List cardFolders = returnValidCardFolders(cardFolderExports); + csvManager.writeFoldersToCsv(cardFolders); } @Override diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java index 750d7863a4a9..1d179812e171 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java @@ -15,4 +15,6 @@ interface CsvCommands { public void readFoldersToCsv(CsvFile csvFile); + public void writeFoldersToCsv(List cardFolders) throws IOException; + } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 5abeff8c7c28..ac6b5935696c 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -49,10 +49,33 @@ public void readFoldersToCsv(CsvFile csvFile) { } + @Override + public void writeFoldersToCsv(List cardFolders) throws IOException { + String filepath = getDefaultFilePath(); + for (ReadOnlyCardFolder readOnlyCardFolder : cardFolders) { + List cardList = readOnlyCardFolder.getCardList(); + String foldername = readOnlyCardFolder.getFolderName(); + FileWriter fileWriter = new FileWriter(filepath + "/" + foldername + ".csv"); + fileWriter.append(CARD_HEADERS + NEW_LINE_SEPARATOR); + for (Card card : cardList) { + String cardRepresentation = getCardString(card); + fileWriter.append(cardRepresentation); + fileWriter.append(NEW_LINE_SEPARATOR); + } + fileWriter.append(NEW_LINE_SEPARATOR); + fileWriter.flush(); + fileWriter.close(); + } + } + public static String getFilePathAsString(CsvFile csvFile) throws IOException { return new File("./" + csvFile.filename).getCanonicalPath(); } + public static String getDefaultFilePath() throws IOException { + return new File("./").getCanonicalPath(); + } + private String getCardString(Card card) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(card.getQuestion() + COMMA_DELIMITTER) From aee056ff62bbaf7760deb6252e7f8089086ad99a Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 21 Mar 2019 22:47:02 +0800 Subject: [PATCH 05/59] change parser exception handling method --- .../java/seedu/address/commons/core/Messages.java | 1 + .../seedu/address/commons/core/index/Index.java | 5 +++++ .../address/logic/commands/ExportCommand.java | 11 ++++++++--- .../address/logic/parser/ExportCommandParser.java | 10 ++++++---- .../seedu/address/logic/parser/ParserUtil.java | 14 +++++++++----- .../java/seedu/address/model/ModelManager.java | 2 +- .../address/storage/csvmanager/CsvManager.java | 12 ++++++++---- 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 73f450e4842d..6dceb465a710 100755 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -16,4 +16,5 @@ public class Messages { + " session"; public static final String MESSAGE_INVALID_ANSWER_COMMAND = "Answer command is valid only when a question is " + "displayed"; + public static final String MESSAGE_NO_NEGATIVE_INDEX = "Negative index not allowed !"; } diff --git a/src/main/java/seedu/address/commons/core/index/Index.java b/src/main/java/seedu/address/commons/core/index/Index.java index 19536439c099..d9ab470a2177 100644 --- a/src/main/java/seedu/address/commons/core/index/Index.java +++ b/src/main/java/seedu/address/commons/core/index/Index.java @@ -51,4 +51,9 @@ public boolean equals(Object other) { || (other instanceof Index // instanceof handles nulls && zeroBasedIndex == ((Index) other).zeroBasedIndex); // state check } + + + public String displayIndex() { + return Integer.toString(getOneBased()); + } } diff --git a/src/main/java/seedu/address/logic/commands/ExportCommand.java b/src/main/java/seedu/address/logic/commands/ExportCommand.java index f2f1b027b8d4..185db539d5aa 100644 --- a/src/main/java/seedu/address/logic/commands/ExportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExportCommand.java @@ -29,18 +29,21 @@ public class ExportCommand extends Command { public static final String MESSAGE_SUCCESS = "Successfully exported card folders to: $1%s"; - public static final String MESSAGE_MISSING_CARD_FOLDERS = "Could not find the specified folder indexes: "; + public static final String MESSAGE_MISSING_CARD_FOLDERS = "Could not find the specified folder index: "; public static final String MESSAGE_FILE_OPS_FAILURE = "Could not export to specified file"; + private List cardFolderIndexes; + /* private Set cardFolders; private CsvFile filename; - private List cardFolderIndexes; + public ExportCommand(Set cardFolders, CsvFile filename) { this.cardFolders = cardFolders; this.filename = filename; } + */ public ExportCommand(List cardFolderIndexes) { this.cardFolderIndexes = cardFolderIndexes; @@ -56,9 +59,10 @@ public CommandResult execute(Model model, CommandHistory history) throws Command } catch (IOException e) { throw new CommandException(MESSAGE_FILE_OPS_FAILURE); } - return new CommandResult(String.format(MESSAGE_SUCCESS, filename)); + return new CommandResult(String.format(MESSAGE_SUCCESS)); } + /* @Override public boolean equals(Object other) { return other == this // short circuit if same object @@ -66,4 +70,5 @@ public boolean equals(Object other) { && (cardFolders.containsAll(((ExportCommand) other).cardFolders) && filename.equals(((ExportCommand) other).filename)); } + */ } diff --git a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java index bcbb46c5a2ee..df4d025b6ebd 100644 --- a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java @@ -1,17 +1,17 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.address.logic.parser.CliSyntax.PREFIX_FILENAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_FOLDERNAME; +import static seedu.address.commons.core.Messages.MESSAGE_NO_NEGATIVE_INDEX; + import java.util.ArrayList; import java.util.List; import java.util.Set; +import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.logic.commands.ExportCommand; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.storage.csvmanager.CardFolderExport; -import seedu.address.storage.csvmanager.CsvFile; + /** * Parses input for export command arguments and creates a new export command object @@ -37,6 +37,8 @@ public ExportCommand parse(String userInput) throws ParseException { return new ExportCommand(folderIndexes); } catch (ParseException e) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE), e); + } catch (IllegalValueException e) { + throw new ParseException(MESSAGE_NO_NEGATIVE_INDEX); } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 19e7d15683d4..9a75922a1097 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -7,6 +7,7 @@ import java.util.stream.Stream; import seedu.address.commons.core.index.Index; +import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.commons.util.StringUtil; import seedu.address.logic.commands.ExportCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -23,6 +24,7 @@ public class ParserUtil { public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer."; public static final String MESSAGE_NOT_AN_INTEGER = "Cannot parse string to integer"; + public static final String MESSAGE_INDEX_LESS_THAN_ZERO = "Index is less than zero"; /** * Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be @@ -114,14 +116,16 @@ public static Integer stringToInt(String element) throws NumberFormatException { } - public static List parseFolderIndex(String folderIndexes) throws ParseException { + public static List parseFolderIndex(String folderIndexes) throws NumberFormatException, IllegalValueException { List indexList = new ArrayList<>(); folderIndexes = folderIndexes.trim(); - try { - return Arrays.stream(folderIndexes.split(" ")).map(ParserUtil::stringToInt).collect(Collectors.toList()); - } catch (NumberFormatException exception) { - throw new ParseException(MESSAGE_NOT_AN_INTEGER); + indexList = Arrays.stream(folderIndexes.split(" ")).map(ParserUtil::stringToInt).collect(Collectors.toList()); + + List InvalidIndexList = indexList.stream().filter(i -> i < 0).collect(Collectors.toList()); + if (InvalidIndexList.size() > 0) { + throw new IllegalValueException(MESSAGE_INDEX_LESS_THAN_ZERO); } + return indexList; } /** diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 9670dd7458cb..92b8e3fd6250 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -439,7 +439,7 @@ private List returnValidCardFolders(List cardFolder ReadOnlyCardFolder cardFolder = filteredFoldersList.get(index.getZeroBased()); readOnlyCardFolders.add(cardFolder); } catch (IndexOutOfBoundsException e) { - throw new CardFolderNotFoundException(); + throw new CardFolderNotFoundException(index.displayIndex()); } } return readOnlyCardFolders; diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index ac6b5935696c..8178db29b2e6 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -18,10 +18,7 @@ public class CsvManager implements CsvCommands { private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; - - /** - * Writes card folders as csv file. - */ + /* @Override public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException { String filepath = getFilePathAsString(filename); @@ -43,6 +40,13 @@ public void writeFoldersToCsv(List cardFolders, CsvFile file fileWriter.flush(); fileWriter.close(); } + */ + + + @Override + public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException { + + } @Override public void readFoldersToCsv(CsvFile csvFile) { From 6f3c5d84a3f587ccb56154870de70a4293d0a1a0 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 14:09:38 +0800 Subject: [PATCH 06/59] change parser command test --- .../address/logic/commands/ExportCommand.java | 8 ++-- .../logic/parser/ExportCommandParser.java | 2 +- .../seedu/address/model/ModelManager.java | 2 +- .../logic/commands/AddCommandTest.java | 5 +++ .../logic/parser/ExportCommandParserTest.java | 37 +++++++------------ 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ExportCommand.java b/src/main/java/seedu/address/logic/commands/ExportCommand.java index 185db539d5aa..f6d57538299c 100644 --- a/src/main/java/seedu/address/logic/commands/ExportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExportCommand.java @@ -59,16 +59,14 @@ public CommandResult execute(Model model, CommandHistory history) throws Command } catch (IOException e) { throw new CommandException(MESSAGE_FILE_OPS_FAILURE); } - return new CommandResult(String.format(MESSAGE_SUCCESS)); + return new CommandResult(MESSAGE_SUCCESS); } - /* + @Override public boolean equals(Object other) { return other == this // short circuit if same object || other instanceof ExportCommand // instanceof handles nulls - && (cardFolders.containsAll(((ExportCommand) other).cardFolders) - && filename.equals(((ExportCommand) other).filename)); + && cardFolderIndexes.containsAll(((ExportCommand) other).cardFolderIndexes); } - */ } diff --git a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java index f1cf19dc538f..beeee9a0dc4f 100644 --- a/src/main/java/seedu/address/logic/parser/ExportCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ExportCommandParser.java @@ -35,7 +35,7 @@ public ExportCommand parse(String userInput) throws ParseException { try { List folderIndexes = ParserUtil.parseFolderIndex(userInput); return new ExportCommand(folderIndexes); - } catch (ParseException e) { + } catch (NumberFormatException e) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE), e); } catch (IllegalValueException e) { throw new ParseException(MESSAGE_NO_NEGATIVE_INDEX); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 67a36bf4dc3c..5c7636786eae 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -441,7 +441,7 @@ private List returnValidCardFolders(List cardFolder List indexList = cardFolderExports.stream().map(Index::fromOneBased).collect(Collectors.toList()); for (Index index : indexList) { try { - ReadOnlyCardFolder cardFolder = filteredFoldersList.get(index.getZeroBased()); + ReadOnlyCardFolder cardFolder = filteredFolders.get(index.getZeroBased()); readOnlyCardFolders.add(cardFolder); } catch (IndexOutOfBoundsException e) { throw new CardFolderNotFoundException(index.displayIndex()); diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 2bed0dc34102..e3ae8efdb471 100755 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -201,6 +201,11 @@ public void exportCardFolders(Set cardFolderExports, CsvFile c throw new AssertionError("This method should not be called."); } + @Override + public void exportCardFolders(List cardFolderExports) throws IOException { + throw new AssertionError("This method should not be called."); + } + @Override public void importCardFolders(CsvFile csvFile) throws IOException { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java index cacc022873cf..bc36ae5ce7a4 100644 --- a/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java @@ -14,6 +14,8 @@ import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -31,18 +33,17 @@ public class ExportCommandParserTest { private ExportCommandParser parser = new ExportCommandParser(); @Test - public void parse_allFields_present() { - ExportCommand exportCommandWithOneFolder = buildExportCommand(VALID_FILENAME, VALID_FOLDER_NAME_1); - ExportCommand exportCommandWithMultipleFolder = buildExportCommand(VALID_FILENAME, - VALID_FOLDER_NAME_1, VALID_FOLDER_NAME_2); - - // whitespace preamble - assertParseSuccess(parser, PREAMBLE_WHITESPACE + FOLDER_DESC_SAMPLE_1 - + FILENAME_DESC_SAMPLE, exportCommandWithOneFolder); - - // multiple folders - assertParseSuccess(parser, FOLDER_DESC_SAMPLE_1 + FOLDER_DESC_SAMPLE_2 - + FILENAME_DESC_SAMPLE, exportCommandWithMultipleFolder); + public void parse_validArgs_returnsExportCommand() { + assertParseSuccess(parser, "1", new ExportCommand(new ArrayList(Arrays.asList(1)))); + + assertParseSuccess(parser, "1 3 5", new ExportCommand(new ArrayList(Arrays.asList(1,3,5)))); + } + + @Test + public void parse_invalidArgs_throwsParseException() { + assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, "1 a 3", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE)); } @Test @@ -77,16 +78,4 @@ public void parse_invalidValue_failure() { CsvFile.MESSAGE_CONSTRAINTS); } - - /** - * - * helper method to build export command for testing - */ - private ExportCommand buildExportCommand(String validFileName, String... validFolderDescriptions) { - Set cardFolderExports = Stream.of(validFolderDescriptions) - .map(CardFolderExport::new) - .collect(Collectors.toSet()); - CsvFile csvFile = new CsvFile(validFileName); - return new ExportCommand(cardFolderExports, csvFile); - } } From f9c389fa2b379e9367702a4a90580a91eb0ac054 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 22:44:28 +0800 Subject: [PATCH 07/59] delete old export implementation --- .../address/logic/commands/ExportCommand.java | 20 ++------ .../address/logic/commands/ImportCommand.java | 2 +- src/main/java/seedu/address/model/Model.java | 7 --- .../seedu/address/model/ModelManager.java | 51 ++++--------------- .../logic/parser/ExportCommandParserTest.java | 49 ------------------ 5 files changed, 14 insertions(+), 115 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ExportCommand.java b/src/main/java/seedu/address/logic/commands/ExportCommand.java index f6d57538299c..d0abd7fef4e6 100644 --- a/src/main/java/seedu/address/logic/commands/ExportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExportCommand.java @@ -1,18 +1,14 @@ package seedu.address.logic.commands; -import static seedu.address.logic.parser.CliSyntax.PREFIX_FILENAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_FOLDERNAME; - import java.io.IOException; import java.util.List; -import java.util.Set; + import seedu.address.logic.CommandHistory; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.CardFolderNotFoundException; import seedu.address.model.Model; -import seedu.address.storage.csvmanager.CardFolderExport; -import seedu.address.storage.csvmanager.CsvFile; + /** * Exports single or multiple card folders into a .csv file. @@ -27,23 +23,13 @@ public class ExportCommand extends Command { + "Can specify more than one card folder index to export" + "Example: " + COMMAND_WORD + "1 3 5 7"; - public static final String MESSAGE_SUCCESS = "Successfully exported card folders to: $1%s"; + public static final String MESSAGE_SUCCESS = "Successfully exported card folders"; public static final String MESSAGE_MISSING_CARD_FOLDERS = "Could not find the specified folder index: "; public static final String MESSAGE_FILE_OPS_FAILURE = "Could not export to specified file"; private List cardFolderIndexes; - /* - private Set cardFolders; - private CsvFile filename; - - - public ExportCommand(Set cardFolders, CsvFile filename) { - this.cardFolders = cardFolders; - this.filename = filename; - } - */ public ExportCommand(List cardFolderIndexes) { this.cardFolderIndexes = cardFolderIndexes; diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index 93e47eccc0b2..4fc91b5161f8 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -19,7 +19,7 @@ public class ImportCommand extends Command { + "card folders information.\n" + "File imported must have a .csv extension.\n" + "Default file path if not specified will be in the root folder of this application\n" - + "Parameters: JSON_FILE_NAME\n" + + "Parameters: CSV_FILE_NAME\n" + "Example: " + COMMAND_WORD + "alice.csv"; public static final String MESSAGE_FILE_OPS_FAILURE = "Could not import from specified file. Check that it exists " + "in root directory"; diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 3ae5b9b43954..8f8673a1be02 100755 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -188,11 +188,6 @@ public interface Model extends Observable { */ void setSelectedCard(Card card); - /** - * Checks whether list of card folder names specified is found inside model - */ - List returnValidCardFolders(Set cardFolders); - /** * Enters a test session using the specified card folder index. */ @@ -237,8 +232,6 @@ public interface Model extends Observable { */ boolean checkIfCardAlreadyAnswered(); - void exportCardFolders(Set cardFolderExports, CsvFile csvFile) throws IOException; - void exportCardFolders(List cardFolderExports) throws IOException; void importCardFolders(CsvFile csvFile) throws IOException; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 226cd2a07531..4da854a16b76 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -29,7 +29,6 @@ import seedu.address.model.card.Answer; import seedu.address.model.card.Card; import seedu.address.model.card.exceptions.CardNotFoundException; -import seedu.address.storage.csvmanager.CardFolderExport; import seedu.address.storage.csvmanager.CsvFile; import seedu.address.storage.csvmanager.CsvManager; @@ -445,28 +444,9 @@ public boolean equals(Object obj) { && Objects.equals(selectedCard.get(), other.selectedCard.get()); } - private List returnValidCardFolders(List cardFolderExports) { - List readOnlyCardFolders = new ArrayList<>(); - List indexList = cardFolderExports.stream().map(Index::fromOneBased).collect(Collectors.toList()); - for (Index index : indexList) { - try { - ReadOnlyCardFolder cardFolder = filteredFolders.get(index.getZeroBased()); - readOnlyCardFolders.add(cardFolder); - } catch (IndexOutOfBoundsException e) { - throw new CardFolderNotFoundException(index.displayIndex()); - } - } - return readOnlyCardFolders; - } //=========== Export / Import card folders ======================================================================== - @Override - public void exportCardFolders(Set cardFolderExports, CsvFile csvFile) throws IOException { - List cardFolderObject = returnValidCardFolders(cardFolderExports); - csvManager.writeFoldersToCsv(cardFolderObject, csvFile); - } - @Override public void exportCardFolders(List cardFolderExports) throws IOException { List cardFolders = returnValidCardFolders(cardFolderExports); @@ -475,31 +455,20 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio @Override public void importCardFolders(CsvFile csvFile) throws IOException { - } - @Override - public List returnValidCardFolders(Set cardFolders) { - List returnCardFolder = new ArrayList<>(); - for (CardFolderExport cardFolderExport : cardFolders) { - addCardFolder(cardFolderExport, returnCardFolder); - } - return returnCardFolder; - } - - /** - * Private method to check if name of card folder to export matches name of ReadOnlyCardFolder in model. - * Throws card Folder not found exception if card folder cannot be found. - */ - private void addCardFolder(CardFolderExport cardFolderExport, List returnCardFolders) { - String exportFolderName = cardFolderExport.folderName; - for (ReadOnlyCardFolder readOnlyCardFolder : filteredFolders) { - if (readOnlyCardFolder.getFolderName().equals(exportFolderName)) { - returnCardFolders.add(readOnlyCardFolder); - return; + private List returnValidCardFolders(List cardFolderExports) { + List readOnlyCardFolders = new ArrayList<>(); + List indexList = cardFolderExports.stream().map(Index::fromOneBased).collect(Collectors.toList()); + for (Index index : indexList) { + try { + ReadOnlyCardFolder cardFolder = filteredFolders.get(index.getZeroBased()); + readOnlyCardFolders.add(cardFolder); + } catch (IndexOutOfBoundsException e) { + throw new CardFolderNotFoundException(index.displayIndex()); } } - throw new CardFolderNotFoundException(cardFolderExport.folderName); + return readOnlyCardFolders; } } diff --git a/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java index bc36ae5ce7a4..cf735442a638 100644 --- a/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ExportCommandParserTest.java @@ -1,31 +1,15 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.address.logic.commands.CommandTestUtil.FILENAME_DESC_SAMPLE; -import static seedu.address.logic.commands.CommandTestUtil.FOLDER_DESC_SAMPLE_1; -import static seedu.address.logic.commands.CommandTestUtil.FOLDER_DESC_SAMPLE_2; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_FILENAME_DESC; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_FILENAME_EXT; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_FOLDER_DESC; -import static seedu.address.logic.commands.CommandTestUtil.PREAMBLE_WHITESPACE; -import static seedu.address.logic.commands.CommandTestUtil.VALID_FILENAME; -import static seedu.address.logic.commands.CommandTestUtil.VALID_FOLDER_NAME_1; -import static seedu.address.logic.commands.CommandTestUtil.VALID_FOLDER_NAME_2; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import java.util.ArrayList; import java.util.Arrays; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import org.junit.Test; import seedu.address.logic.commands.ExportCommand; -import seedu.address.storage.csvmanager.CardFolderExport; -import seedu.address.storage.csvmanager.CsvFile; public class ExportCommandParserTest { @@ -45,37 +29,4 @@ public void parse_invalidArgs_throwsParseException() { assertParseFailure(parser, "1 a 3", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE)); } - - @Test - public void parse_compulsoryFieldMissing_failure() { - String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE); - - // missing folder prefix - assertParseFailure(parser, VALID_FOLDER_NAME_1 - + FILENAME_DESC_SAMPLE, String.format(expectedMessage)); - - // missing filename prefix - assertParseFailure(parser, FOLDER_DESC_SAMPLE_1 - + VALID_FILENAME, String.format(expectedMessage)); - - // missing all prefix - assertParseFailure(parser, VALID_FOLDER_NAME_1 - + VALID_FILENAME, String.format(expectedMessage)); - } - - @Test - public void parse_invalidValue_failure() { - - // missing Folder name - assertParseFailure(parser, INVALID_FOLDER_DESC + FILENAME_DESC_SAMPLE, - CardFolderExport.MESSAGE_CONSTRAINTS); - - // missing filename - assertParseFailure(parser, FOLDER_DESC_SAMPLE_1 + INVALID_FILENAME_DESC, - CsvFile.MESSAGE_CONSTRAINTS); - - assertParseFailure(parser, FOLDER_DESC_SAMPLE_1 + INVALID_FILENAME_EXT, - CsvFile.MESSAGE_CONSTRAINTS); - } - } From 3ac1d52211bb6fec81b71b66d90ef3baf8e0ae05 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 22:46:24 +0800 Subject: [PATCH 08/59] delete CSV implementation of export --- .../seedu/address/model/ModelManager.java | 1 + .../storage/csvmanager/CsvCommands.java | 4 +-- .../storage/csvmanager/CsvManager.java | 30 +------------------ 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 4da854a16b76..16b125bc1df9 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -455,6 +455,7 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio @Override public void importCardFolders(CsvFile csvFile) throws IOException { + } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java index 1d179812e171..579580086179 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java @@ -11,9 +11,7 @@ */ interface CsvCommands { - public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException; - - public void readFoldersToCsv(CsvFile csvFile); + public void readFoldersToCsv(CsvFile csvFile) throws IOException; public void writeFoldersToCsv(List cardFolders) throws IOException; diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 8178db29b2e6..75daf17f386a 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -18,38 +18,10 @@ public class CsvManager implements CsvCommands { private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; - /* - @Override - public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException { - String filepath = getFilePathAsString(filename); - FileWriter fileWriter = new FileWriter(filepath); - - // get card folder objects - for (ReadOnlyCardFolder readOnlyCardFolder : cardFolders) { - List cardList = readOnlyCardFolder.getCardList(); - String foldername = readOnlyCardFolder.getFolderName(); - fileWriter.append(foldername + NEW_LINE_SEPARATOR); - fileWriter.append(CARD_HEADERS + NEW_LINE_SEPARATOR); - for (Card card : cardList) { - String cardRepresentation = getCardString(card); - fileWriter.append(cardRepresentation); - fileWriter.append(NEW_LINE_SEPARATOR); - } - fileWriter.append(NEW_LINE_SEPARATOR); - } - fileWriter.flush(); - fileWriter.close(); - } - */ - @Override - public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException { - - } + public void readFoldersToCsv(CsvFile csvFile) throws IOException { - @Override - public void readFoldersToCsv(CsvFile csvFile) { } From e0e018b1d23650fa1da0a4d80d83827891f74f7c Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 22:48:41 +0800 Subject: [PATCH 09/59] delete CSV export old export command implementation --- .../seedu/address/model/ModelManager.java | 1 + .../storage/csvmanager/CsvCommands.java | 4 +-- .../storage/csvmanager/CsvManager.java | 30 +------------------ 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 4da854a16b76..16b125bc1df9 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -455,6 +455,7 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio @Override public void importCardFolders(CsvFile csvFile) throws IOException { + } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java index 1d179812e171..579580086179 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java @@ -11,9 +11,7 @@ */ interface CsvCommands { - public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException; - - public void readFoldersToCsv(CsvFile csvFile); + public void readFoldersToCsv(CsvFile csvFile) throws IOException; public void writeFoldersToCsv(List cardFolders) throws IOException; diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 8178db29b2e6..75daf17f386a 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -18,38 +18,10 @@ public class CsvManager implements CsvCommands { private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; - /* - @Override - public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException { - String filepath = getFilePathAsString(filename); - FileWriter fileWriter = new FileWriter(filepath); - - // get card folder objects - for (ReadOnlyCardFolder readOnlyCardFolder : cardFolders) { - List cardList = readOnlyCardFolder.getCardList(); - String foldername = readOnlyCardFolder.getFolderName(); - fileWriter.append(foldername + NEW_LINE_SEPARATOR); - fileWriter.append(CARD_HEADERS + NEW_LINE_SEPARATOR); - for (Card card : cardList) { - String cardRepresentation = getCardString(card); - fileWriter.append(cardRepresentation); - fileWriter.append(NEW_LINE_SEPARATOR); - } - fileWriter.append(NEW_LINE_SEPARATOR); - } - fileWriter.flush(); - fileWriter.close(); - } - */ - @Override - public void writeFoldersToCsv(List cardFolders, CsvFile filename) throws IOException { - - } + public void readFoldersToCsv(CsvFile csvFile) throws IOException { - @Override - public void readFoldersToCsv(CsvFile csvFile) { } From 7dd389522d7dfda69c1a54ba1460e4f5a2e31e0b Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 23:31:12 +0800 Subject: [PATCH 10/59] add csvManager to import --- src/main/java/seedu/address/model/ModelManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 16b125bc1df9..a57fd10b88eb 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -455,7 +455,7 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio @Override public void importCardFolders(CsvFile csvFile) throws IOException { - + csvManager.readFoldersToCsv(csvFile); } From b76bdbd58b9baa5abfa653adc5b0808e31388437 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 23:31:38 +0800 Subject: [PATCH 11/59] add logic for file import --- .../storage/csvmanager/CsvManager.java | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 75daf17f386a..f618ca55a205 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -1,8 +1,6 @@ package seedu.address.storage.csvmanager; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; import java.util.List; import seedu.address.model.ReadOnlyCardFolder; @@ -17,12 +15,43 @@ public class CsvManager implements CsvCommands { private static final String COMMA_DELIMITTER = ","; private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; + private BufferedReader bufferedReader; @Override public void readFoldersToCsv(CsvFile csvFile) throws IOException { + String filePath = getDefaultFilePath() + "/" + csvFile.filename; + try { + bufferedReader = new BufferedReader(new FileReader(filePath)); + String line; + String header = bufferedReader.readLine(); + while ((line = bufferedReader.readLine()) != null) { + // use comma as separator + String[] card = line.split(COMMA_DELIMITTER); + + System.out.println("card : " + card[0] + " " + card[1]); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private boolean checkCorrectHeaders(String header) { + String[] cardHeaders = CARD_HEADERS.split(","); + String[] fileHeaders = header.split(",") } @Override From 996e804d910f894f5b671b9810bb62cfe299dee9 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 23:48:08 +0800 Subject: [PATCH 12/59] make import method throw CommandException --- .../java/seedu/address/storage/csvmanager/CsvCommands.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java index 579580086179..769392688fd7 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.util.List; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.ReadOnlyCardFolder; @@ -11,7 +12,7 @@ */ interface CsvCommands { - public void readFoldersToCsv(CsvFile csvFile) throws IOException; + public void readFoldersToCsv(CsvFile csvFile) throws IOException, CommandException; public void writeFoldersToCsv(List cardFolders) throws IOException; From b3121ada8a20560e0a0be4967648708efe12bf14 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 23:48:28 +0800 Subject: [PATCH 13/59] implement check for file headers --- .../storage/csvmanager/CsvManager.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index f618ca55a205..5c207bc5a031 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -3,6 +3,8 @@ import java.io.*; import java.util.List; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.ReadOnlyCardFolder; import seedu.address.model.card.Card; @@ -19,39 +21,43 @@ public class CsvManager implements CsvCommands { @Override - public void readFoldersToCsv(CsvFile csvFile) throws IOException { + public void readFoldersToCsv(CsvFile csvFile) throws IOException, FileNotFoundException, CommandException { String filePath = getDefaultFilePath() + "/" + csvFile.filename; - try { - bufferedReader = new BufferedReader(new FileReader(filePath)); - String line; - String header = bufferedReader.readLine(); + bufferedReader = new BufferedReader(new FileReader(filePath)); + String line; + String header = bufferedReader.readLine(); - while ((line = bufferedReader.readLine()) != null) { + if (!checkCorrectHeaders(header)) { + throw new CommandException("hekki"); + } - // use comma as separator - String[] card = line.split(COMMA_DELIMITTER); + while ((line = bufferedReader.readLine()) != null) { - System.out.println("card : " + card[0] + " " + card[1]); - } + // use comma as separator + String[] card = line.split(COMMA_DELIMITTER); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (bufferedReader != null) { - try { - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + System.out.println("card : " + card[0] + " " + card[1]); + } + if (bufferedReader != null) { + bufferedReader.close(); } } + private boolean checkCorrectHeaders(String header) { String[] cardHeaders = CARD_HEADERS.split(","); - String[] fileHeaders = header.split(",") + String[] fileHeaders = header.split(","); + + if (cardHeaders.length != fileHeaders.length) { + return false; + } + + for (int i = 0; i < cardHeaders.length; i++) { + if (!cardHeaders[i].toLowerCase().equals(fileHeaders[i].toLowerCase())) { + return false; + } + } + return true; } @Override From e5be0802ad7b35b272097914935b824d615dda1b Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Mon, 25 Mar 2019 23:54:01 +0800 Subject: [PATCH 14/59] update file exists for import --- src/main/java/seedu/address/model/ModelManager.java | 1 + .../seedu/address/storage/csvmanager/CsvManager.java | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index a57fd10b88eb..89f65b3f68a6 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -26,6 +26,7 @@ import seedu.address.commons.core.LogsCenter; import seedu.address.commons.core.index.Index; import seedu.address.commons.util.InvalidationListenerManager; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.card.Answer; import seedu.address.model.card.Card; import seedu.address.model.card.exceptions.CardNotFoundException; diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 5c207bc5a031..33148e7980c0 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -21,7 +21,10 @@ public class CsvManager implements CsvCommands { @Override - public void readFoldersToCsv(CsvFile csvFile) throws IOException, FileNotFoundException, CommandException { + public void readFoldersToCsv(CsvFile csvFile) throws IOException, CommandException, FileNotFoundException { + if (!fileExists(csvFile)) { + throw new FileNotFoundException(); + } String filePath = getDefaultFilePath() + "/" + csvFile.filename; bufferedReader = new BufferedReader(new FileReader(filePath)); String line; @@ -83,6 +86,10 @@ public static String getFilePathAsString(CsvFile csvFile) throws IOException { return new File("./" + csvFile.filename).getCanonicalPath(); } + public static boolean fileExists(CsvFile csvFile) throws IOException { + return new File(getDefaultFilePath() + "/" + csvFile.filename).isFile(); + } + public static String getDefaultFilePath() throws IOException { return new File("./").getCanonicalPath(); } From 9a5c3d1cb506b4266553bbac7a9bab23795770e0 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 00:11:01 +0800 Subject: [PATCH 15/59] add build card method from import --- .../java/seedu/address/model/ModelManager.java | 2 +- .../address/storage/csvmanager/CsvManager.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 89f65b3f68a6..f4602fd5a20a 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -455,7 +455,7 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio } @Override - public void importCardFolders(CsvFile csvFile) throws IOException { + public void importCardFolders(CsvFile csvFile) throws IOException, CommandExceptionƒ { csvManager.readFoldersToCsv(csvFile); } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 33148e7980c0..eafbd6b08cd2 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -1,12 +1,17 @@ package seedu.address.storage.csvmanager; import java.io.*; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.ReadOnlyCardFolder; +import seedu.address.model.card.Answer; import seedu.address.model.card.Card; +import seedu.address.model.card.Question; +import seedu.address.model.card.Score; +import seedu.address.model.hint.Hint; /** @@ -46,6 +51,16 @@ public void readFoldersToCsv(CsvFile csvFile) throws IOException, CommandExcepti } } + private Card buildCard(String[] cardValues) { + Question question = new Question(cardValues[0]); + Answer answer = new Answer(cardValues[1]); + String[] hintArray = Arrays.copyOfRange(cardValues, 2, cardValues.length); + + Set hintSet = Arrays.stream(hintArray).map(Hint::new).collect(Collectors.toSet()); + Card card = new Card(question, answer, new Score(0, 0), hintSet); + return card; + } + private boolean checkCorrectHeaders(String header) { String[] cardHeaders = CARD_HEADERS.split(","); From 0c1e2703316e25dccf05cef90b33487159e54f0f Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 00:19:28 +0800 Subject: [PATCH 16/59] implement logic for import command --- src/main/java/seedu/address/model/Model.java | 5 ++--- src/main/java/seedu/address/model/ModelManager.java | 4 ++-- .../address/storage/csvmanager/CsvCommands.java | 3 ++- .../seedu/address/storage/csvmanager/CsvManager.java | 12 +++++++++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 8f8673a1be02..dcbe3d49f92d 100755 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -4,16 +4,15 @@ import java.nio.file.Path; import java.util.Comparator; import java.util.List; -import java.util.Set; import java.util.function.Predicate; import javafx.beans.Observable; import javafx.beans.property.ReadOnlyProperty; import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.card.Answer; import seedu.address.model.card.Card; -import seedu.address.storage.csvmanager.CardFolderExport; import seedu.address.storage.csvmanager.CsvFile; /** @@ -234,7 +233,7 @@ public interface Model extends Observable { void exportCardFolders(List cardFolderExports) throws IOException; - void importCardFolders(CsvFile csvFile) throws IOException; + void importCardFolders(CsvFile csvFile) throws IOException, CommandException; } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index f4602fd5a20a..42cc29dcc16b 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -455,8 +455,8 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio } @Override - public void importCardFolders(CsvFile csvFile) throws IOException, CommandExceptionƒ { - csvManager.readFoldersToCsv(csvFile); + public void importCardFolders(CsvFile csvFile) throws IOException, CommandException { + CardFolder cardFolder = csvManager.readFoldersToCsv(csvFile); } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java index 769392688fd7..b338acf85fd4 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvCommands.java @@ -4,6 +4,7 @@ import java.util.List; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.CardFolder; import seedu.address.model.ReadOnlyCardFolder; @@ -12,7 +13,7 @@ */ interface CsvCommands { - public void readFoldersToCsv(CsvFile csvFile) throws IOException, CommandException; + public CardFolder readFoldersToCsv(CsvFile csvFile) throws IOException, CommandException; public void writeFoldersToCsv(List cardFolders) throws IOException; diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index eafbd6b08cd2..2b8a05952434 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -6,6 +6,7 @@ import seedu.address.logic.commands.Command; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.CardFolder; import seedu.address.model.ReadOnlyCardFolder; import seedu.address.model.card.Answer; import seedu.address.model.card.Card; @@ -26,11 +27,13 @@ public class CsvManager implements CsvCommands { @Override - public void readFoldersToCsv(CsvFile csvFile) throws IOException, CommandException, FileNotFoundException { + public CardFolder readFoldersToCsv(CsvFile csvFile) throws IOException, CommandException { if (!fileExists(csvFile)) { throw new FileNotFoundException(); } String filePath = getDefaultFilePath() + "/" + csvFile.filename; + String folderName = filePath.split(".")[0]; + CardFolder cardFolder = new CardFolder(folderName); bufferedReader = new BufferedReader(new FileReader(filePath)); String line; String header = bufferedReader.readLine(); @@ -42,15 +45,18 @@ public void readFoldersToCsv(CsvFile csvFile) throws IOException, CommandExcepti while ((line = bufferedReader.readLine()) != null) { // use comma as separator - String[] card = line.split(COMMA_DELIMITTER); + String[] stringCard = line.split(COMMA_DELIMITTER); - System.out.println("card : " + card[0] + " " + card[1]); + Card card = buildCard(stringCard); + cardFolder.addCard(card); } if (bufferedReader != null) { bufferedReader.close(); } + return cardFolder; } + private Card buildCard(String[] cardValues) { Question question = new Question(cardValues[0]); Answer answer = new Answer(cardValues[1]); From 85bcdb9593cbc2f9af5edd78eeadb0bd70f22c35 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 00:39:21 +0800 Subject: [PATCH 17/59] check logic for import --- src/main/java/seedu/address/model/ModelManager.java | 1 + src/main/java/seedu/address/storage/csvmanager/CsvManager.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 42cc29dcc16b..782bb6735e8d 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -457,6 +457,7 @@ public void exportCardFolders(List cardFolderExports) throws IOExceptio @Override public void importCardFolders(CsvFile csvFile) throws IOException, CommandException { CardFolder cardFolder = csvManager.readFoldersToCsv(csvFile); + addFolder(cardFolder); } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 2b8a05952434..270fe681c775 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -32,7 +32,8 @@ public CardFolder readFoldersToCsv(CsvFile csvFile) throws IOException, CommandE throw new FileNotFoundException(); } String filePath = getDefaultFilePath() + "/" + csvFile.filename; - String folderName = filePath.split(".")[0]; + String filename = csvFile.filename; + String folderName = filename.split("\\.")[0]; CardFolder cardFolder = new CardFolder(folderName); bufferedReader = new BufferedReader(new FileReader(filePath)); String line; From 4be63cb21a12cc77ecfce63933345e3c3f8958d3 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 09:53:51 +0800 Subject: [PATCH 18/59] implement parser test for import command --- .../address/logic/commands/ImportCommand.java | 4 +- .../logic/commands/AddCommandTest.java | 8 ---- .../logic/parser/ImportCommandParserTest.java | 40 +++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index 4fc91b5161f8..ad0d2907fc04 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -41,12 +41,12 @@ public CommandResult execute(Model model, CommandHistory history) throws Command return null; } - - /** * Returns true if file extension is of .json format. */ private boolean isCorrectFileExtension(String filename) { return filename.split("\\.(?=[^\\.]+$)")[1].equals("csv"); } + + } diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 2a0f8132e3bc..7673f426ff9f 100755 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -177,10 +177,6 @@ public void endTestSession() { throw new AssertionError("This method should not be called."); } - @Override - public List returnValidCardFolders(Set cardFolers) { - throw new AssertionError("This method should not be called."); - } public boolean markAttemptedAnswer(Answer attemptedAnswer) { throw new AssertionError("This method should not be called."); @@ -196,10 +192,6 @@ public boolean checkIfCardAlreadyAnswered() { throw new AssertionError("This method should not be called."); } - @Override - public void exportCardFolders(Set cardFolderExports, CsvFile csvFile) throws IOException { - throw new AssertionError("This method should not be called."); - } @Override public void exportCardFolders(List cardFolderExports) throws IOException { diff --git a/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java new file mode 100644 index 000000000000..34731e336ae1 --- /dev/null +++ b/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java @@ -0,0 +1,40 @@ +package seedu.address.logic.parser; + +import org.junit.Test; +import seedu.address.logic.commands.ExportCommand; +import seedu.address.logic.commands.ImportCommand; +import seedu.address.storage.csvmanager.CsvFile; + +import java.util.ArrayList; +import java.util.Arrays; + +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; +import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; + +public class ImportCommandParserTest { + + private static String EMPTY_STRING = ""; + private ImportCommandParser parser = new ImportCommandParser(); + + @Test + public void parse_validArgs_returnsExportCommand() { + String testFile = "myCsvFile.csv"; + + assertParseSuccess(parser, testFile, new ImportCommand(new CsvFile(testFile))); + + // assertParseSuccess(parser, "1 3 5", new ExportCommand(new ArrayList(Arrays.asList(1,3,5)))); + } + + @Test + public void parse_invalidArgs_throwsParseException() { + String testFileIncorrect = "hello.exe"; + + assertParseFailure(parser, EMPTY_STRING, String.format(MESSAGE_INVALID_COMMAND_FORMAT, + ImportCommand.MESSAGE_USAGE)); + + assertParseFailure(parser, testFileIncorrect, String.format(MESSAGE_INVALID_COMMAND_FORMAT, + ImportCommand.MESSAGE_USAGE)); + + } +} From ce6c7b0991162e76f97d6e720e44ac64e6fa20ac Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 09:58:53 +0800 Subject: [PATCH 19/59] refactor CsvFile.filename to CsvFile.csvFile --- config/travis/check-eof-newline.sh | 10 +++++----- docs/DeveloperGuide.adoc | 2 +- docs/UserGuide.adoc | 2 +- .../address/logic/commands/ImportCommand.java | 16 +++++++++++----- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/config/travis/check-eof-newline.sh b/config/travis/check-eof-newline.sh index b771f3988dd8..9dd231bbfe94 100755 --- a/config/travis/check-eof-newline.sh +++ b/config/travis/check-eof-newline.sh @@ -3,14 +3,14 @@ ret=0 -# Preserve filename with spaces by only splitting on newlines. +# Preserve csvFile with spaces by only splitting on newlines. IFS=' ' -for filename in $(git grep --cached -I -l -e '' -- ':/'); do - if [ "$(tail -c 1 "./$filename")" != '' ]; then - line="$(wc -l "./$filename" | cut -d' ' -f1)" - echo "ERROR:$filename:$line: no newline at EOF." +for csvFile in $(git grep --cached -I -l -e '' -- ':/'); do + if [ "$(tail -c 1 "./$csvFile")" != '' ]; then + line="$(wc -l "./$csvFile" | cut -d' ' -f1)" + echo "ERROR:$csvFile:$line: no newline at EOF." ret=1 fi done diff --git a/docs/DeveloperGuide.adoc b/docs/DeveloperGuide.adoc index d1105fc17b1d..751106d9126f 100755 --- a/docs/DeveloperGuide.adoc +++ b/docs/DeveloperGuide.adoc @@ -399,7 +399,7 @@ CsvManager's API’s are exposed in the model interface as `Model#ExportCardFol [discrete] ==== Example Usage -1. User wants to export folders `Human Anatomy` and `Nervous System`. User inputs the following folders and the filename specified that he wants to export the folders to. +1. User wants to export folders `Human Anatomy` and `Nervous System`. User inputs the following folders and the csvFile specified that he wants to export the folders to. picture of command box with user inputs diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 50dfb12ab0d8..999da213b98f 100755 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -320,7 +320,7 @@ Format: `search KEYWORDS [MORE_KEYWORDS]` ==== Import flashcards : `import` -Searches for a json file with the specified filename in the program directory and parses the file to generate a flashcard folder. +Searches for a json file with the specified csvFile in the program directory and parses the file to generate a flashcard folder. Format: `import FILENAME` diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index ad0d2907fc04..646df80e2a43 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -24,17 +24,17 @@ public class ImportCommand extends Command { public static final String MESSAGE_FILE_OPS_FAILURE = "Could not import from specified file. Check that it exists " + "in root directory"; - private CsvFile filename; + private CsvFile csvFile; - public ImportCommand(CsvFile filename) { - this.filename = filename; + public ImportCommand(CsvFile csvFile) { + this.csvFile = csvFile; } @Override public CommandResult execute(Model model, CommandHistory history) throws CommandException { try { - model.importCardFolders(filename); + model.importCardFolders(csvFile); } catch (IOException e) { throw new CommandException(MESSAGE_FILE_OPS_FAILURE); } @@ -48,5 +48,11 @@ private boolean isCorrectFileExtension(String filename) { return filename.split("\\.(?=[^\\.]+$)")[1].equals("csv"); } - + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || other instanceof ImportCommand // instanceof handles nulls + && csvFile.filename.equals() + } + } } From e1a98e0522d561aa690c4280015e7a442198a537 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 09:59:38 +0800 Subject: [PATCH 20/59] add equals method to import command --- src/main/java/seedu/address/logic/commands/ImportCommand.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index 646df80e2a43..0927e193e08a 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -52,7 +52,6 @@ private boolean isCorrectFileExtension(String filename) { public boolean equals(Object other) { return other == this // short circuit if same object || other instanceof ImportCommand // instanceof handles nulls - && csvFile.filename.equals() - } + && csvFile.filename.equals(((ImportCommand) other).csvFile); } } From 8504bc8f5ffda06d9c57a6357e04a9baf0829aee Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 10:09:01 +0800 Subject: [PATCH 21/59] implement import command parser tests --- src/main/java/seedu/address/logic/commands/ImportCommand.java | 2 +- src/main/java/seedu/address/logic/parser/ParserUtil.java | 4 ++-- .../seedu/address/logic/parser/ImportCommandParserTest.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index 0927e193e08a..bc473c4dcdc2 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -52,6 +52,6 @@ private boolean isCorrectFileExtension(String filename) { public boolean equals(Object other) { return other == this // short circuit if same object || other instanceof ImportCommand // instanceof handles nulls - && csvFile.filename.equals(((ImportCommand) other).csvFile); + && csvFile.filename.equals(((ImportCommand) other).csvFile.filename); } } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 359e70ad7987..34cf1f95dc48 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -6,10 +6,10 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import seedu.address.commons.core.Messages; import seedu.address.commons.core.index.Index; import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.commons.util.StringUtil; -import seedu.address.logic.commands.ExportCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.card.Answer; import seedu.address.model.card.Question; @@ -156,7 +156,7 @@ private static CardFolderExport parseFolder(String folderName) throws ParseExcep public static CsvFile parseFileName(String filename) throws ParseException { requireNonNull(filename); if (!CsvFile.isValidFileName(filename)) { - throw new ParseException(CsvFile.MESSAGE_CONSTRAINTS); + throw new ParseException(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, CsvFile.MESSAGE_CONSTRAINTS)); } return new CsvFile(filename); } diff --git a/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java index 34731e336ae1..9b4e7aeeed4c 100644 --- a/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java @@ -31,10 +31,10 @@ public void parse_invalidArgs_throwsParseException() { String testFileIncorrect = "hello.exe"; assertParseFailure(parser, EMPTY_STRING, String.format(MESSAGE_INVALID_COMMAND_FORMAT, - ImportCommand.MESSAGE_USAGE)); + CsvFile.MESSAGE_CONSTRAINTS)); assertParseFailure(parser, testFileIncorrect, String.format(MESSAGE_INVALID_COMMAND_FORMAT, - ImportCommand.MESSAGE_USAGE)); + CsvFile.MESSAGE_CONSTRAINTS)); } } From 9e3cbaa8d3996adcbbeef2a222a42f8ed260a304 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 10:09:42 +0800 Subject: [PATCH 22/59] remove unnecessary import statements --- .../seedu/address/logic/parser/ImportCommandParserTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java index 9b4e7aeeed4c..315e6ff86387 100644 --- a/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ImportCommandParserTest.java @@ -1,13 +1,10 @@ package seedu.address.logic.parser; import org.junit.Test; -import seedu.address.logic.commands.ExportCommand; + import seedu.address.logic.commands.ImportCommand; import seedu.address.storage.csvmanager.CsvFile; -import java.util.ArrayList; -import java.util.Arrays; - import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; From 500b982c3def8fb9b73ac5d47f0df7751b1f2420 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 11:17:46 +0800 Subject: [PATCH 23/59] implement partial export command test --- .../logic/commands/AddCommandTest.java | 10 ---- .../logic/commands/ExportCommandTest.java | 51 +++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/ExportCommandTest.java diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 2a0f8132e3bc..cc66b4700017 100755 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -11,7 +11,6 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; -import java.util.Set; import java.util.function.Predicate; import org.junit.Rule; @@ -31,7 +30,6 @@ import seedu.address.model.VersionedCardFolder; import seedu.address.model.card.Answer; import seedu.address.model.card.Card; -import seedu.address.storage.csvmanager.CardFolderExport; import seedu.address.storage.csvmanager.CsvFile; import seedu.address.testutil.CardBuilder; @@ -177,10 +175,6 @@ public void endTestSession() { throw new AssertionError("This method should not be called."); } - @Override - public List returnValidCardFolders(Set cardFolers) { - throw new AssertionError("This method should not be called."); - } public boolean markAttemptedAnswer(Answer attemptedAnswer) { throw new AssertionError("This method should not be called."); @@ -196,10 +190,6 @@ public boolean checkIfCardAlreadyAnswered() { throw new AssertionError("This method should not be called."); } - @Override - public void exportCardFolders(Set cardFolderExports, CsvFile csvFile) throws IOException { - throw new AssertionError("This method should not be called."); - } @Override public void exportCardFolders(List cardFolderExports) throws IOException { diff --git a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java new file mode 100644 index 000000000000..5adbf12d53ba --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java @@ -0,0 +1,51 @@ +package seedu.address.logic.commands; + +import java.io.File; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import seedu.address.logic.CommandHistory; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static seedu.address.testutil.TypicalCards.getTypicalCardFolders; + +public class ExportCommandTest { + + @Rule + public TemporaryFolder tmpFolder = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private Model model = new ModelManager(getTypicalCardFolders(), new UserPrefs()); + private CommandHistory commandHistory = new CommandHistory(); + + + @Test + public void execute_exportSingleValidCardFolderIndex_success() { + List myList = new ArrayList<>(Arrays.asList(1)); + ExportCommand exportCommand = new ExportCommand(myList); + + + } + + + @Test + public void testXYZ(File expected, File actual) throws IOException { + byte[] f1 = Files.readAllBytes(expected.toPath()); + byte[] f2 = Files.readAllBytes(actual.toPath()); + } +} From deb65e5d727bb3e8c0f5a2b3e07e5eff49fb948d Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 14:36:43 +0800 Subject: [PATCH 24/59] add export single card folder success and failure methods --- .../logic/commands/ExportCommandTest.java | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java index 5adbf12d53ba..7db712045d1d 100644 --- a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java @@ -2,25 +2,26 @@ import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static seedu.address.testutil.TypicalCards.*; + import seedu.address.logic.CommandHistory; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; -import java.io.IOException; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import static seedu.address.testutil.TypicalCards.getTypicalCardFolders; public class ExportCommandTest { @@ -34,18 +35,40 @@ public class ExportCommandTest { private CommandHistory commandHistory = new CommandHistory(); + @Test - public void execute_exportSingleValidCardFolderIndex_success() { + public void execute_exportSingleValidCardFolderIndex_success() throws Exception { + System.out.println(tmpFolder.getRoot()); List myList = new ArrayList<>(Arrays.asList(1)); ExportCommand exportCommand = new ExportCommand(myList); + CommandResult commandResult = exportCommand.execute(model, commandHistory); + assertEquals(ExportCommand.MESSAGE_SUCCESS, commandResult.getFeedbackToUser()); } + @After + public void deleteExportFile() { + File file = new File("./Typical Cards.csv"); + if (file.exists()) { + boolean isDeleted = file.delete(); + assert (isDeleted); + } + } + + @Test + public void execute_exportSingleInvalidCardFolderIndex_failure() throws Exception { + List myList = new ArrayList<>(Arrays.asList(1,3)); + ExportCommand exportCommand = new ExportCommand(myList); + + thrown.expect(CommandException.class); + thrown.expectMessage(ExportCommand.MESSAGE_MISSING_CARD_FOLDERS); + CommandResult commandResult = exportCommand.execute(model, commandHistory); + } @Test - public void testXYZ(File expected, File actual) throws IOException { - byte[] f1 = Files.readAllBytes(expected.toPath()); - byte[] f2 = Files.readAllBytes(actual.toPath()); + public void execute_exportMultipleValidCardFolderIndex_success() throws Exception { + } + } From fbe2ceb889169ca494aa4f846a738c0139e51239 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 15:02:10 +0800 Subject: [PATCH 25/59] implement base import command tester class --- .../logic/commands/ImportCommandTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/java/seedu/address/logic/commands/ImportCommandTest.java diff --git a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java new file mode 100644 index 000000000000..3b5ea27b955e --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java @@ -0,0 +1,36 @@ +package seedu.address.logic.commands; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import seedu.address.logic.CommandHistory; +import seedu.address.model.*; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +public class ImportCommandTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + + private File file; + + private CommandHistory commandHistory = new CommandHistory(); + private Model model = new ModelManager(new ArrayList(), new UserPrefs()); + + public ImportCommandTest() throws IOException { + } + + @Before + public void setUp() throws Exception { + file = new File("./Typical Cards.csv").getCanonicalFile(); + + } + + + +} From 8089bf891adb87578970de7e9b429bc14ba95bd2 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 15:08:36 +0800 Subject: [PATCH 26/59] add import success message --- src/main/java/seedu/address/logic/commands/ImportCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index bc473c4dcdc2..736effb0813c 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -23,6 +23,7 @@ public class ImportCommand extends Command { + "Example: " + COMMAND_WORD + "alice.csv"; public static final String MESSAGE_FILE_OPS_FAILURE = "Could not import from specified file. Check that it exists " + "in root directory"; + public static final String MESSAGE_SUCCESS = "Successfully imported: %1$s"; private CsvFile csvFile; From f7f65038257399783715ec4fd2aa2e399ccfb387 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 15:15:40 +0800 Subject: [PATCH 27/59] add return command result for import command --- src/main/java/seedu/address/logic/commands/ImportCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/commands/ImportCommand.java b/src/main/java/seedu/address/logic/commands/ImportCommand.java index 736effb0813c..91f567a8b8f1 100644 --- a/src/main/java/seedu/address/logic/commands/ImportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ImportCommand.java @@ -39,7 +39,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command } catch (IOException e) { throw new CommandException(MESSAGE_FILE_OPS_FAILURE); } - return null; + return new CommandResult(String.format(MESSAGE_SUCCESS, csvFile.filename)); } /** From c62e412614461c9e36898d103d0638d62b01ec60 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 15:16:04 +0800 Subject: [PATCH 28/59] add import typical cards success test --- .../logic/commands/ImportCommandTest.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java index 3b5ea27b955e..efcf45ed6e37 100644 --- a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java @@ -3,15 +3,20 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.junit.Assert.assertEquals; import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; import seedu.address.logic.CommandHistory; import seedu.address.model.*; +import seedu.address.storage.csvmanager.CsvFile; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalCards.getTypicalCardFolders; +import static seedu.address.testutil.TypicalCards.getTypicalCards; + public class ImportCommandTest { @Rule public ExpectedException thrown = ExpectedException.none(); @@ -28,7 +33,18 @@ public ImportCommandTest() throws IOException { @Before public void setUp() throws Exception { file = new File("./Typical Cards.csv").getCanonicalFile(); - + + } + + @Test + public void execute_importTypicalCards_success() throws Exception { + String filename = "Typical Cards.csv"; + ImportCommand importCommand = new ImportCommand(new CsvFile(filename)); + + Model expectedModel = new ModelManager(getTypicalCardFolders(), new UserPrefs()); + CommandResult commandResult = importCommand.execute(model, commandHistory); + String expectedMessage = String.format(ImportCommand.MESSAGE_SUCCESS, filename); + assertCommandSuccess(importCommand, model, commandHistory, expectedMessage, expectedModel); } From c95aae3b753d8ea266f976ded0924638a015fb31 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 15:23:43 +0800 Subject: [PATCH 29/59] add import non existent csv file --- .../address/storage/csvmanager/CsvManager.java | 2 +- .../logic/commands/ImportCommandTest.java | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 270fe681c775..b34e339c0148 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -109,7 +109,7 @@ public static String getFilePathAsString(CsvFile csvFile) throws IOException { } public static boolean fileExists(CsvFile csvFile) throws IOException { - return new File(getDefaultFilePath() + "/" + csvFile.filename).isFile(); + return new File(getDefaultFilePath() + "/" + csvFile.filename).isFile(); } public static String getDefaultFilePath() throws IOException { diff --git a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java index efcf45ed6e37..1ca4dd138597 100644 --- a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertEquals; import org.junit.rules.ExpectedException; import seedu.address.logic.CommandHistory; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.*; import seedu.address.storage.csvmanager.CsvFile; @@ -15,7 +16,6 @@ import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.testutil.TypicalCards.getTypicalCardFolders; -import static seedu.address.testutil.TypicalCards.getTypicalCards; public class ImportCommandTest { @Rule @@ -23,6 +23,8 @@ public class ImportCommandTest { private File file; + private final String validFilename = "Typical Cards.csv"; + private final String invalidFileName = "Fake Cards.csv"; private CommandHistory commandHistory = new CommandHistory(); private Model model = new ModelManager(new ArrayList(), new UserPrefs()); @@ -38,15 +40,22 @@ public void setUp() throws Exception { @Test public void execute_importTypicalCards_success() throws Exception { - String filename = "Typical Cards.csv"; - ImportCommand importCommand = new ImportCommand(new CsvFile(filename)); + ImportCommand importCommand = new ImportCommand(new CsvFile(validFilename)); Model expectedModel = new ModelManager(getTypicalCardFolders(), new UserPrefs()); CommandResult commandResult = importCommand.execute(model, commandHistory); - String expectedMessage = String.format(ImportCommand.MESSAGE_SUCCESS, filename); + String expectedMessage = String.format(ImportCommand.MESSAGE_SUCCESS, validFilename); assertCommandSuccess(importCommand, model, commandHistory, expectedMessage, expectedModel); } + @Test + public void execute_importNonExistentCards_failure() throws Exception { + ImportCommand importCommand = new ImportCommand(new CsvFile(invalidFileName)); + thrown.expect(CommandException.class); + thrown.expectMessage(ImportCommand.MESSAGE_FILE_OPS_FAILURE); + importCommand.execute(model, commandHistory); + } + } From daffdebb1b994ecf13f65ad285486a6dae072598 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Tue, 26 Mar 2019 15:26:41 +0800 Subject: [PATCH 30/59] add @after for deletion of temp file --- .../address/logic/commands/ImportCommandTest.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java index 1ca4dd138597..aed817db2567 100644 --- a/src/test/java/seedu/address/logic/commands/ImportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ImportCommandTest.java @@ -1,9 +1,9 @@ package seedu.address.logic.commands; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.junit.Assert.assertEquals; import org.junit.rules.ExpectedException; import seedu.address.logic.CommandHistory; import seedu.address.logic.commands.exceptions.CommandException; @@ -56,6 +56,10 @@ public void execute_importNonExistentCards_failure() throws Exception { importCommand.execute(model, commandHistory); } - - + @After + public void deleteTempFile() { + if (file.exists()) { + assert(file.delete()); + } + } } From 9f018d0132c1306e2ef75580f46dd46fe6fe035b Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 22:11:11 +0800 Subject: [PATCH 31/59] add sample folder csv test file --- .../java/seedu/address/logic/commands/ExportCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java index 7db712045d1d..4c387921b4ee 100644 --- a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java @@ -49,7 +49,7 @@ public void execute_exportSingleValidCardFolderIndex_success() throws Exception @After public void deleteExportFile() { - File file = new File("./Typical Cards.csv"); + File file = new File("./Sample Folder.csv"); if (file.exists()) { boolean isDeleted = file.delete(); assert (isDeleted); From ade3c99bd011e2c040c62679a39702ed348a0c87 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 22:30:59 +0800 Subject: [PATCH 32/59] add model method to change default csv path --- src/main/java/seedu/address/model/Model.java | 3 +++ .../seedu/address/model/ModelManager.java | 5 ++++ .../address/logic/commands/CsvFileTest.java | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/java/seedu/address/logic/commands/CsvFileTest.java diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 2b6166c82174..d8d01bcfc20c 100755 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -15,6 +15,7 @@ import seedu.address.model.card.Card; import seedu.address.storage.csvmanager.CardFolderExport; import seedu.address.storage.csvmanager.CsvFile; +import seedu.address.storage.csvmanager.CsvManager; /** * The API of the Model component. @@ -243,5 +244,7 @@ public interface Model extends Observable { void importCardFolders(CsvFile csvFile) throws IOException; + void setTestCsvPath(CsvManager csvManager); + } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index e2e608e9fcaf..0b344791db5c 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -491,6 +491,11 @@ public void importCardFolders(CsvFile csvFile) throws IOException { } + @Override + public void setTestCsvPath(CsvManager csvManager) { + + } + private List returnValidCardFolders(List cardFolderExports) { List readOnlyCardFolders = new ArrayList<>(); diff --git a/src/test/java/seedu/address/logic/commands/CsvFileTest.java b/src/test/java/seedu/address/logic/commands/CsvFileTest.java new file mode 100644 index 000000000000..6ce1c44a45b8 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/CsvFileTest.java @@ -0,0 +1,23 @@ +package seedu.address.logic.commands; + +import org.junit.Test; +import seedu.address.logic.CommandHistory; +import seedu.address.model.*; + +import java.util.ArrayList; +import java.util.List; + +import static seedu.address.testutil.TypicalCards.getTypicalCardFolders; + +public class CsvFileTest { + + private Model model = new ModelManager(new ArrayList(), new UserPrefs()); + private CommandHistory commandHistory = new CommandHistory(); + + + @Test + public void execute_importCsvFile_success() { + List readOnlyCardFolders = getTypicalCardFolders(); + model.importCardFolders(); + } +} From 58f5c7d4de71b557920ace965a20892068324ef5 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 22:51:00 +0800 Subject: [PATCH 33/59] change implementation of default path in CsvManager --- src/main/java/seedu/address/model/ModelManager.java | 2 +- .../seedu/address/storage/csvmanager/CsvManager.java | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 0b344791db5c..1e8cf99bfef8 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -493,7 +493,7 @@ public void importCardFolders(CsvFile csvFile) throws IOException { @Override public void setTestCsvPath(CsvManager csvManager) { - + } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 75daf17f386a..fb5f00685ed2 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -17,7 +17,11 @@ public class CsvManager implements CsvCommands { private static final String COMMA_DELIMITTER = ","; private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; + private static String defaultPath; + public CsvManager() throws IOException { + defaultPath = getDefaultFilePath(); + } @Override public void readFoldersToCsv(CsvFile csvFile) throws IOException { @@ -27,7 +31,7 @@ public void readFoldersToCsv(CsvFile csvFile) throws IOException { @Override public void writeFoldersToCsv(List cardFolders) throws IOException { - String filepath = getDefaultFilePath(); + String filepath = defaultPath; for (ReadOnlyCardFolder readOnlyCardFolder : cardFolders) { List cardList = readOnlyCardFolder.getCardList(); String foldername = readOnlyCardFolder.getFolderName(); @@ -52,6 +56,10 @@ public static String getDefaultFilePath() throws IOException { return new File("./").getCanonicalPath(); } + public static void setTestDefaultPath() throws IOException { + defaultPath = new File("./test/data/").getCanonicalPath(); + } + private String getCardString(Card card) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(card.getQuestion() + COMMA_DELIMITTER) From b49609200d01820a0f9e5920d49f91a562223433 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 23:03:38 +0800 Subject: [PATCH 34/59] make model throw IOException in csvManager init --- src/main/java/seedu/address/MainApp.java | 2 +- src/main/java/seedu/address/model/Model.java | 2 +- src/main/java/seedu/address/model/ModelManager.java | 8 ++++---- .../java/seedu/address/storage/csvmanager/CsvManager.java | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index 6591e9984ac4..c2f20b02ee4b 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -94,7 +94,7 @@ public void init() throws Exception { * The data from the sample card folder will be used instead if {@code storage}'s card folder is not found, * or an empty card folder will be used instead if errors occur when reading {@code storage}'s card folder. */ - Model initModelManager(boolean withSample, Storage storage, ReadOnlyUserPrefs userPrefs) { + Model initModelManager(boolean withSample, Storage storage, ReadOnlyUserPrefs userPrefs) throws IOException { List initialCardFolders; if (withSample) { diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index d8d01bcfc20c..e490abd801f2 100755 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -244,7 +244,7 @@ public interface Model extends Observable { void importCardFolders(CsvFile csvFile) throws IOException; - void setTestCsvPath(CsvManager csvManager); + void setTestCsvPath() throws IOException; } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 1e8cf99bfef8..615cb1106d48 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -66,7 +66,7 @@ public class ModelManager implements Model { /** * Initializes a ModelManager with the given cardFolders and userPrefs. */ - public ModelManager(List cardFolders, ReadOnlyUserPrefs userPrefs) { + public ModelManager(List cardFolders, ReadOnlyUserPrefs userPrefs) throws IOException { super(); requireAllNonNull(cardFolders, userPrefs); @@ -93,7 +93,7 @@ public ModelManager(List cardFolders, ReadOnlyUserPrefs user inFolder = true; } - public ModelManager(String newFolderName) { + public ModelManager(String newFolderName) throws IOException { this(Collections.singletonList(new CardFolder(newFolderName)), new UserPrefs()); } @@ -492,8 +492,8 @@ public void importCardFolders(CsvFile csvFile) throws IOException { } @Override - public void setTestCsvPath(CsvManager csvManager) { - + public void setTestCsvPath() throws IOException { + csvManager.setTestDefaultPath(); } diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index fb5f00685ed2..518c5edaeb99 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -17,7 +17,8 @@ public class CsvManager implements CsvCommands { private static final String COMMA_DELIMITTER = ","; private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; - private static String defaultPath; + private String defaultPath; + public CsvManager() throws IOException { defaultPath = getDefaultFilePath(); @@ -56,7 +57,7 @@ public static String getDefaultFilePath() throws IOException { return new File("./").getCanonicalPath(); } - public static void setTestDefaultPath() throws IOException { + public void setTestDefaultPath() throws IOException { defaultPath = new File("./test/data/").getCanonicalPath(); } From 2c7edbe3f2b8f3159e9d45349ebe1c8b352f500f Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 23:19:28 +0800 Subject: [PATCH 35/59] variable to decide if folder name is test --- .../address/storage/csvmanager/CsvManager.java | 9 ++++++++- .../{logic/commands => storage}/CsvFileTest.java | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) rename src/test/java/seedu/address/{logic/commands => storage}/CsvFileTest.java (52%) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 518c5edaeb99..9f26264dbb4a 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -18,6 +18,7 @@ public class CsvManager implements CsvCommands { private static final String NEW_LINE_SEPARATOR = "\n"; private static final String CARD_HEADERS = "Question,Answer,Hints"; private String defaultPath; + private boolean setTestDefaultPath = false; public CsvManager() throws IOException { @@ -35,7 +36,12 @@ public void writeFoldersToCsv(List cardFolders) throws IOExc String filepath = defaultPath; for (ReadOnlyCardFolder readOnlyCardFolder : cardFolders) { List cardList = readOnlyCardFolder.getCardList(); - String foldername = readOnlyCardFolder.getFolderName(); + String foldername; + if (setTestDefaultPath) { + foldername = readOnlyCardFolder.getFolderName() + " test"; + } else { + foldername = readOnlyCardFolder.getFolderName(); + } FileWriter fileWriter = new FileWriter(filepath + "/" + foldername + ".csv"); fileWriter.append(CARD_HEADERS + NEW_LINE_SEPARATOR); for (Card card : cardList) { @@ -59,6 +65,7 @@ public static String getDefaultFilePath() throws IOException { public void setTestDefaultPath() throws IOException { defaultPath = new File("./test/data/").getCanonicalPath(); + setTestDefaultPath = true; } private String getCardString(Card card) { diff --git a/src/test/java/seedu/address/logic/commands/CsvFileTest.java b/src/test/java/seedu/address/storage/CsvFileTest.java similarity index 52% rename from src/test/java/seedu/address/logic/commands/CsvFileTest.java rename to src/test/java/seedu/address/storage/CsvFileTest.java index 6ce1c44a45b8..613a9f3252bc 100644 --- a/src/test/java/seedu/address/logic/commands/CsvFileTest.java +++ b/src/test/java/seedu/address/storage/CsvFileTest.java @@ -1,9 +1,12 @@ package seedu.address.logic.commands; import org.junit.Test; +import org.junit.Assert; import seedu.address.logic.CommandHistory; import seedu.address.model.*; +import seedu.address.storage.csvmanager.CsvFile; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -13,11 +16,19 @@ public class CsvFileTest { private Model model = new ModelManager(new ArrayList(), new UserPrefs()); private CommandHistory commandHistory = new CommandHistory(); + private final String TYPICAL_CARD_FOLDER = "Typical Cards.csv"; + + public CsvFileTest() throws IOException { + } @Test - public void execute_importCsvFile_success() { + public void execute_importCsvFile_success() throws IOException { List readOnlyCardFolders = getTypicalCardFolders(); - model.importCardFolders(); + Model expectedModel = new ModelManager(getTypicalCardFolders(), new UserPrefs()); + model.setTestCsvPath(); + model.importCardFolders(new CsvFile(TYPICAL_CARD_FOLDER)); + + assertEqual } } From fa05b57d06be6a7ff229d117e328ebe95f561e24 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 23:31:02 +0800 Subject: [PATCH 36/59] implement check export file correct test --- .../seedu/address/storage/CsvFileTest.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/test/java/seedu/address/storage/CsvFileTest.java b/src/test/java/seedu/address/storage/CsvFileTest.java index 613a9f3252bc..54bcbcf2f815 100644 --- a/src/test/java/seedu/address/storage/CsvFileTest.java +++ b/src/test/java/seedu/address/storage/CsvFileTest.java @@ -1,15 +1,19 @@ -package seedu.address.logic.commands; +package seedu.address.storage; import org.junit.Test; -import org.junit.Assert; +import static org.junit.Assert.assertEquals; import seedu.address.logic.CommandHistory; import seedu.address.model.*; import seedu.address.storage.csvmanager.CsvFile; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import static seedu.address.storage.csvmanager.CsvManager.DEFAULT_TEST_PATH; import static seedu.address.testutil.TypicalCards.getTypicalCardFolders; public class CsvFileTest { @@ -17,18 +21,28 @@ public class CsvFileTest { private Model model = new ModelManager(new ArrayList(), new UserPrefs()); private CommandHistory commandHistory = new CommandHistory(); private final String TYPICAL_CARD_FOLDER = "Typical Cards.csv"; + private final String TYPICAL_CARD_FOLDER_TEST = "Typical Cards test.csv"; public CsvFileTest() throws IOException { + model.setTestCsvPath(); } @Test - public void execute_importCsvFile_success() throws IOException { - List readOnlyCardFolders = getTypicalCardFolders(); + public void execute_importCsvFile_correctFormat() throws IOException { Model expectedModel = new ModelManager(getTypicalCardFolders(), new UserPrefs()); - model.setTestCsvPath(); model.importCardFolders(new CsvFile(TYPICAL_CARD_FOLDER)); - assertEqual + assertEquals(model, expectedModel); + } + + @Test + void execute_exportCsvFile_correctFile() throws IOException { + model.exportCardFolders(new ArrayList<>(Arrays.asList(1))); + File testFile = new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER_TEST); + assert(testFile.exists()); + byte[] f1 = Files.readAllBytes(new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER); + byte[] f2 = Files.readAllBytes(new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER_TEST); + assertEquals(f1,f2); } } From c897dca7bab6915c305f6f329a1fb47e5bef69b5 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 23:33:08 +0800 Subject: [PATCH 37/59] add default file path const variable --- .../java/seedu/address/storage/csvmanager/CsvManager.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 9f26264dbb4a..37f31f676998 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -19,6 +19,8 @@ public class CsvManager implements CsvCommands { private static final String CARD_HEADERS = "Question,Answer,Hints"; private String defaultPath; private boolean setTestDefaultPath = false; + public static final String DEFAULT_TEST_PATH = "./test/data/CsvCardFolderTest"; + private static final String DEFAULT_FILE_PATH = "./"; public CsvManager() throws IOException { @@ -56,15 +58,15 @@ public void writeFoldersToCsv(List cardFolders) throws IOExc } public static String getFilePathAsString(CsvFile csvFile) throws IOException { - return new File("./" + csvFile.filename).getCanonicalPath(); + return new File(DEFAULT_FILE_PATH + csvFile.filename).getCanonicalPath(); } public static String getDefaultFilePath() throws IOException { - return new File("./").getCanonicalPath(); + return new File(DEFAULT_FILE_PATH).getCanonicalPath(); } public void setTestDefaultPath() throws IOException { - defaultPath = new File("./test/data/").getCanonicalPath(); + defaultPath = new File(DEFAULT_TEST_PATH).getCanonicalPath(); setTestDefaultPath = true; } From ac566c9505ff0acef945b5780f4b4579a2c9d10e Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Wed, 27 Mar 2019 23:40:27 +0800 Subject: [PATCH 38/59] add new CsvManager not initliazed exception --- .../java/seedu/address/model/ModelManager.java | 17 +++++++++++++++-- .../Exceptions/CsvManagerNotInitialized.java | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seedu/address/storage/csvmanager/Exceptions/CsvManagerNotInitialized.java diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 615cb1106d48..eb03fc632fa4 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -26,6 +26,8 @@ import seedu.address.commons.core.LogsCenter; import seedu.address.commons.core.index.Index; import seedu.address.commons.util.InvalidationListenerManager; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.logic.parser.Parser; import seedu.address.model.card.Answer; import seedu.address.model.card.Card; import seedu.address.model.card.exceptions.CardNotFoundException; @@ -61,12 +63,20 @@ public class ModelManager implements Model { private int numAnsweredCorrectly = 0; // Export related - private CsvManager csvManager = new CsvManager(); + private CsvManager csvManager; + { + try { + csvManager = new CsvManager(); + } catch (IOException e) { + csvManager = null; + logger.warning("Unable to carry out import and export of card folders"); + } + } /** * Initializes a ModelManager with the given cardFolders and userPrefs. */ - public ModelManager(List cardFolders, ReadOnlyUserPrefs userPrefs) throws IOException { + public ModelManager(List cardFolders, ReadOnlyUserPrefs userPrefs) { super(); requireAllNonNull(cardFolders, userPrefs); @@ -482,6 +492,9 @@ public boolean equals(Object obj) { //=========== Export / Import card folders ======================================================================== @Override public void exportCardFolders(List cardFolderExports) throws IOException { + if (csvManager == null) { + throw new CommandException() + } List cardFolders = returnValidCardFolders(cardFolderExports); csvManager.writeFoldersToCsv(cardFolders); } diff --git a/src/main/java/seedu/address/storage/csvmanager/Exceptions/CsvManagerNotInitialized.java b/src/main/java/seedu/address/storage/csvmanager/Exceptions/CsvManagerNotInitialized.java new file mode 100644 index 000000000000..ce4be1e69cee --- /dev/null +++ b/src/main/java/seedu/address/storage/csvmanager/Exceptions/CsvManagerNotInitialized.java @@ -0,0 +1,8 @@ +package seedu.address.storage.csvmanager.Exceptions; + +public class CsvManagerNotInitialized extends Exception { + + public CsvManagerNotInitialized(String message) { + super(message); + } +} From c0b37c932f9f304d31367b1b1b5bae56431baf3b Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 28 Mar 2019 00:04:22 +0800 Subject: [PATCH 39/59] change implementation of IOException for csv manager --- src/main/java/seedu/address/MainApp.java | 2 +- .../java/seedu/address/commons/core/Messages.java | 1 + .../address/logic/commands/ExportCommand.java | 4 ++++ src/main/java/seedu/address/model/Model.java | 3 ++- .../java/seedu/address/model/ModelManager.java | 8 +++++--- .../address/logic/commands/AddCommandTest.java | 13 +++++-------- .../address/logic/commands/ExportCommandTest.java | 4 +++- .../java/seedu/address/storage/CsvFileTest.java | 14 +++++++++----- 8 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index c2f20b02ee4b..6591e9984ac4 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -94,7 +94,7 @@ public void init() throws Exception { * The data from the sample card folder will be used instead if {@code storage}'s card folder is not found, * or an empty card folder will be used instead if errors occur when reading {@code storage}'s card folder. */ - Model initModelManager(boolean withSample, Storage storage, ReadOnlyUserPrefs userPrefs) throws IOException { + Model initModelManager(boolean withSample, Storage storage, ReadOnlyUserPrefs userPrefs) { List initialCardFolders; if (withSample) { diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index c0638a2fa420..cb331191d334 100755 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -28,5 +28,6 @@ public class Messages { public static final String MESSAGE_INVALID_COMMAND_OUTSIDE_FOLDER = "Command can only be executed in folder"; public static final String MESSAGE_INVALID_COMMAND_INSIDE_FOLDER = "Command can only be executed in home directory"; + public static final String MESSAGE_CSV_MANAGER_NOT_INITIALIZED = "Unable to carry out import and export commands"; } diff --git a/src/main/java/seedu/address/logic/commands/ExportCommand.java b/src/main/java/seedu/address/logic/commands/ExportCommand.java index d0abd7fef4e6..3b7075a84cc0 100644 --- a/src/main/java/seedu/address/logic/commands/ExportCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExportCommand.java @@ -4,10 +4,12 @@ import java.util.List; +import seedu.address.commons.core.Messages; import seedu.address.logic.CommandHistory; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.CardFolderNotFoundException; import seedu.address.model.Model; +import seedu.address.storage.csvmanager.Exceptions.CsvManagerNotInitialized; /** @@ -44,6 +46,8 @@ public CommandResult execute(Model model, CommandHistory history) throws Command throw new CommandException(MESSAGE_MISSING_CARD_FOLDERS + e.getMessage()); } catch (IOException e) { throw new CommandException(MESSAGE_FILE_OPS_FAILURE); + } catch (CsvManagerNotInitialized e) { + throw new CommandException(Messages.MESSAGE_CSV_MANAGER_NOT_INITIALIZED); } return new CommandResult(MESSAGE_SUCCESS); } diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index e490abd801f2..cbfc27e577cd 100755 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -16,6 +16,7 @@ import seedu.address.storage.csvmanager.CardFolderExport; import seedu.address.storage.csvmanager.CsvFile; import seedu.address.storage.csvmanager.CsvManager; +import seedu.address.storage.csvmanager.Exceptions.CsvManagerNotInitialized; /** * The API of the Model component. @@ -240,7 +241,7 @@ public interface Model extends Observable { */ boolean checkIfCardAlreadyAnswered(); - void exportCardFolders(List cardFolderExports) throws IOException; + void exportCardFolders(List cardFolderExports) throws IOException, CsvManagerNotInitialized; void importCardFolders(CsvFile csvFile) throws IOException; diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index eb03fc632fa4..99987e39c0f6 100755 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -24,6 +24,7 @@ import javafx.collections.transformation.FilteredList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; +import seedu.address.commons.core.Messages; import seedu.address.commons.core.index.Index; import seedu.address.commons.util.InvalidationListenerManager; import seedu.address.logic.commands.exceptions.CommandException; @@ -33,6 +34,7 @@ import seedu.address.model.card.exceptions.CardNotFoundException; import seedu.address.storage.csvmanager.CsvFile; import seedu.address.storage.csvmanager.CsvManager; +import seedu.address.storage.csvmanager.Exceptions.CsvManagerNotInitialized; /** * Represents the in-memory model of the card folder data. @@ -103,7 +105,7 @@ public ModelManager(List cardFolders, ReadOnlyUserPrefs user inFolder = true; } - public ModelManager(String newFolderName) throws IOException { + public ModelManager(String newFolderName) { this(Collections.singletonList(new CardFolder(newFolderName)), new UserPrefs()); } @@ -491,9 +493,9 @@ public boolean equals(Object obj) { //=========== Export / Import card folders ======================================================================== @Override - public void exportCardFolders(List cardFolderExports) throws IOException { + public void exportCardFolders(List cardFolderExports) throws IOException, CsvManagerNotInitialized { if (csvManager == null) { - throw new CommandException() + throw new CsvManagerNotInitialized(Messages.MESSAGE_CSV_MANAGER_NOT_INITIALIZED); } List cardFolders = returnValidCardFolders(cardFolderExports); csvManager.writeFoldersToCsv(cardFolders); diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 1d7610e1d186..281bae1d48ae 100755 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -174,20 +174,12 @@ public void endTestSession() { throw new AssertionError("This method should not be called."); } -<<<<<<< HEAD -======= @Override public boolean testNextCard() { throw new AssertionError("This method should not be called."); } - @Override - public List returnValidCardFolders(Set cardFolers) { - throw new AssertionError("This method should not be called."); - } ->>>>>>> 2ccca9a576e1bced2ba5fa99a6eb573589bcdc5d - public boolean markAttemptedAnswer(Answer attemptedAnswer) { throw new AssertionError("This method should not be called."); } @@ -213,6 +205,11 @@ public void importCardFolders(CsvFile csvFile) throws IOException { throw new AssertionError("This method should not be called."); } + @Override + public void setTestCsvPath() throws IOException { + throw new AssertionError("This method should not be called."); + } + @Override public boolean hasCard(Card card) { throw new AssertionError("This method should not be called."); diff --git a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java index 4c387921b4ee..e6ab83016929 100644 --- a/src/test/java/seedu/address/logic/commands/ExportCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ExportCommandTest.java @@ -47,14 +47,16 @@ public void execute_exportSingleValidCardFolderIndex_success() throws Exception assertEquals(ExportCommand.MESSAGE_SUCCESS, commandResult.getFeedbackToUser()); } + /* @After public void deleteExportFile() { - File file = new File("./Sample Folder.csv"); + File file = new File("./Typical Cards.csv"); if (file.exists()) { boolean isDeleted = file.delete(); assert (isDeleted); } } + */ @Test public void execute_exportSingleInvalidCardFolderIndex_failure() throws Exception { diff --git a/src/test/java/seedu/address/storage/CsvFileTest.java b/src/test/java/seedu/address/storage/CsvFileTest.java index 54bcbcf2f815..458cf7a967ac 100644 --- a/src/test/java/seedu/address/storage/CsvFileTest.java +++ b/src/test/java/seedu/address/storage/CsvFileTest.java @@ -5,10 +5,12 @@ import seedu.address.logic.CommandHistory; import seedu.address.model.*; import seedu.address.storage.csvmanager.CsvFile; +import seedu.address.storage.csvmanager.Exceptions.CsvManagerNotInitialized; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -33,16 +35,18 @@ public void execute_importCsvFile_correctFormat() throws IOException { Model expectedModel = new ModelManager(getTypicalCardFolders(), new UserPrefs()); model.importCardFolders(new CsvFile(TYPICAL_CARD_FOLDER)); - assertEquals(model, expectedModel); + assertEquals(model.getCardFolders(), expectedModel.getCardFolders()); } @Test - void execute_exportCsvFile_correctFile() throws IOException { + public void execute_exportCsvFile_correctFile() throws IOException, CsvManagerNotInitialized { model.exportCardFolders(new ArrayList<>(Arrays.asList(1))); File testFile = new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER_TEST); assert(testFile.exists()); - byte[] f1 = Files.readAllBytes(new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER); - byte[] f2 = Files.readAllBytes(new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER_TEST); - assertEquals(f1,f2); + byte[] f1 = Files.readAllBytes(Paths.get(new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER) + .getCanonicalPath())); + byte[] f2 = Files.readAllBytes(Paths.get(new File(DEFAULT_TEST_PATH + TYPICAL_CARD_FOLDER_TEST) + .getCanonicalPath())); + assertEquals(f1, f2); } } From 7f29dd90952c2c34e7b55492f6a8c69accfff843 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 28 Mar 2019 20:15:07 +0800 Subject: [PATCH 40/59] implement method for export option --- .../java/seedu/address/storage/csvmanager/CsvManager.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 4e4728671a5b..6a9a948abebb 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -70,7 +70,7 @@ public CardFolder readFoldersToCsv(CsvFile csvFile) throws IOException, CommandE private Card buildCard(String[] cardValues) { Question question = new Question(cardValues[0]); Answer answer = new Answer(cardValues[1]); - String[] hintArray = Arrays.copyOfRange(cardValues, 2, cardValues.length); + String hint = cardValues[cardValues.length - 1]; Set hintSet = Arrays.stream(hintArray).map(Hint::new).collect(Collectors.toSet()); Card card = new Card(question, answer, new Score(0, 0), hintSet); return card; @@ -138,8 +138,10 @@ private String getCardString(Card card) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(card.getQuestion() + COMMA_DELIMITTER) .append(card.getAnswer() + COMMA_DELIMITTER); - card.getHints().forEach(hint -> stringBuilder.append(hint.hintName) + card.getOptions().forEach(option -> stringBuilder.append(option.optionValue) .append(COMMA_DELIMITTER)); + card.getHints().forEach(hint -> stringBuilder.append(hint.hintName) + .append(COMMA_DELIMITTER)); return stringBuilder.toString(); } } From 684a84b35c5fa21e4a9c524c756b42204f73ca34 Mon Sep 17 00:00:00 2001 From: Yi Chong Ong Date: Thu, 28 Mar 2019 20:28:42 +0800 Subject: [PATCH 41/59] add parseOptions method for export --- .../storage/csvmanager/CsvManager.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java index 6a9a948abebb..160653a21416 100644 --- a/src/main/java/seedu/address/storage/csvmanager/CsvManager.java +++ b/src/main/java/seedu/address/storage/csvmanager/CsvManager.java @@ -8,10 +8,7 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.CardFolder; import seedu.address.model.ReadOnlyCardFolder; -import seedu.address.model.card.Answer; -import seedu.address.model.card.Card; -import seedu.address.model.card.Question; -import seedu.address.model.card.Score; +import seedu.address.model.card.*; import seedu.address.model.hint.Hint; @@ -71,8 +68,10 @@ private Card buildCard(String[] cardValues) { Question question = new Question(cardValues[0]); Answer answer = new Answer(cardValues[1]); String hint = cardValues[cardValues.length - 1]; - Set hintSet = Arrays.stream(hintArray).map(Hint::new).collect(Collectors.toSet()); - Card card = new Card(question, answer, new Score(0, 0), hintSet); + Set