From 8f9cfa7ecf415a7071e3fd335d03f7aad1437521 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 23 Aug 2016 11:33:39 +0200 Subject: [PATCH] Please cherry-pick: - detachAllContactPhoneEntries() was a bad idea, beter do it the old way. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../database/BaseAddressbookDatabaseBean.java | 273 ++++-------------- 1 file changed, 56 insertions(+), 217 deletions(-) diff --git a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java index dbdcbc6..f5d4310 100644 --- a/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java +++ b/src/org/mxchange/addressbook/database/BaseAddressbookDatabaseBean.java @@ -29,13 +29,9 @@ import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper; import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.fax.FaxNumber; import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; -import org.mxchange.jphone.phonenumbers.mobile.MobileNumber; 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; @@ -59,79 +55,6 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N } - /** - * Detaches all contact's phone instances. - *

- * @param contact Contact to set instances - */ - protected void detachAllContactPhoneEntries (final Contact contact) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.detachAllContactPhoneEntries: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N - - // Both must be the same and not null - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.detachAllContactPhoneEntries: contact.contactMobileNumber={1}", this.getClass().getSimpleName(), contact.getContactMobileNumber())); //NOI18N - - // Is contact cellphone set? - if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { - // Fetch it from entity manager - DialableMobileNumber fetchedNumber = this.getEntityManager().getReference(MobileNumber.class, contact.getContactMobileNumber().getPhoneId()); - - // Get detached number - DialableMobileNumber detachedNumber = this.getDetached(contact.getContactMobileNumber(), fetchedNumber); - - // Is it null? - if (detachedNumber instanceof DialableMobileNumber) { - // Copy mobile number - contact.setContactMobileNumber(detachedNumber); - } - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.detachAllContactPhoneEntries: contact.contactLandLineNumber={1}", this.getClass().getSimpleName(), contact.getContactLandLineNumber())); //NOI18N - - // Is the land-line number set? - if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { - // Fetch it from entity manager - DialableLandLineNumber fetchedNumber = this.getEntityManager().getReference(LandLineNumber.class, contact.getContactLandLineNumber().getPhoneId()); - - // Get detached number - DialableLandLineNumber detachedNumber = this.getDetached(contact.getContactLandLineNumber(), fetchedNumber); - - // Is it null? - if (detachedNumber instanceof DialableLandLineNumber) { - // Copy mobile number - contact.setContactLandLineNumber(detachedNumber); - } - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.detachAllContactPhoneEntries: contact.contactFaxNumber={1}", this.getClass().getSimpleName(), contact.getContactFaxNumber())); //NOI18N - - // Is the fax number set? - if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { - // Fetch it from entity manager - DialableFaxNumber fetchedNumber = this.getEntityManager().getReference(FaxNumber.class, contact.getContactFaxNumber().getPhoneId()); - - // Get detached number - DialableFaxNumber detachedNumber = this.getDetached(contact.getContactFaxNumber(), fetchedNumber); - - // Is it null? - if (detachedNumber instanceof DialableFaxNumber) { - // Copy mobile number - contact.setContactFaxNumber(detachedNumber); - } - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.detachAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - /** * Updates all contacts's phone entry's created timestamps *

