package enseirb.myinpulse.service; import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING; import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.service.database.*; 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; import org.springframework.web.server.ResponseStatusException; import java.time.LocalDateTime; @Service public class EntrepreneurApiService { protected static final Logger logger = LogManager.getLogger(); private final SectionCellService sectionCellService; private final ProjectService projectService; private final UtilsService utilsService; private final UserService userService; private final EntrepreneurService entrepreneurService; private final AdministratorService administratorService; private final AppointmentService appointmentService; private final AnnotationService annotationService; @Autowired EntrepreneurApiService( SectionCellService sectionCellService, ProjectService projectService, UtilsService utilsService, UserService userService, EntrepreneurService entrepreneurService, AdministratorService administratorService, AppointmentService appointmentService, AnnotationService annotationService) { this.sectionCellService = sectionCellService; this.projectService = projectService; this.utilsService = utilsService; this.userService = userService; this.entrepreneurService = entrepreneurService; this.administratorService = administratorService; this.appointmentService = appointmentService; this.annotationService = annotationService; } public void editSectionCell(Long sectionCellId, String content, String mail) { if (sectionCellId == null) { logger.warn("Trying to edit unknown section cell"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId); if (!utilsService.isAllowedToCheckProject( mail, this.sectionCellService.getProjectId(sectionCellId))) { logger.warn( "User {} tried to edit section cells {} of the project {} but is not allowed to.", mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); throw new ResponseStatusException( HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } logger.info( "User {} edited section cell {} of the project with id {}", mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); SectionCell newSectionCell = new SectionCell( null, sectionCell.getSectionId(), content, LocalDateTime.now(), sectionCell.getProjectSectionCell()); newSectionCell.setIdReference(sectionCell.getIdReference()); this.addSectionCell(newSectionCell, mail); sectionCell .getAppointmentSectionCell() .forEach( appointment -> { this.appointmentService.updateAppointmentListSectionCell( appointment.getIdAppointment(), newSectionCell); }); sectionCell .getListAnnotation() .forEach( annotation -> { this.annotationService.updateAnnotationSectionCell( annotation.getIdAnnotation(), newSectionCell); }); } public void removeSectionCell(Long sectionCellId, String mail) { if (sectionCellId == null) { logger.warn("Trying to remove unknown section cell"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); if (!utilsService.isAllowedToCheckProject( mail, this.sectionCellService.getProjectId(sectionCellId))) { logger.warn( "User {} tried to remove section cells {} of the project {} but is not allowed to.", mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); throw new ResponseStatusException( HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } logger.info( "User {} removed section cell {} of the project with id {}", mail, sectionCellId, this.sectionCellService.getProjectId(sectionCellId)); SectionCell removedSectionCell = new SectionCell( null, -1L, "", LocalDateTime.now(), this.projectService.getProjectById( editSectionCell.getProjectSectionCell().getIdProject())); sectionCellService.addNewSectionCell(removedSectionCell); this.sectionCellService.updateSectionCellReferenceId( removedSectionCell.getIdSectionCell(), editSectionCell.getIdReference()); projectService.updateProjectListSectionCell( sectionCellService.getProjectId(sectionCellId), removedSectionCell); // sectionCellService.removeSectionCellById(sectionCellId); } public void addSectionCell(SectionCell sectionCell, String mail) { if (sectionCell == null) { logger.warn("Trying to create an empty section cell"); throw new ResponseStatusException( HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide"); } if (sectionCell.getSectionId() == -1) { logger.warn("Trying to create an illegal section cell"); throw new ResponseStatusException( HttpStatus.BAD_REQUEST, "La cellule de section fournie n'est pas valide"); } if (!utilsService.isAllowedToCheckProject( mail, sectionCell.getProjectSectionCell().getIdProject())) { logger.warn( "User {} tried to add a section cell to the project {} but is not allowed to.", mail, sectionCell.getProjectSectionCell().getIdProject()); throw new ResponseStatusException( HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } logger.info( "User {} added a new section cell {} to the project {}", mail, sectionCell.getIdSectionCell(), sectionCell.getProjectSectionCell().getIdProject()); SectionCell newSectionCell = sectionCellService.addNewSectionCell( sectionCell); // if here, logger fails cause id is null (not added yet) newSectionCell.getProjectSectionCell().updateListSectionCell(newSectionCell); newSectionCell .getAppointmentSectionCell() .forEach( appointment -> { this.appointmentService.updateAppointmentListSectionCell( appointment.getIdAppointment(), newSectionCell); }); newSectionCell .getListAnnotation() .forEach( annotation -> { this.annotationService.updateAnnotationSectionCell( annotation.getIdAnnotation(), newSectionCell); }); } public void requestNewProject(Project project, String mail) { if (project == null) { logger.warn("Trying to request the creation of a null project"); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le projet fourni est vide"); } logger.info("User {} created a new project named {}", mail, project.getProjectName()); project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail)); projectService.addNewProject(project); this.projectService.updateProjectStatus(project.getIdProject(), PENDING); if (project.getProjectAdministrator() != null) { this.administratorService.updateAdministratorListProject( project.getProjectAdministrator().getIdUser(), project); } this.entrepreneurService.updateEntrepreneurProjectProposed( this.userService.getUserByEmail(mail).getIdUser(), project); this.entrepreneurService.updateEntrepreneurProjectParticipation( this.userService.getUserByEmail(mail).getIdUser(), project); project.getListEntrepreneurParticipation() .forEach( entrepreneur -> this.entrepreneurService.updateEntrepreneurProjectParticipation( entrepreneur.getIdUser(), project)); project.getListSectionCell() .forEach( sectionCell -> this.sectionCellService.updateSectionCellProject( sectionCell.getIdSectionCell(), project)); } public void createAccount(Entrepreneur e) { try { userService.getUserByEmail(e.getPrimaryMail()); logger.error("The user {} already exists in the system", e.getPrimaryMail()); } catch (ResponseStatusException err) { this.entrepreneurService.addEntrepreneur(e); return; } throw new ResponseStatusException(HttpStatus.CONFLICT, "User already exists in the system"); } }