]> git.mxchange.org Git - pizzaservice-ejb.git/blobdiff - src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
implemented addContact()
[pizzaservice-ejb.git] / src / java / org / mxchange / jcontacts / contact / PizzaContactSessionBean.java
index 2083847e3868e70101f4a935390a62d971d6c8d4..a7dc1b7e1734b81cf5263f2b0e36b0aa3192e84b 100644 (file)
@@ -27,9 +27,7 @@ import javax.persistence.Query;
 import org.mxchange.jcontacts.contact.utils.ContactUtils;
 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
-import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;;
-import org.mxchange.jcontacts.contact.utils.ContactUtils;
-
+import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
 /**
  * A contact EJB
  * <p>
@@ -51,34 +49,76 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
 
        @Override
        public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
-               // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact={0} - CALLED!", contact)); //NOI18N
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact={0} - CALLED!", contact));
 
-               // The contact instance must be valid
+               // Is the instance set?
                if (null == contact) {
-                       // Throw NPE again
-                       throw new NullPointerException("contact is null"); //NOI18N
-               } else if (contact.getContactId() instanceof Long) {
-                       // Throw NPE again
-                       throw new NullPointerException("contact.contactId is not null"); //NOI18N //NOI18N
-               } else if (this.lookupContact(contact) instanceof Contact) {
-                       // Already found
-                       throw new ContactAlreadyAddedException(contact);
+                       // Throw NPE
+                       throw new NullPointerException("contact is null");
+               } else if (contact.getContactId() != null) {
+                       // Should be null
+                       throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId()));
                }
 
                // Set created timestamp
                contact.setContactCreated(new GregorianCalendar());
 
-               // Set all created timestamps in cellphone, land-line and fax number(s)
+               // Set all created timestamps, if instance is there
                this.setAllContactPhoneEntriesCreated(contact);
 
                // Persist it
                this.getEntityManager().persist(contact);
 
-               // Flush it
+               // Flush it to get contactId set
                this.getEntityManager().flush();
 
-               // Return updated instance
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact.contactId={0} after persisting - EXIT!", contact.getContactId()));
+
+               // Return it
+               return contact;
+       }
+
+       @Override
+       public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: emailAddress={0} - CALLED!", 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("findContactByEmailAddress: Found contact={0}", contact)); //NOI18N
+               } catch (final NoResultException ex) {
+                       // No result found
+                       throw new ContactNotFoundException(emailAddress, ex);
+               }
+
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactByEmailAddress: contact={0} - EXIT!", contact)); //NOI18N
+
+               // Return found instance
                return contact;
        }
 
@@ -162,10 +202,47 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
                return emailAddresses;
        }
 
+       @Override
+       public boolean isEmailAddressRegistered (final String emailAddress) {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
+
+               // The email address should be valid
+               if (null == emailAddress) {
+                       // Is null
+                       throw new NullPointerException("emailAddress is null");
+               } else if (emailAddress.isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("emailAddress is empty");
+               }
+
+               // 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("isEmailAddressRegistered: Found contact={0} for emailAddress={1}", contact, emailAddress));
+
+                       // 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("isEmailAddressRegistered: isFound={0} - EXIT!", isFound));
+
+               // Return status
+               return isFound;
+       }
+
        @Override
        public Contact lookupContact (final Contact contact) {
                // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: contact={0} - CALLED!", contact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: contact={0} - CALLED!", contact)); //NOI18N
 
                // Parameter should be valid
                if (null == contact) {
@@ -190,7 +267,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
                // Is the list empty?
                if (contacts.isEmpty()) {
                        // Then abort here
-                       this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
+                       this.getLoggerBeanLocal().logTrace("lookupContact: No contacts registered, returning 'false' ..."); //NOI18N
                        return null;
                }
 
@@ -203,12 +280,12 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
                        Contact next = iterator.next();
 
                        // Debug message
-                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: next={0}", next)); //NOI18N
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("lookupContact: next={0}", next)); //NOI18N
 
                        // Is same contact?
                        if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) {
                                // Debug message
-                               this.getLoggerBeanLocal().logDebug(MessageFormat.format("isContactFound: Found same contact, id={0}", next.getContactId())); //NOI18N
+                               this.getLoggerBeanLocal().logDebug(MessageFormat.format("lookupContact: Found same contact, id={0}", next.getContactId())); //NOI18N
 
                                // Found it
                                foundContact = next;
@@ -217,7 +294,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
                }
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: foundContact={0} - EXIT!", foundContact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: foundContact={0} - EXIT!", foundContact)); //NOI18N
 
                // Return status
                return foundContact;