47 lines
1.5 KiB
Java
47 lines
1.5 KiB
Java
package eu.dnetlib.manager.wf.notification;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import eu.dnetlib.manager.wf.model.NotificationCondition;
|
|
import eu.dnetlib.manager.wf.repository.WorkflowSubscriptionRepository;
|
|
import eu.dnetlib.manager.wf.workflows.procs.WorkflowProcess;
|
|
import eu.dnetlib.manager.wf.workflows.procs.WorkflowProcess.Status;
|
|
import eu.dnetlib.notifications.mail.EmailDispatcher;
|
|
|
|
@Service
|
|
public class EmailSender {
|
|
|
|
private static final Log log = LogFactory.getLog(EmailSender.class);
|
|
|
|
@Autowired
|
|
private WorkflowSubscriptionRepository wfSubscriptionRepository;
|
|
|
|
@Autowired
|
|
private EmailDispatcher dispatcher;
|
|
|
|
public void sendMails(final WorkflowProcess proc) {
|
|
|
|
wfSubscriptionRepository.findByWfInstanceId(proc.getWfInstanceId()).forEach(s -> {
|
|
if (s.getCondition() == NotificationCondition.ALWAYS ||
|
|
s.getCondition() == NotificationCondition.ONLY_FAILED && proc.getStatus() == Status.FAILURE ||
|
|
s.getCondition() == NotificationCondition.ONLY_SUCCESS && proc.getStatus() == Status.SUCCESS) {
|
|
try {
|
|
final Map<String, Object> params = new HashMap<>();
|
|
|
|
dispatcher.sendStoredMail(s.getEmail(), s.getMessageId(), params);
|
|
|
|
} catch (final Exception e) {
|
|
log.error("Error sending mail to " + s.getEmail(), e);
|
|
}
|
|
}
|
|
|
|
});
|
|
}
|
|
}
|