From c58a1aaae6014553c5353f7dfddde50ec3334a2f Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Tue, 26 Apr 2016 11:27:17 +0200
Subject: [PATCH] Continued with customer: - implemented business method
 addCustomer() - implemented business method isContactFound() - implemented
 business method updateContactData() with only a contact instance - added much
 more log messages
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../AddressbookContactSessionBean.java        | 130 +++++++++++++++++-
 .../user/AddressbookUserSessionBean.java      |   5 +
 2 files changed, 131 insertions(+), 4 deletions(-)

diff --git a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
index e083d98..aa2886b 100644
--- a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
+++ b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java
@@ -17,20 +17,23 @@
 package org.mxchange.jcontacts.contact;
 
 import java.text.MessageFormat;
+import java.util.Iterator;
 import java.util.List;
 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.ContactAlreadyAddedException;
 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
-import org.mxchange.jcoreee.database.BaseDatabaseBean;
 
 /**
  * A contact EJB
  * <p>
  * @author Roland Haeder<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,6 +46,11 @@ public class AddressbookContactSessionBean extends BaseDatabaseBean implements C
 	public AddressbookContactSessionBean () {
 	}
 
+	@Override
+	public Contact addContact (Contact contact) throws ContactAlreadyAddedException {
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+	}
+
 	@Override
 	public Contact findContactById (final Long contactId) throws ContactNotFoundException {
 		// Log trace message
@@ -123,9 +131,123 @@ public class AddressbookContactSessionBean extends BaseDatabaseBean implements C
 		return emailAddresses;
 	}
 
+	@Override
+	public boolean isContactFound (final Contact contact) {
+		// Log trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("isContactFound: 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() > 0) {
+			try {
+				// Id set, ask other method
+				return (this.findContactById(contact.getContactId()) instanceof Contact);
+			} 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
+		boolean isFound = false;
+
+		// Get whole list
+		List<Contact> contacts = this.getAllContacts();
+
+		// Is the list empty?
+		if (contacts.isEmpty()) {
+			// Then abort here
+			this.getLoggerBeanLocal().logTrace("isContactFound: No contacts registered, returning 'false' ..."); //NOI18N
+			return false;
+		}
+
+		// Get iterator
+		Iterator<Contact> iterator = contacts.iterator();
+
+		// Loop through all
+		while (iterator.hasNext()) {
+			// Get contact
+			Contact next = iterator.next();
+
+			// Is same contact?
+			if (ContactUtils.isSameContact(contact, next)) {
+				// Found it
+				isFound = true;
+				break;
+			}
+		}
+
+		// Return status
+		return isFound;
+	}
+
+	@Override
+	public Contact lookupContact (Contact contact) {
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+	}
+
 	@Override
 	public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) {
-		throw new UnsupportedOperationException("Not supported yet."); //NOI18N
+		// 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;
 	}
 
 }
diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
index cb012dc..2bd1fac 100644
--- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
+++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
@@ -503,6 +503,11 @@ public class AddressbookUserSessionBean extends BaseDatabaseBean implements User
 		return true;
 	}
 
+	@Override
+	public User linkUser (User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+	}
+
 	@Override
 	public User updateUserData (final User user) {
 		// Trace message
-- 
2.39.5