Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 143 and 144 #148

Merged
merged 11 commits into from
Nov 8, 2023
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import seedu.address.model.contact.Type;
import seedu.address.model.contact.Url;
import seedu.address.model.jobapplication.ApplicationStage;
import seedu.address.model.jobapplication.JobApplication;
import seedu.address.model.jobapplication.JobStatus;
import seedu.address.model.tag.Tag;

Expand Down Expand Up @@ -254,8 +255,12 @@ private static Contact createEditedContact(Model model, Contact contactToEdit,

// TODO: Refactor into two methods to handle the two cases.
if (contactToEdit.getType() == Type.ORGANIZATION) {
Organization org = (Organization) contactToEdit;
List<JobApplication> applications = Arrays.asList(org.getJobApplications());

return new Organization(updatedName, updatedId, updatedPhone, updatedEmail,
updatedUrl, updatedAddress, updatedTags);
updatedUrl, updatedAddress, updatedTags, applications);

} else if (contactToEdit.getType() == Type.RECRUITER) {
Optional<Id> updatedOid = editContactDescriptor
.getOrganizationId()
Expand Down
55 changes: 47 additions & 8 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalOperationException;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.Messages;
import seedu.address.model.contact.Contact;
Expand Down Expand Up @@ -56,11 +57,8 @@
this.sortedContacts = new SortedList<>(this.addressBook.getContactList());
this.filteredContacts = new FilteredList<>(sortedContacts);
this.displayedContacts = filteredContacts;
ObservableList<JobApplication> applicationList = FXCollections.observableArrayList(filteredContacts.stream()
.filter(c -> c.getType() == Type.ORGANIZATION)
.flatMap(c -> Arrays.stream(((Organization) c).getJobApplications()))
.sorted(JobApplication.LAST_UPDATED_COMPARATOR)
.collect(Collectors.toList()));
ObservableList<JobApplication> applicationList =
FXCollections.observableArrayList(extractApplicationsFromContacts(filteredContacts));
this.jobApplicationList = new JobApplicationList(applicationList);
this.sortedApplications = new SortedList<>(applicationList);
this.filteredApplications = new FilteredList<>(this.sortedApplications, s->true);
Expand Down Expand Up @@ -111,6 +109,7 @@
@Override
public void setAddressBook(ReadOnlyAddressBook addressBook) {
this.addressBook.resetData(addressBook);
this.jobApplicationList.setAll(extractApplicationsFromContacts(addressBook.getContactList()));
}

@Override
Expand Down Expand Up @@ -146,6 +145,18 @@
requireAllNonNull(target, editedContact);

addressBook.setContact(target, editedContact);

if (target.getType() != Type.ORGANIZATION || editedContact.getType() != Type.ORGANIZATION) {
// guard clause
return;
}
if (target.getName().equals(editedContact.getName()) && target.getId().equals(editedContact.getId())) {
// guard clause
return;
}

updateApplicationNames((Organization) editedContact, (Organization) target);

}

@Override
Expand Down Expand Up @@ -184,14 +195,19 @@
public void replaceApplication(Index index, JobApplication newApplication) throws IllegalValueException {
JobApplication oldApplication = jobApplicationList.get(index.getZeroBased());

jobApplicationList.set(index.getZeroBased(), newApplication);
Contact contact = getContactById(newApplication.getOrganizationId());
if (contact == null || contact.getType() != Type.ORGANIZATION) {
throw new IllegalValueException("Id field is invalid!");
throw new IllegalValueException("Id field is invalid!"); // TODO: Should I change this?

Check warning on line 200 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L200

Added line #L200 was not covered by tests
}

Organization organization = (Organization) contact;
organization.replaceJobApplication(oldApplication, newApplication);
try {
organization.replaceJobApplication(oldApplication, newApplication);
} catch (IllegalOperationException e) {
throw new IllegalValueException(e.getMessage());
}

Check warning on line 208 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L205-L208

Added lines #L205 - L208 were not covered by tests

jobApplicationList.set(index.getZeroBased(), newApplication);

Check warning on line 210 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L210

Added line #L210 was not covered by tests

}

Expand Down Expand Up @@ -265,4 +281,27 @@
&& filteredContacts.equals(otherModelManager.filteredContacts);
}

private void updateApplicationNames(Organization newOrg, Organization oldOrg) {
List<JobApplication> oldApplications = List.of(oldOrg.getJobApplications());
List<JobApplication> newApplications = List.of(newOrg.getJobApplications());

for (JobApplication newApplication: newApplications) {
for (JobApplication oldApplication: oldApplications) {
if (!newApplication.looseEquals(oldApplication)) {
continue;

Check warning on line 291 in src/main/java/seedu/address/model/ModelManager.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/ModelManager.java#L291

Added line #L291 was not covered by tests
}
int index = jobApplicationList.indexOf(oldApplication);
jobApplicationList.set(index, newApplication);
}
}
}

