/*
 * Wegas
 * http://www.albasim.com/wegas/
 *
 * Copyright (c) 2013 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
package com.wegas.messaging.ejb;

import com.wegas.core.persistence.game.Player;
import com.wegas.messaging.persistence.Message;
import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.enterprise.event.Observes;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @fixme @important The mail should be sent in an async queue, so they don't hang the reply endlessly
 *
 * @author Francois-Xavier Aeberhard <fx@red-agent.com>
 */
@Stateless
@LocalBean
public class EMailFacade {


    /**
     *
     * @param to
     * @param from
     * @param subject
     * @param body
     */
    public void send(String to, String from,
            String subject, String body) {

        Properties props = System.getProperties();

        ResourceBundle res = ResourceBundle.getBundle("wegas");
        props.put("mail.smtp.host", res.getString("mail.smtp.host"));           // Attaching to default Session, or we could start a new one

        Session session = Session.getDefaultInstance(props, null);

        javax.mail.Message msg = new MimeMessage(session);                                 // Create a new message

        try {
            msg.setFrom(new InternetAddress(from));                             // Set the FROM and TO fields
            msg.setRecipients(javax.mail.Message.RecipientType.TO,
                    InternetAddress.parse(to, false));
            // -- We could include CC recipients too --
            // if (cc != null)
            // msg.setRecipients(Message.RecipientType.CC
            // ,InternetAddress.parse(cc, false));

            msg.setSubject(subject);                                            // Set the subject and body text
            msg.setText(body);
            msg.setSentDate(new Date());
            Transport.send(msg);                                                // Send the message
            System.out.println("Message sent OK.");
        }
        catch (MessagingException ex) {
            Logger.getLogger(EMailFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param p
     * @param from
     * @param subject
     * @param body
     */
    public void send(Player p, String from, String subject, String body) {
        this.send(p.getUser().getName(), from, subject, body);
    }

    /**
     *
     * @param p
     * @param msg
     */
    public void send(Player p, Message msg) {
        this.send(p, "admin@wegas.com", msg.getSubject(), msg.getBody());
    }
    /**
     *
     * @param messageEvent
     */
    public void listener(@Observes MessageEvent messageEvent) {
        // @fixme remove this hardcoded condition w/ some db values or at least a line in the prop file
//        if (messageEvent.getType().equals("important")) {
//            this.send("fx@red-agent.com", "admin@wegas.com",
//                    messageEvent.getMessage().getSubject(),
//                    messageEvent.getMessage().getBody());
//        }
    }
}

@@ -541,30 +541,105 @@
 ax.mail.
-MessagingExcep
+Authenticator;%0Aimport javax.mail.MessagingException;%0Aimport javax.mail.PasswordAuthentica
 tion;%0Aim
@@ -858,16 +858,19 @@
 ey don't
+%0A *
  hang th
@@ -1000,17 +1000,16 @@
 cade %7B%0A%0A
-%0A
     /**%0A
@@ -1220,18 +1220,12 @@
 s = 
-System.get
+new 
 Prop
@@ -1234,24 +1234,29 @@
 ties();%0A
-%0A
+ 
        
+final
  Resourc
@@ -1299,17 +1299,165 @@
 le(%22
-wegas
+systemsettings%22);%0A        final String username = res.getString(%22mail.smtp.username%22);%0A        final String password = res.getString(%22mail.smtp.password
 %22);%0A
+%0A
     
@@ -1606,64 +1606,996 @@
     
-Session session = Session.getDefaultInstance(props, null
+props.setProperty(%22mail.smtp.auth%22, res.getString(%22mail.smtp.auth%22));%0A        props.put(%22mail.smtp.port%22, res.getString(%22mail.smtp.port%22));%0A        if (res.getString(%22mail.smtp.starttls.enable%22).equals(%22true%22)) %7B        // TLS%0A            props.put(%22mail.smtp.starttls.enable%22, %22true%22);%0A            props.put(%22mail.smtp.ssl.trust%22, res.getString(%22mail.smtp.host%22));%0A        %7D else %7B                                                                // SSL%0A            props.put(%22mail.smtp.socketFactory.class%22, %22javax.net.ssl.SSLSocketFactory%22);%0A            props.put(%22mail.smtp.socketFactory.port%22, res.getString(%22mail.smtp.port%22));%0A            props.put(%22mail.smtp.ssl.trust%22, res.getString(%22mail.smtp.host%22));%0A        %7D%0A%0A%0A        Session session = Session.getInstance(props, new Authenticator() %7B%0A            @Override%0A            protected PasswordAuthentication getPasswordAuthentication() %7B%0A                return new PasswordAuthentication(username, password);%0A            %7D%0A        %7D
 );%0A%0A
@@ -3499,24 +3499,16 @@
        %7D
-%0A       
  catch (
@@ -4059,24 +4059,25 @@
 y());%0A    %7D%0A
+%0A
     /**%0A    

Send email with login