package enseirb.myinpulse.service.database; import enseirb.myinpulse.model.Annotation; import enseirb.myinpulse.model.Appointment; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.repository.SectionCellRepository; 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; import java.util.List; import java.util.Optional; @Service public class SectionCellService { protected static final Logger logger = LogManager.getLogger(); private final SectionCellRepository sectionCellRepository; @Autowired SectionCellService(SectionCellRepository sectionCellRepository) { this.sectionCellRepository = sectionCellRepository; } public Iterable getAllSectionCells() { return this.sectionCellRepository.findAll(); } public SectionCell getSectionCellById(Long id) { Optional sectionCell = this.sectionCellRepository.findById(id); if (sectionCell.isEmpty()) { logger.error("getSectionCellById : No sectionCell found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } return sectionCell.get(); } public SectionCell addNewSectionCell(SectionCell sectionCell) { return this.sectionCellRepository.save(sectionCell); } public void removeSectionCellById(Long id) { this.sectionCellRepository.deleteById(id); } public void updateSectionCellReferenceId(Long idSectionCell, Long referenceId) { SectionCell sectionCell = this.getSectionCellById(idSectionCell); sectionCell.setIdReference(referenceId); this.sectionCellRepository.save(sectionCell); } public void updateSectionCellContent(long idSectionCell, String content) { SectionCell sectionCell = getSectionCellById(idSectionCell); sectionCell.setContentSectionCell(content); this.sectionCellRepository.save(sectionCell); } public void updateSectionCellListAppointment(long idSectionCell, Appointment appointment) { SectionCell sectionCell = getSectionCellById(idSectionCell); sectionCell.updateAppointmentSectionCell(appointment); this.sectionCellRepository.save(sectionCell); } public void updateSectionCellListAnnotation(long idSectionCell, Annotation annotation) { SectionCell sectionCell = getSectionCellById(idSectionCell); sectionCell.updateListAnnotation(annotation); this.sectionCellRepository.save(sectionCell); } public void updateSectionCellProject(long idSectionCell, Project project) { SectionCell sectionCell = getSectionCellById(idSectionCell); sectionCell.setProjectSectionCell(project); this.sectionCellRepository.save(sectionCell); } public SectionCell updateSectionCell( Long id, String contentSectionCell, Appointment appointment, Annotation annotation, Project project) { Optional sectionCell = this.sectionCellRepository.findById(id); if (sectionCell.isEmpty()) { logger.error("updateSectionCell : No sectionCell found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } if (contentSectionCell != null) { sectionCell.get().setContentSectionCell(contentSectionCell); sectionCell.get().setModificationDate(LocalDateTime.now()); } if (appointment != null) { sectionCell.get().updateAppointmentSectionCell(appointment); sectionCell.get().setModificationDate(LocalDateTime.now()); } if (annotation != null) { sectionCell.get().updateListAnnotation(annotation); sectionCell.get().setModificationDate(LocalDateTime.now()); } if (project != null) { sectionCell.get().setProjectSectionCell(project); sectionCell.get().setModificationDate(LocalDateTime.now()); } return this.sectionCellRepository.save(sectionCell.get()); } public Iterable getSectionCellsByProject(Project project, Long sectionId) { return this.sectionCellRepository.findByProjectSectionCellAndSectionId(project, sectionId); } public Long getProjectId(Long sectionCellId) { SectionCell sectionCell = getSectionCellById(sectionCellId); Project sectionProject = sectionCell.getProjectSectionCell(); return sectionProject.getIdProject(); } public List getAppointmentsBySectionCellId(Long sectionCellId) { SectionCell sectionCell = getSectionCellById(sectionCellId); return sectionCell.getAppointmentSectionCell(); } public Iterable getSectionCellsByProjectAndSectionIdBeforeDate( Project project, long sectionId, LocalDateTime date) { return sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore( project, sectionId, date); } }