]> git.mxchange.org Git - pizzaservice-core.git/blobdiff - src/org/mxchange/pizzaaplication/database/BasePizzaDatabaseBean.java
Please cherry-pick:
[pizzaservice-core.git] / src / org / mxchange / pizzaaplication / database / BasePizzaDatabaseBean.java
index c3dcea80a0b5475453d3219f62de8d4399cceb9b..359223c7bd3be9163f63453c06f189ce98a0c90f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Cho-Time GmbH
+ * Copyright (C) 2016, 2017 Roland Häder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,16 +18,29 @@ package org.mxchange.pizzaaplication.database;
 
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
+import java.util.Objects;
+import java.util.Properties;
+import javax.ejb.EJBException;
+import javax.jms.JMSException;
+import javax.jms.ObjectMessage;
+import javax.mail.Address;
 import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.ContactUtils;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
-import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
+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.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;
 
 /**
  * A helper class for beans that access the database.
  * <p>
- * @author Roland Haeder<rhaeder@cho-time.de>
+ * @author Roland Häder<roland@mxchange.org>
  */
 public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
 
@@ -41,46 +54,298 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
         */
        protected BasePizzaDatabaseBean () {
                // Call super constructor
-               super();
+               super("jms/pizzaservice-queue-factory", "jms/pizzaservice-email-queue"); //NOI18N
        }
 
        /**
-        * Merges given contact's data
+        * Updates all contact's phone entry's created timestamps
         * <p>
-        * @param contact Contact instance to merge
-        * <p>
-        * @return Detached contact instance
+        * @param contact Contact instance to update
         */
-       protected Contact mergeContactData (final Contact contact) {
+       protected void setAllContactPhoneEntriesCreated (final Contact contact) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
+
                // The contact instance must be valid
                if (null == contact) {
                        // Throw NPE again
                        throw new NullPointerException("contact is null"); //NOI18N
-               } else if (contact.getContactId() == null) {
+               }
+
+               // Get all phone instances
+               DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
+               DialableFaxNumber faxNumber = contact.getContactFaxNumber();
+               DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: landLineNumber={1},faxNumber={2},mobileNumber={3}", this.getClass().getSimpleName(), landLineNumber, faxNumber, mobileNumber)); //NOI18N
+
+               // Is a phone number instance set?
+               if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
+
+                       // Set updated timestamp
+                       landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
+               }
+
+               // Is a fax number instance set?
+               if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() == null)) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
+
+                       // Set updated timestamp
+                       faxNumber.setPhoneEntryCreated(new GregorianCalendar());
+               }
+
+               // Is a mobile number instance set?
+               if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() == null)) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: Setting created timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
+
+                       // Set updated timestamp
+                       mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesCreated: EXIT!", this.getClass().getSimpleName())); //NOI18N
+       }
+
+       /**
+        * Returns a managed instance from given mobile number
+        * <p>
+        * @param mobileNumber  Mobile instance
+        * @param fetchedNumber Found mobile number in database
+        * <p>
+        * @return Managed instance
+        */
+       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
+
+               // Should be valid
+               if (null == mobileNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("mobileNumber is null"); //NOI18N
+               } else if (null == fetchedNumber) {
                        // Throw NPE again
-                       throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
-               } else if (contact.getContactId() < 1) {
+                       throw new NullPointerException("fetchedNumber is null"); //NOI18N
+               } else if (fetchedNumber.getPhoneId() == null) {
+                       // ..and again
+                       throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
+
+               // Default is null
+               DialableMobileNumber managedNumber = null;
+
+               // Is there a difference?
+               if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
+                       // Merge this entry
+                       managedNumber = this.getEntityManager().merge(fetchedNumber);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
+
+               // Return it
+               return managedNumber;
+       }
+
+       /**
+        * Returns a managed instance from given land-line number
+        * <p>
+        * @param landLineNumber Land-line instance
+        * @param fetchedNumber  Found land-line number in database
+        * <p>
+        * @return Managed instance
+        */
+       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
+
+               // Should be valid
+               if (null == landLineNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("landLineNumber is null"); //NOI18N
+               } else if (null == fetchedNumber) {
+                       // Throw NPE again
+                       throw new NullPointerException("fetchedNumber is null"); //NOI18N
+               } else if (fetchedNumber.getPhoneId() == null) {
+                       // ..and again
+                       throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
+
+               // Default is null
+               DialableLandLineNumber managedNumber = null;
+
+               // Is there a difference?
+               if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
+                       // Merge this entry
+                       managedNumber = this.getEntityManager().merge(fetchedNumber);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
+
+               // Return it
+               return managedNumber;
+       }
+
+       /**
+        * Returns a managed instance from given fax number
+        * <p>
+        * @param faxNumber     Fax instance
+        * @param fetchedNumber Found fax number in database
+        * <p>
+        * @return Managed instance
+        */
+       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
+
+               // Should be valid
+               if (null == faxNumber) {
+                       // Throw NPE
+                       throw new NullPointerException("faxNumber is null"); //NOI18N
+               } else if (null == fetchedNumber) {
+                       // Throw NPE again
+                       throw new NullPointerException("fetchedNumber is null"); //NOI18N
+               } else if (fetchedNumber.getPhoneId() == null) {
+                       // ..and again
+                       throw new NullPointerException("fetchedNumber.phoneId is null"); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.getDetached: fetchedNumber.phoneId={1}", this.getClass().getSimpleName(), fetchedNumber.getPhoneId())); //NOI18N
+
+               // Default is null
+               DialableFaxNumber managedNumber = null;
+
+               // Is there a difference?
+               if (!PhoneUtils.isSameFaxNumber(faxNumber, fetchedNumber)) {
+                       // Merge this entry
+                       managedNumber = this.getEntityManager().merge(fetchedNumber);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getDetached: managedNumber={1} - EXIT!", this.getClass().getSimpleName(), managedNumber)); //NOI18N
+
+               // Return it
+               return managedNumber;
+       }
+
+       /**
+        * 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 (detached) contact's data
+        * <p>
+        * @param detachedContact Contact instance to merge
+        * <p>
+        * @return Detached contact instance
+        */
+       protected Contact mergeContactData (final Contact detachedContact) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
+
+               // The contact instance must be valid
+               if (null == detachedContact) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedContact is null"); //NOI18N
+               } else if (detachedContact.getContactId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
+               } else if (detachedContact.getContactId() < 1) {
                        // Not valid
-                       throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+                       throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
                }
 
                // Get contact from it and find it
