--- /dev/null
+/*
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jfinancials.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.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.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.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 Häder<roland@mxchange.org>
+ */
+public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 12_895_410_275_811_963L;
+
+ /**
+ * Protected constructor
+ */
+ protected BaseFinancialsDatabaseBean () {
+ // Call super contructor
+ super();
+ }
+
+ /**
+ * Constructor with queue factory JNDI and queue JNDI names
+ * <p>
+ * @param factoryJndi JNDI name for queue factory
+ * @param queueJndi JNDI name for email queue
+ */
+ protected BaseFinancialsDatabaseBean (final String factoryJndi, final String queueJndi) {
+ // Call super constructor
+ super(factoryJndi, queueJndi);
+ }
+
+ /**
+ * Updates all contact's phone entry's created timestamps
+ * <p>
+ * @param contact Contact instance to update
+ */
+ protected void setAllContactPhoneEntriesCreated (final Contact contact) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesCreated: contact={0} - CALLED!", contact)); //NOI18N
+
+ // The contact instance must be valid
+ if (null == contact) {
+ // Throw NPE again
+ throw new NullPointerException("contact is null"); //NOI18N
+ }
+
+ // Get all phone instances
+ DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
+ DialableFaxNumber faxNumber = contact.getContactFaxNumber();
+ DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntriesCreated: landLineNumber={0},faxNumber={1},mobileNumber={2}", landLineNumber, faxNumber, mobileNumber)); //NOI18N
+
+ // Is a phone number instance set?
+ if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() == null)) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesCreated: Setting created timestamp for land-line number ..."); //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("setAllContactPhoneEntriesCreated: Setting created timestamp for fax number ..."); //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("setAllContactPhoneEntriesCreated: Setting created timestamp for mobile number ..."); //NOI18N
+
+ // Set updated timestamp
+ mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N
+ }
+
+ /**
+ * Returns a detached instance from given mobile instance
+ * <p>
+ * @param mobileNumber Mobile instance
+ * @param fetchedNumber Found mobile number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: mobileNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == mobileNumber) {
+ // Throw NPE
+ throw new NullPointerException("mobileNumber 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("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+ // Init query instance
+ DialableMobileNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+ // Default is null
+ DialableMobileNumber detachedNumber = null;
+
+ // Is there a difference?
+ if (!PhoneUtils.isSameMobileNumber(mobileNumber, fetchedNumber)) {
+ // Merge this entry
+ detachedNumber = this.getEntityManager().merge(foundNumber);
+
+ // Copy all
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+ // Return it
+ return detachedNumber;
+ }
+
+ /**
+ * Returns a detached instance from given land-line instance
+ * <p>
+ * @param landLineNumber Land-line instance
+ * @param fetchedNumber Found land-line number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableLandLineNumber getDetached (final DialableLandLineNumber landLineNumber, final DialableLandLineNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: landLineNumber={0},fetchedNumber={1} - CALLED!", landLineNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == landLineNumber) {
+ // Throw NPE
+ throw new NullPointerException("landLineNumber 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("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+ // Init query instance
+ DialableLandLineNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+ // Default is null
+ DialableLandLineNumber detachedNumber = null;
+
+ // Is there a difference?
+ if (!PhoneUtils.isSameLandLineNumber(landLineNumber, fetchedNumber)) {
+ // Merge this entry
+ detachedNumber = this.getEntityManager().merge(foundNumber);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+ // Return it
+ return detachedNumber;
+ }
+
+ /**
+ * Returns a detached instance from given fax instance
+ * <p>
+ * @param faxNumber Fax instance
+ * @param fetchedNumber Found fax number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableFaxNumber getDetached (final DialableFaxNumber faxNumber, final DialableFaxNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: faxNumber={0},fetchedNumber={1} - CALLED!", faxNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == faxNumber) {
+ // Throw NPE
+ throw new NullPointerException("faxNumber 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("getDetached: fetchedNumber.phoneId={0}", fetchedNumber.getPhoneId())); //NOI18N
+
+ // Init query instance
+ DialableFaxNumber foundNumber = this.getEntityManager().find(fetchedNumber.getClass(), fetchedNumber.getPhoneId());
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("getDetached: foundNumber={0}", foundNumber)); //NOI18N
+
+ // Default is null
+ DialableFaxNumber detachedNumber = 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("getDetached: detachedNumber={0} - EXIT!", detachedNumber)); //NOI18N
+
+ // Return it
+ return detachedNumber;
+ }
+
+ /**
+ * Get back a managed instance from given user
+ * <p>
+ * @param user Unmanaged/detached user instance
+ * <p>
+ * @return Managed user instance
+ */
+ protected User getManagedUser (final User user) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
+
+ // user should not be null
+ if (null == user) {
+ // Abort here
+ throw new NullPointerException("user is null"); //NOI18N
+ } else if (user.getUserId() == null) {
+ // Id is set
+ throw new NullPointerException("user.userId is null"); //NOI18N
+ } else if (user.getUserId() < 1) {
+ // Id is set
+ throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
+ } else if (user.getUserContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("user.userContact is null"); //NOI18N
+ } else if (user.getUserContact().getContactId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
+ } else if (user.getUserContact().getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N#
+ }
+
+ // Try to find it (should be there)
+ User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
+
+ // Should be there
+ assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
+
+ // Return it
+ return managedUser;
+ }
+
+ /**
+ * Merges given contact's data
+ * <p>
+ * @param detachedContact Contact instance to merge
+ * <p>
+ * @return Detached contact instance
+ */
+ protected Contact mergeContactData (final Contact detachedContact) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - CALLED!", 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("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
+ }
+
+ // Set updated timestamp
+ detachedContact.setContactUpdated(new GregorianCalendar());
+
+ // Get contact from it and find it
+ Contact foundContact = 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.", detachedContact.getContactId()); //NOI18N
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
+
+ // Merge contact instance
+ Contact managedContact = this.getEntityManager().merge(foundContact);
+
+ // Copy all
+ ContactUtils.copyAll(detachedContact, managedContact);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", managedContact)); //NOI18N
+
+ // Return detached contact
+ return managedContact;
+ }
+
+ /**
+ * Merges given (detached) contact's mobile, land-line and fax numbers
+ * <p>
+ * @param detachedContact Detached contact instance
+ */
+ protected void mergeContactsMobileLandLineFaxNumbers (final Contact detachedContact) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactsMobileLandLineFaxNumbers: detachedContact={0} - CALLED!", 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("detachedContact.contactId={0} is not valid.", detachedContact.getContactId())); //NOI18N
+ }
+
+ // Get all instances
+ DialableMobileNumber mobile = detachedContact.getContactMobileNumber();
+ DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
+ DialableFaxNumber fax = detachedContact.getContactFaxNumber();
+
+ // Is there a mobile instance set?
+ if (mobile instanceof DialableMobileNumber) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobile.getPhoneId())); //NOI18N
+
+ // Then find it, too
+ DialableMobileNumber foundMobile = this.getEntityManager().find(mobile.getClass(), mobile.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(detachedContact.getContactMobileNumber(), managedMobile);
+
+ // Set it back
+ detachedContact.setContactMobileNumber(managedMobile);
+ }
+
+ // Is there a fax instance set?
+ if (fax instanceof DialableFaxNumber) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
+
+ // 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 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(detachedContact.getContactFaxNumber(), managedFax);
+
+ // Set it back
+ detachedContact.setContactFaxNumber(managedFax);
+ }
+
+ // Is there a fax instance set?
+ if (landLine instanceof DialableLandLineNumber) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
+
+ // Then find it, too
+ 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 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(detachedContact.getContactLandLineNumber(), managedLandLine);
+
+ // Set it back
+ detachedContact.setContactLandLineNumber(managedLandLine);
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("mergeContactsMobileLandLineFaxNumbers: EXIT!"); //NOI18N
+ }
+
+ /**
+ * Sends an email with given subject line, template name to given recipient
+ * and user data
+ * <p>
+ * @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
+ } else if (user.getUserLocale() == null) {
+ // Throw NPE again
+ throw new NullPointerException("user.userLocale 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("setAllContactPhoneEntries: contact={0},other={1} - CALLED!", 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("setAllContactPhoneEntries: other.contactMobileNumber={0}", other.getContactMobileNumber())); //NOI18N
+
+ // Is other mobile not set?
+ if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying mobile entry ..."); //NOI18N
+
+ // Is the fax number set?
+ if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
+ // Copy mobile number
+ contact.setContactMobileNumber(this.getDetached(other.getContactMobileNumber(), contact.getContactMobileNumber()));
+ } else {
+ // Null it
+ contact.setContactMobileNumber(null);
+ }
+ }
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactLandLineNumber={0}", other.getContactLandLineNumber())); //NOI18N
+
+ // Is other mobile not set?
+ if ((other.getContactLandLineNumber() == null) || (PhoneUtils.isSameLandLineNumber(contact.getContactLandLineNumber(), other.getContactLandLineNumber()))) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying land-line entry ..."); //NOI18N
+
+ // Is the land-line number set?
+ if (other.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+ // Copy land-line number
+ contact.setContactLandLineNumber(this.getDetached(other.getContactLandLineNumber(), contact.getContactLandLineNumber()));
+ } else {
+ // Null it
+ contact.setContactLandLineNumber(null);
+ }
+ }
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("setAllContactPhoneEntries: other.contactFaxNumber={0}", other.getContactFaxNumber())); //NOI18N
+
+ // Is other mobile not set?
+ if ((other.getContactFaxNumber() == null) || (PhoneUtils.isSameFaxNumber(contact.getContactFaxNumber(), other.getContactFaxNumber()))) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying fax entry ..."); //NOI18N
+
+ // Is the fax number set?
+ if (other.getContactFaxNumber() instanceof DialableFaxNumber) {
+ // Copy fax number
+ contact.setContactFaxNumber(this.getDetached(other.getContactFaxNumber(), contact.getContactFaxNumber()));
+ } else {
+ // Null it
+ contact.setContactFaxNumber(null);
+ }
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntries: EXIT!"); //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
+ */
+ protected void setAllContactPhoneEntriesUpdated (final Contact contact, final boolean isMobileUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("setAllContactPhoneEntriesUpdated: contact={0},isMobileUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED", contact, isMobileUnlinked, isLandlineUnlinked, isFaxUnlinked)); //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) {
+ // Throw NPE again
+ throw new NullPointerException("contact.contactId is null"); //NOI18N //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid
+ throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
+ // Get all phone instances
+ DialableLandLineNumber landLineNumber = contact.getContactLandLineNumber();
+ DialableFaxNumber faxNumber = contact.getContactFaxNumber();
+ DialableMobileNumber mobileNumber = contact.getContactMobileNumber();
+
+ // Flags and instances must be constistent
+ if (isMobileUnlinked && mobileNumber instanceof DialableMobileNumber) {
+ // Bad state
+ 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."); //NOI18N
+ } else if (isFaxUnlinked && faxNumber instanceof DialableFaxNumber) {
+ // Bad state
+ throw new IllegalStateException("isFaxUnlinked is TRUE, but faxNumber is set."); //NOI18N
+ }
+
+ // Is a phone number instance set?
+ if ((landLineNumber instanceof DialableLandLineNumber) && (landLineNumber.getPhoneId() instanceof Long) && (landLineNumber.getPhoneId() > 0)) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for land-line number ..."); //NOI18N
+
+ // Set updated timestamp
+ landLineNumber.setPhoneEntryUpdated(new GregorianCalendar());
+ }
+
+ // Is a fax number instance set?
+ if ((faxNumber instanceof DialableFaxNumber) && (faxNumber.getPhoneId() instanceof Long) && (faxNumber.getPhoneId() > 0)) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for fax number ..."); //NOI18N
+
+ // Set updated timestamp
+ faxNumber.setPhoneEntryUpdated(new GregorianCalendar());
+ }
+
+ // Is a mobile number instance set?
+ if ((mobileNumber instanceof DialableMobileNumber) && (mobileNumber.getPhoneId() instanceof Long) && (mobileNumber.getPhoneId() > 0)) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for mobile number ..."); //NOI18N
+
+ // Set updated timestamp
+ mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N
+ }
+
+}