From c94d3654ced7b5e4fa113d2fb4f932280ca63fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Le=20Lez?= Date: Sat, 15 Mar 2025 15:23:18 +0100 Subject: [PATCH] fix: updating foreign keys when adding new entity to the db --- .../myinpulse/model/Administrator.java | 24 ++++++++++ .../enseirb/myinpulse/model/Annotation.java | 24 ++++++++++ .../enseirb/myinpulse/model/Appointment.java | 4 ++ .../enseirb/myinpulse/model/Entrepreneur.java | 42 +++++++++++++++++ .../java/enseirb/myinpulse/model/Project.java | 47 +++++++++++++++++-- .../enseirb/myinpulse/model/SectionCell.java | 28 +++++++++-- .../myinpulse/service/AdminApiService.java | 21 +++++++-- .../service/EntrepreneurApiService.java | 15 +++++- .../myinpulse/service/SharedApiService.java | 17 +++++-- .../service/database/ProjectService.java | 2 +- 10 files changed, 206 insertions(+), 18 deletions(-) diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java index 3b1ee7a..d6eecda 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Administrator.java @@ -39,4 +39,28 @@ public class Administrator extends User { String phoneNumber) { super(null, userSurname, username, primaryMail, secondaryMail, phoneNumber); } + + public List getListProject() { + return listProject; + } + + public void updateListProject(Project project) { + listProject.add(project); + } + + public List getListAnnotation() { + return listAnnotation; + } + + public void updateListAnnotation(Annotation annotation) { + listAnnotation.add(annotation); + } + + public MakeAppointment getMakeAppointment() { + return makeAppointment; + } + + public void setMakeAppointment(MakeAppointment makeAppointment) { + this.makeAppointment = makeAppointment; + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java index a7ba0d8..f34f663 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Annotation.java @@ -34,4 +34,28 @@ public class Annotation { public void setComment(String comment) { this.comment = comment; } + + public Long getIdAnnotation() { + return idAnnotation; + } + + public void setIdAnnotation(Long idAnnotation) { + this.idAnnotation = idAnnotation; + } + + public SectionCell getSectionCellAnnotation() { + return sectionCellAnnotation; + } + + public void setSectionCellAnnotation(SectionCell sectionCellAnnotation) { + this.sectionCellAnnotation = sectionCellAnnotation; + } + + public Administrator getAdministratorAnnotation() { + return administratorAnnotation; + } + + public void setAdministratorAnnotation(Administrator administratorAnnotation) { + this.administratorAnnotation = administratorAnnotation; + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java index 352ab17..a683d3f 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Appointment.java @@ -112,6 +112,10 @@ public class Appointment { return listSectionCell; } + public void updateListSectionCell(SectionCell sectionCell) { + listSectionCell.add(sectionCell); + } + public Report getAppointmentReport() { return report; } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java index 7eb64d5..9763b4e 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Entrepreneur.java @@ -52,6 +52,28 @@ public class Entrepreneur extends User { this.sneeStatus = sneeStatus; } + public Entrepreneur( + Long idUser, + String userSurname, + String userName, + String primaryMail, + String secondaryMail, + String phoneNumber, + String school, + String course, + boolean sneeStatus, + Project projectParticipation, + Project projectProposed, + MakeAppointment makeAppointment) { + super(idUser, userSurname, userName, primaryMail, secondaryMail, phoneNumber); + this.school = school; + this.course = course; + this.sneeStatus = sneeStatus; + this.projectParticipation = projectParticipation; + this.projectProposed = projectProposed; + this.makeAppointment = makeAppointment; + } + public String getSchool() { return school; } @@ -79,4 +101,24 @@ public class Entrepreneur extends User { public Project getProjectParticipation() { return projectParticipation; } + + public void setProjectParticipation(Project projectParticipation) { + this.projectParticipation = projectParticipation; + } + + public Project getProjectProposed() { + return projectProposed; + } + + public void setProjectProposed(Project projectProposed) { + this.projectProposed = projectProposed; + } + + public MakeAppointment getMakeAppointment() { + return makeAppointment; + } + + public void setMakeAppointment(MakeAppointment makeAppointment) { + this.makeAppointment = makeAppointment; + } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java index b745c6f..866e685 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/Project.java @@ -51,6 +51,21 @@ public class Project { this.projectAdministrator = projectAdministrator; } + public Project( + String projectName, + byte[] logo, + LocalDate creationDate, + ProjectDecisionValue projectStatus, + Administrator projectAdministrator, + Entrepreneur entrepreneurProposed) { + this.projectName = projectName; + this.logo = logo; + this.creationDate = creationDate; + this.projectStatus = projectStatus; + this.projectAdministrator = projectAdministrator; + this.entrepreneurProposed = entrepreneurProposed; + } + public Long getIdProject() { return idProject; } @@ -91,11 +106,35 @@ public class Project { this.projectStatus = projectStatus; } - public Administrator getAdministrator() { - return this.projectAdministrator; + public List getListEntrepreneurParticipation() { + return listEntrepreneurParticipation; } - public void setAdministrator(Administrator administrator) { - this.projectAdministrator = administrator; + public void updateListEntrepreneurParticipation(Entrepreneur projectParticipant) { + listEntrepreneurParticipation.add(projectParticipant); + } + + public List getListSectionCell() { + return listSectionCell; + } + + public void updateListSectionCell(SectionCell projectSectionCell) { + listSectionCell.add(projectSectionCell); + } + + public Administrator getProjectAdministrator() { + return projectAdministrator; + } + + public void setProjectAdministrator(Administrator projectAdministrator) { + this.projectAdministrator = projectAdministrator; + } + + public Entrepreneur getEntrepreneurProposed() { + return entrepreneurProposed; + } + + public void setEntrepreneurProposed(Entrepreneur entrepreneurProposed) { + this.entrepreneurProposed = entrepreneurProposed; } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java index 0fb8c89..2bd1888 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/model/SectionCell.java @@ -11,7 +11,7 @@ import java.util.List; public class SectionCell { @ManyToMany(mappedBy = "listSectionCell") - private final List appointment = new ArrayList<>(); + private final List listAppointment = new ArrayList<>(); @OneToMany(mappedBy = "sectionCellAnnotation", fetch = FetchType.LAZY, orphanRemoval = true) private final List listAnnotation = new ArrayList<>(); @@ -39,11 +39,13 @@ public class SectionCell { Long idSectionCell, Long sectionId, String contentSectionCell, - LocalDateTime modificationDate) { + LocalDateTime modificationDate, + Project projectSectionCell) { this.idSectionCell = idSectionCell; this.sectionId = sectionId; this.contentSectionCell = contentSectionCell; this.modificationDate = modificationDate; + this.projectSectionCell = projectSectionCell; } public Long getIdSectionCell() { @@ -83,6 +85,26 @@ public class SectionCell { } public List getAppointmentSectionCell() { - return appointment; + return listAppointment; + } + + public void updateAppointmentSectionCell(Appointment appointment) { + listAppointment.add(appointment); + } + + public List getListAnnotation() { + return listAnnotation; + } + + public void updateListAnnotation(Annotation annotation) { + listAnnotation.add(annotation); + } + + public void setSectionId(long sectionId) { + this.sectionId = sectionId; + } + + public void setProjectSectionCell(Project projectSectionCell) { + this.projectSectionCell = projectSectionCell; } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java index e00171e..d6efc7d 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/AdminApiService.java @@ -5,14 +5,13 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.REJECTED; import enseirb.myinpulse.model.*; import enseirb.myinpulse.service.database.AdministratorService; -import enseirb.myinpulse.service.database.ProjectService; -import enseirb.myinpulse.service.database.UserService; import enseirb.myinpulse.service.database.AppointmentService; +import enseirb.myinpulse.service.database.ProjectService; import enseirb.myinpulse.service.database.ReportService; +import enseirb.myinpulse.service.database.UserService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -86,8 +85,22 @@ public class AdminApiService { if (e.getStatusCode() == HttpStatus.CONFLICT) { throw new ResponseStatusException(HttpStatus.CONFLICT, "Project already exists"); } - projectService.addNewProject(project); } + Project newProject = projectService.addNewProject(project); + newProject.getProjectAdministrator().updateListProject(newProject); + newProject.getEntrepreneurProposed().setProjectProposed(newProject); + newProject + .getListEntrepreneurParticipation() + .forEach( + participation -> { + participation.setProjectParticipation(newProject); + }); + newProject + .getListSectionCell() + .forEach( + sectionCell -> { + sectionCell.setProjectSectionCell(newProject); + }); } public void createAppointmentReport(long appointmentId, Report report, String mail) { diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java index f1d6260..1e9cd92 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/EntrepreneurApiService.java @@ -107,7 +107,20 @@ public class EntrepreneurApiService { mail, sectionCell.getIdSectionCell(), this.sectionCellService.getProjectId(sectionCell.getIdSectionCell())); - sectionCellService.addNewSectionCell(sectionCell); + SectionCell newSectionCell = sectionCellService.addNewSectionCell(sectionCell); + newSectionCell.getProjectSectionCell().updateListSectionCell(newSectionCell); + newSectionCell + .getAppointmentSectionCell() + .forEach( + appointment -> { + appointment.updateListSectionCell(newSectionCell); + }); + newSectionCell + .getListAnnotation() + .forEach( + annotation -> { + annotation.setSectionCellAnnotation(newSectionCell); + }); } public void requestNewProject(Project project, String mail) { diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java index 869b278..fc539b7 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/SharedApiService.java @@ -1,5 +1,8 @@ package enseirb.myinpulse.service; +import com.itextpdf.text.*; +import com.itextpdf.text.pdf.PdfWriter; + import enseirb.myinpulse.model.*; import enseirb.myinpulse.service.database.*; @@ -10,9 +13,6 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; -import com.itextpdf.text.*; -import com.itextpdf.text.pdf.PdfWriter; - import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.time.LocalDateTime; @@ -92,7 +92,7 @@ public class SharedApiService { HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } Project project = this.projectService.getProjectById(projectId); - return project.getAdministrator(); + return project.getProjectAdministrator(); } // TODO @@ -221,6 +221,13 @@ public class SharedApiService { HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } logger.info("User {} tried to create an appointment for project {}", mail, projectId); - this.appointmentService.addNewAppointment(appointment); + Appointment newAppointment = this.appointmentService.addNewAppointment(appointment); + newAppointment + .getAppointmentListSectionCell() + .forEach( + sectionCell -> { + sectionCell.updateAppointmentSectionCell(newAppointment); + }); + newAppointment.getAppointmentReport().setAppointmentReport(newAppointment); } } diff --git a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/ProjectService.java b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/ProjectService.java index 32fc4c4..7eb0651 100644 --- a/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/ProjectService.java +++ b/MyINPulse-back/src/main/java/enseirb/myinpulse/service/database/ProjectService.java @@ -91,7 +91,7 @@ public class ProjectService { } if (administrator != null) { - project.get().setAdministrator(administrator); + project.get().setProjectAdministrator(administrator); } return this.projectRepository.save(project.get());