Skip to content

Commit

Permalink
Update setpicture command to work similarly to edit command by making…
Browse files Browse the repository at this point in the history
… a new Employee. Hence Employees are now properly immutable.
  • Loading branch information
abenx162 committed Apr 9, 2023
1 parent 1a9713c commit de87e76
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 36 deletions.
16 changes: 14 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ static Employee createEditedEmployee(Employee employeeToEdit,
if (updatedDateOfJoining.isEmpty()) {
updatedDateOfJoining = employeeToEdit.getDateOfJoiningOptional();
}
Optional<PicturePath> updatedPicturePath = Optional.of(employeeToEdit.getPicturePath());
PicturePath updatedPicturePath = editEmployeeDescriptor.getPicturePath()
.orElse(employeeToEdit.getPicturePath());
Set<Tag> updatedTags = editEmployeeDescriptor.getTags().orElse(employeeToEdit.getTags());

EmployeeId employeeId = employeeToEdit.getEmployeeId();
Expand Down Expand Up @@ -171,6 +172,7 @@ public static class EditEmployeeDescriptor {
private LeaveCounter leaveCounter;
private Optional<LocalDate> dateOfBirth;
private Optional<LocalDate> dateOfJoining;
private PicturePath picturePath;
private Set<Tag> tags;

public EditEmployeeDescriptor() {}
Expand All @@ -189,6 +191,7 @@ public EditEmployeeDescriptor(EditEmployeeDescriptor toCopy) {
setLeaveCounter(toCopy.leaveCounter);
setDateOfBirth(toCopy.dateOfBirth);
setDateOfJoining(toCopy.dateOfJoining);
setPicturePath(toCopy.picturePath);
setTags(toCopy.tags);
}

Expand All @@ -197,7 +200,7 @@ public EditEmployeeDescriptor(EditEmployeeDescriptor toCopy) {
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, department, payroll, leaveCounter,
dateOfBirth, dateOfJoining, tags);
dateOfBirth, dateOfJoining, picturePath, tags);
}

