X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=inline;f=src%2Fjava%2Forg%2Fmxchange%2Fjcontacts%2Fcontact%2FPizzaContactSessionBean.java;h=a7dc1b7e1734b81cf5263f2b0e36b0aa3192e84b;hb=514fcc5585e4b0eddeae821c4bf540bf0f602bf9;hp=f41072ca9a9cd5040a92c72072733344cef5ffc0;hpb=958dcff9cf4f0467ecfe5bf9dcc615953f755f12;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 f41072c..a7dc1b7 100644
--- a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
+++ b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
@@ -17,19 +17,24 @@
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;
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.jcoreee.database.BaseDatabaseBean;
-
+import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
/**
* A contact EJB
*
* @author Roland Haeder
*/
-@Stateless (name = "contact", mappedName = "ejb/stateless-pizza-contact", description = "A bean handling contact data")
-public class PizzaContactSessionBean extends BaseDatabaseBean implements ContactSessionBeanRemote {
+@Stateless (name = "contact", description = "A bean handling contact data")
+public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements ContactSessionBeanRemote {
/**
* Serial number
@@ -42,8 +47,86 @@ public class PizzaContactSessionBean extends BaseDatabaseBean implements Contact
public PizzaContactSessionBean () {
}
+ @Override
+ public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: contact={0} - CALLED!", contact));
+
+ // Is the instance set?
+ if (null == 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, if instance is there
+ this.setAllContactPhoneEntriesCreated(contact);
+
+ // Persist it
+ this.getEntityManager().persist(contact);
+
+ // Flush it to get contactId set
+ this.getEntityManager().flush();
+
+ // 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;
+ }
+
@Override
public Contact findContactById (final Long contactId) throws ContactNotFoundException {
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: contactId={0} - CALLED!", contactId)); //NOI18N
+
// The parameter must be valid
if (null == contactId) {
// Throw NPE
@@ -54,7 +137,7 @@ public class PizzaContactSessionBean extends BaseDatabaseBean implements Contact
}
// Get query instance
- Query query = this.getEntityManager().createNamedQuery("SearchContactId", UserContact.class); //NOI18N
+ Query query = this.getEntityManager().createNamedQuery("SearchContactById", Contact.class); //NOI18N
// Set parameter
query.setParameter("contactId", contactId); //NOI18N
@@ -66,13 +149,217 @@ public class PizzaContactSessionBean extends BaseDatabaseBean implements Contact
try {
// Find a single result
contact = (Contact) query.getSingleResult();
+
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("findContactById: Found contact={0}", 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
+
// Return found instance
return contact;
}
+ @Override
+ @SuppressWarnings ("unchecked")
+ public List getAllContacts () {
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace("getAllContacts - CALLED!"); //NOI18N
+
+ // Create query instance
+ Query query = this.getEntityManager().createNamedQuery("AllContacts", List.class); //NOI18N
+
+ // Get list
+ List contacts = query.getResultList();
+
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAllContacts: contacts.size()={0} - EXIT!", contacts.size())); //NOI18N
+
+ // Return it
+ return contacts;
+ }
+
+ @Override
+ @SuppressWarnings ("unchecked")
+ public List getEmailAddressList () {
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace("getEmailAddressList - CALLED!"); //NOI18N
+
+ // Create query instance
+ Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", List.class); //NOI18N
+
+ // Get list
+ List emailAddresses = query.getResultList();
+
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getEmailAddressList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
+
+ // Return it
+ 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("lookupContact: contact={0} - CALLED!", contact)); //NOI18N
+
+ // Parameter should be valid
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if ((contact.getContactId() instanceof Long) && (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 contacts = this.getAllContacts();
+
+ // Is the list empty?
+ if (contacts.isEmpty()) {
+ // Then abort here
+ this.getLoggerBeanLocal().logTrace("lookupContact: No contacts registered, returning 'false' ..."); //NOI18N
+ return null;
+ }
+
+ // Get iterator
+ Iterator iterator = contacts.iterator();
+
+ // Loop through all
+ while (iterator.hasNext()) {
+ // Get contact
+ Contact next = iterator.next();
+
+ // Debug message
+ 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("lookupContact: Found same contact, id={0}", next.getContactId())); //NOI18N
+
+ // Found it
+ foundContact = next;
+ break;
+ }
+ }
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("lookupContact: foundContact={0} - EXIT!", foundContact)); //NOI18N
+
+ // Return status
+ 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("updateContactData: contact={0},isCellphoneUnlinked={1},isLandlineUnlinked={2},isFaxUnlinked={3} - CALLED!", 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 //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
+ this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked);
+
+ // Merge cellphone, land-line and fix
+ Contact detachedContact = this.mergeContactData(contact);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
+
+ // Return it
+ return detachedContact;
+ }
+
+ @Override
+ public Contact updateContactData (final Contact contact) {
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateContactData: 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
+ }
+
+ // Is cell phone/land-line/fax number unlinked?
+ boolean isCellphoneUnlinked = (contact.getContactCellphoneNumber() == 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("updateContactData: detachedContact={0} - EXIT!", detachedContact)); //NOI18N
+
+ // Return it
+ return detachedContact;
+ }
+
}