public class NotifyResponsibleAbo utAuditStartBean implements NotifyRe

 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
public class NotifyResponsibleAboutAuditStartBean implements NotifyResponsibleAboutAuditStart {
@Inject
private Persistence persistence;
@Inject
private EmailService emailService;
@Inject
private GlobalConfig globalConfig;
@Inject
private QMSProperty qmsProperty;
@Override
@Transactional
public void notifyResponsible() {
try (Transaction tx = persistence.createTransaction()){
Integer countDay = qmsProperty.getEmailAboutAuditStart();
Calendar calendar = new GregorianCalendar();
Date currentDate = calendar.getTime();
calendar.add(calendar.DATE, - countDay);
Date date = calendar.getTime();
String queryTemplate = "select a from qms$AuditPlan a where a.dateStart = :date";
// Ответственным за countDay дней до старта
TypedQuery<AuditPlan> query = persistence.getEntityManager().createQuery(queryTemplate, AuditPlan.class);
query.setParameter("date", date);
List<AuditPlan> audits = query.getResultList();
String url = globalConfig.getWebAppUrl();
for (AuditPlan audit : audits) {
Auditor responsible = audit.getResponsible();
String email = responsible.getEmployee().getEmail();
String link = String.format("%s/open?screen=qms$AuditPlan.edit&item=qms$AuditPlan-%s", url, audit.getId().toString());
String subject = countDay + " дней до начала аудита \"" + audit.getName();
sendEmail(email, subject, link);
}
// Отправка писем ответственным в день старта аудита
query = persistence.getEntityManager().createQuery(queryTemplate, AuditPlan.class);
query.setParameter("date", currentDate);
audits = query.getResultList();
if (audits != null) {
String url = globalConfig.getWebAppUrl();
for (AuditPlan audit : audits) {
Auditor responsible = audit.getResponsible();
Employee employee = responsible.getEmployee();
if (audit.getDateStartFact() == null) {
String email = employee.getEmail();
String link = String.format("%s/open?screen=qms$AuditPlan.edit&item=qms$AuditPlan-%s", url, audit.getId().toString());
String subject = "Начало аудита \"" + audit.getName();
sendEmail(email, subject, link);
}
}
}
tx.commit();
}
}
private void sendEmail(String email, String subject, String link) {
if (email != null) {
EmailInfo emailInfo = new EmailInfo(email, subject, link, subject);
emailService.sendEmailAsync(emailInfo);
}
}
}