From e3e954c125da0954b14ff47edba9c795f530261c Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Tue, 17 May 2016 17:37:29 +0200
Subject: [PATCH] Continued with rewrites, fixes: - used
 Base<Project-Name>DatabaseBean where possible - use new sendEmail() which
 prepares the wrapper instance and then sending it - the message object is now
 no longer created here as the Base<Project>DatabaseBean does it -
 resendConfirmationLink() almost finished: changing confirmation key + merging
 database is unfinished
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../AddressbookResendLinkSessionBean.java     |  98 ++++----------
 .../AddressbookEmailChangeSessionBean.java    | 125 +-----------------
 ...ddressbookUserRegistrationSessionBean.java |   2 +
 3 files changed, 33 insertions(+), 192 deletions(-)

diff --git a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java b/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
index feb4cde..825f4ef 100644
--- a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
+++ b/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
@@ -19,19 +19,11 @@ package org.mxchange.addressbook.beans.resendlink;
 import de.chotime.landingpage.beans.resendlink.ResendLinkSessionBeanRemote;
 import java.text.MessageFormat;
 import java.util.Locale;
-import javax.annotation.PostConstruct;
+import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.faces.FacesException;
-import javax.jms.Connection;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.ObjectMessage;
-import javax.jms.Queue;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.Session;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
+import javax.mail.Address;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
@@ -49,70 +41,10 @@ public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBea
 	 */
 	private static final long serialVersionUID = 71_546_726_857_195_360L;
 
-	/**
-	 * Connection
-	 */
-	private Connection connection;
-
-	/**
-	 * Object message
-	 */
-	private ObjectMessage message;
-
-	/**
-	 * Message producer
-	 */
-	private MessageProducer messageProducer;
-
-	/**
-	 * Mailer message queue
-	 */
-	private Queue queue;
-
-	/**
-	 * Session instance
-	 */
-	private Session session;
-
-	/**
-	 * Initialization of this bean
-	 */
-	@PostConstruct
-	public void init () {
-		// Trace message
-		this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
-
-		try {
-			// Get initial context
-			Context context = new InitialContext();
-
-			// Get factory from JMS resource
-			QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
-
-			// Lookup queue
-			this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //NOI18N
-
-			// Create connection
-			this.connection = connectionFactory.createConnection();
-
-			// Init session instance
-			this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
-			// And message producer
-			this.messageProducer = this.session.createProducer(this.queue);
-
-			// Finally the message instance itself
-			this.message = this.session.createObjectMessage();
-		} catch (final NamingException | JMSException e) {
-			// Continued to throw
-			throw new FacesException(e);
-		}
-	}
-
 	@Override
 	public String resendConfirmationLink (final User user, final Locale locale) {
 		// Log trace message
-		this.getLoggerBeanLocal().logTrace(MessageFormat.format("resendConfirmationLink: user={0} - CALLED!", user)); //NOI18N
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("resendConfirmationLink: user={0},locale={1} - CALLED!", user, locale)); //NOI18N
 
 		// The user instance should be valid
 		if (null == user) {
@@ -135,7 +67,25 @@ public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBea
 			throw new NullPointerException("locale is null"); //NOI18N
 		}
 
-		// @TODO Unfinished!
+		// @TODO Unfinished: Change key + merge database
+
+		// Init variable
+		Address emailAddress;
+
+		try {
+			// Create email address and set
+			emailAddress = new InternetAddress(user.getUserContact().getContactEmailAddress());
+		} catch (final AddressException ex) {
+			// Throw again
+			throw new EJBException(ex);
+		}
+
+		// Send email
+		this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user); //NOI18N
+
+		// Log trace message
+		this.getLoggerBeanLocal().logTrace("resendConfirmationLink: CALLED!"); //NOI18N
+
 		// All fine
 		return "resend_done"; //NOI18N
 	}
diff --git a/src/java/org/mxchange/jusercore/model/email_address/AddressbookEmailChangeSessionBean.java b/src/java/org/mxchange/jusercore/model/email_address/AddressbookEmailChangeSessionBean.java
index f65a2ee..a77a00b 100644
--- a/src/java/org/mxchange/jusercore/model/email_address/AddressbookEmailChangeSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/email_address/AddressbookEmailChangeSessionBean.java
@@ -19,29 +19,15 @@ package org.mxchange.jusercore.model.email_address;
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import java.util.List;
-import java.util.Locale;
-import java.util.Properties;
-import javax.annotation.PostConstruct;
 import javax.ejb.EJB;
 import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.faces.FacesException;
