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.HashMap; import java.util.List; import java.util.Map; 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); } public Iterable getLatestSectionCellsByIdReferenceBeforeDate( Project project, long sectionId, LocalDateTime date) { // 1. Fetch ALL relevant SectionCells modified before the date Iterable allMatchingCells = sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore( project, sectionId, date); // 2. Find the latest for each idReference Map latestCellsByIdReference = new HashMap<>(); for (SectionCell cell : allMatchingCells) { Long idReference = cell.getIdReference(); // Check if we've seen this idReference before if (latestCellsByIdReference.containsKey(idReference)) { // If yes, compare modification dates SectionCell existingLatest = latestCellsByIdReference.get(idReference); // If the current cell is more recent, update the map if (cell.getModificationDate().isAfter(existingLatest.getModificationDate())) { latestCellsByIdReference.put(idReference, cell); } } else { // If this is the first time we see this idReference, add it to the map latestCellsByIdReference.put(idReference, cell); } } // 3. Return the collection of the latest cells (the values from the map) return latestCellsByIdReference.values(); } }