@@ -187,14 +110,14 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { } /** - * Returns a detached instance from given cellphone instance + * Returns a managed instance from given mobile number *

- * @param mobileNumber Mobile instance - * @param fetchedNumber Found cellphone number in database + * @param mobileNumber Mobile number + * @param fetchedNumber Found mobile number in database *

- * @return Detached instance + * @return Managed instance */ - protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) { + protected DialableMobileNumber getManaged (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) { // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: mobileNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), mobileNumber, fetchedNumber)); //NOI18N @@ -213,37 +136,31 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { // Debug message this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N - // Init query instance - DialableMobileNumber foundNumber = this.getEntityManager().getReference(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: foundNumber={1}", this.getClass().getSimpleName(), foundNumber)); //NOI18N - // Default is null - DialableMobileNumber detachedNumber = null; + DialableMobileNumber managedNumber = null; // Is there a difference? if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) { // Merge this entry - detachedNumber = this.getEntityManager().merge(foundNumber); + managedNumber = this.getEntityManager().merge(fetchedNumber); } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N // Return it - return detachedNumber; + return managedNumber; } /** - * Returns a detached instance from given land-line instance + * Returns a managed instance from given land-line number *

- * @param landLineNumber Land-line instance + * @param landLineNumber Land-line number * @param fetchedNumber Found land-line number in database *

- * @return Detached instance + * @return Managed instance */ - protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) { + protected DialableLandLineNumber getManaged (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) { // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: landLineNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), landLineNumber, fetchedNumber)); //NOI18N @@ -262,37 +179,31 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { // Debug message this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N - // Init query instance - DialableLandLineNumber foundNumber = this.getEntityManager().getReference(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: foundNumber={1}", this.getClass().getSimpleName(), foundNumber)); //NOI18N - // Default is null - DialableLandLineNumber detachedNumber = null; + DialableLandLineNumber managedNumber = null; // Is there a difference? if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) { // Merge this entry - detachedNumber = this.getEntityManager().merge(foundNumber); + managedNumber = this.getEntityManager().merge(fetchedNumber); } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N // Return it - return detachedNumber; + return managedNumber; } /** - * Returns a detached instance from given fax instance + * Returns a managed instance from given fax number *

- * @param faxNumber Fax instance + * @param faxNumber Fax number * @param fetchedNumber Found fax number in database *

- * @return Detached instance + * @return Managed instance */ - protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) { + protected DialableFaxNumber getManaged (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) { // Trace message this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: faxNumber={1},fetchedNumber={2} - CALLED!", this.getClass().getSimpleName(), faxNumber, fetchedNumber)); //NOI18N @@ -311,119 +222,68 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { // Debug message this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N - // Init query instance - DialableFaxNumber foundNumber = this.getEntityManager().getReference(fetchedNumber.getClass(), fetchedNumber.getPhoneId()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: foundNumber={1}", this.getClass().getSimpleName(), foundNumber)); //NOI18N - // Default is null - DialableFaxNumber detachedNumber = null; + DialableFaxNumber managedNumber = null; // Is there a difference? if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) { // Merge this entry - detachedNumber = this.getEntityManager().merge(foundNumber); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N - - // Return it - return detachedNumber; - } - - /** - * Get back a managed instance from given user - *

- * @param user Unmanaged/detached user instance - *

- * @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# + managedNumber = this.getEntityManager().merge(fetchedNumber); } - // 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 + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N // Return it - return managedUser; + return managedNumber; } /** - * Merges given contact's data + * Merges given (detached) contact's data *

- * @param contact Contact instance to merge + * @param detachedContact Contact instance to merge *

* @return Detached contact instance */ - protected Contact mergeContactData (final Contact contact) { + protected Contact mergeContactData (final Contact detachedContact) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N // The contact instance must be valid - if (null == contact) { + if (null == detachedContact) { // Throw NPE again - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { + throw new NullPointerException("detachedContact is null"); //NOI18N + } else if (detachedContact.getContactId() == null) { // Throw NPE again - throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N - } else if (contact.getContactId() < 1) { + throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N + } else if (detachedContact.getContactId() < 1) { // Not valid - throw new IllegalStateException(MessageFormat.format("{0}.contact.contactId={1} is not valid.", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N + throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N } - // Set updated timestamp - contact.setContactUpdated(new GregorianCalendar()); - // Get contact from it and find it - Contact foundContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId()); + Contact foundContact = this.getEntityManager().getReference(detachedContact.getClass(), detachedContact.getContactId()); // Should be found - assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N + assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N // Debug message this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: foundContact.contactId={1}", this.getClass().getSimpleName(), foundContact.getContactId())); //NOI18N // Merge contact instance - Contact detachedContact = this.getEntityManager().merge(foundContact); + Contact managedContact = this.getEntityManager().merge(foundContact); + + // Set updated timestamp + managedContact.setContactUpdated(new GregorianCalendar()); // Copy all - detachedContact.copyAll(contact); + managedContact.copyAll(detachedContact); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N // Return detached contact - return detachedContact; + return managedContact; } /** @@ -458,22 +318,16 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N // Then find it, too - DialableMobileNumber foundMobile = this.getEntityManager().getReference(cellphone.getClass(), cellphone.getPhoneId()); + DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.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 detachedMobile = this.getEntityManager().merge(foundMobile); - - // Should be there - assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N - // Copy all - detachedMobile.copyAll(detachedContact.getContactMobileNumber()); + foundMobile.copyAll(detachedContact.getContactMobileNumber()); // Set it back - detachedContact.setContactMobileNumber(detachedMobile); + detachedContact.setContactMobileNumber(foundMobile); } // Is there a fax instance set? @@ -482,22 +336,13 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N // Then find it, too - DialableFaxNumber foundFax = this.getEntityManager().getReference(fax.getClass(), fax.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 detachedFax = this.getEntityManager().merge(foundFax); - - // Should be there - assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N + DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); // Copy all - detachedFax.copyAll(detachedContact.getContactFaxNumber()); + foundFax.copyAll(detachedContact.getContactFaxNumber()); // Set it back - detachedContact.setContactFaxNumber(detachedFax); + detachedContact.setContactFaxNumber(foundFax); } // Is there a fax instance set? @@ -506,22 +351,16 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N // Then find it, too - DialableLandLineNumber foundLandLine = this.getEntityManager().getReference(landLine.getClass(), landLine.getPhoneId()); + DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.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 detachedLandLine = this.getEntityManager().merge(foundLandLine); - - // Should be there - assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N - // Copy all - detachedLandLine.copyAll(detachedContact.getContactLandLineNumber()); + foundLandLine.copyAll(detachedContact.getContactLandLineNumber()); // Set it back - detachedContact.setContactLandLineNumber(detachedLandLine); + detachedContact.setContactLandLineNumber(foundLandLine); } // Trace message @@ -627,7 +466,7 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { // Is the fax number set? if (other.getContactMobileNumber() instanceof DialableMobileNumber) { // Copy cellphone number - contact.setContactMobileNumber(this.getDetached(other.getContactMobileNumber(), contact.getContactMobileNumber())); + contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber())); } else { // Null it contact.setContactMobileNumber(null); @@ -645,7 +484,7 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { // Is the land-line number set? if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) { // Copy land-line number - contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber())); + contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber())); } else { // Null it contact.setContactLandLineNumber(null); @@ -663,7 +502,7 @@ public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean { // Is the fax number set? if (other.getContactFaxNumber() instanceof DialableFaxNumber) { // Copy fax number - contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber())); + contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber())); } else { // Null it contact.setContactFaxNumber(null); -- 2.39.5