diff --git a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/job/CalculateCountdownJob.java b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/job/CalculateCountdownJob.java new file mode 100644 index 0000000..0590fe7 --- /dev/null +++ b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/job/CalculateCountdownJob.java @@ -0,0 +1,48 @@ +package com.axelor.apps.qvm.job; + +import com.axelor.exception.service.TraceBackService; +import com.axelor.inject.Beans; +import java.util.List; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.quartz.SchedulerException; +import com.axelor.apps.qvm.service.QvmOperationService; +import com.axelor.apps.qvm.service.QvmOperationServiceImpl; +public class CalculateCountdownJob implements Job { + private final Logger log = LoggerFactory.getLogger(CalculateCountdownJob.class); + + @Override + public void execute(JobExecutionContext context) throws JobExecutionException{ + + if (isRunning(context)) { + return; + } + + try{ + log.debug("Calculating operation countdown ..."); + Beans.get(QvmOperationServiceImpl.class).updateAllCountdowns(); + }catch(Exception e){ + log.error("Error while operation countdown"); + TraceBackService.trace(e); + } + + } + + private boolean isRunning(JobExecutionContext context) { + try { + return context + .getScheduler() + .getCurrentlyExecutingJobs() + .stream() + .anyMatch( + j -> + j.getTrigger().equals(context.getTrigger()) + && !j.getFireInstanceId().equals(context.getFireInstanceId())); + } catch (SchedulerException e) { + return false; + } + } +} diff --git a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationService.java b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationService.java index 88c6727..652569b 100644 --- a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationService.java +++ b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationService.java @@ -39,4 +39,7 @@ public interface QvmOperationService { @Transactional public QvmOperation createNextOperation(QvmOperation operation, Long userId) throws AxelorException; + + @Transactional + public void updateAllCountdowns()throws AxelorException; } diff --git a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationServiceImpl.java b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationServiceImpl.java index 5d5439f..47d276b 100644 --- a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationServiceImpl.java +++ b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/service/QvmOperationServiceImpl.java @@ -28,10 +28,13 @@ import com.axelor.i18n.I18n; import com.axelor.inject.Beans; import com.google.inject.Inject; import com.google.inject.persist.Transactional; + +import java.time.LocalDate; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.axelor.apps.qvm.exception.IExceptionMessage; +import java.time.temporal.ChronoUnit; public class QvmOperationServiceImpl implements QvmOperationService { @@ -152,4 +155,27 @@ public class QvmOperationServiceImpl implements QvmOperationService { System.out.println("Create next operation : "+nextOperation); return nextOperation; } + + @Transactional + public void updateCountdown(QvmOperation operation, LocalDate today) { + if (operation.getOperationDate() != null) { + + LocalDate operationDate = operation.getOperationDate(); + int daysBetween = (int) ChronoUnit.DAYS.between(today, operationDate); + operation.setCountdown(daysBetween); + qvmOperationRepo.save(operation); + } + } + + public void updateAllCountdowns() { + List operations = qvmOperationRepo.all() + .filter("self.operationDate IS NOT NULL AND self.statusSelect = :status") + .bind("status", 2) + .fetch(); + LocalDate today = LocalDate.now(); + for (QvmOperation operation : operations){ + System.out.println("operation : "+operation); + updateCountdown(operation,today); + } + } } diff --git a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/web/QvmOperationController.java b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/web/QvmOperationController.java index e9ef93e..3b292a5 100644 --- a/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/web/QvmOperationController.java +++ b/modules/axelor-open-suite/axelor-qvm/src/main/java/com.axelor/apps.qvm/web/QvmOperationController.java @@ -165,4 +165,14 @@ public class QvmOperationController { throw e; } } + + public void updateCountdowns(ActionRequest request, ActionResponse response) { + try { + Beans.get(QvmOperationService.class).updateAllCountdowns(); + } catch (AxelorException e) { + TraceBackService.trace(e); + response.setError(e.getMessage()); + } + response.setFlash("Countdowns updated successfully"); + } }