Skip to content

Commit

Permalink
[Complete] Import Interviewer's Data from .csv file (#67)
Browse files Browse the repository at this point in the history
* Import from excel function

* Adds implementation for ExcelReader

* debug importExcel classes

* Check style for Import branch

* Reads csv file correctly

* Finished up import interviewers feature

* Integrated with Schedule class and removed ScheduleStub

* Handles import exceptions

* Corrected Styling

* Adds test for Import commands

* Corrects styling for Import Tests

* Rename ExcelReader to CsvReader
  • Loading branch information
mirozo authored and ChrisKheng committed Oct 17, 2019
1 parent 53ad58f commit 624f005
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 83 deletions.
52 changes: 36 additions & 16 deletions src/main/java/seedu/address/logic/commands/ImportCommand.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,63 @@
package seedu.address.logic.commands;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import seedu.address.model.Model;
import seedu.address.model.util.ExcelReader;
import seedu.address.model.Schedule;
import seedu.address.model.util.CsvReader;


/**
* Import excel file containing interviewee's information.
* Import csv file containing interviewer's/ interviewers's information.
*/
public class ImportCommand extends Command {

public static final String COMMAND_WORD = "import";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Import excel file containing "
+ "interviewee's information.\n"
+ "Example: " + COMMAND_WORD + "<excelFilePath>";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Import .csv file containing "
+ "interviewer or interviewee's information.\n"
+ "Example: " + COMMAND_WORD + " interviewer " + "<csvFilePath>";

public static final String SHOWING_MESSAGE = "Data imported successfully.";
public static final String SUCCESS_MESSAGE = "Data imported successfully.";
public static final String MESSAGE_NOT_IMPLEMENTED_YET = "Command not implemented yet";
public static final String INCORRECT_FORMAT = "Data is in incorrect format. Please refer to the "
+ "User Guide for the supported format";
public static final String FILE_DOES_NOT_EXIST = "Target file does not exist. Please ensure that "
+ "the file path is correct.";

private String filePath;
private String type;

public ImportCommand(String filePath) {
this.filePath = filePath;
public ImportCommand(String args) {
String[] strings = args.split(" ");
this.type = strings[0];
this.filePath = strings[1];
}

@Override
public CommandResult execute(Model model) {
String result = SHOWING_MESSAGE;
try {
ExcelReader excelReader = new ExcelReader(filePath);
result = excelReader.translate();

try {
if (this.type.equals("interviewer")) {
ArrayList<Schedule> schedules;
CsvReader csvReader = new CsvReader(filePath);
schedules = csvReader.read();
model.setSchedulesList(schedules);
return new CommandResult(SUCCESS_MESSAGE, false, false);
} else if (this.type.equals("interviewee")) {
return new CommandResult(MESSAGE_NOT_IMPLEMENTED_YET, false, false);
} else {
return new CommandResult(MESSAGE_USAGE, false, false);
}
} catch (FileNotFoundException e) {
return new CommandResult(FILE_DOES_NOT_EXIST, false, false);
} catch (IOException e) {
e.printStackTrace();
result = "File is in wrong format";
return new CommandResult("Failed", false, false);
} catch (ArrayIndexOutOfBoundsException e) {
return new CommandResult(INCORRECT_FORMAT, false, false);
}

return new CommandResult(result, false, false);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();


case ImportCommand.COMMAND_WORD:
return new ImportCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.ImportCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand All @@ -20,7 +19,7 @@ public ImportCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportCommand.MESSAGE_USAGE));
}
return new ImportCommand(trimmedArgs);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.address.model;

import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

Expand Down Expand Up @@ -41,7 +40,7 @@ public interface Model {
/**
* Replaces schedule data with the data in {@code schedule}.
*/
void setSchedulesList(LinkedList<Schedule> schedulesList);
void setSchedulesList(List<Schedule> schedulesList);

/** Returns the schedulesList **/
List<Schedule> getSchedulesList();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ public void setAddressBookFilePath(Path addressBookFilePath) {
//=========== Schedule ================================================================================
/**
* Replaces schedule data with the data in {@code schedule}.
* @param list
*/
@Override
public void setSchedulesList(LinkedList<Schedule> list) {
public void setSchedulesList(List<Schedule> list) {
schedulesList.clear();
schedulesList.addAll(cloneSchedulesList(list));
logger.fine("Schedules list is reset");
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/seedu/address/model/util/CsvReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package seedu.address.model.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;

import seedu.address.model.Schedule;

/**
* Helper class to read .csv files (Comma separated values).
*/
public class CsvReader {
private String filePath;

/**
* Constructor for CsvReader object to read from excel.
* @param filePath
*/
public CsvReader(String filePath) {
this.filePath = filePath;
}

/**
* Reads from excel and returns the corresponding string.
* @return String
* @throws IOException if input file is not found.
*/
public ArrayList<Schedule> read() throws IOException {

BufferedReader csvReader = new BufferedReader(new FileReader(filePath));
String firstLine = csvReader.readLine();
int numberOfDays = getValue(firstLine.split(",")[0]);
int numberOfColumns = getValue(firstLine.split(",")[1]) + 1;
ArrayList<Schedule> schedules = new ArrayList<>();
csvReader.readLine(); //removes next line
for (int i = 0; i < numberOfDays; i++) {
LinkedList<LinkedList<String>> table = new LinkedList<>();
String row;
boolean firstEncounter = true;
while ((row = csvReader.readLine()) != null) {
String[] data = row.split(",", -1);
if (data[0].equals("")) {
if (firstEncounter) {
firstEncounter = false;
} else {
break;
}
} else if (numberOfColumns != 0) {
LinkedList<String> dataRow = new LinkedList<>();
for (int j = 0; j < numberOfColumns; j++) {
String element = data[j];
dataRow.add(element);
}
table.add(dataRow);
}
}
String date = table.getFirst().getFirst();
schedules.add(new Schedule(date, table));
}
csvReader.close();
return schedules;
}

private static int getValue(String element) {
String[] strings = element.split("= ");
return Integer.parseInt(strings[1]);
}

private boolean fileExists() {
File file = new File(this.filePath);
return file.exists();
}
}
60 changes: 0 additions & 60 deletions src/main/java/seedu/address/model/util/ExcelReader.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/test/data/ImportsTest/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Days = 3,Interviewers = 10,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
9/10/2019 (Wed),Deparment A - Person A,Deparment A - Person B,Deparment B - Person C,Deparment B - Person D,Department B - Person E,Deparment C - Person F,Deparment C - Person G,Deparment D - Person H,Deparment E - Person I,Deparment E - Person J
6:00pm - 6:30pm,1,1,,,,,,,,
6:30pm - 7:00pm,0,1,,,,,,,,
7:00pm - 7:30 pm,,,,,,,,,,
7:30pm - 8:00 pm,,,,,,,,,,
8:00pm - 8:30 pm,,,,,,,,,,
8:30pm - 9:00pm,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
10/10/2019 (Thur),Deparment A - Person A,Deparment A - Person B,Deparment B - Person C,Deparment B - Person D,Department B - Person E,Deparment C - Person F,Deparment C - Person G,Deparment D - Person H,Deparment E - Person I,Deparment E - Person J
6:00pm - 6:30pm,,,,,,,,,,
6:30pm - 7:00pm,,,,,,,,,,
7:00pm - 7:30 pm,,,,,,,,,,
7:30pm - 8:00 pm,,,,,,,,,,
8:00pm - 8:30 pm,,,,,,,,,,
8:30pm - 9:00pm,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
11/10/2019 (Fri),Deparment A - Person A,Deparment A - Person B,Deparment B - Person C,Deparment B - Person D,Department B - Person E,Deparment C - Person F,Deparment C - Person G,Deparment D - Person H,Deparment E - Person I,Deparment E - Person J
6:00pm - 6:30pm,,,,,,,,,,
6:30pm - 7:00pm,,,,,,,,,,
7:00pm - 7:30 pm,,,,,,,,,,
7:30pm - 8:00 pm,,,,,,,,,,
8:00pm - 8:30 pm,,,,,,,,,,
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setGuiSettings(GuiSettings guiSettings) {
}

@Override
public void setSchedulesList(LinkedList<Schedule> schedulesList) {
public void setSchedulesList(List<Schedule> schedulesList) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static void assertCommandSuccess(Command command, Model actualModel, Stri
assertCommandSuccess(command, actualModel, expectedCommandResult, expectedModel);
}


/**
* Executes the given {@code command}, confirms that <br>
* - a {@code CommandException} is thrown <br>
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/seedu/address/logic/commands/ImportCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic.commands;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;

import org.junit.jupiter.api.Test;

import seedu.address.model.Model;
import seedu.address.model.ModelManager;


public class ImportCommandTest {
private static final String SUCCESS_FILE_PATH = "src/test/data/ImportsTest/test.csv";
private static final String INVALID_FILE_PATH = "invalid/file/path";
private static final String INTERVIEWER = "interviewer";
private Model model = new ModelManager();

@Test
public void interviewerImportCommandSuccess() {
ImportCommand importCommand = new ImportCommand(INTERVIEWER + " " + SUCCESS_FILE_PATH);
CommandResult expectedCommandResult = new CommandResult(ImportCommand.SUCCESS_MESSAGE, false, false);
Model expectedModel = model;
assertCommandSuccess(importCommand, model, expectedCommandResult, expectedModel);
}

@Test
public void interviewerImportCommandFailure() {
ImportCommand importCommand = new ImportCommand(INTERVIEWER + " " + INVALID_FILE_PATH);
CommandResult expectedCommandResult = new CommandResult(ImportCommand.FILE_DOES_NOT_EXIST, false, false);
assertEquals(importCommand.execute(model), expectedCommandResult);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package seedu.address.logic.parser;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.ImportCommand;



public class ImportCommandParserTest {
private ImportCommandParser parser = new ImportCommandParser();
@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportCommand.MESSAGE_USAGE));
}
}

0 comments on commit 624f005

Please sign in to comment.