fix: updating foreign keys when adding new entity to the db
This commit is contained in:
		@@ -39,4 +39,28 @@ public class Administrator extends User {
 | 
			
		||||
            String phoneNumber) {
 | 
			
		||||
        super(null, userSurname, username, primaryMail, secondaryMail, phoneNumber);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<Project> getListProject() {
 | 
			
		||||
        return listProject;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void updateListProject(Project project) {
 | 
			
		||||
        listProject.add(project);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<Annotation> getListAnnotation() {
 | 
			
		||||
        return listAnnotation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void updateListAnnotation(Annotation annotation) {
 | 
			
		||||
        listAnnotation.add(annotation);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MakeAppointment getMakeAppointment() {
 | 
			
		||||
        return makeAppointment;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setMakeAppointment(MakeAppointment makeAppointment) {
 | 
			
		||||
        this.makeAppointment = makeAppointment;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -112,6 +112,10 @@ public class Appointment {
 | 
			
		||||
        return listSectionCell;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void updateListSectionCell(SectionCell sectionCell) {
 | 
			
		||||
        listSectionCell.add(sectionCell);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Report getAppointmentReport() {
 | 
			
		||||
        return report;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Entrepreneur> getListEntrepreneurParticipation() {
 | 
			
		||||
        return listEntrepreneurParticipation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setAdministrator(Administrator administrator) {
 | 
			
		||||
        this.projectAdministrator = administrator;
 | 
			
		||||
    public void updateListEntrepreneurParticipation(Entrepreneur projectParticipant) {
 | 
			
		||||
        listEntrepreneurParticipation.add(projectParticipant);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<SectionCell> 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ import java.util.List;
 | 
			
		||||
public class SectionCell {
 | 
			
		||||
 | 
			
		||||
    @ManyToMany(mappedBy = "listSectionCell")
 | 
			
		||||
    private final List<Appointment> appointment = new ArrayList<>();
 | 
			
		||||
    private final List<Appointment> listAppointment = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    @OneToMany(mappedBy = "sectionCellAnnotation", fetch = FetchType.LAZY, orphanRemoval = true)
 | 
			
		||||
    private final List<Annotation> 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<Appointment> getAppointmentSectionCell() {
 | 
			
		||||
        return appointment;
 | 
			
		||||
        return listAppointment;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void updateAppointmentSectionCell(Appointment appointment) {
 | 
			
		||||
        listAppointment.add(appointment);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<Annotation> 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -91,7 +91,7 @@ public class ProjectService {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (administrator != null) {
 | 
			
		||||
            project.get().setAdministrator(administrator);
 | 
			
		||||
            project.get().setProjectAdministrator(administrator);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return this.projectRepository.save(project.get());
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user