Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103T-T09-2#120 from ncmathan/Update-im…
Browse files Browse the repository at this point in the history
…port

Update import command for developers and clients
  • Loading branch information
mingyu-wan authored Oct 31, 2023
2 parents 8171590 + a55a282 commit f61dc63
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROJECT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE;
import static seedu.address.storage.JsonSerializableAddressBook.MESSAGE_DUPLICATE_CLIENT;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand Down Expand Up @@ -51,7 +50,7 @@ public class AddClientCommand extends Command {
+ PREFIX_DOCUMENT + "google.com ";

public static final String MESSAGE_SUCCESS = "New client added: %1$s";
public static final String MESSAGE_DUPLICATE_DEVELOPER = "This client already exists in the address book";
public static final String MESSAGE_DUPLICATE_CLIENT = "This client already exists in the address book";

private final Client toAdd;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package seedu.address.logic.commands.imports;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ImportCommand;
import seedu.address.logic.commands.TabIndex;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.client.Client;
import seedu.address.model.developer.Developer;

import java.util.ArrayList;

import static java.util.Objects.requireNonNull;

public class ImportClientCommand extends Command {
public static final String COMMAND_WORD = "import-client";
//Name name, Phone phone, Email email, Address address, Role role, Set<String> projects,
// Name organisation, Document document

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Imports clients from csv file.\n"
+ "Column titles should follow this format strictly:\n"
+ "Name, Contact Number, Email, Address, Role, Organisation, Document, Projects";
public static final String MESSAGE_SUCCESS = "New client added: %1$s";
public static final String MESSAGE_DUPLICATE_CLIENT = " is a client that already exists in the address book\n";

private final ArrayList<Client> toAddList;

/**
* Creates an AddClientCommand to add the specified {@code Client}
*/
public ImportClientCommand(ArrayList<Client> clientList) {
requireNonNull(clientList);
for(Client i: clientList) {
requireNonNull(i);
}
toAddList = clientList;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
String output = "";
for(Client toAdd: toAddList) {
if (model.hasClient(toAdd)) {
output += toAdd.getName().fullName + MESSAGE_DUPLICATE_CLIENT;
}

model.addClient(toAdd);
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
output += "\n";
}
return new CommandResult(output, TabIndex.Client);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof ImportCommand)) {
return false;
}

ImportClientCommand otherImportCommand = (ImportClientCommand) other;
return toAddList.equals(otherImportCommand.toAddList);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAddList", toAddList)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package seedu.address.logic.commands.imports;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ImportCommand;
import seedu.address.logic.commands.TabIndex;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.developer.Developer;

import java.util.ArrayList;

import static java.util.Objects.requireNonNull;

public class ImportDeveloperCommand extends Command {
public static final String COMMAND_WORD = "import-developer";
//Name name, Phone phone, Email email, Address address, Role role, Set<String> projects,
// Salary salary, Date dateJoined, GithubId githubId, Rating rating

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Imports developers from csv file.\n"
+ "Column titles should follow this format strictly:\n"
+ "Name, Contact Number, Email, Address, Date Joined, Role, Salary, GithubId, Rating, Projects";
public static final String MESSAGE_SUCCESS = "New developer added: %1$s";
public static final String MESSAGE_DUPLICATE_DEVELOPER = " is a developer that already exists in the address book\n";

private final ArrayList<Developer> toAddList;

/**
* Creates an AddDeveloperCommand to add the specified {@code Developer}
*/
public ImportDeveloperCommand(ArrayList<Developer> developerList) {
requireNonNull(developerList);
for(Developer i: developerList) {
requireNonNull(i);
}
toAddList = developerList;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
String output = "";
for(Developer toAdd: toAddList) {
if (model.hasDeveloper(toAdd)) {
output += toAdd.getName().fullName + MESSAGE_DUPLICATE_DEVELOPER;
}

model.addDeveloper(toAdd);
output += String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
output += "\n";
}
return new CommandResult(output, TabIndex.Developer);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof ImportCommand)) {
return false;
}

ImportDeveloperCommand otherImportCommand = (ImportDeveloperCommand) other;
return toAddList.equals(otherImportCommand.toAddList);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAddList", toAddList)
.toString();
}
}
12 changes: 10 additions & 2 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
import seedu.address.logic.commands.edit.EditClientCommand;
import seedu.address.logic.commands.edit.EditDeveloperCommand;
import seedu.address.logic.commands.edit.EditProjectCommand;
import seedu.address.logic.commands.imports.ImportDeveloperCommand;
import seedu.address.logic.commands.imports.ImportClientCommand;
import seedu.address.logic.parser.add.AddClientCommandParser;
import seedu.address.logic.parser.add.AddDeveloperCommandParser;
import seedu.address.logic.parser.add.AddProjectCommandParser;
import seedu.address.logic.parser.edit.EditClientCommandParser;
import seedu.address.logic.parser.edit.EditDeveloperCommandParser;
import seedu.address.logic.parser.edit.EditProjectCommandParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.imports.ImportClientCommandParser;
import seedu.address.logic.parser.imports.ImportDeveloperCommandParser;
import seedu.address.logic.parser.find.FindClientCommandParser;
import seedu.address.logic.parser.find.FindDeveloperCommandParser;
import seedu.address.logic.parser.find.FindProjectCommandParser;
Expand Down Expand Up @@ -77,8 +81,12 @@ public Command parseCommand(String userInput) throws ParseException {
return new AddClientCommandParser().parse(arguments);
case AddProjectCommand.COMMAND_WORD:
return new AddProjectCommandParser().parse(arguments);
case ImportCommand.COMMAND_WORD:
return new ImportCommandParser().parse(arguments);

case ImportDeveloperCommand.COMMAND_WORD:
return new ImportDeveloperCommandParser().parse(arguments);
case ImportClientCommand.COMMAND_WORD:
return new ImportClientCommandParser().parse(arguments);

case EditDeveloperCommand.COMMAND_WORD:
return new EditDeveloperCommandParser().parse(arguments);
case EditClientCommand.COMMAND_WORD:
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public static Set<Project> parseProjects(Collection<String> projects) throws Par
}
return projectSet;
}

