From eae6392f7702b92047b71cfa639136a5bff7c422 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Sat, 8 Jul 2017 00:27:02 +0200
Subject: [PATCH] Please cherry-pick: - rewrote email delivery as
 EmailDeliveryWrapper() has now a constructor with all required fields - also
 saved one parameter (one lesser = easier code)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../database/BaseAddressbookDatabaseBean.java | 36 ++++++++++---------
 .../AddressbookEmailDeliveryMessageBean.java  |  9 +++--
 .../user/AddressbookAdminUserSessionBean.java | 28 ++-------------
 .../user/AddressbookUserSessionBean.java      | 29 ++-------------
 ...AddressbookUserEmailChangeSessionBean.java | 15 +-------
 ...ddressbookUserRegistrationSessionBean.java | 17 +--------
 .../AddressbookResendLinkSessionBean.java     | 17 +--------
 7 files changed, 32 insertions(+), 119 deletions(-)

diff --git a/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java b/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
index 13f251e..b74a841 100644
--- a/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
+++ b/src/java/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
@@ -24,6 +24,8 @@ import javax.ejb.EJBException;
 import javax.jms.JMSException;
 import javax.jms.ObjectMessage;
 import javax.mail.Address;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.ContactUtils;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
@@ -63,8 +65,8 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 	/**
 	 * Constructor with queue factory JNDI and queue JNDI names
 	 * <p>
-	 * @param factoryJndi    JNDI name for queue factory
-	 * @param queueJndi JNDI name for email queue
+	 * @param factoryJndi JNDI name for queue factory
+	 * @param queueJndi   JNDI name for email queue
 	 */
 	protected BaseAddressbookDatabaseBean (final String factoryJndi, final String queueJndi) {
 		// Call super constructor
@@ -470,15 +472,14 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 	 * <p>
 	 * @param subjectLine    Subject line
 	 * @param templateName   Template name
-	 * @param emailAddress   Recipient's email address
 	 * @param user           User instance
 	 * @param baseUrl        Base URL
 	 * @param randomPassword A randomly-generated password or NULL if user had
 	 *                       to enter it.
 	 */
-	protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl, final String randomPassword) {
+	protected void sendEmail (final String subjectLine, final String templateName, final User user, final String baseUrl, final String randomPassword) {
 		// Trace message
-		this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},user={2},baseUrl={3} - CALLED!", subjectLine, templateName, user, baseUrl)); //NOI18N
 
 		// All should be set
 		if (null == subjectLine) {
@@ -493,9 +494,6 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 		} else if (templateName.isEmpty()) {
 			// No template name
 			throw new IllegalArgumentException("templateName is empty"); //NOI18N
-		} else if (null == emailAddress) {
-			// Throw NPE
-			throw new NullPointerException("emailAddress is null"); //NOI18N
 		} else if (null == user) {
 			// Throw NPE
 			throw new NullPointerException("user is null"); //NOI18N
@@ -534,9 +532,6 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 			throw new NullPointerException("this.session is not set. Have you forgotten to call super(String, String) and called only super() ?"); //NOI18N
 		}
 
-		// Prepare mail wrapper
-		WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
-
 		// Set all values
 		Properties variables = UserUtils.getAllUserFields(user);
 
@@ -549,13 +544,20 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 			variables.put("randomPassword", randomPassword); //NOI18N
 		}
 
-		// Set all
+		// Init addresss
+		Address recipientAddress;
+
+		try {
+			// Create email address and set
+			recipientAddress = new InternetAddress(user.getUserContact().getContactEmailAddress());
+		} catch (final AddressException ex) {
+			// Throw again
+			throw new EJBException(ex);
+		}
+
+		// Prepare mail wrapper
 		// @TODO Language from message bundle
