Skip to content

Commit

Permalink
Fix import bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuLeYao committed Apr 10, 2023
1 parent 40d8066 commit ab06333
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 27 deletions.
3 changes: 2 additions & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ Format: Click on `Delivery Job System` in menu bar > `Import Jobs` > select a fi
* File **must be a CSV file**.
* There must be a header row as the first row will be skipped when file is parsed.
* These columns `Recipient` `Sender` `Delivery date` `Delivery slot` `Price` `Description` `Recipient` `Recipient's Name` `Recipient's Phone` `Recipient's Email` `Recipient's Address` `Recipient's Tag` `Sender's Name` `Sender's Phone` `Sender's Email` `Sender's Address` `Sender's Tag` must exist.
* The optional details may be empty cells. e.g. `Sender's Tag` may be empty.
* The optional details may be filled in with "na". e.g. `Sender's Tag` may be "na".
* Tags should be input with a spacing between each tag. e.g. "Customer Nearby", to tag as 2 tags: customer and nearby.
* If recipient/ sender does not already exist in customer address book i.e. new customer, recipient/ sender will also be added into the address book.

### 2.3. Listing all jobs : `list_job`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public boolean isShowUnschedule() {
}

/**
* Checks if compelted job window is shown
* Checks if completed job window is shown
*/
public boolean isShowComplete() {
return showComplete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ImportDeliveryJobCommand extends DeliveryJobCommand {
public static final String COMMAND_WORD = "import_job";
public static final String MESSAGE_EMPTY_FILE = "File is empty";
public static final String MESSAGE_SUCCESS = "File is imported";
public static final String MESSAGE_WRONG_FILE = "File type is unsupported. Please upload a CSV file and check if " +
"file extension is named .csv";

private final File toAdd;

Expand All @@ -40,6 +42,9 @@ public CommandResult execute(Model model) throws CommandException, ParseExceptio
requireNonNull(model);
List<Person> listOfCustomers = new ArrayList<>();

if (!getFileExtensions(toAdd).equals("csv")) {
throw new CommandException(MESSAGE_WRONG_FILE);
}
if (toAdd.length() == 0) {
throw new CommandException(MESSAGE_EMPTY_FILE);
}
Expand All @@ -55,10 +60,25 @@ public CommandResult execute(Model model) throws CommandException, ParseExceptio
}

for (int i = 0; i < listOfAddDeliveryJob.size(); i++) {
model.addDeliveryJob(listOfAddDeliveryJob.get(i));
if (!model.hasDeliveryJob(listOfAddDeliveryJob.get(i))) {
model.addDeliveryJob(listOfAddDeliveryJob.get(i));
}
}

return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
return new CommandResult(MESSAGE_SUCCESS);
}

