From 6145a3ab3b22a1f5ee68a7865268fae5d31bd3dd 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. --- .../jjobs/database/BaseJobsDatabaseBean.java | 227 +++++------------- 1 file changed, 56 insertions(+), 171 deletions(-) diff --git a/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java b/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java index 1d0c09d..c8ffce0 100644 --- a/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java +++ b/src/org/mxchange/jjobs/database/BaseJobsDatabaseBean.java @@ -38,11 +38,8 @@ 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.User; import org.mxchange.jusercore.model.user.UserUtils; @@ -110,79 +107,6 @@ public abstract class BaseJobsDatabaseBean extends BaseDatabaseBean { } } - /** - * 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 *

@@ -238,14 +162,14 @@ public abstract class BaseJobsDatabaseBean extends BaseDatabaseBean { } /** - * Returnes a detached instance from given cellphone instance + * Returnes 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 @@ -264,37 +188,31 @@ public abstract class BaseJobsDatabaseBean 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; } /** - * Returnes a detached instance from given land-line instance + * Returnes 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 @@ -313,37 +231,31 @@ public abstract class BaseJobsDatabaseBean 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; } /** - * Returnes a detached instance from given fax instance + * Returnes 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 @@ -362,74 +274,68 @@ public abstract class BaseJobsDatabaseBean 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); + 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; } /** - * 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; } /** @@ -464,22 +370,16 @@ public abstract class BaseJobsDatabaseBean 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? @@ -488,22 +388,13 @@ public abstract class BaseJobsDatabaseBean 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? @@ -512,22 +403,16 @@ public abstract class BaseJobsDatabaseBean 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 @@ -633,7 +518,7 @@ public abstract class BaseJobsDatabaseBean 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); @@ -651,7 +536,7 @@ public abstract class BaseJobsDatabaseBean 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); @@ -669,7 +554,7 @@ public abstract class BaseJobsDatabaseBean 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