From c5c8797ba2b7f86cb110db7d836677a1cbd8642b Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Tue, 12 Nov 2024 23:34:01 +0100 Subject: [PATCH] fix: This was literally hell --- .../maradona/backend/DefaultValueLoader.java | 1 - .../maradona/backend/entities/Employee.java | 56 ++++++++++--------- .../entities/EmployeeSecondarySkill.java | 12 ++-- .../backend/entities/FormOfAddress.java | 6 +- .../backend/entities/PrimarySkill.java | 6 +- .../maradona/backend/entities/Project.java | 7 ++- .../backend/entities/SecondarySkill.java | 11 ++-- .../services/details/EmployeeDetails.java | 16 ++++++ .../services/transfer/SkillService.java | 2 +- 9 files changed, 74 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/maradona/backend/DefaultValueLoader.java b/src/main/java/com/maradona/backend/DefaultValueLoader.java index aae7469..5c899a4 100644 --- a/src/main/java/com/maradona/backend/DefaultValueLoader.java +++ b/src/main/java/com/maradona/backend/DefaultValueLoader.java @@ -13,7 +13,6 @@ import com.maradona.backend.repositories.ProjectRepository; import com.maradona.backend.repositories.SecondarySkillRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; -import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; import java.time.LocalDate; diff --git a/src/main/java/com/maradona/backend/entities/Employee.java b/src/main/java/com/maradona/backend/entities/Employee.java index 546f917..6c5ce4f 100644 --- a/src/main/java/com/maradona/backend/entities/Employee.java +++ b/src/main/java/com/maradona/backend/entities/Employee.java @@ -3,12 +3,10 @@ package com.maradona.backend.entities; import jakarta.persistence.*; import lombok.Data; -import com.fasterxml.jackson.annotation.JsonBackReference; -import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.fasterxml.jackson.annotation.JsonIgnore; + import java.time.LocalTime; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; /** * Represents an employee. @@ -48,15 +46,6 @@ public class Employee { @Column(length = 100) private String lastName; - /** - * The form of address of the employee. - * - * This is a reference to the form of address entity. - */ - @ManyToOne - @JoinColumn(name = "form_of_address_fid") - private FormOfAddress formOfAddress; - /** * The start time of the employee's daylie business. */ @@ -72,18 +61,27 @@ public class Employee { * * This is a list of secondary skill entities. */ - @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true) - @JsonManagedReference + @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + @JsonIgnore private List secondarySkills; /** - * The projects of the employee. + * The form of address of the employee. * - * This is a list of project entities. + * This is a reference to the form of address entity. */ - @ManyToMany - @JoinTable(name = "employee_project", joinColumns = @JoinColumn(name = "employee_eid"), inverseJoinColumns = @JoinColumn(name = "project_pid")) - @JsonBackReference + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "form_of_address_fid") + @JsonIgnore + private FormOfAddress formOfAddress; + + /** + * The projects that the employee works on. + * + * This is a list of references to the project entity. + */ + @ManyToMany(mappedBy = "employees", fetch = FetchType.LAZY) + @JsonIgnore private List projects; /** @@ -231,12 +229,20 @@ public class Employee { } /** - * Sets the projects of the employee. + * Returns the projects that the employee works on. * - * @param projects The projects of the employee. + * @return The projects that the employee works on. */ - public Map getSecondarySkillLevels() { - return secondarySkills.stream() - .collect(Collectors.toMap(EmployeeSecondarySkill::getSecondarySkill, EmployeeSecondarySkill::getLevel)); + public List getProjects() { + return projects; + } + + /** + * Sets the projects that the employee works on. + * + * @param projects The projects that the employee works on. + */ + public void setProjects(List projects) { + this.projects = projects; } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java b/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java index 52aee71..b94ee41 100644 --- a/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java +++ b/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java @@ -3,6 +3,7 @@ package com.maradona.backend.entities; import jakarta.persistence.*; import lombok.Data; import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnore; /** * Represents an employee's secondary skill. @@ -27,9 +28,9 @@ public class EmployeeSecondarySkill { * * This is a reference to the employee entity. */ - @ManyToOne - @JoinColumn(name = "employee_eid", nullable = false) - @JsonBackReference + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "employee_id") + @JsonIgnore private Employee employee; /** @@ -37,8 +38,9 @@ public class EmployeeSecondarySkill { * * This is a reference to the secondary skill entity. */ - @ManyToOne - @JoinColumn(name = "secondary_skill_id", nullable = false) + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "secondary_skill_id") + @JsonIgnore private SecondarySkill secondarySkill; /** diff --git a/src/main/java/com/maradona/backend/entities/FormOfAddress.java b/src/main/java/com/maradona/backend/entities/FormOfAddress.java index 9ff5422..1730e1b 100644 --- a/src/main/java/com/maradona/backend/entities/FormOfAddress.java +++ b/src/main/java/com/maradona/backend/entities/FormOfAddress.java @@ -2,6 +2,9 @@ package com.maradona.backend.entities; import java.util.List; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnore; + import jakarta.persistence.*; import lombok.Data; @@ -37,7 +40,8 @@ public class FormOfAddress { * * This is a list of references to the employee entity. */ - @OneToMany(mappedBy = "formOfAddress") + @OneToMany(mappedBy = "formOfAddress", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + @JsonIgnore private List employees; /** diff --git a/src/main/java/com/maradona/backend/entities/PrimarySkill.java b/src/main/java/com/maradona/backend/entities/PrimarySkill.java index 386a622..47913f3 100644 --- a/src/main/java/com/maradona/backend/entities/PrimarySkill.java +++ b/src/main/java/com/maradona/backend/entities/PrimarySkill.java @@ -1,6 +1,8 @@ package com.maradona.backend.entities; import jakarta.persistence.*; + +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonManagedReference; import lombok.Data; import java.util.List; @@ -37,8 +39,8 @@ public class PrimarySkill { * * This is a list of references to the secondary skill entity. */ - @OneToMany(mappedBy = "primarySkill", cascade = CascadeType.ALL, orphanRemoval = true) - @JsonManagedReference + @OneToMany(mappedBy = "primarySkill", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + @JsonIgnore private List secondarySkills; /** diff --git a/src/main/java/com/maradona/backend/entities/Project.java b/src/main/java/com/maradona/backend/entities/Project.java index a7cdb98..15f3cce 100644 --- a/src/main/java/com/maradona/backend/entities/Project.java +++ b/src/main/java/com/maradona/backend/entities/Project.java @@ -5,7 +5,7 @@ import lombok.Data; import java.time.LocalDate; import java.util.List; -import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.fasterxml.jackson.annotation.JsonIgnore; /** * Represents a project. @@ -37,8 +37,9 @@ public class Project { * * This is a list of references to the employee entity. */ - @ManyToMany(mappedBy = "projects") - @JsonManagedReference + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable(name = "project_employee", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "employee_id")) + @JsonIgnore private List employees; /** diff --git a/src/main/java/com/maradona/backend/entities/SecondarySkill.java b/src/main/java/com/maradona/backend/entities/SecondarySkill.java index b377eef..481c3a6 100644 --- a/src/main/java/com/maradona/backend/entities/SecondarySkill.java +++ b/src/main/java/com/maradona/backend/entities/SecondarySkill.java @@ -2,6 +2,7 @@ package com.maradona.backend.entities; import jakarta.persistence.*; import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonManagedReference; import lombok.Data; import java.util.List; @@ -38,9 +39,9 @@ public class SecondarySkill { * * This is a reference to the primary skill entity. */ - @ManyToOne - @JoinColumn(name = "psid", nullable = false) - @JsonBackReference + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "primary_skill_id") + @JsonIgnore private PrimarySkill primarySkill; /** @@ -48,8 +49,8 @@ public class SecondarySkill { * * This is a list of references to the employee's secondary skill entity. */ - @OneToMany(mappedBy = "secondarySkill", cascade = CascadeType.ALL, orphanRemoval = true) - @JsonManagedReference + @OneToMany(mappedBy = "secondarySkill", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + @JsonIgnore private List employeeSecondarySkills; /** diff --git a/src/main/java/com/maradona/backend/services/details/EmployeeDetails.java b/src/main/java/com/maradona/backend/services/details/EmployeeDetails.java index b6b2b13..6e93280 100644 --- a/src/main/java/com/maradona/backend/services/details/EmployeeDetails.java +++ b/src/main/java/com/maradona/backend/services/details/EmployeeDetails.java @@ -3,8 +3,12 @@ package com.maradona.backend.services.details; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.maradona.backend.entities.Employee; +import com.maradona.backend.entities.EmployeeSecondarySkill; +import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.repositories.EmployeeRepository; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; /** @@ -55,4 +59,16 @@ public class EmployeeDetails { // TODO: This is a placeholder implementation return employeeRepository.findAll(); } + + // Map secondarySkillLevels = + // employee.getSecondarySkillLevels(); + public Map getSecondarySkillLevels(Long eid) { + var employee = getEmployeeByEid(eid).orElse(null); + var employeeSecondarySkills = employee.getSecondarySkills(); + var levels = new HashMap(); + for (EmployeeSecondarySkill employeeSecondarySkill : employeeSecondarySkills) { + levels.put(employeeSecondarySkill.getSecondarySkill(), employeeSecondarySkill.getLevel()); + } + return levels; + } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/services/transfer/SkillService.java b/src/main/java/com/maradona/backend/services/transfer/SkillService.java index fca515d..0bf38e3 100644 --- a/src/main/java/com/maradona/backend/services/transfer/SkillService.java +++ b/src/main/java/com/maradona/backend/services/transfer/SkillService.java @@ -78,7 +78,7 @@ public class SkillService { return new ArrayList<>(); } - Map secondarySkillLevels = employee.getSecondarySkillLevels(); + Map secondarySkillLevels = employeeDetails.getSecondarySkillLevels(eid); List skills = new ArrayList<>(); for (PrimarySkill primarySkill : primarySkillDetails.getAllPrimarySkills()) {