-		emailWrapper.setRecipient(emailAddress);
-		emailWrapper.setLocale(user.getUserLocale());
-		emailWrapper.setSubjectLine(subjectLine);
-		emailWrapper.setTemplateName(templateName);
-		emailWrapper.setTemplateVariables(variables);
+		WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper(recipientAddress, subjectLine, templateName, variables, user.getUserLocale());
 
 		try {
 			// Send out email change
diff --git a/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java b/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java
index daa9f24..044b302 100644
--- a/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java
+++ b/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java
@@ -28,6 +28,7 @@ import javax.jms.Message;
 import javax.jms.MessageListener;
 import javax.jms.ObjectMessage;
 import javax.mail.MessagingException;
+import javax.naming.NamingException;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
 
@@ -62,8 +63,10 @@ public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabase
 
 	/**
 	 * Default constructor
+	 * <p>
+	 * @throws javax.naming.NamingException If a JNDI name could not be found
 	 */
-	public AddressbookEmailDeliveryMessageBean () {
+	public AddressbookEmailDeliveryMessageBean () throws NamingException {
 		// Call super constructor
 		super();
 
@@ -161,9 +164,9 @@ public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabase
 		if (wrapper.getLocale() == null) {
 			// Throw NPE
 			throw new NullPointerException("wrapper.locale is null"); //NOI18N
-		} else if (wrapper.getRecipient() == null) {
+		} else if (wrapper.getRecipientAddress()== null) {
 			// Throw again ...
-			throw new NullPointerException("wrapper.recipient is null"); //NOI18N
+			throw new NullPointerException("wrapper.recipientAddress is null"); //NOI18N
 		} else if (wrapper.getSubjectLine() == null) {
 			// ... and again
 			throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
index f8e1c95..1dc8927 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
@@ -19,11 +19,7 @@ package org.mxchange.jusercore.model.user;
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import javax.ejb.EJB;
-import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.mail.Address;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
@@ -266,20 +262,10 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean
 		User managedUser = this.userBean.updateUserData(user);
 
 		// @TODO Create user lock history entry
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
 
 		// Send out email
 		// @TODO externalize subject line
-		this.sendEmail("User account locked", "user_account_locked", emailAddress, managedUser, baseUrl, null); //NOI18N
+		this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
@@ -336,20 +322,10 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean
 		User managedUser = this.userBean.updateUserData(user);
 
 		// @TODO Create user lock history entry
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
 
 		// Send out email
 		// @TODO externalize subject line
-		this.sendEmail("User account unlocked", "user_account_unlocked", emailAddress, managedUser, baseUrl, null); //NOI18N
+		this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
index c8db69d..9cdc6a7 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
@@ -22,9 +22,6 @@ import java.util.List;
 import javax.ejb.EJB;
 import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.mail.Address;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
 import javax.persistence.NoResultException;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
@@ -180,19 +177,8 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		// Update user account
 		User updatedUser = this.updateUserData(user);
 
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
-
 		// Send out email
-		this.sendEmail("User account confirmed", "user_account_confirmed", emailAddress, updatedUser, baseUrl, null); //NOI18N
+		this.sendEmail("User account confirmed", "user_account_confirmed", updatedUser, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
@@ -659,19 +645,8 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		// Flush it to get id number back
 		this.getEntityManager().flush();
 
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
-
 		// Send email to user
-		this.sendEmail("User password change", "user_password_change", emailAddress, user, baseUrl, null); //NOI18N
+		this.sendEmail("User password change", "user_password_change", user, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
diff --git a/src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java b/src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java
index a3c5a46..c035310 100644
--- a/src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java
@@ -22,9 +22,6 @@ import java.util.List;
 import javax.ejb.EJB;
 import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.mail.Address;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
@@ -114,19 +111,9 @@ public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDataba
 
 		// Persist it
 		//@TODO Fix email delivery then allow this: this.getEntityManager().persist(emailChange);
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(emailChange.getEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
 
 		// Send email
-		this.sendEmail("User email change", "user_email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl, null); //NOI18N
+		this.sendEmail("User email change", "user_email_change", emailChange.getEmailChangeUser(), baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange - EXIT!", this.getClass().getSimpleName())); //NOI18N
diff --git a/src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java
index 72315ae..385111d 100644
--- a/src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java
@@ -19,11 +19,7 @@ package org.mxchange.jusercore.model.user.register;
 import java.text.MessageFormat;
 import java.util.Objects;
 import javax.ejb.EJB;
-import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.mail.Address;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
@@ -183,17 +179,6 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
 		// Call other EJB
 		User addedUser = this.adminUserBean.addUser(user);
 
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
-
 		// Default template is with no random password
 		String templateName = "user_registration"; //NOI18N
 
@@ -205,7 +190,7 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
 
 		// Send email
 		// @TODO: Internationlize the subject line somehow
-		this.sendEmail("Registration", templateName, emailAddress, addedUser, baseUrl, randomPassword); //NOI18N
+		this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
diff --git a/src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java b/src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java
index 1f3dfae..70ed720 100644
--- a/src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java
@@ -19,11 +19,7 @@ package org.mxchange.jusercore.model.user.resendlink;
 import java.text.MessageFormat;
 import java.util.Locale;
 import javax.ejb.EJB;
-import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.mail.Address;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
@@ -108,20 +104,9 @@ public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBea
 		// Set it in user
 		managedUser.setUserConfirmKey(confirmationKey);
 
-		// Init variable
-		Address emailAddress;
-
-		try {
-			// Create email address and set
-			emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
-		} catch (final AddressException ex) {
-			// Throw again
-			throw new EJBException(ex);
-		}
-
 		// Send email
 		// @TODO: Internationlize the subject line somehow
-		this.sendEmail("Resend user confirmation link", "user_resend_confirmation_link", emailAddress, user, baseUrl, null); //NOI18N
+		this.sendEmail("Resend user confirmation link", "user_resend_confirmation_link",  user, baseUrl, null); //NOI18N
 
 		// Log trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: EXIT!", this.getClass().getSimpleName())); //NOI18N
-- 
2.39.5