From 1321a0b9bd81dbb10fb225d845e0f6f245142825 Mon Sep 17 00:00:00 2001
From: Roland Haeder <roland@mxchange.org>
Date: Mon, 10 Apr 2017 21:36:25 +0200
Subject: [PATCH] added missing method getManagedUser()

---
 .../database/BaseAddressbookDatabaseBean.java | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
index 95db905..3c15337 100644
--- a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
+++ b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java
@@ -41,6 +41,7 @@ import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
 import org.mxchange.jphone.utils.PhoneUtils;
+import org.mxchange.jusercore.model.user.LoginUser;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserUtils;
 
@@ -301,6 +302,51 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
 		return detachedNumber;
 	}
 
+	/**
+	 * Get back a managed instance from given user
+	 * <p>
+	 * @param user Unmanaged/detached user instance
+	 * <p>
+	 * @return Managed user instance
+	 */
+	protected User getManagedUser (final User user) {
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: 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() == null) {
+			// Id is set
+			throw new NullPointerException("user.userId is null"); //NOI18N
+		} else if (user.getUserId() < 1) {
+			// Id is set
+			throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //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#
+		}
+
+		// Try to find it (should be there)
+		User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
+
+		// Should be there
+		assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
+
+		// Trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
+
+		// Return it
+		return managedUser;
+	}
+
 	/**
 	 * Merges given contact's data
 	 * <p>
-- 
2.39.5