From 29d62f6f70bd7cdc474394a20d144f60bbd0ea7e Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Thu, 22 Jun 2017 22:55:38 +0200
Subject: [PATCH] Please cherry-pick: - moved EJBs to proper package as they
 are user-related beans - handled over randomPassword parameter to sendEmail()
 - or null if not possible/wanted
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../AddressbookContactSessionBean.java        |  1 -
 .../AddressbookUserLoginSessionBean.java      |  2 +-
 .../user/AddressbookAdminUserSessionBean.java |  6 ++---
 .../user/AddressbookUserSessionBean.java      | 13 +++++-----
 ...AddressbookUserEmailChangeSessionBean.java | 26 +++++++++----------
 ...ddressbookUserRegistrationSessionBean.java | 19 +++++++++++---
 .../AddressbookResendLinkSessionBean.java     |  4 +--
 7 files changed, 41 insertions(+), 30 deletions(-)
 rename src/java/org/mxchange/jusercore/model/{ => user}/email_address/AddressbookUserEmailChangeSessionBean.java (91%)
 rename src/java/org/mxchange/jusercore/model/{ => user}/register/AddressbookUserRegistrationSessionBean.java (89%)
 rename src/java/org/mxchange/{addressbook/beans => jusercore/model/user}/resendlink/AddressbookResendLinkSessionBean.java (96%)

diff --git a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
index b39bc76..61dd937 100644
--- a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
+++ b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
@@ -24,7 +24,6 @@ import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
-import org.mxchange.jcontacts.contact.utils.ContactUtils;
 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
 
 /**
diff --git a/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java b/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java
index c33ba5f..3802daa 100644
--- a/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java
@@ -25,7 +25,7 @@ import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
-import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
+import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserUtils;
diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
index 664a0b0..fa2ca78 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
@@ -32,7 +32,7 @@ import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
-import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
+import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
 /**
@@ -279,7 +279,7 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean
 
 		// Send out email
 		// @TODO externalize subject line
-		this.sendEmail("Account locked", "account_locked", emailAddress, managedUser, baseUrl); //NOI18N
+		this.sendEmail("User account locked", "user_account_locked", emailAddress, managedUser, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
@@ -349,7 +349,7 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean
 
 		// Send out email
 		// @TODO externalize subject line
-		this.sendEmail("Account unlocked", "account_unlocked", emailAddress, managedUser, baseUrl); //NOI18N
+		this.sendEmail("User account unlocked", "user_account_unlocked", emailAddress, 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 33ac6a3..33db348 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
@@ -30,6 +30,7 @@ import javax.persistence.PersistenceException;
 import javax.persistence.Query;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.ContactUtils;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
@@ -37,10 +38,10 @@ import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
-import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
+import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
 /**
@@ -188,7 +189,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		}
 
 		// Send out email
-		this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N
+		this.sendEmail("User account confirmed", "user_account_confirmed", emailAddress, updatedUser, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
@@ -587,7 +588,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
 
 		// Copy all data
-		managedUser.copyAll(user);
+		UserUtils.copyAll(user, managedUser);
 
 		// Set as updated
 		managedUser.setUserUpdated(new GregorianCalendar());
@@ -667,7 +668,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		}
 
 		// Send email to user
-		this.sendEmail("User password change", "user_password_change", emailAddress, user, baseUrl); //NOI18N
+		this.sendEmail("User password change", "user_password_change", emailAddress, user, baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
@@ -706,7 +707,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
 
 		// Copy all data
-		managedUser.copyAll(user);
+		UserUtils.copyAll(user, managedUser);
 
 		// Set as updated
 		managedUser.setUserUpdated(new GregorianCalendar());
@@ -722,7 +723,7 @@ public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean impl
 		this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: managedContact.contactId={0}", managedContact.getContactId())); //NOI18N
 
 		// Copy all
-		managedContact.copyAll(user.getUserContact());
+		ContactUtils.copyAll(user.getUserContact(), managedContact);
 
 		// Set it back in user
 		user.setUserContact(managedContact);
diff --git a/src/java/org/mxchange/jusercore/model/email_address/AddressbookUserEmailChangeSessionBean.java b/src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java
similarity index 91%
rename from src/java/org/mxchange/jusercore/model/email_address/AddressbookUserEmailChangeSessionBean.java
rename to src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java
index db07376..bcfd656 100644
--- a/src/java/org/mxchange/jusercore/model/email_address/AddressbookUserEmailChangeSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/email_address/AddressbookUserEmailChangeSessionBean.java
@@ -14,7 +14,7 @@
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-package org.mxchange.jusercore.model.email_address;
+package org.mxchange.jusercore.model.user.email_address;
 
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
@@ -28,6 +28,9 @@ import javax.mail.internet.InternetAddress;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
+import org.mxchange.jusercore.model.email_address.EmailAddressChange;
+import org.mxchange.jusercore.model.email_address.UserEmailChangeSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserUtils;
 
@@ -110,9 +113,6 @@ public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDataba
 		emailChange.setEmailChangeCreated(new GregorianCalendar());
 		this.generateSecureHash(emailChange);
 
-		// Make user managed
-		emailChange.setEmailChangeUser(this.getManagedUser(emailChange.getEmailChangeUser()));
-
 		// Persist it
 		//@TODO Fix email delivery then allow this: this.getEntityManager().persist(emailChange);
 		// Init variable
@@ -127,7 +127,7 @@ public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDataba
 		}
 
 		// Send email
-		this.sendEmail("Email change", "email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl); //NOI18N
+		this.sendEmail("User email change", "user_email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl, null); //NOI18N
 
 		// Trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange - EXIT!", this.getClass().getSimpleName())); //NOI18N
@@ -199,16 +199,16 @@ public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDataba
 	 * Generates a secure, unique hash for given email address change. This
 	 * requires to check if the hash is really not there.
 	 * <p>
-	 * @param emailAddressChange Email address change
+	 * @param emailAddress Email address change
 	 */
