package at.ac.tuwien.inso.service.impl;

import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.repository.*;
import at.ac.tuwien.inso.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.mail.javamail.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.*;
import org.thymeleaf.*;
import org.thymeleaf.context.*;

import javax.mail.*;
import javax.mail.internet.*;

@Service
public class UserCreationServiceImpl implements UserCreationService {

    public static final String MAIL_SUBJECT = "user.account.activation.mail.subject";
    private static final String MAIL_TEMPLATE = "emails/user-account-activation-mail";

    @Autowired
    private PendingAccountActivationRepository pendingAccountActivationRepository;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private Messages messages;

    @Value("${uis.server.account.activation.url.prefix}")
    private String activationUrlPrefix;

    @Transactional
    @Override
    public PendingAccountActivation create(UisUser user) {
        PendingAccountActivation activation = pendingAccountActivationRepository.save(new PendingAccountActivation(user));

        mailSender.send(createActivationMail(activation));

        return activation;
    }

    private MimeMessage createActivationMail(PendingAccountActivation activation) {
        MimeMessage mimeMsg = mailSender.createMimeMessage();
        MimeMessageHelper msg = new MimeMessageHelper(mimeMsg, "UTF-8");

        try {
            msg.setTo(activation.getForUser().getEmail());
            msg.setSubject(messages.get(MAIL_SUBJECT));
            msg.setText(messageContent(activation), true);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        return msg.getMimeMessage();
    }

    private String messageContent(PendingAccountActivation activation) {
        Context context = new Context(Messages.LOCALE);
        context.setVariable("user", activation.getForUser());
        context.setVariable("activationUrl", activationUrlPrefix + activation.getId());

        return templateEngine.process(MAIL_TEMPLATE, context);
    }
}

@@ -45,164 +45,178 @@
 ort 
-at.ac.tuwien.inso.entity.*;%0Aimport at.ac.tuwien.inso.repository.*;%0Aimport at.ac.tuwien.inso.service.*;%0Aimport org.springframework.beans.factory.annotation.*
+org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.beans.factory.annotation.Value;%0Aimport org.springframework.mail.javamail.JavaMailSender
 ;%0Aim
@@ -250,25 +250,41 @@
 il.javamail.
-*
+MimeMessageHelper
 ;%0Aimport org
@@ -370,17 +370,29 @@
 otation.
-*
+Transactional
 ;%0Aimport
@@ -406,17 +406,30 @@
 ymeleaf.
-*
+TemplateEngine
 ;%0Aimport
@@ -451,17 +451,23 @@
 context.
-*
+Context
 ;%0A%0Aimpor
@@ -479,17 +479,34 @@
 ax.mail.
-*
+MessagingException
 ;%0Aimport
@@ -530,9 +530,245 @@
 net.
-*
+MimeMessage;%0A%0Aimport at.ac.tuwien.inso.entity.PendingAccountActivation;%0Aimport at.ac.tuwien.inso.entity.UisUser;%0Aimport at.ac.tuwien.inso.repository.PendingAccountActivationRepository;%0Aimport at.ac.tuwien.inso.service.UserCreationService
 ;%0A%0A@

Fix code style for imports