-               Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
+               Contact managedContact = this.getEntityManager().find(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 (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", detachedContact.getContactId()); //NOI18N
 
                // Debug message
-               this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.updateUserPersonalData: managedContact.contactId={1}", this.getClass().getSimpleName(), managedContact.getContactId())); //NOI18N
 
-               // Merge contact instance
-               Contact detachedContact = this.getEntityManager().merge(foundContact);
+               // Is a fax number set?
+               if (detachedContact.getContactFaxNumber() instanceof DialableFaxNumber) {
+                       // Make fax numbers managed
+                       managedContact.setContactFaxNumber(this.getManaged(detachedContact.getContactFaxNumber(), detachedContact.getContactFaxNumber()));
+               }
+
+               // Is a land-line number set?
+               if (detachedContact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+                       // Make land-line numbers managed
+                       managedContact.setContactLandLineNumber(this.getManaged(detachedContact.getContactLandLineNumber(), detachedContact.getContactLandLineNumber()));
+               }
+
+               // Is a mobile number set?
+               if (detachedContact.getContactMobileNumber() instanceof DialableMobileNumber) {
+                       // Make mobile numbers managed
+                       managedContact.setContactMobileNumber(this.getManaged(detachedContact.getContactMobileNumber(), detachedContact.getContactMobileNumber()));
+               }
+
+               // Set updated timestamp
+               managedContact.setContactUpdated(new GregorianCalendar());
 
                // Copy all
-               detachedContact.copyAll(contact);
+               ContactUtils.copyAll(detachedContact, managedContact);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return detached contact
-               return detachedContact;
+               return managedContact;
        }
 
        /**
@@ -88,7 +353,10 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
         * <p>
         * @param detachedContact Detached contact instance
         */
-       protected void mergeContactsCellphoneLandLineFaxNumbers (final Contact detachedContact) {
+       protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.mergeContactsMobileLandLineFaxNumbers: detachedContact={1} - CALLED!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
+
                // The contact instance must be valid
                if (null == detachedContact) {
                        // Throw NPE again
@@ -98,36 +366,30 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                        throw new NullPointerException("detachedContact.contactId is null"); //NOI18N //NOI18N
                } else if (detachedContact.getContactId() < 1) {
                        // Not valid
-                       throw new IllegalStateException(MessageFormat.format("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
+                       throw new IllegalStateException(MessageFormat.format("{0}.detachedContact.contactId={1} is not valid.", this.getClass().getSimpleName(), detachedContact.getContactId())); //NOI18N
                }
 
                // Get all instances
-               DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
+               DialableMobileNumber cellphone = detachedContact.getContactMobileNumber();
                DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
                DialableFaxNumber fax = detachedContact.getContactFaxNumber();
 
                // Is there a  cellphone instance set?
-               if (cellphone instanceof DialableCellphoneNumber) {
+               if (cellphone instanceof DialableMobileNumber) {
                        // Debug message
                        this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
 
                        // Then find it, too
-                       DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
-
-                       // Should be there
-                       assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
-
-                       // Then merge it, too
-                       DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
+                       DialableMobileNumber foundMobile = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
 
                        // Should be there
-                       assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
+                       assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
 
                        // Copy all
-                       detachedCellphone.copyAll(detachedContact.getContactCellphoneNumber());
+                       foundMobile.copyAll(detachedContact.getContactMobileNumber());
 
                        // Set it back
-                       detachedContact.setContactCellphoneNumber(detachedCellphone);
+                       detachedContact.setContactMobileNumber(foundMobile);
                }
 
                // Is there a  fax instance set?
@@ -138,20 +400,11 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                        // Then find it, too
                        DialableFaxNumber foundFax = this.getEntityManager().find(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
-
                        // 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?
@@ -165,32 +418,216 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                        // 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
+               this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
        }
 
        /**
-        * Updates all contacts's phone entry's updated timestamps
+        * Sends an email with given subject line, template name to given recipient
+        * and user data
         * <p>
-        * @param contact Contact instance to update
-        * @param isCellphoneUnlinked Whether a cellphone entry has been unlinked in
-        * contact instance
+        * @param subjectLine    Subject line
+        * @param templateName   Template name
+        * @param emailAddress   Recipient's email address
+        * @param user           User instance
+        * @param baseUrl        Base URL
+        * @param randomPassword A randomly-generated password or NULL if user had
+        *                       to enter it.
+        */
+       protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl, final String randomPassword) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendEmail: subjectLine={0},templateName={1},emailAddress={2},user={3},baseUrl={4} - CALLED!", subjectLine, templateName, emailAddress, user, baseUrl)); //NOI18N
+
+               // All should be set
+               if (null == subjectLine) {
+                       // Throw NPE
+                       throw new NullPointerException("subjectLine is null"); //NOI18N
+               } else if (subjectLine.isEmpty()) {
+                       // No subject line
+                       throw new IllegalArgumentException("subjectLine is empty"); //NOI18N
+               } else if (null == templateName) {
+                       // Throw NPE
+                       throw new NullPointerException("templateName is null"); //NOI18N
+               } else if (templateName.isEmpty()) {
+                       // No template name
+                       throw new IllegalArgumentException("templateName is empty"); //NOI18N
+               } else if (null == emailAddress) {
+                       // Throw NPE
+                       throw new NullPointerException("emailAddress is null"); //NOI18N
+               } else if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Not valid number
+                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
+               } else if (user.getUserName() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userName is null"); //NOI18N
+               } else if (user.getUserName().isEmpty()) {
+                       // Empty string
+                       throw new IllegalArgumentException("user.userName is empty"); //NOI18N
+               } else if (user.getUserAccountStatus() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
+               } else if (user.getUserContact() == null) {
+                       // Throw it again
+                       throw new NullPointerException("user.userContact is null"); //NOI18N
+               } else if (user.getUserContact().getContactId() == null) {
+                       // .. and again
+                       throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
+               } else if (user.getUserContact().getContactId() < 1) {
+                       // Invalid id
+                       throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
+               } else if (user.getUserContact().getContactPersonalTitle() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
+               }
+
+               // Prepare mail wrapper
+               WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
+
+               // Set all values
+               Properties variables = UserUtils.getAllUserFields(user);
+
+               // Set base URL and random password
+               variables.put("baseUrl", baseUrl); //NOI18N
+               variables.put("randomPassword", ""); //NOI18N
+
+               // Is the random password set?
+               if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
+                       variables.put("randomPassword", randomPassword); //NOI18N
+               }
+
+               // Set all
+               // @TODO Language from message bundle
+               emailWrapper.setRecipient(emailAddress);
+               emailWrapper.setLocale(user.getUserLocale());
+               emailWrapper.setSubjectLine(subjectLine);
+               emailWrapper.setTemplateName(templateName);
+               emailWrapper.setTemplateVariables(variables);
+
+               try {
+                       // Send out email change
+                       ObjectMessage message = this.getSession().createObjectMessage();
+                       message.setObject(emailWrapper);
+
+                       // Send message
+                       this.sendMessage(message);
+               } catch (final JMSException ex) {
+                       // Throw again
+                       throw new EJBException(ex);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace("sendEmail: EXIT!"); //NOI18N
+       }
+
+       /**
+        * Updates all contact's phone instances from other contact, both contacts
+        * should be the same.
+        * <p>
+        * @param contact Contact to set instances
+        * @param other   Other contact to get instances from
+        */
+       protected void setAllContactPhoneEntries (final Contact contact, final Contact other) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: contact={1},other={2} - CALLED!", this.getClass().getSimpleName(), contact, other)); //NOI18N
+
+               // Both must be the same and not null
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("contact is null"); //NOI18N
+               } else if (null == other) {
+                       // Throw NPE
+                       throw new NullPointerException("other is null"); //NOI18N
+               } else if (!Objects.equals(contact, other)) {
+                       // Not same instances
+                       throw new IllegalArgumentException(MessageFormat.format("contact={0} and other={1} are not equal!", contact, other)); //NOI18N
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
+
+               // Is other cellphone not set?
+               if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying cellphone entry ...", this.getClass().getSimpleName())); //NOI18N
+
+                       // Is the fax number set?
+                       if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
+                               // Copy cellphone number
+                               contact.setContactMobileNumber(this.getManaged(other.getContactMobileNumber(), contact.getContactMobileNumber()));
+                       } else {
+                               // Null it
+                               contact.setContactMobileNumber(null);
+                       }
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
+
+               // Is other cellphone not set?
+               if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying land-line entry ...", this.getClass().getSimpleName())); //NOI18N
+
+                       // Is the land-line number set?
+                       if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+                               // Copy land-line number
+                               contact.setContactLandLineNumber(this.getManaged(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
+                       } else {
+                               // Null it
+                               contact.setContactLandLineNumber(null);
+                       }
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: other.contactFaxNumber={1}", this.getClass().getSimpleName(), other.getContactFaxNumber())); //NOI18N
+
+               // Is other cellphone not set?
+               if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntries: Copying fax entry ...", this.getClass().getSimpleName())); //NOI18N
+
+                       // Is the fax number set?
+                       if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
+                               // Copy fax number
+                               contact.setContactFaxNumber(this.getManaged(other.getContactFaxNumber(), contact.getContactFaxNumber()));
+                       } else {
+                               // Null it
+                               contact.setContactFaxNumber(null);
+                       }
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntries: EXIT!", this.getClass().getSimpleName())); //NOI18N
+       }
+
+       /**
+        * Updates all contact's phone entry's updated timestamps
+        * <p>
+        * @param contact            Contact instance to update
+        * @param isMobileUnlinked   Whether a mobile entry has been unlinked in
+        *                           contact instance
         * @param isLandlineUnlinked Whether a land-line entry has been unlinked in
-        * contact instance
-        * @param isFaxUnlinked Whether a fax entry has been unlinked in contact
-        * instance
+        *                           contact instance
+        * @param isFaxUnlinked      Whether a fax entry has been unlinked in
+        *                           contact instance
         */
