Search in sources :

Example 1 with EmailTemplate

use of pl.morecraft.dev.settler.domain.EmailTemplate in project Settler by EmhyrVarEmreis.

the class EmailService method sendEmail.

@Async
public void sendEmail(List<String> to, EmailTemplate emailTemplate, Map<String, String> options) {
    EmailTemplateTmp emailTemplateTmp = EmailTemplateTmp.from(emailTemplate);
    for (Map.Entry<String, String> optionsEntry : options.entrySet()) {
        emailTemplateTmp.subject = emailTemplateTmp.subject.replaceAll("\\$" + optionsEntry.getKey(), optionsEntry.getValue());
        emailTemplateTmp.content = emailTemplateTmp.content.replaceAll("\\$" + optionsEntry.getKey(), optionsEntry.getValue());
    }
    MimeMessagePreparator preparator = mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setTo(to.toArray(new String[to.size()]));
        message.setFrom(settlerProperties.getMail().getFromAddress());
        message.setSubject(emailTemplateTmp.subject);
        message.setText(emailTemplateTmp.content, true);
    };
    try {
        mailSender.send(preparator);
    } catch (Exception e) {
        log.error("Error occurred during sendEmail", e);
    }
}
Also used : Async(org.springframework.scheduling.annotation.Async) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) EmailTemplateRepository(pl.morecraft.dev.settler.dao.repository.EmailTemplateRepository) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) HashMap(java.util.HashMap) List(java.util.List) Language(pl.morecraft.dev.settler.domain.dictionaries.Language) Stream(java.util.stream.Stream) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Transaction(pl.morecraft.dev.settler.domain.Transaction) Service(org.springframework.stereotype.Service) EmailTemplate(pl.morecraft.dev.settler.domain.EmailTemplate) Map(java.util.Map) EmailTemplateType(pl.morecraft.dev.settler.domain.dictionaries.EmailTemplateType) Collections(java.util.Collections) SettlerProperties(pl.morecraft.dev.settler.configuration.SettlerProperties) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) Async(org.springframework.scheduling.annotation.Async)

Example 2 with EmailTemplate

use of pl.morecraft.dev.settler.domain.EmailTemplate in project Settler by EmhyrVarEmreis.

the class EmailService method sendNotificationEmailNewTransaction.

public void sendNotificationEmailNewTransaction(Transaction transaction) {
    EmailTemplate emailTemplate = emailTemplateRepository.findOneByTypeAndLanguage(EmailTemplateType.NEW_TRANSACTION, Language.EN_GB);
    if (emailTemplate != null) {
        Stream.concat(transaction.getContractors() == null ? Stream.empty() : transaction.getContractors().stream(), transaction.getOwners() == null ? Stream.empty() : transaction.getOwners().stream()).filter(redistribution -> !redistribution.getId().getUser().getId().equals(transaction.getCreator().getId())).forEach(redistribution -> {
            Map<String, String> options = new HashMap<>();
            options.put("description", transaction.getDescription());
            options.put("creator", transaction.getCreator().getLogin());
            options.put("user", redistribution.getId().getUser().getFirstName());
            options.put("transactionId", transaction.getReference());
            options.put("value", String.format("%.2f", redistribution.getPercentage() * transaction.getValue() / 100.0));
            options.put("total", String.format("%.2f", transaction.getValue()));
            sendEmail(Collections.singletonList(redistribution.getId().getUser().getEmail()), emailTemplate, options);
        });
    }
}
Also used : Async(org.springframework.scheduling.annotation.Async) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) EmailTemplateRepository(pl.morecraft.dev.settler.dao.repository.EmailTemplateRepository) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) HashMap(java.util.HashMap) List(java.util.List) Language(pl.morecraft.dev.settler.domain.dictionaries.Language) Stream(java.util.stream.Stream) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Transaction(pl.morecraft.dev.settler.domain.Transaction) Service(org.springframework.stereotype.Service) EmailTemplate(pl.morecraft.dev.settler.domain.EmailTemplate) Map(java.util.Map) EmailTemplateType(pl.morecraft.dev.settler.domain.dictionaries.EmailTemplateType) Collections(java.util.Collections) SettlerProperties(pl.morecraft.dev.settler.configuration.SettlerProperties) HashMap(java.util.HashMap) EmailTemplate(pl.morecraft.dev.settler.domain.EmailTemplate)

Aggregations

Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Stream (java.util.stream.Stream)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)2 Async (org.springframework.scheduling.annotation.Async)2 Service (org.springframework.stereotype.Service)2 SettlerProperties (pl.morecraft.dev.settler.configuration.SettlerProperties)2 EmailTemplateRepository (pl.morecraft.dev.settler.dao.repository.EmailTemplateRepository)2 EmailTemplate (pl.morecraft.dev.settler.domain.EmailTemplate)2 Transaction (pl.morecraft.dev.settler.domain.Transaction)2 EmailTemplateType (pl.morecraft.dev.settler.domain.dictionaries.EmailTemplateType)2 Language (pl.morecraft.dev.settler.domain.dictionaries.Language)2