From: Roland Häder <roland@mxchange.org>
Date: Fri, 5 Aug 2016 14:39:51 +0000 (+0200)
Subject: Continued with unlocking users: (please cherry-pick)
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=160d676ce03ec636e59d9b40a23d71a5782d2fd4;p=jfinancials-mailer-ejb.git

Continued with unlocking users: (please cherry-pick)
- implemented business method unlockUserAccount()

Signed-off-by: Roland Häder <roland@mxchange.org>
---

diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
index 15f9c6d..b1e110b 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Cho-Time GmbH
+ * Copyright (C) 2016 Roland Haeder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -19,12 +19,21 @@ 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;
 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
+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.status.UserAccountStatus;
 
 /**
  * An administrative user EJB
@@ -151,4 +160,170 @@ public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean
 		return user;
 	}
 
+	@Override
+	public void lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
+
+		// user should not be null
+		if (null == user) {
+			// Abort here
+			throw new NullPointerException("user is null"); //NOI18N
+		} else if (user.getUserId() instanceof Long) {
+			// Id is set
+			throw new IllegalArgumentException("user.userId is not null"); //NOI18N
+		} else if (user.getUserContact() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userContact is null"); //NOI18N
+		} else if (user.getUserContact().getContactId() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
+		} else if (user.getUserContact().getContactId() < 1) {
+			// Not valid id number
+			throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
+		} else if (user.getUserAccountStatus() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
+		} else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
+			// Account is locked
+			throw new UserStatusLockedException(user);
+		} else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
+			// Account is locked
+			throw new UserStatusUnconfirmedException(user);
+		} else if (!this.userBean.ifUserExists(user)) {
+			// Name already found
+			throw new UserNotFoundException(user);
+		} else if (null == userLockReason) {
+			// Throw NPE again
+			throw new NullPointerException("userLockReason is null"); //NOI18N
+		} else if (userLockReason.isEmpty()) {
+			// Is empty
+			throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
+		}
+
+		// Remove contact instance as this is not updated
+		user.setUserContact(null);
+
+		// Find the instance
+		User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
+
+		// Should be found!
+		assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
+
+		// Merge user
+		User detachedUser = this.getEntityManager().merge(foundUser);
+
+		// Should be found!
+		assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
+
+		// Copy all data
+		detachedUser.copyAll(user);
+
+		// Set as locked
+		detachedUser.setUserAccountStatus(UserAccountStatus.LOCKED);
+		detachedUser.setUserLastLocked(new GregorianCalendar());
+		detachedUser.setUserLastLockedReason(userLockReason);
+
+		// Update user
+		User updatedUser = this.userBean.updateUserData(user);
+
+		// @TODO Create user lock history entry
+		// 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
+		// @TODO externalize subject line
+		this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount - EXIT!", this.getClass().getSimpleName())); //NOI18N
+	}
+
+	@Override
+	public void unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
+
+		// user should not be null
+		if (null == user) {
+			// Abort here
+			throw new NullPointerException("user is null"); //NOI18N
+		} else if (user.getUserId() instanceof Long) {
+			// Id is set
+			throw new IllegalArgumentException("user.userId is not null"); //NOI18N
+		} else if (user.getUserContact() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userContact is null"); //NOI18N
+		} else if (user.getUserContact().getContactId() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
+		} else if (user.getUserContact().getContactId() < 1) {
+			// Not valid id number
+			throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
+		} else if (user.getUserAccountStatus() == null) {
+			// Throw NPE again
+			throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
+		} else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
+			// Account is locked
+			throw new UserStatusConfirmedException(user);
+		} else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
+			// Account is locked
+			throw new UserStatusUnconfirmedException(user);
+		} else if (!this.userBean.ifUserExists(user)) {
+			// Name already found
+			throw new UserNotFoundException(user);
+		}
+
+		// Remove contact instance as this is not updated
+		user.setUserContact(null);
+
+		// Find the instance
+		User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
+
+		// Should be found!
+		assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
+
+		// Merge user
+		User detachedUser = this.getEntityManager().merge(foundUser);
+
+		// Should be found!
+		assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
+
+		// Copy all data
+		detachedUser.copyAll(user);
+
+		// Unlock account
+		detachedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
+
+		// Update user
+		User updatedUser = this.userBean.updateUserData(user);
+
+		// @TODO Create user lock history entry
+		// 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
+		// @TODO externalize subject line
+		this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount - EXIT!", this.getClass().getSimpleName())); //NOI18N
+	}
+
 }