-       protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
+       protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: contact={1},isMobileUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED", this.getClass().getSimpleName(), contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N
+
                // The contact instance must be valid
                if (null == contact) {
                        // Throw NPE again
@@ -206,37 +643,49 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                // Get all phone instances
                DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
                DialableFaxNumber faxNumber = contact.getContactFaxNumber();
-               DialableCellphoneNumber cellphoneNumber = contact.getContactCellphoneNumber();
+               DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
 
                // Flags and instances must be constistent
-               if (isCellphoneUnlinked && cellphoneNumber instanceof DialableCellphoneNumber) {
+               if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
                        // Bad state
-                       throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber is set.");
+                       throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but mobileNumber is set."); //NOI18N
                } else if (isLandlineUnlinked && landLineNumber instanceof DialableLandLineNumber) {
                        // Bad state
-                       throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set.");
+                       throw new IllegalStateException("isLandlineUnlinked is TRUE, but landLineNumber is set."); //NOI18N
                } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
                        // Bad state
-                       throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set.");
+                       throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
                }
 
                // Is a phone number instance set?
-               if (landLineNumber instanceof DialableLandLineNumber) {
+               if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ...", this.getClass().getSimpleName())); //NOI18N
+
                        // Set updated timestamp
                        landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
                }
 
                // Is a fax number instance set?
-               if (faxNumber instanceof DialableFaxNumber) {
+               if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ...", this.getClass().getSimpleName())); //NOI18N
+
                        // Set updated timestamp
                        faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
                }
 
                // Is a mobile number instance set?
-               if (cellphoneNumber instanceof DialableCellphoneNumber) {
+               if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ...", this.getClass().getSimpleName())); //NOI18N
+
                        // Set updated timestamp
-                       cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
+                       mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
                }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.setAllContactPhoneEntriesUpdated: EXIT!", this.getClass().getSimpleName())); //NOI18N
        }
 
 }