package enseirb.myinpulse.service; import static enseirb.myinpulse.model.ProjectDecisionValue.ACTIVE; import static enseirb.myinpulse.model.ProjectDecisionValue.REJECTED; import enseirb.myinpulse.model.*; 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.util.ArrayList; import java.util.List; @Service public class AdminApiService { protected static final Logger logger = LogManager.getLogger(); private final ProjectService projectService; private final UserService userService; private final AdministratorService administratorService; private final UtilsService utilsService; private final AppointmentService appointmentService; private final ReportService reportService; private final SectionCellService sectionCellService; @Autowired AdminApiService( ProjectService projectService, UserService userService, AdministratorService administratorService, UtilsService utilsService, AppointmentService appointmentService, ReportService reportService, SectionCellService sectionCellService) { this.projectService = projectService; this.userService = userService; this.administratorService = administratorService; this.utilsService = utilsService; this.appointmentService = appointmentService; this.reportService = reportService; this.sectionCellService = sectionCellService; } // TODO: check if tests are sufficient - peer verification required public Iterable getProjectsOfAdmin(String mail) { return projectService.getProjectsByAdminId( administratorService.getAdministratorById( this.userService.getUserByEmail(mail).getIdUser())); } public Iterable getUpcomingAppointments(String mail) { logger.info("User {} check their upcoming appointments", mail); User user = this.userService.getUserByEmail(mail); List appointments = new ArrayList<>(); if (user instanceof Administrator) { List projects = new ArrayList<>(((Administrator) user).getListProject()); projects.forEach( project -> { project.getListSectionCell() .forEach( sectionCell -> { appointments.addAll( this.sectionCellService .getAppointmentsBySectionCellId( sectionCell .getIdSectionCell())); }); }); } if (user instanceof Entrepreneur) { Project project = ((Entrepreneur) user).getProjectParticipation(); project.getListSectionCell() .forEach( sectionCell -> { appointments.addAll( this.sectionCellService.getAppointmentsBySectionCellId( sectionCell.getIdSectionCell())); }); } return appointments; } // TODO: check if tests are sufficient - peer verification required public Iterable getPendingProjects() { return this.projectService.getPendingProjects(); } // TODO: check if tests are sufficient - peer verification required public void validateProject(ProjectDecision decision) { projectService.updateProject( decision.projectId, null, null, (decision.isAccepted == 1) ? ACTIVE : REJECTED, null, null, this.administratorService.getAdministratorById(decision.adminId)); } // TODO: check if tests are sufficient - peer verification required public void addNewProject(Project project) { project.setIdProject(null); // We remove the ID from the request to be sure that it will be auto generated try { this.projectService.getProjectByName(project.getProjectName(), true); throw new ResponseStatusException(HttpStatus.CONFLICT, "Project already exists"); } catch (ResponseStatusException e) { if (e.getStatusCode() == HttpStatus.CONFLICT) { throw new ResponseStatusException(HttpStatus.CONFLICT, "Project already exists"); } } Project newProject = projectService.addNewProject(project); if (project.getProjectAdministrator() != null) { newProject.getProjectAdministrator().updateListProject(newProject); } if (newProject.getEntrepreneurProposed() != null) { Entrepreneur proposed = newProject.getEntrepreneurProposed(); proposed.setProjectProposed(newProject); proposed.setProjectParticipation(newProject); } newProject .getListEntrepreneurParticipation() .forEach( participation -> { participation.setProjectParticipation(newProject); }); newProject .getListSectionCell() .forEach( sectionCell -> { sectionCell.setProjectSectionCell(newProject); }); } public void createAppointmentReport(long appointmentId, Report report, String mail) { long projectId = this.appointmentService .getAppointmentById(appointmentId) .getAppointmentListSectionCell() .getFirst() .getProjectSectionCell() .getIdProject(); if (!utilsService.isAllowedToCheckProject(mail, projectId)) { logger.warn( "User {} tried to add an report for appointment {} but is not allowed to.", mail, projectId); throw new ResponseStatusException( HttpStatus.UNAUTHORIZED, "You're not allowed to check this project"); } logger.info("User {} added a report for appointment {}", mail, projectId); Report addedReport = this.reportService.addNewReport(report); addedReport.setAppointmentReport(this.appointmentService.getAppointmentById(appointmentId)); this.appointmentService.getAppointmentById(appointmentId).setAppointmentReport(addedReport); } // TODO: test public void deleteProject(long projectId) { this.projectService.deleteProjectById(projectId); } }