Snippets
Created by
Fabio Bottan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | package com.axelor.apps.hr.job;
import com.axelor.apps.base.db.Company;
import com.axelor.apps.base.db.repo.CompanyRepository;
import com.axelor.apps.hr.db.Employee;
import com.axelor.apps.hr.db.HRConfig;
import com.axelor.apps.hr.db.repo.EmployeeHRRepository;
import com.axelor.apps.hr.db.repo.TimesheetHRRepository;
import com.axelor.apps.hr.service.config.HRConfigService;
import com.axelor.apps.message.service.TemplateMessageService;
import com.axelor.exception.AxelorException;
import com.google.inject.Inject;
import java.lang.invoke.MethodHandles;
import java.time.LocalDate;
import java.util.List;
import javax.transaction.Transactional;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TimesheetReminderEmployee implements Job {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected EmployeeHRRepository employeeRepository;
protected TemplateMessageService templateMessageService;
protected HRConfigService hrConfigService;
protected CompanyRepository companyRepository;
protected TimesheetHRRepository timesheetHRRepository;
@Inject
public TimesheetReminderEmployee(
EmployeeHRRepository employeeRepository,
TemplateMessageService templateMessageService,
HRConfigService hrConfigService,
CompanyRepository companyRepository,
TimesheetHRRepository timesheetHRRepository) {
this.employeeRepository = employeeRepository;
this.templateMessageService = templateMessageService;
this.hrConfigService = hrConfigService;
this.companyRepository = companyRepository;
this.timesheetHRRepository = timesheetHRRepository;
}
private boolean isRunning(JobExecutionContext context) {
try {
return context
.getScheduler()
.getCurrentlyExecutingJobs()
.stream()
.filter(j -> j.getTrigger().equals(context.getTrigger()))
.filter(j -> !j.getFireInstanceId().equals(context.getFireInstanceId()))
.findFirst()
.isPresent();
} catch (SchedulerException e) {
return false;
}
}
@Override
@Transactional
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info("TimesheetReminderEmployee - START JOB");
try {
List<Company> companyList = this.companyRepository.all().fetch();
for (Company company : companyList) {
HRConfig hrConfig = hrConfigService.getHRConfig(company);
if (hrConfig.getTimesheetMailNotification()) {
// Return list of all employee who didn't submitted timesheet today;
List<Employee> employeeList =
this.employeeRepository.findAllEmployeeForTimesheetReminderPayCompany(company);
employeeList.forEach(
employee -> {
LocalDate today = LocalDate.now();
if (employee.getDateOfHire().isBefore(today)
&& (employee
.getEmploymentContractList()
.get(employee.getEmploymentContractList().size() - 1)
.getEndDate()
== null
|| employee
.getEmploymentContractList()
.get(employee.getEmploymentContractList().size() - 1)
.getEndDate()
.isAfter(today))) {
if (this.timesheetHRRepository
.findForTimesheetReminderToday(employee, company, today)
.size()
== 0) {
// If employee didn't submitted yet his timesheet we send reminder email
log.info(
"Reminding Timesheet to {}", employee.getContactPartner().getFullName());
try {
templateMessageService.generateAndSendMessage(
employee, hrConfigService.getReminderTimesheetTemplate(hrConfig));
} catch (Exception e) {
log.error("TimesheetReminderEmployee Error Sending email to employee", e);
}
}
}
});
}
}
} catch (AxelorException e) {
log.error("TimesheetReminderEmployee Error", e);
throw new JobExecutionException(e);
} finally {
log.info("TimesheetReminderEmployee - COMPLETED EXECUTE");
}
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.