diff --git a/src/main/java/com/systelab/seed/App.java b/src/main/java/com/systelab/seed/App.java index 5dde697..2bc10db 100644 --- a/src/main/java/com/systelab/seed/App.java +++ b/src/main/java/com/systelab/seed/App.java @@ -9,12 +9,14 @@ import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.context.annotation.Bean; @EnableCircuitBreaker @EnableHystrixDashboard @EnableFeignClients @EnableCaching +@EnableScheduling @SpringBootApplication public class App { diff --git a/src/main/java/com/systelab/seed/model/ModelBase.java b/src/main/java/com/systelab/seed/model/ModelBase.java index 5bccc30..c048c1f 100644 --- a/src/main/java/com/systelab/seed/model/ModelBase.java +++ b/src/main/java/com/systelab/seed/model/ModelBase.java @@ -33,4 +33,6 @@ public abstract class ModelBase { @JsonIgnore protected Timestamp updateTime; + protected Boolean active = Boolean.TRUE; + } diff --git a/src/main/java/com/systelab/seed/repository/PatientRepository.java b/src/main/java/com/systelab/seed/repository/PatientRepository.java index 08b2409..e102542 100644 --- a/src/main/java/com/systelab/seed/repository/PatientRepository.java +++ b/src/main/java/com/systelab/seed/repository/PatientRepository.java @@ -2,9 +2,13 @@ import com.systelab.seed.model.patient.Patient; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import java.util.Date; import java.util.Optional; import java.util.UUID; @@ -13,4 +17,9 @@ public interface PatientRepository extends JpaRepository { Optional findById(@Param("id") UUID id); + @Modifying + @Transactional + @Query("update Patient p set p.active = FALSE where p.updateTime < ?1") + int setActiveForUpdatedBefore(Date somedate); + } \ No newline at end of file diff --git a/src/main/java/com/systelab/seed/service/PatientMaintenanceService.java b/src/main/java/com/systelab/seed/service/PatientMaintenanceService.java new file mode 100644 index 0000000..8bdbfb0 --- /dev/null +++ b/src/main/java/com/systelab/seed/service/PatientMaintenanceService.java @@ -0,0 +1,27 @@ +package com.systelab.seed.service; + +import com.systelab.seed.repository.PatientRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.util.Calendar; + +@Service +public class PatientMaintenanceService { + + private Logger logger = LoggerFactory.getLogger(PatientMaintenanceService.class); + + @Autowired + private PatientRepository patientRepository; + + @Scheduled(cron = "${patient.maintenance.cron.expression}") + public void schedulePurgeOlderRecordsTask() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.YEAR, -1); + this.patientRepository.setActiveForUpdatedBefore(cal.getTime()); + logger.info("Patients DB purged!"); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 308ce5a..279124d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -90,4 +90,10 @@ bucket4j: - bandwidths: - capacity: 10000 time: 1 - unit: minutes \ No newline at end of file + unit: minutes + +patient: + maintenance: + cron: + # Check https://www.baeldung.com/cron-expressions + expression: "0/20 * * * * *"