package hu.sch.ejb;

import hu.sch.domain.config.Configuration;
import hu.sch.domain.config.Configuration.Environment;
import hu.sch.services.MailManagerLocal;
import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author aldaris
 */
@Stateless
public class MailManagerBean implements MailManagerLocal {

    private static final Logger log = LoggerFactory.getLogger(MailManagerBean.class);
    @Resource(name = "mail/korokMail")
    private Session mailSession;

    @Override
    public boolean sendEmail(String to, String subject, String message) {
        StringBuilder sb = new StringBuilder();
        sb.append("E-mail küldése ");
        sb.append(to);
        sb.append("-nak.");
        log.info(sb.toString());

        if (log.isDebugEnabled() || Configuration.getEnvironment() == Environment.TESTING) {
            StringBuilder sbd = new StringBuilder();

            sbd.append("E-mail küldése\n");
            sbd.append("Címzett: ");
            sbd.append(to);
            sbd.append("\nTárgy: ");
            sbd.append(subject);
            sbd.append("\nÜzenet: ");
            sbd.append(message);

            log.debug(sbd.toString());
        }

        Message msg = new MimeMessage(mailSession);
        try {
            if (log.isDebugEnabled()) {
                log.debug("Eredeti cím: " + to);
            }
            if (Configuration.getEnvironment() != Environment.PRODUCTION) {
                to = Configuration.getDevEmail();
            }
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

            msg.setSubject("[VIR Körök] " + subject);
            msg.setText(message);
            msg.setSentDate(new Date());

            if( Configuration.getEnvironment() != Environment.TESTING ) {
                // TESTING esetén ne küldjünk levelet!
                Transport.send(msg);
                log.info("Levél sikeresen elküldve.");
            }
        } catch (Exception ex) {
            log.error("Hiba az e-mail elküldése közben.", ex);
            return false;
        }

        return true;
    }
}

@@ -776,16 +776,22 @@
 ring to,
+ final
  String 
@@ -798,16 +798,22 @@
 subject,
+ final
  String 
@@ -835,605 +835,206 @@
     
-StringBuilder sb = new StringBuilder();%0A        sb.append(%22E-mail k%C3%BCld%C3%A9se %22);%0A        sb.append(to);%0A        sb.append(%22-nak.%22);%0A        log.info(sb.toString());%0A%0A        if (log.isDebugEnabled() %7C%7C Configuration.getEnvironment() == Environment.TESTING) %7B%0A            StringBuilder sbd = new StringBuilder();%0A%0A            sbd.append(%22E-mail k%C3%BCld%C3%A9se%5Cn%22);%0A            sbd.append(%22C%C3%ADmzett: %22);%0A            sbd.append(to);%0A            sbd.append(%22%5CnT%C3%A1rgy: %22);%0A            sbd.append(subject);%0A            sbd.append(%22%5Cn%C3%9Czenet: %22);%0A            sbd.append(message);%0A%0A            log.debug(sbd.toString()
+log.info(%22E-mail k%C3%BCld%C3%A9s, c%C3%ADmzett=%7B%7D%22, to);%0A%0A        if (log.isDebugEnabled() %7C%7C Configuration.getEnvironment() == Environment.TESTING) %7B%0A            log.debug(%22T%C3%A1rgy=%7B%7D%5Cn%C3%9Czenet=%7B%7D%22, subject, message
 );%0A 
@@ -1051,16 +1051,22 @@
         
+final 
 Message 
@@ -1066,18 +1066,19 @@
 essage m
-sg
+ail
  = new M
@@ -1120,111 +1120,8 @@
 y %7B%0A
-            if (log.isDebugEnabled()) %7B%0A                log.debug(%22Eredeti c%C3%ADm: %22 + to);%0A            %7D%0A
     
@@ -1246,33 +1246,92 @@
 ();%0A            
-%7D
+    log.debug(%22%5Bdev m%C3%B3d%5D %C3%BAj c%C3%ADmzett=%7B%7D%22, to);%0A            %7D%0A
 %0A            msg
@@ -1324,26 +1324,27 @@
            m
-sg
+ail
 .setRecipien
@@ -1417,26 +1417,27 @@
            m
-sg
+ail
 .setSubject(
@@ -1472,26 +1472,27 @@
            m
-sg
+ail
 .setText(mes
@@ -1511,18 +1511,19 @@
        m
-sg
+ail
 .setSent
@@ -1559,10 +1559,10 @@
   if
-(
  
+(
 Conf
@@ -1610,17 +1610,16 @@
 .TESTING
- 
 ) %7B%0A    
@@ -1705,10 +1705,11 @@
 nd(m
-sg
+ail
 );%0A 

Refactor mail sending * replace stringbuilders with string formats in log messages * remove unnecessary log messages * rename confusing variable (String message vs Message msg)