From 5cc2b78b973b429ed856ce1f79ffb77c625499a3 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Mon, 29 Aug 2016 12:22:00 +0200
Subject: [PATCH] Please cherry-pick: - implemented business method for
 deleting user accounts

---
 .../model/user/PizzaAdminUserSessionBean.java | 44 ++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java
index b6c0397..debec57 100644
--- a/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java
@@ -109,6 +109,48 @@ public class PizzaAdminUserSessionBean extends BasePizzaDatabaseBean implements
 		return user;
 	}
 
+	@Override
+	public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //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 (!this.userBean.ifUserExists(user)) {
+			// Name already found
+			throw new UserNotFoundException(user);
+		}
+
+		// Get a managed instance
+		User managedUser = this.getManagedUser(user);
+
+		// Should be found!
+		assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
+
+		// Delete it
+		this.getEntityManager().refresh(managedUser);
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
+	}
+
 	@Override
 	public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
 		// Trace message
@@ -142,7 +184,7 @@ public class PizzaAdminUserSessionBean extends BasePizzaDatabaseBean implements
 		Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
 
 		// Should be found!
-		assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", managedContact.getContactId()); //NOI18N
+		assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
 
 		// Set detached object in rexcruiter instance
 		user.setUserContact(managedContact);
-- 
2.39.5