feat : add qvm countdown job
This commit is contained in:
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -39,4 +39,7 @@ public interface QvmOperationService {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public QvmOperation createNextOperation(QvmOperation operation, Long userId)
|
public QvmOperation createNextOperation(QvmOperation operation, Long userId)
|
||||||
throws AxelorException;
|
throws AxelorException;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void updateAllCountdowns()throws AxelorException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,10 +28,13 @@ import com.axelor.i18n.I18n;
|
|||||||
import com.axelor.inject.Beans;
|
import com.axelor.inject.Beans;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.persist.Transactional;
|
import com.google.inject.persist.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import com.axelor.apps.qvm.exception.IExceptionMessage;
|
import com.axelor.apps.qvm.exception.IExceptionMessage;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
public class QvmOperationServiceImpl implements QvmOperationService {
|
public class QvmOperationServiceImpl implements QvmOperationService {
|
||||||
|
|
||||||
@ -152,4 +155,27 @@ public class QvmOperationServiceImpl implements QvmOperationService {
|
|||||||
System.out.println("Create next operation : "+nextOperation);
|
System.out.println("Create next operation : "+nextOperation);
|
||||||
return 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<QvmOperation> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -165,4 +165,14 @@ public class QvmOperationController {
|
|||||||
throw e;
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user