/** Gets type of file extension.
*
* @param file File that extension is checked
* @return String file extension name
*/
public String getFileExtensions(File file) {
String fileName = file.getName();
String extension = "";
int i = fileName.lastIndexOf(".");
extension = fileName.substring(i + 1, fileName.length());
return extension;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.util.Set;

import seedu.address.logic.commands.jobs.AddDeliveryJobCommand;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.jobs.DeliveryJob;
Expand All @@ -26,6 +27,16 @@
public class ImportDeliveryJobCommandParser {

public static final String MESSAGE_MISSING_ELEMENT_IN_IMPORT = "Missing element in import";
public static final String MESSAGE_EMPTY_FILE = "File is empty";
public static final String MESSAGE_WRONG_FILE = "File type is unsupported. Please upload a CSV file and check if " +
"file extension is named .csv";

public static final String MESSAGE_WRONG_DELIMITER = "Wrong delimiter used, please use comma as delimiter.";
public static final String MESSAGE_MISSING_HEADER = "Missing header in import. " +
"Header needs to consist in this order of Recipient ID, Sender ID, Delivery date, Delivery slot, Price, " +
"Recipient ID, Recipient's Name, Recipient's Phone, Recipient's Email, Recipient's Address, " +
"Recipient's Tag, Sender's ID, Sender's Name, Sender's Phone, Sender's Email, Sender's Address," +
" Sender's Tag." ;

/**
* Parses the given CSV File in the context of the ImportCommand
Expand All @@ -37,32 +48,32 @@ public class ImportDeliveryJobCommandParser {
*/
public static List<DeliveryJob> parse(File file, List<Person> listOfCustomers)
throws ParseException, FileNotFoundException {
try (Scanner sc = new Scanner(file)) {
Scanner sc = new Scanner(file);
if (sc.hasNextLine()) {
List<DeliveryJob> listOfAddDeliveryJob = new ArrayList<>();
sc.nextLine();
String header = sc.nextLine();
String[] arrOfHeader = header.split(",");
if (arrOfHeader.length == 0) {
throw new ParseException(MESSAGE_EMPTY_FILE);
} else if (!getFileExtensions(file).equals("csv")) {
throw new ParseException(MESSAGE_WRONG_FILE);
} else if (arrOfHeader.length == 1) {
throw new ParseException(MESSAGE_WRONG_DELIMITER);
} else if (!headerValidity(arrOfHeader)) {
throw new ParseException(MESSAGE_MISSING_HEADER);
}
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] arrOfStr = line.split(",");
if (arrOfStr.length < 12) {
throw new ParseException(
String.format(MESSAGE_MISSING_ELEMENT_IN_IMPORT, AddDeliveryJobCommand.MESSAGE_USAGE));
if (arrOfStr.length < 17) {
throw new ParseException(MESSAGE_MISSING_ELEMENT_IN_IMPORT);
}
String sid = arrOfStr[0];
String rid = arrOfStr[1];
String ded = arrOfStr[2];
String des = arrOfStr[3];
String ear = arrOfStr[4];
String description = arrOfStr[5];
Person recipient = recipientOrSender(arrOfStr, 6);
Person sender = recipientOrSender(arrOfStr, 12);
listOfCustomers.add(sender);
listOfCustomers.add(recipient);

DeliveryJob job = new DeliveryJob(rid, sid, ded, des, ear, description);
listOfAddDeliveryJob.add(job);
addJobs(arrOfStr, listOfCustomers, listOfAddDeliveryJob);
}
sc.close();
return listOfAddDeliveryJob;
} else {
throw new ParseException(MESSAGE_EMPTY_FILE);
}
}

Expand All @@ -83,9 +94,78 @@ public static Person recipientOrSender(String[] arrOfStr, int index) throws Pars
Address address = new Address(arrOfStr[index + 4]);
String[] arrOfTags = arrOfStr[index + 5].split(" ");
Set<Tag> tags = ParserUtil.parseTags(Arrays.asList(arrOfTags));
Person person = new Person(personID, name, phone, email, address, tags);
Person person;
if (arrOfStr[0].equals("na")) {
Set<Tag> tagSet = new HashSet<>();
person = new Person(name, phone, email, address, tags);
} else {
person = new Person(personID, name, phone, email, address, tags);
}
return person;
}

/** Ensures Header elements are all present and in right order.
*
* @param arrOfHeader array of header elements
* @return Boolean whether header elements are correct
*/
public static boolean headerValidity(String[] arrOfHeader) {
if (!arrOfHeader[0].equals("Recipient ID") || !arrOfHeader[1].equals("Sender ID")
|| !arrOfHeader[2].equals("Delivery date") || !arrOfHeader[3].equals("Delivery slot")
|| !arrOfHeader[4].equals("Price")
|| !arrOfHeader[5].equals("Recipient ID") || !arrOfHeader[6].equals("Recipient Name")
|| !arrOfHeader[7].equals("Recipient Phone") || !arrOfHeader[8].equals("Recipient Email")
|| !arrOfHeader[9].equals("Recipient Address") || !arrOfHeader[10].equals("Recipient Tag")
|| !arrOfHeader[11].equals("Sender ID")
|| !arrOfHeader[12].equals("Sender Name") || !arrOfHeader[13].equals("Sender Phone")
|| !arrOfHeader[14].equals("Sender Email") || !arrOfHeader[15].equals("Sender Address")
|| !arrOfHeader[16].equals("Sender Tag")) {
return false;
}
return true;
}

/** Gets type of file extension.
*
* @param file File that extension is checked
* @return String file extension name
*/
public static String getFileExtensions(File file) {
String fileName = file.getName();
String extension = "";
int i = fileName.lastIndexOf(".");
extension = fileName.substring(i + 1, fileName.length());
return extension;
}


/** Adds new delivery job to list of jobs to be imported in.
*
* @param arrOfStr array of job and its customers details
* @param listOfCustomers list of job's customers to be checked if
* exist and added into address book.
* @param listOfAddDeliveryJob list of delivery jobs to be
* imported
*/
public static void addJobs(String[] arrOfStr, List<Person> listOfCustomers,
List<DeliveryJob> listOfAddDeliveryJob) throws ParseException {
String sid = arrOfStr[0];
String rid = arrOfStr[1];
String ded = arrOfStr[2];
String des = arrOfStr[3];
String ear = arrOfStr[4];
Person recipient = recipientOrSender(arrOfStr, 5);
Person sender = recipientOrSender(arrOfStr, 11);
listOfCustomers.add(sender);
listOfCustomers.add(recipient);

if (ded.equals("na") && des.equals("na")) {
DeliveryJob job = new DeliveryJob(rid, sid, ear, "");
}

DeliveryJob job = new DeliveryJob(rid, sid, ded, des, ear, "");
listOfAddDeliveryJob.add(job);
}

}

9 changes: 6 additions & 3 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,14 @@ private void handleDeliveryJobSystemImportAction() {
fileChooser.setTitle("Open Files");
File selectedFile = fileChooser.showOpenDialog(new Stage());

logic.execute(new ImportDeliveryJobCommand(selectedFile));
CommandResult commandResult = logic.execute(new ImportDeliveryJobCommand(selectedFile));
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());
} catch (ParseException | CommandException e) {
logger.warning("[Event] importDeliveryJob" + e.getMessage());
logger.warning("[Event] importDeliveryJob " + e.getMessage());
resultDisplay.setFeedbackToUser(e.getMessage());
} catch (FileNotFoundException | IllegalArgumentException e) {
logger.warning("[Event] importDeliveryJob" + e.getMessage());
logger.warning("[Event] importDeliveryJob " + e.getMessage());
resultDisplay.setFeedbackToUser(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package seedu.address.logic.parser.jobs;

public class ImportDeliveryJobParserTest {

}

0 comments on commit ab06333

Please sign in to comment.