private List<JobApplication> extractApplicationsFromContacts(List<Contact> contacts) {
return contacts.stream()
.filter(c -> c.getType() == Type.ORGANIZATION)
.flatMap(c -> Arrays.stream(((Organization) c).getJobApplications()))
.sorted(JobApplication.LAST_UPDATED_COMPARATOR)
.collect(Collectors.toList());
}

}
34 changes: 31 additions & 3 deletions src/main/java/seedu/address/model/contact/Organization.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package seedu.address.model.contact;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.address.commons.exceptions.IllegalOperationException;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.jobapplication.JobApplication;
import seedu.address.model.tag.Tag;
Expand Down Expand Up @@ -41,8 +44,15 @@
Name name, Id id, Phone phone, Email email, Url url,
Address address, Set<Tag> tags, List<JobApplication> jobApplications
) {
// TODO: Tech debt - undeprecate super
super(name, id, phone, email, url, address, tags, null);
this.jobApplications.addAll(jobApplications);
// Ensure that the new job applications are modified correctly.
List<JobApplication> newApplications = jobApplications
.stream()
.map(a -> a.changeCompanyDetails(name, id))
.collect(Collectors.toList());

this.jobApplications.addAll(newApplications);
}

@Override
Expand Down Expand Up @@ -75,10 +85,13 @@
/**
* Replaces the old job application in the list with the new one.
*/
public void replaceJobApplication(JobApplication oldApplication, JobApplication newApplication) {
public void replaceJobApplication(JobApplication oldApplication, JobApplication newApplication) throws
IllegalOperationException {
assert newApplication.getOrganizationId().equals(this.getId());
assert newApplication.getOrganizationId().equals(oldApplication.getOrganizationId());

if (hasApplicationWithSameNameWithExclusion(newApplication, oldApplication)) {
throw new IllegalOperationException("Job Application with same name found. Set a different name");

Check warning on line 93 in src/main/java/seedu/address/model/contact/Organization.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/contact/Organization.java#L93

Added line #L93 was not covered by tests
}
this.jobApplications.remove(oldApplication);
this.jobApplications.add(newApplication);
}
Expand Down Expand Up @@ -124,4 +137,19 @@
return super.toStringBuilder();
}

private boolean hasApplicationWithSameNameWithExclusion(JobApplication application,
JobApplication... excludedApplications) {
List<JobApplication> applicationsExcludedList =
Arrays.stream(excludedApplications).collect(Collectors.toList());

Check warning on line 143 in src/main/java/seedu/address/model/contact/Organization.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/contact/Organization.java#L142-L143

Added lines #L142 - L143 were not covered by tests
for (JobApplication a: jobApplications) {
if (applicationsExcludedList.contains(a)) {
continue;

Check warning on line 146 in src/main/java/seedu/address/model/contact/Organization.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/contact/Organization.java#L146

Added line #L146 was not covered by tests
}
if (application.getJobTitle().equals(a.getJobTitle())) {
return true;

Check warning on line 149 in src/main/java/seedu/address/model/contact/Organization.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/contact/Organization.java#L149

Added line #L149 was not covered by tests
}
}
return false;

Check warning on line 152 in src/main/java/seedu/address/model/contact/Organization.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/contact/Organization.java#L151-L152

Added lines #L151 - L152 were not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ public Name getOrgName() {
return orgName;
}

/**
* Checks if the details of the job application is the same excluding org name and id and last updated time.
*/
public boolean looseEquals(JobApplication other) {
return this.jobTitle.equals(other.jobTitle)
&& this.jobDescription.equals(other.jobDescription)
&& this.applicationStage.equals(other.applicationStage)
&& this.status.equals(other.status);
}

/**
* Gives a new job application given change in company details.
*/
public JobApplication changeCompanyDetails(Name orgName, Id oid) {
return new JobApplication(oid, orgName, this.jobTitle, this.jobDescription.orElse(null), this.deadline,
this.status, this.applicationStage, new LastUpdatedTime());
}

@Override
public int hashCode() {
return Objects.hash(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package seedu.address.model.jobapplication;


import java.util.Collection;

import javafx.collections.ObservableList;

/**
Expand Down Expand Up @@ -51,4 +53,18 @@ public void set(int index, JobApplication jobApplication) {
public JobApplication get(int index) {
return applications.get(index);
}

/**
* Gets the index of the application in the list. Returns -1 if not found.
*/
public int indexOf(JobApplication application) {
return applications.indexOf(application);
}

/**
* Replaces all the applications in the list with the new applications
*/
public void setAll(Collection<JobApplication> applications) {
this.applications.setAll(applications);
}
}
Loading