-	private void generateSecureHash (final ChangeableEmailAddress emailAddressChange) {
+	private void generateSecureHash (final ChangeableEmailAddress emailAddress) {
 		// Email address change should be valid
-		if (null == emailAddressChange) {
+		if (null == emailAddress) {
 			// Abort here
-			throw new NullPointerException("emailAddressChange is null"); //NOI18N
-		} else if (emailAddressChange.getEmailAddress().trim().isEmpty()) {
+			throw new NullPointerException("emailAddress is null"); //NOI18N
+		} else if (emailAddress.getEmailAddress().trim().isEmpty()) {
 			// Email address is empty
-			throw new IllegalArgumentException("emailAddressChange.emaiLAddress is empty."); //NOI18N
+			throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
 		}
 
 		// Initialize loop with null
@@ -220,7 +220,7 @@ public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDataba
 		// Search for free hash
 		while (isUsed) {
 			// Generate hash, there is already in UserUtils a nice method that can be used for this purpose.
-			hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddressChange.getEmailAddress(), emailAddressChange.toString())); //NOI18N
+			hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddress.getEmailAddress(), emailAddress.toString())); //NOI18N
 
 			// The hash *may* be unique, better test it
 			Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByHash", EmailAddressChange.class); //NOI18N
@@ -243,7 +243,7 @@ public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDataba
 		assert (!hash.isEmpty()) : "hash is empty"; //NOI18N
 
 		// Set it in email change
-		emailAddressChange.setEmailChangeHash(hash);
+		emailAddress.setEmailChangeHash(hash);
 	}
 
 }
diff --git a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java
similarity index 89%
rename from src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
rename to src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java
index ff98ffc..707116e 100644
--- a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/register/AddressbookUserRegistrationSessionBean.java
@@ -14,9 +14,10 @@
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-package org.mxchange.jusercore.model.register;
+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;
@@ -29,6 +30,7 @@ import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
+import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.LoginUser;
 import org.mxchange.jusercore.model.user.User;
@@ -151,9 +153,9 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
 	}
 
 	@Override
-	public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
+	public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
 		// Trace message
-		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
 
 		// user should not be null
 		if (null == user) {
@@ -193,9 +195,18 @@ public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatab
 			throw new EJBException(ex);
 		}
 
+		// Default template is with no random password
+		String templateName = "user_registration"; //NOI18N
+
+		// Is password set?
+		if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
+			// Switch to other template
+			templateName = "user_registration_random"; //NOI18N
+		}
+
 		// Send email
 		// @TODO: Internationlize the subject line somehow
-		this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N
+		this.sendEmail("Registration", templateName, emailAddress, 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/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java b/src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java
similarity index 96%
rename from src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
rename to src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java
index 08cc740..259b43a 100644
--- a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/resendlink/AddressbookResendLinkSessionBean.java
@@ -14,7 +14,7 @@
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-package org.mxchange.addressbook.beans.resendlink;
+package org.mxchange.jusercore.model.user.resendlink;
 
 import java.text.MessageFormat;
 import java.util.Locale;
@@ -122,7 +122,7 @@ public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBea
 
 		// Send email
 		// @TODO: Internationlize the subject line somehow
-		this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user, baseUrl); //NOI18N
+		this.sendEmail("Resend user confirmation link", "user_resend_confirmation_link", emailAddress, user, baseUrl, null); //NOI18N
 
 		// Log trace message
 		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: EXIT!", this.getClass().getSimpleName())); //NOI18N
-- 
2.39.5