From ae4bfd4afa453fec97804f2e7143522ec85978b8 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Mon, 26 Jun 2017 21:26:23 +0200
Subject: [PATCH] Please cherry-pick: - had moved copyAll() to new utility
 classes which is a much better place for   them. Per EJB standards, no
 "complex" methods in POJOs/entity classes which   makes sense. :-)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../phone/PizzaAdminPhoneSessionBean.java     |  9 ++-
 .../model/user/PizzaUserSessionBean.java      | 68 ++++++++++++++++---
 2 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaAdminPhoneSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaAdminPhoneSessionBean.java
index 555dfbe..c8c0d23 100644
--- a/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaAdminPhoneSessionBean.java
+++ b/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaAdminPhoneSessionBean.java
@@ -20,8 +20,11 @@ import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import javax.ejb.Stateless;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.fax.FaxNumbers;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumbers;
 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
+import org.mxchange.jphone.phonenumbers.mobile.MobileNumbers;
 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
 
 /**
@@ -231,7 +234,7 @@ public class PizzaAdminPhoneSessionBean extends BasePizzaDatabaseBean implements
 		this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateFaxData: managedNumber.phoneId={1}", this.getClass().getSimpleName(), managedNumber.getPhoneId())); //NOI18N
 
 		// Set updated timestamp
-		managedNumber.copyAll(faxNumber);
+		FaxNumbers.copyAll(faxNumber, managedNumber);
 		managedNumber.setPhoneEntryUpdated(new GregorianCalendar());
 
 		// Trace message
@@ -289,7 +292,7 @@ public class PizzaAdminPhoneSessionBean extends BasePizzaDatabaseBean implements
 		this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateLandLineData: managedNumber.phoneId={1}", this.getClass().getSimpleName(), managedNumber.getPhoneId())); //NOI18N
 
 		// Set updated timestamp
-		managedNumber.copyAll(landLineNumber);
+		LandLineNumbers.copyAll(landLineNumber, managedNumber);
 		managedNumber.setPhoneEntryUpdated(new GregorianCalendar());
 
 		// Trace message
@@ -341,7 +344,7 @@ public class PizzaAdminPhoneSessionBean extends BasePizzaDatabaseBean implements
 		this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateMobileData: managedNumber.phoneId={1}", this.getClass().getSimpleName(), managedNumber.getPhoneId())); //NOI18N
 
 		// Set updated timestamp
-		managedNumber.copyAll(mobileNumber);
+		MobileNumbers.copyAll(mobileNumber, managedNumber);
 		managedNumber.setPhoneEntryUpdated(new GregorianCalendar());
 
 		// Trace message
diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
index 5bcd50a..96d7058 100644
--- a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
@@ -31,8 +31,11 @@ import javax.persistence.Query;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.ContactUtils;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.fax.FaxNumbers;
 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumbers;
 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
+import org.mxchange.jphone.phonenumbers.mobile.MobileNumbers;
 import org.mxchange.jusercore.exceptions.UserNotFoundException;
 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
@@ -708,17 +711,17 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS
 		}
 
 		// Find the instance
-		User updatedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
+		User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
 
 		// Should be found!
-		assert (updatedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
+		assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
 
 		// Copy all data
-		UserUtils.copyAll(user, updatedUser);
+		UserUtils.copyAll(user, managedUser);
 
 		// Set as updated
-		updatedUser.setUserUpdated(new GregorianCalendar());
-		updatedUser.getUserContact().setContactUpdated(new GregorianCalendar());
+		managedUser.setUserUpdated(new GregorianCalendar());
+		managedUser.getUserContact().setContactUpdated(new GregorianCalendar());
 
 		// Get contact from it and find it
 		Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
@@ -744,7 +747,22 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS
 		// Is there a  mobile instance set?
 		if (mobileNumber instanceof DialableMobileNumber) {
 			// Debug message
-			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobileNumber.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
+			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
+
+			// Then find it, too
+			DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId());
+
+			// Should be there
+			assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
+
+			// Then merge it, too
+			DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
+
+			// Should be there
+			assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getPhoneId()); //NOI18N
+
+			// Copy all
+			MobileNumbers.copyAll(managedUser.getUserContact().getContactMobileNumber(), managedMobile);
 
 			// Set it back
 			managedContact.setContactMobileNumber(this.getManaged(mobileNumber, mobileNumber));
@@ -758,8 +776,23 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS
 			// Debug message
 			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
 
+			// Then find it, too
+			DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
+
+			// Should be there
+			assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
+
+			// Then merge it, too
+			DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
+
+			// Should be there
+			assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
+
+			// Copy all
+			FaxNumbers.copyAll(managedUser.getUserContact().getContactFaxNumber(), managedFax);
+
 			// Set it back
-			managedContact.setContactFaxNumber(this.getManaged(faxNumber, faxNumber));
+			managedContact.setContactFaxNumber(managedFax);
 		}
 
 		// Get mobile instance
@@ -770,15 +803,30 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS
 			// Debug message
 			this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
 
+			// Then find it, too
+			DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
+
+			// Should be there
+			assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
+
+			// Then merge it, too
+			DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
+
+			// Should be there
+			assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
+
+			// Copy all
+			LandLineNumbers.copyAll(managedUser.getUserContact().getContactLandLineNumber(), managedLandLine);
+
 			// Set it back
-			managedContact.setContactLandLineNumber(this.getManaged(landLineNumber, landLineNumber));
+			managedContact.setContactLandLineNumber(managedLandLine);
 		}
 
 		// Trace message
-		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
 
 		// Return updated user instance
-		return updatedUser;
+		return managedUser;
 	}
 
 }
-- 
2.39.5