public void setName(Name name) {
Expand Down Expand Up @@ -272,6 +275,14 @@ public Optional<LocalDate> getDateOfJoining() {
return Optional.ofNullable(dateOfJoining).flatMap(s -> s);
}

public void setPicturePath(PicturePath picturePath) {
this.picturePath = picturePath;
}

public Optional<PicturePath> getPicturePath() {
return Optional.ofNullable(picturePath);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
Expand Down Expand Up @@ -313,6 +324,7 @@ && getPayroll().equals(e.getPayroll())
&& getLeaveCounter().equals(e.getLeaveCounter())
&& getDateOfBirth().equals(e.getDateOfBirth())
&& getDateOfJoining().equals(e.getDateOfJoining())
&& getPicturePath().equals(e.getPicturePath())
&& getTags().equals(e.getTags());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/LeaveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public CommandResult execute(Model model) throws CommandException {
EditCommand editCommand = new EditCommand(employeeId, editEmployeeDescriptor);
editCommand.execute(model);

return new CommandResult(String.format(String.format(MESSAGE_LEAVE_SUCCESS, employeeToTakeLeave.getName(),
numberOfDaysLeave)));
return new CommandResult(String.format(MESSAGE_LEAVE_SUCCESS, employeeToTakeLeave.getName(),
numberOfDaysLeave));
}

@Override
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/seedu/address/logic/commands/SetPictureCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,12 @@ public CommandResult execute(Model model) throws CommandException {
if (employee.getEmployeeId().equals(employeeId)) {
employeeToSetPicture = employee;
Path sourcePath = chooseSourcePicture();
PicturePath destPicturePath = new PicturePath("data/employeepictures/"
+ employeeToSetPicture.getName().fullName + PicturePath.VALID_EXTENSION);
Path destPath = destPicturePath.toPath().toAbsolutePath();
try {
if (!destPath.toFile().exists()) {
Files.createDirectories(destPath);
}
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new CommandException(MESSAGE_IO_ERROR);
}
employeeToSetPicture.setPicturePath(destPicturePath);
PicturePath destPicturePath = savePicture(employeeToSetPicture, sourcePath);

EditCommand.EditEmployeeDescriptor editEmployeeDescriptor = new EditCommand.EditEmployeeDescriptor();
editEmployeeDescriptor.setPicturePath(destPicturePath);
EditCommand editCommand = new EditCommand(employeeId, editEmployeeDescriptor);
editCommand.execute(model);
return new CommandResult(String.format(MESSAGE_SET_PICTURE_SUCCESS, employeeToSetPicture));
}
}
Expand All @@ -79,6 +73,21 @@ private Path chooseSourcePicture() throws CommandException {
return selectedFile.toPath();
}

private PicturePath savePicture(Employee employeeToSetPicture, Path sourcePath) throws CommandException {
PicturePath destPicturePath = new PicturePath(PicturePath.VALID_DIRECTORY
+ employeeToSetPicture.getName().fullName + PicturePath.VALID_EXTENSION);
Path destPath = destPicturePath.toPath().toAbsolutePath();
try {
if (!destPath.toFile().exists()) {
Files.createDirectories(destPath);
}
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new CommandException(MESSAGE_IO_ERROR);
}
return destPicturePath;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import seedu.address.model.employee.Name;
import seedu.address.model.employee.Payroll;
import seedu.address.model.employee.Phone;
import seedu.address.model.employee.PicturePath;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -62,6 +63,8 @@ public AddCommand parse(String args) throws ParseException {
// Optional fields
Address address = Address.getNullAddress();
Email email = Email.getNullEmail();
// picturePath can NOT be set via AddCommand
PicturePath picturePath = PicturePath.getNullPicturePath();

// Setting values for optional fields
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
Expand All @@ -76,9 +79,10 @@ public AddCommand parse(String args) throws ParseException {
Optional<LocalDate> dateOfJoining = ParserUtil.parseDateOfJoining(argMultimap.getValue(PREFIX_DATE_OF_JOINING));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));


EmployeeId employeeId = new EmployeeId();
Employee employee = new Employee(name, employeeId, phone, email, address, department, payroll, leaveCount,
dateOfBirth, dateOfJoining, Optional.empty(), tagList);
dateOfBirth, dateOfJoining, picturePath, tagList);

return new AddCommand(employee);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/employee/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Employee {
public Employee(Name name, EmployeeId employeeId, Phone phone, Email email, Address address,
Department department, Payroll payroll, LeaveCounter leaveCounter,
Optional<LocalDate> dateOfBirth, Optional<LocalDate> dateOfJoining,
Optional<PicturePath> picturePath, Set<Tag> tags) {
PicturePath picturePath, Set<Tag> tags) {
requireAllNonNull(name, employeeId, phone, email, address, department, payroll, leaveCounter,
dateOfBirth, dateOfJoining, tags);
this.name = name;
Expand All @@ -53,7 +53,7 @@ public Employee(Name name, EmployeeId employeeId, Phone phone, Email email, Addr
this.leaveCounter = leaveCounter;
this.dateOfBirth = dateOfBirth;
this.dateOfJoining = dateOfJoining;
this.picturePath = picturePath.orElseGet(() -> new PicturePath(""));
this.picturePath = picturePath;
this.tags.addAll(tags);
}

Expand Down Expand Up @@ -163,14 +163,15 @@ public boolean equals(Object other) {
&& otherEmployee.getLeaveCounter().equals(getLeaveCounter())
&& otherEmployee.getDateOfBirth().equals(getDateOfBirth())
&& otherEmployee.getDateOfJoining().equals(getDateOfJoining())
&& otherEmployee.getPicturePath().equals(getPicturePath())
&& otherEmployee.getTags().equals(getTags());
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, employeeId, phone, email, address, department, payroll, leaveCounter,
dateOfBirth, dateOfJoining, tags);
dateOfBirth, dateOfJoining, picturePath, tags);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/model/employee/PicturePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class PicturePath {
public static final String VALID_DIRECTORY = "data/employeepictures/";
public static final String VALID_EXTENSION = ".png";

private static final PicturePath NULL = new PicturePath();

public final String value;

/**
Expand All @@ -32,6 +34,17 @@ public PicturePath(String picturePath) {
this.value = picturePath;
}

/**
* Bypass input validation, allows actual null values.
*/
public PicturePath() {
this.value = "";
}

public static PicturePath getNullPicturePath() {
return PicturePath.NULL;
}

/**
* Returns true if a given string is a valid name.
*/
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import seedu.address.model.employee.Name;
import seedu.address.model.employee.Payroll;
import seedu.address.model.employee.Phone;
import seedu.address.model.employee.PicturePath;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -29,32 +30,32 @@ public static Employee[] getSampleEmployees() {
new Email("[email protected]"), new Address("Blk 30 Geylang Street 29, #06-40"),
new Department("Marketing"), new Payroll(4000, 15), new LeaveCounter(),
Optional.of(LocalDate.parse("1998-09-08")), Optional.of(LocalDate.parse("2019-09-07")),
Optional.empty(), getTagSet("friends")),
new PicturePath(), getTagSet("friends")),
new Employee(new Name("Bernice Yu"), new EmployeeId(), new Phone("99272758"),
new Email("[email protected]"), new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
new Department("Marketing"), new Payroll(4000, 15), new LeaveCounter(),
Optional.of(LocalDate.parse("1988-12-30")), Optional.of(LocalDate.parse("2019-09-07")),
Optional.empty(), getTagSet("colleagues", "friends")),
new PicturePath(), getTagSet("colleagues", "friends")),
new Employee(new Name("Charlotte Oliveiro"), new EmployeeId(), new Phone("93210283"),
new Email("[email protected]"), new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
new Department("Marketing"), new Payroll(4000, 15), new LeaveCounter(),
Optional.of(LocalDate.parse("1990-09-01")), Optional.of(LocalDate.parse("2019-09-07")),
Optional.empty(), getTagSet("neighbours")),
new PicturePath(), getTagSet("neighbours")),
new Employee(new Name("David Li"), new EmployeeId(), new Phone("91031282"),
new Email("[email protected]"), new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
new Department("Sales"), new Payroll(4000, 15), new LeaveCounter(),
Optional.of(LocalDate.parse("2000-03-21")), Optional.of(LocalDate.parse("2019-09-07")),
Optional.empty(), getTagSet("family")),
new PicturePath(), getTagSet("family")),
new Employee(new Name("Irfan Ibrahim"), new EmployeeId(), new Phone("92492021"),
new Email("[email protected]"), new Address("Blk 47 Tampines Street 20, #17-35"),
new Department("Sales"), new Payroll(4000, 15), new LeaveCounter(),
Optional.of(LocalDate.parse("1999-04-07")), Optional.of(LocalDate.parse("2019-09-07")),
Optional.empty(), getTagSet("classmates")),
new PicturePath(), getTagSet("classmates")),
new Employee(new Name("Roy Balakrishnan"), new EmployeeId(), new Phone("92624417"),
new Email("[email protected]"), new Address("Blk 45 Aljunied Street 85, #11-31"),
new Department("Sales"), new Payroll(4000, 15), new LeaveCounter(),
Optional.of(LocalDate.parse("1995-06-02")), Optional.of(LocalDate.parse("2019-09-07")),
Optional.empty(), getTagSet("colleagues"))
new PicturePath(), getTagSet("colleagues"))
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedEmployee.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class JsonAdaptedEmployee {
private final JsonAdaptedLeaveCounter leaveCounter;
private final String dateOfBirth;
private final String dateOfJoining;
private String picturePath;
private final String picturePath;
private final List<JsonAdaptedTag> tagged = new ArrayList<>();

@JsonCreator
Expand Down Expand Up @@ -187,7 +187,7 @@ public Employee toModelType() throws IllegalValueException {
if (!PicturePath.isValidPicturePath(picturePath)) {
throw new IllegalValueException(PicturePath.MESSAGE_CONSTRAINTS);
}
final Optional<PicturePath> modelPicturePath = Optional.of(new PicturePath(picturePath));
final PicturePath modelPicturePath = new PicturePath(picturePath);

final Set<Tag> modelTags = new HashSet<>(employeeTags);
return new Employee(modelName, modelEmployeeId, modelPhone, modelEmail, modelAddress, modelDepartment,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/ui/PersonListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public void setInformation(Employee employee) {
dateOfBirth.setText("Date of Birth: " + employee.getDateOfBirth());
dateOfJoining.setText("Date of Joining: " + employee.getDateOfJoining());

String val = employee.getPicturePath().value;

InputStream isImage;
String val = employee.getPicturePath().value;
if (val.equals("")) {
isImage = getClass().getResourceAsStream("/images/default_employee.png");
requireNonNull(isImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class CommandTestUtil {
public static final String VALID_DATE_OF_BIRTH_BOB = "1998-02-09";
public static final String VALID_DATE_OF_JOINING_AMY = "2022-01-01";
public static final String VALID_DATE_OF_JOINING_BOB = "2023-01-01";
public static final String VALID_PICTURE_PATH_AMY = "data/employeepictures/default.png";
public static final String VALID_PICTURE_PATH_BOB = "data/employeepictures/default.png";
public static final String VALID_PICTURE_PATH_AMY = "";
public static final String VALID_PICTURE_PATH_BOB = "";
public static final String VALID_TAG_SOFTWARE_ENGINEER = "software engineer";
public static final String VALID_TAG_MANAGER = "manager";

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/seedu/address/testutil/EmployeeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class EmployeeBuilder {
public static final int DEFAULT_LEAVE_COUNTER = 15;
public static final String DEFAULT_DATE_OF_BIRTH = "2000-04-21";
public static final String DEFAULT_DATE_OF_JOINING = "2022-01-01";
public static final String DEFAULT_PICTURE_PATH = "data/employeepictures/default.png";
public static final String DEFAULT_PICTURE_PATH = "";

private Name name;
private EmployeeId employeeId;
Expand All @@ -45,7 +45,7 @@ public class EmployeeBuilder {
private LeaveCounter leaveCounter;
private Optional<LocalDate> dateOfBirth;
private Optional<LocalDate> dateOfJoining;
private Optional<PicturePath> picturePath;
private PicturePath picturePath;
private Set<Tag> tags;

/**
Expand All @@ -66,7 +66,7 @@ public EmployeeBuilder() {
leaveCounter = new LeaveCounter(DEFAULT_LEAVE_COUNTER);
dateOfBirth = Optional.ofNullable(LocalDate.parse(DEFAULT_DATE_OF_BIRTH));
dateOfJoining = Optional.ofNullable(LocalDate.parse(DEFAULT_DATE_OF_JOINING));
picturePath = Optional.of(new PicturePath(DEFAULT_PICTURE_PATH));
picturePath = new PicturePath(DEFAULT_PICTURE_PATH);
tags = new HashSet<>();
}

Expand All @@ -84,7 +84,7 @@ public EmployeeBuilder(Employee employeeToCopy) {
leaveCounter = employeeToCopy.getLeaveCounter();
dateOfBirth = employeeToCopy.getDateOfBirthOptional();
dateOfJoining = employeeToCopy.getDateOfJoiningOptional();
picturePath = Optional.ofNullable(employeeToCopy.getPicturePath());
picturePath = employeeToCopy.getPicturePath();
tags = new HashSet<>(employeeToCopy.getTags());
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public EmployeeBuilder withDateOfJoining(String date) {
* Sets the {@code PicturePath} of the {@code Employee} that we are building.
*/
public EmployeeBuilder withPicturePath(String picturePath) {
this.picturePath = Optional.of(new PicturePath(picturePath));
this.picturePath = new PicturePath(picturePath);
return this;
}

Expand Down

0 comments on commit de87e76

Please sign in to comment.