/**
* Parses {@code Collection<String> projects} and checks whether each String is the name of an existing project.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package seedu.address.logic.parser.imports;
import seedu.address.logic.commands.imports.ImportClientCommand;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.client.Client;
import seedu.address.model.client.Document;
import seedu.address.model.commons.Name;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Role;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_INVALID_FILE;

public class ImportClientCommandParser implements Parser<ImportClientCommand> {

@Override
public ImportClientCommand parse(String fileName) throws ParseException {
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
fileName = fileName.trim();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = "";
String splitBy = ",";

// Check if the CSV file contains valid column names
boolean isValid = checkColumnNames(br.readLine());
if (!isValid) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportClientCommand.MESSAGE_USAGE));
}

ArrayList<Client> toAddList = new ArrayList<>();
while ((line = br.readLine()) != null) {
String[] clientData = line.split(splitBy);

// Parse client data from CSV columns
Name name = ParserUtil.parseName(clientData[0]);
Phone phone = ParserUtil.parsePhone(clientData[1]);
Email email = ParserUtil.parseEmail(clientData[2]);
Address address = ParserUtil.parseAddress(clientData[3]);
Role role = ParserUtil.parseRole(clientData[4]);
Name organisation = ParserUtil.parseName(clientData[5]);
Document document = ParserUtil.parseDocument(clientData[6]);
ArrayList<String> projects = new ArrayList<>();
for(int i=7;i< clientData.length;i++) {
projects.add(clientData[i]);
}
Set<String> projectList = ParserUtil.parseProjectsWithCheck(projects);

// Create a Client object
Client client = new Client(name, phone, email, address, role, projectList, organisation, document);

// Add the client to the list
toAddList.add(client);
}

return new ImportClientCommand(toAddList);
} catch (FileNotFoundException ex) {
throw new ParseException(MESSAGE_INVALID_FILE);
} catch (IOException e) {
throw new ParseException("Error reading line from file " + fileName);
}
}

// Define a method to check if the CSV file contains valid column names
//Name name, Phone phone, Email email, Address address, Role role, Set<String> projects,
// Name organisation, Document document
private boolean checkColumnNames(String line) {
String[] columnNames = line.split(",");
return columnNames.length == 8 && columnNames[0].contains("Name") &&
columnNames[1].contains("Contact Number") && columnNames[2].contains("Email") &&
columnNames[3].contains("Address") && columnNames[4].contains("Role") &&
columnNames[5].contains("Organisation") && columnNames[6].contains("Document") &&
columnNames[7].contains("Projects");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package seedu.address.logic.parser;
package seedu.address.logic.parser.imports;

import seedu.address.logic.commands.ImportCommand;
import seedu.address.logic.commands.imports.ImportDeveloperCommand;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.commons.Date;
import seedu.address.model.commons.Name;
import seedu.address.model.developer.Developer;
import seedu.address.model.developer.GithubId;
import seedu.address.model.developer.Rating;
import seedu.address.model.person.Role;
import seedu.address.model.developer.Salary;
import seedu.address.model.person.*;
Expand All @@ -17,10 +22,10 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_INVALID_FILE;

public class ImportCommandParser implements Parser<ImportCommand>{
public class ImportDeveloperCommandParser implements Parser<ImportDeveloperCommand> {

@Override
public ImportCommand parse(String fileName) throws ParseException {
public ImportDeveloperCommand parse(String fileName) throws ParseException {
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
Expand All @@ -31,7 +36,7 @@ public ImportCommand parse(String fileName) throws ParseException {
String splitBy = ",";
boolean isValid = checkColumnNames(br.readLine());
if(!isValid){
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportCommand.MESSAGE_USAGE));
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ImportDeveloperCommand.MESSAGE_USAGE));
}
ArrayList<Developer> toAddList = new ArrayList<>();
while ((line = br.readLine()) != null) //returns a Boolean value
Expand All @@ -42,25 +47,28 @@ public ImportCommand parse(String fileName) throws ParseException {
Email email = ParserUtil.parseEmail(employee[2]);
Address address = ParserUtil.parseAddress(employee[3]);
Date dateJoined = ParserUtil.parseDateJoined(employee[4]);
Role role = ParserUtil.parseRole(employee[7]);
Salary salary = ParserUtil.parseSalary(employee[8]);
Role role = ParserUtil.parseRole(employee[5]);
Salary salary = ParserUtil.parseSalary(employee[6]);
GithubId githubId = ParserUtil.parseGithubId(employee[7]);
Rating rating = ParserUtil.parseRating(employee[8]);
ArrayList<String> projects = new ArrayList<>();
for(int i=9;i< employee.length;i++) {
projects.add(employee[i]);
}
Set<Project> projectList = ParserUtil.parseProjects(projects);
Set<String> projectList = ParserUtil.parseProjectsWithCheck(projects);

//Developer developer = new Developer(name, phone, email, address, dateJoined,role, salary,projectList);
//toAddList.add(developer);
Developer developer = new Developer(name, phone, email, address,role,projectList, salary, dateJoined, githubId, rating);
toAddList.add(developer);

}
return new ImportCommand(toAddList);
return new ImportDeveloperCommand(toAddList);
} catch (FileNotFoundException ex) {
throw new ParseException(MESSAGE_INVALID_FILE);
} catch (IOException e) {
throw new ParseException("Error reading line from file " + fileName);
}
}
//Name, Contact Number, Email, Address, Date Joined, Role, Salary, githubId, Rating, Projects"
private boolean checkColumnNames(String line) {
String[] columnNames = line.split(",");
if (!columnNames[0].contains("Name")) {
Expand All @@ -84,10 +92,10 @@ private boolean checkColumnNames(String line) {
if (!columnNames[6].contains("Salary")) {
return false;
}
if (!columnNames[7].contains("Username")) {
if (!columnNames[7].contains("GithubId")) {
return false;
}
if (!columnNames[8].contains("Password")) {
if (!columnNames[8].contains("Rating")) {
return false;
}
if (!columnNames[9].contains("Projects")) {
Expand Down
Loading

0 comments on commit f61dc63

Please sign in to comment.