+++ /dev/null
-/*
- * Copyright (C) 2016 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.addressbook.database;
-
-import java.text.MessageFormat;
-import java.util.GregorianCalendar;
-import java.util.Objects;
-import java.util.Properties;
-import javax.ejb.EJBException;
-import javax.faces.FacesException;
-import javax.jms.Connection;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.ObjectMessage;
-import javax.jms.Queue;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.Session;
-import javax.mail.Address;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import org.mxchange.jcontacts.contact.Contact;
-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.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 Häder<roland@mxchange.org>
- */
-public abstract class BaseAddressbookDatabaseBean extends BaseDatabaseBean {
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 12_895_410_275_811_963L;
-
- /**
- * Connection
- */
- private Connection connection;
-
- /**
- * Message producer
- */
- private MessageProducer messageProducer;
-
- /**
- * Mailer message queue
- */
- private Queue queue;
-
- /**
- * Session instance
- */
- private Session session;
-
- /**
- * Protected constructor
- */
- protected BaseAddressbookDatabaseBean () {
- // Call super constructor
- super();
-
- try {
- // Get initial context
- Context context = new InitialContext();
-
- // Get factory from JMS resource
- QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/addressbook-queue-factory"); //NOI18N
-
- // Lookup queue
- this.queue = (Queue) context.lookup("jms/addressbook-email-queue"); //NOI18N
-
- // Create connection
- this.connection = connectionFactory.createConnection();
-
- // Init session instance
- this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- // And message producer
- this.messageProducer = this.session.createProducer(this.queue);
- } catch (final NamingException | JMSException e) {
- // Continued to throw
- throw new FacesException(e);
- }
- }
-
- /**
- * Updates all contacts'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},cellphoneNumber={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 cellphone number ..."); //NOI18N
-
- // Set updated timestamp
- mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
- }
-
- // Trace message
- this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N
- }
-
- /**
- * Returnes a detached instance from given cellphone instance
- * <p>
- * @param mobileNumber Mobile instance
- * @param fetchedNumber Found cellphone number in database
- * <p>
- * @return Detached instance
- */
- protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
-
- // Should be valid
- if (null == mobileNumber) {
- // Throw NPE
- throw new NullPointerException("cellphoneNumber 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;
- }
-
- /**
- * Returnes 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;
- }
-
- /**
- * Returnes 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 contact Contact instance to merge
- * <p>
- * @return Detached contact instance
- */
- protected Contact mergeContactData (final Contact contact) {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", 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) {
- // 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
- }
-
- // Set updated timestamp
- contact.setContactUpdated(new GregorianCalendar());
-
- // Get contact from it and find it
- Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
-
- // Should be found
- assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
-
- // Debug message
- this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
-
- // Merge contact instance
- Contact detachedContact = this.getEntityManager().merge(foundContact);
-
- // Copy all
- detachedContact.copyAll(contact);
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
-
- // Return detached contact
- return detachedContact;
- }
-
- /**
- * Merges given (detached) contact's cellphone, 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 cellphone = detachedContact.getContactMobileNumber();
- DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
- DialableFaxNumber fax = detachedContact.getContactFaxNumber();
-
- // Is there a cellphone instance set?
- 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
- 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());
-
- // Set it back
- detachedContact.setContactMobileNumber(detachedMobile);
- }
-
- // 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 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());
-
- // Set it back
- detachedContact.setContactFaxNumber(detachedFax);
- }
-
- // 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 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());
-
- // Set it back
- detachedContact.setContactLandLineNumber(detachedLandLine);
- }
-
- // 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
- */
- protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
- // 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
- }
-
- // Prepare mail wrapper
- WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
-
- // Set all values
- Properties variables = UserUtils.getAllUserFields(user);
-
- // Set base URL
- variables.put("baseUrl", baseUrl); //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.session.createObjectMessage();
- message.setObject(emailWrapper);
-
- // Send message
- this.sendMessage(message, this.messageProducer);
- } 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 cellphone not set?
- if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
- // Debug message
- this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N
-
- // Is the fax number set?
- if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
- // Copy cellphone 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 cellphone 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 cellphone 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 contacts's phone entry's updated timestamps
- * <p>
- * @param contact Contact instance to update
- * @param isMobileUnlinked Whether a cellphone 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 cellphoneNumber = contact.getContactMobileNumber();
-
- // Flags and instances must be constistent
- if (isMobileUnlinked && cellphoneNumber instanceof DialableMobileNumber) {
- // Bad state
- throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber 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 ((cellphoneNumber instanceof DialableMobileNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) {
- // Debug message
- this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N
-
- // Set updated timestamp
- cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
- }
-
- // Trace message
- this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N
- }
-
-}
--- /dev/null
+/*
+ * Copyright (C) 2016 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.faces.FacesException;
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.MessageProducer;
+import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.Session;
+import javax.mail.Address;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcontacts.contact.Contact;
+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.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 Häder<roland@mxchange.org>
+ */
+public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 12_895_410_275_811_963L;
+
+ /**
+ * Connection
+ */
+ private Connection connection;
+
+ /**
+ * Message producer
+ */
+ private MessageProducer messageProducer;
+
+ /**
+ * Mailer message queue
+ */
+ private Queue queue;
+
+ /**
+ * Session instance
+ */
+ private Session session;
+
+ /**
+ * Protected constructor
+ */
+ protected BaseFinancialsDatabaseBean () {
+ // Call super constructor
+ super();
+
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Get factory from JMS resource
+ QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/addressbook-queue-factory"); //NOI18N
+
+ // Lookup queue
+ this.queue = (Queue) context.lookup("jms/addressbook-email-queue"); //NOI18N
+
+ // Create connection
+ this.connection = connectionFactory.createConnection();
+
+ // Init session instance
+ this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // And message producer
+ this.messageProducer = this.session.createProducer(this.queue);
+ } catch (final NamingException | JMSException e) {
+ // Continued to throw
+ throw new FacesException(e);
+ }
+ }
+
+ /**
+ * Updates all contacts'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},cellphoneNumber={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 cellphone number ..."); //NOI18N
+
+ // Set updated timestamp
+ mobileNumber.setPhoneEntryCreated(new GregorianCalendar());
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesCreated: EXIT!"); //NOI18N
+ }
+
+ /**
+ * Returnes a detached instance from given cellphone instance
+ * <p>
+ * @param mobileNumber Mobile instance
+ * @param fetchedNumber Found cellphone number in database
+ * <p>
+ * @return Detached instance
+ */
+ protected DialableMobileNumber getDetached (final DialableMobileNumber mobileNumber, final DialableMobileNumber fetchedNumber) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getDetached: cellphoneNumber={0},fetchedNumber={1} - CALLED!", mobileNumber, fetchedNumber)); //NOI18N
+
+ // Should be valid
+ if (null == mobileNumber) {
+ // Throw NPE
+ throw new NullPointerException("cellphoneNumber 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;
+ }
+
+ /**
+ * Returnes 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;
+ }
+
+ /**
+ * Returnes 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 contact Contact instance to merge
+ * <p>
+ * @return Detached contact instance
+ */
+ protected Contact mergeContactData (final Contact contact) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: contact={0} - CALLED!", 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) {
+ // 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
+ }
+
+ // Set updated timestamp
+ contact.setContactUpdated(new GregorianCalendar());
+
+ // Get contact from it and find it
+ Contact foundContact = this.getEntityManager().find(contact.getClass(), contact.getContactId());
+
+ // Should be found
+ assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", contact.getContactId()); //NOI18N
+
+ // Debug message
+ this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundContact.contactId={0}", foundContact.getContactId())); //NOI18N
+
+ // Merge contact instance
+ Contact detachedContact = this.getEntityManager().merge(foundContact);
+
+ // Copy all
+ detachedContact.copyAll(contact);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
+
+ // Return detached contact
+ return detachedContact;
+ }
+
+ /**
+ * Merges given (detached) contact's cellphone, 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 cellphone = detachedContact.getContactMobileNumber();
+ DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
+ DialableFaxNumber fax = detachedContact.getContactFaxNumber();
+
+ // Is there a cellphone instance set?
+ 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
+ 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());
+
+ // Set it back
+ detachedContact.setContactMobileNumber(detachedMobile);
+ }
+
+ // 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 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());
+
+ // Set it back
+ detachedContact.setContactFaxNumber(detachedFax);
+ }
+
+ // 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 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());
+
+ // Set it back
+ detachedContact.setContactLandLineNumber(detachedLandLine);
+ }
+
+ // 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
+ */
+ protected void sendEmail (final String subjectLine, final String templateName, final Address emailAddress, final User user, final String baseUrl) {
+ // 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
+ }
+
+ // Prepare mail wrapper
+ WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
+
+ // Set all values
+ Properties variables = UserUtils.getAllUserFields(user);
+
+ // Set base URL
+ variables.put("baseUrl", baseUrl); //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.session.createObjectMessage();
+ message.setObject(emailWrapper);
+
+ // Send message
+ this.sendMessage(message, this.messageProducer);
+ } 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 cellphone not set?
+ if ((other.getContactMobileNumber() == null) || (PhoneUtils.isSameMobileNumber(contact.getContactMobileNumber(), other.getContactMobileNumber()))) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntries: Copying cellphone entry ..."); //NOI18N
+
+ // Is the fax number set?
+ if (other.getContactMobileNumber() instanceof DialableMobileNumber) {
+ // Copy cellphone 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 cellphone 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 cellphone 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 contacts's phone entry's updated timestamps
+ * <p>
+ * @param contact Contact instance to update
+ * @param isMobileUnlinked Whether a cellphone 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 cellphoneNumber = contact.getContactMobileNumber();
+
+ // Flags and instances must be constistent
+ if (isMobileUnlinked && cellphoneNumber instanceof DialableMobileNumber) {
+ // Bad state
+ throw new IllegalStateException("isCellPhoneUnlinked is TRUE, but cellphoneNumber 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 ((cellphoneNumber instanceof DialableMobileNumber) && (cellphoneNumber.getPhoneId() instanceof Long) && (cellphoneNumber.getPhoneId() > 0)) {
+ // Debug message
+ this.getLoggerBeanLocal().logDebug("setAllContactPhoneEntriesUpdated: Setting updated timestamp for cellphone number ..."); //NOI18N
+
+ // Set updated timestamp
+ cellphoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace("setAllContactPhoneEntriesUpdated: EXIT!"); //NOI18N
+ }
+
+}