-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/RSJ-75' into dev
- Loading branch information
Showing
5 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Backend/src/main/java/unical/demacs/rdm/utils/ScheduleStatusUpdaterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package unical.demacs.rdm.utils; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import unical.demacs.rdm.persistence.entities.Schedule; | ||
import unical.demacs.rdm.persistence.enums.ScheduleStatus; | ||
import unical.demacs.rdm.persistence.repository.ScheduleRepository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ScheduleStatusUpdaterService { | ||
|
||
private final ScheduleRepository scheduleRepository; | ||
|
||
@Scheduled(fixedRate = 60000) // every minute todo: ragionare su frequenza | ||
@Transactional | ||
public void updateScheduleStatuses() { | ||
LocalDateTime now = LocalDateTime.now(); | ||
|
||
List<Schedule> activeSchedules = scheduleRepository.findByStatusNotInAndStartTimeBefore( | ||
List.of(ScheduleStatus.COMPLETED, ScheduleStatus.CANCELLED), | ||
now | ||
); | ||
|
||
for (Schedule schedule : activeSchedules) { | ||
updateScheduleStatus(schedule, now); | ||
} | ||
|
||
scheduleRepository.saveAll(activeSchedules); | ||
} | ||
|
||
|
||
|
||
private void updateScheduleStatus(Schedule schedule, LocalDateTime now) { | ||
LocalDateTime endTime = schedule.getStartTime().plusSeconds(schedule.getDuration()); | ||
|
||
if (now.isBefore(schedule.getStartTime())) { | ||
schedule.setStatus(ScheduleStatus.SCHEDULED); | ||
} else if (now.isAfter(schedule.getStartTime()) && now.isBefore(endTime)) { | ||
schedule.setStatus(ScheduleStatus.IN_PROGRESS); | ||
} else if (now.isAfter(endTime)) { | ||
schedule.setStatus(ScheduleStatus.COMPLETED); | ||
} | ||
} | ||
} |