-import javax.jms.Connection;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.ObjectMessage;
-import javax.jms.Queue;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.Session;
 import javax.mail.Address;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
-import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserUtils;
 
@@ -51,38 +37,13 @@ import org.mxchange.jusercore.model.user.UserUtils;
  * @author Roland Haeder<roland@mxchange.org>
  */
 @Stateless (name = "emailchange", description = "A bean handling email changes")
-public class AddressbookEmailChangeSessionBean extends BaseDatabaseBean implements EmailChangeSessionBeanRemote {
+public class AddressbookEmailChangeSessionBean extends BaseAddressbookDatabaseBean implements EmailChangeSessionBeanRemote {
 
 	/**
 	 * Serial number
 	 */
 	private static final long serialVersionUID = 182_698_165_971_548L;
 
-	/**
-	 * Connection
-	 */
-	private Connection connection;
-
-	/**
-	 * Object message
-	 */
-	private ObjectMessage message;
-
-	/**
-	 * Message producer
-	 */
-	private MessageProducer messageProducer;
-
-	/**
-	 * Mailer message queue
-	 */
-	private Queue queue;
-
-	/**
-	 * Session instance
-	 */
-	private Session session;
-
 	/**
 	 * User bean
 	 */
@@ -150,78 +111,24 @@ public class AddressbookEmailChangeSessionBean extends BaseDatabaseBean implemen
 		// Persist it
 		//this.getEntityManager().persist(emailChange);
 
-		// Prepare mail wrapper
-		WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
+		// Init variable
+		Address emailAddress;
 
 		try {
 			// Create email address and set
-			Address emailAddress = new InternetAddress(emailChange.getEmailAddress());
-			emailWrapper.setRecipient(emailAddress);
+			emailAddress = new InternetAddress(emailChange.getEmailAddress());
 		} catch (final AddressException ex) {
 			// Throw again
 			throw new EJBException(ex);
 		}
 
-		// Set all values
-		Properties variables = UserUtils.getAllUserFields(emailChange.getEmailChangeUser());
-
-		// Set all
-		// @TODO Get locale from user + language from message bundle
-		emailWrapper.setLocale(Locale.GERMAN);
-		emailWrapper.setSubjectLine("Email change");
-		emailWrapper.setTemplateName("email_change"); //NOI18N
-		emailWrapper.setTemplateVariables(variables);
-
-		try {
-			// Send out email change
-			this.message.setObject(emailWrapper);
-
-			// Send message
-			this.sendMessage(this.message);
-		} catch (final JMSException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
+		// Send email
+		this.sendEmail("Email change", "email_change", emailAddress, emailChange.getEmailChangeUser()); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace("enqueueEmailAddressForChange - EXIT!"); //NOI18N
 	}
 
-	/**
-	 * Initialization of this bean
-	 */
-	@PostConstruct
-	public void init () {
-		// Trace message
-		this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
-
-		try {
-			// Get initial context
-			Context context = new InitialContext();
-
-			// Get factory from JMS resource
-			QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
-
-			// Lookup queue
-			this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //NOI18N
-
-			// Create connection
-			this.connection = connectionFactory.createConnection();
-
-			// Init session instance
-			this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
-			// And message producer
-			this.messageProducer = this.session.createProducer(this.queue);
-
-			// Finally the message instance itself
-			this.message = this.session.createObjectMessage();
-		} catch (final NamingException | JMSException e) {
-			// Continued to throw
-			throw new FacesException(e);
-		}
-	}
-
 	@Override
 	public boolean isEmailAddressEnqueued (final String emailAddress) {
 		// Trace message
@@ -335,22 +242,4 @@ public class AddressbookEmailChangeSessionBean extends BaseDatabaseBean implemen
 		emailAddress.setEmailChangeHash(hash);
 	}
 
-	/**
-	 * Sends given message to configured queue
-	 * <p>
-	 * @param message Message to send
-	 * <p>
-	 * @throws JMSException if something went wrong
-	 */
-	private void sendMessage (final ObjectMessage message) throws JMSException {
-		// The parameter should be valid
-		if (null == message) {
-			// Throw NPE
-			throw new NullPointerException("message is null"); //NOI18N
-		}
-
-		// Send it
-		this.messageProducer.send(message);
-	}
-
 }
diff --git a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
index 1427119..c529621 100644
--- a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
@@ -154,6 +154,8 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
 		// Call other EJB
 		User addedUser = this.userBean.addUser(user);
 
+		// Create email
+
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userIdd={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N
 
-- 
2.39.5