]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blobdiff - src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
Please cherry-pick:
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jcontacts / contact / AddressbookContactSessionBean.java
index 9e8a66a3b2e1694f30bf3df1300e06ed646a4992..e3f6cf6bd7178f1277a9dfb1794b75efaa97b07e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Roland Haeder
+ * 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 Affero General Public License as
 package org.mxchange.jcontacts.contact;
 
 import java.text.MessageFormat;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
+import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+import org.mxchange.jcontacts.contact.utils.ContactUtils;
 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
-import org.mxchange.jcoreee.database.BaseDatabaseBean;
 
 /**
  * A contact EJB
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
  */
-@Stateless (name = "contact", mappedName = "ejb/stateless-addressbook-contact", description = "A bean handling contact data")
-public class AddressbookContactSessionBean extends BaseDatabaseBean implements ContactSessionBeanRemote {
+@Stateless (name = "contact", description = "A bean handling contact data")
+public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote {
 
        /**
         * Serial number
@@ -43,10 +46,52 @@ public class AddressbookContactSessionBean extends BaseDatabaseBean implements C
        public AddressbookContactSessionBean () {
        }
 
+       @Override
+       public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
+
+               // The parameter must be valid
+               if (null == emailAddress) {
+                       // Throw NPE
+                       throw new NullPointerException("emailAddress is null"); //NOI18N
+               } else if (emailAddress.isEmpty()) {
+                       // Not valid
+                       throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
+               }
+
+               // Get query instance
+               Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N
+
+               // Set parameter
+               query.setParameter("emailAddress", emailAddress); //NOI18N
+
+               // Init contact instance
+               Contact contact;
+
+               // Try to find a result
+               try {
+                       // Find a single result
+                       contact = (Contact) query.getSingleResult();
+
+                       // Log trace message
+                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
+               } catch (final NoResultException ex) {
+                       // No result found
+                       throw new ContactNotFoundException(emailAddress, ex);
+               }
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
+
+               // Return found instance
+               return contact;
+       }
+
        @Override
        public Contact findContactById (final Long contactId) throws ContactNotFoundException {
                // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N
 
                // The parameter must be valid
                if (null == contactId) {
@@ -72,14 +117,14 @@ public class AddressbookContactSessionBean extends BaseDatabaseBean implements C
                        contact = (Contact) query.getSingleResult();
 
                        // Log trace message
-                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", contact)); //NOI18N
+                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
                } catch (final NoResultException ex) {
                        // No result found
                        throw new ContactNotFoundException(contactId, ex);
                }
 
                // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contact={0} - EXIT!", contact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N
 
                // Return found instance
                return contact;
@@ -89,16 +134,16 @@ public class AddressbookContactSessionBean extends BaseDatabaseBean implements C
        @SuppressWarnings ("unchecked")
        public List<Contact> getAllContacts () {
                // Log trace message
-               this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N
 
                // Create query instance
-               Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N
 
                // Get list
                List<Contact> contacts = query.getResultList();
 
                // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N
 
                // Return it
                return contacts;
@@ -108,24 +153,173 @@ public class AddressbookContactSessionBean extends BaseDatabaseBean implements C
        @SuppressWarnings ("unchecked")
        public List<String> getEmailAddressList () {
                // Log trace message
-               this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N
 
                // Create query instance
-               Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N
 
                // Get list
                List<String> emailAddresses = query.getResultList();
 
                // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N
 
                // Return it
                return emailAddresses;
        }
 
        @Override
-       public void updateContactPersonalData (final Contact contact) {
-               throw new UnsupportedOperationException("Not supported yet."); //NOI18N
+       public boolean isEmailAddressRegistered (final String emailAddress) {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N
+
+               // The email address should be valid
+               if (null == emailAddress) {
+                       // Is null
+                       throw new NullPointerException("emailAddress is null"); //NOI18N
+               } else if (emailAddress.isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
+               }
+
+               // Default is not found
+               boolean isFound = false;
+
+               try {
+                       // Ask other method for contact instance
+                       Contact contact = this.findContactByEmailAddress(emailAddress);
+
+                       // Log debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N
+
+                       // It is found ...
+                       isFound = true;
+               } catch (final ContactNotFoundException ex) {
+                       // @TODO Was not found, log exception for spam check?
+               }
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
+
+               // Return status
+               return isFound;
+       }
+
+       @Override
+       public Contact lookupContact (final Contact contact) {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
+
+               // Parameter should be valid
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("contact is null"); //NOI18N
+               } else if (contact.getContactId() > 0) {
+                       try {
+                               // Id set, ask other method
+                               return this.findContactById(contact.getContactId());
+                       } catch (final ContactNotFoundException ex) {
+                               // Not found, should not happen
+                               throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N
+                       }
+               }
+
+               // Default is not found
+               Contact foundContact = null;
+
+               // Get whole list
+               List<Contact> contacts = this.getAllContacts();
+
+               // Is the list empty?
+               if (contacts.isEmpty()) {
+                       // Then abort here
+                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N
+                       return null;
+               }
+
+               // Get iterator
+               Iterator<Contact> iterator = contacts.iterator();
+
+               // Loop through all
+               while (iterator.hasNext()) {
+                       // Get contact
+                       Contact next = iterator.next();
+
+                       // Is same contact?
+                       if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
+                               // Debug message
+                               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N
+
+                               // Found it
+                               foundContact = next;
+                               break;
+                       }
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N
+
+               // Return found contact
+               return foundContact;
+       }
+
+       @Override
+       public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, 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
+               } else if (contact.getContactId() < 1) {
+                       // Not valid
+                       throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+               }
+
+               // Merge cellphone, land-line and fix
+               Contact managedContact = this.mergeContactData(contact);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
+
+               // Return it
+               return managedContact;
+       }
+
+       @Override
+       public Contact updateContactData (final Contact contact) {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: 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) {
+                       // Throw NPE again
+                       throw new NullPointerException("contact.contactId is null"); //NOI18N
+               } else if (contact.getContactId() < 1) {
+                       // Not valid
+                       throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+               }
+
+               // Is cell phone/land-line/fax number unlinked?
+               boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null);
+               boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null);
+               boolean isFaxUnlinked = (contact.getContactFaxNumber() == null);
+
+               // Call other Method
+               Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N
+
+               // Return it
+               return detachedContact;
        }
 
 }