X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjcontacts%2Fcontact%2FPizzaContactSessionBean.java;h=f2d21cecb9f696e38d2b4d7d4913facc2b7da860;hb=5ec6cc2c6f5792bdb6807d2f2099b8cb06bd8537;hp=2083847e3868e70101f4a935390a62d971d6c8d4;hpb=25d55c458d9cecce8324850db619e4c08f1f70ff;p=pizzaservice-ejb.git diff --git a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java index 2083847..f2d21ce 100644 --- a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java +++ b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java @@ -17,7 +17,6 @@ package org.mxchange.jcontacts.contact; import java.text.MessageFormat; -import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; import java.util.Objects; @@ -25,11 +24,8 @@ import javax.ejb.Stateless; import javax.persistence.NoResultException; 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 *

@@ -50,42 +46,51 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co } @Override - public Contact addContact (final Contact contact) throws ContactAlreadyAddedException { + public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException { // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact={0} - CALLED!", contact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N - // The contact instance must be valid - 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); + // 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 } - // Set created timestamp - contact.setContactCreated(new GregorianCalendar()); + // Get query instance + Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N - // Set all created timestamps in cellphone, land-line and fax number(s) - this.setAllContactPhoneEntriesCreated(contact); + // 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(); - // Persist it - this.getEntityManager().persist(contact); + // 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); + } - // Flush it - this.getEntityManager().flush(); + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N - // Return updated instance + // 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) { @@ -111,14 +116,14 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co 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; @@ -128,16 +133,16 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co @SuppressWarnings ("unchecked") public List 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 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; @@ -147,25 +152,62 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co @SuppressWarnings ("unchecked") public List 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 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 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("isContactFound: contact={0} - CALLED!", contact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N // Parameter should be valid if (null == contact) { @@ -190,7 +232,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(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N return null; } @@ -203,12 +245,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("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N // Found it foundContact = next; @@ -217,7 +259,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("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N // Return status return foundContact; @@ -226,7 +268,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co @Override public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) { // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N + 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) { @@ -247,7 +289,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co Contact detachedContact = this.mergeContactData(contact); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N // Return it return detachedContact; @@ -256,7 +298,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co @Override public Contact updateContactData (final Contact contact) { // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: contact={0} - CALLED!", contact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N // The contact instance must be valid if (null == contact) { @@ -279,7 +321,7 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N // Return it return detachedContact;