From 091bb1c0614753bcb4c3ebe53d9db801f6b1b2b2 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Fri, 13 May 2016 11:57:04 +0200
Subject: [PATCH] implemented business methods findContactByEmailAddress() and
 isEmailAddressRegistered()

---
 .../AddressbookContactSessionBean.java        | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
index 56f1cab..8b1aadd 100644
--- a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
+++ b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
@@ -52,6 +52,48 @@ public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean i
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
 	}
 
+	@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
@@ -132,6 +174,43 @@ public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean i
 		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
-- 
2.39.5