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.EntrepreneurService; import enseirb.myinpulse.service.database.ProjectService; import enseirb.myinpulse.service.database.SectionCellService; 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; 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; @Autowired EntrepreneurApiService( SectionCellService sectionCellService, ProjectService projectService, UtilsService utilsService, UserService userService, EntrepreneurService entrepreneurService) { this.sectionCellService = sectionCellService; this.projectService = projectService; this.utilsService = utilsService; this.userService = userService; this.entrepreneurService = entrepreneurService; } public void editSectionCell(Long sectionCellId, String content, String mail) { SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId); if (sectionCell == null) { System.err.println("Trying to edit unknown section cell"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } 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()); sectionCell .getAppointmentSectionCell() .forEach( appointment -> { newSectionCell.updateAppointmentSectionCell(appointment); }); sectionCell .getListAnnotation() .forEach( annotation -> { newSectionCell.updateListAnnotation(annotation); }); this.addSectionCell(newSectionCell, mail); // sectionCellService.updateSectionCell(sectionCellId, content, null, null, null); } public void removeSectionCell(Long sectionCellId, String mail) { SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId); if (editSectionCell == null) { System.err.println("Trying to remove unknown section cell"); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } 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) { System.err.println("Trying to create an empty section cell"); throw new ResponseStatusException( HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide"); } if (sectionCell.getSectionId() == -1) { System.err.println("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 -> { appointment.updateListSectionCell(newSectionCell); }); newSectionCell .getListAnnotation() .forEach( annotation -> { annotation.setSectionCellAnnotation(newSectionCell); }); } public void requestNewProject(Project project, String mail) { if (project == null) { logger.error("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 with id {}", mail, project.getIdProject()); project.setProjectStatus(PENDING); project.setEntrepreneurProposed((Entrepreneur) this.userService.getUserByEmail(mail)); projectService.addNewProject(project); project.getProjectAdministrator().updateListProject(project); this.entrepreneurService.updateEntrepreneurProjectProposed( 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"); } }