From 25d55c458d9cecce8324850db619e4c08f1f70ff Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Wed, 27 Apr 2016 16:56:05 +0200
Subject: [PATCH] implemented business method addContact()
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../contact/PizzaContactSessionBean.java      | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
index 9cd13d5..2083847 100644
--- a/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
+++ b/src/java/org/mxchange/jcontacts/contact/PizzaContactSessionBean.java
@@ -17,12 +17,15 @@
 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.pizzaaplication.database.BasePizzaDatabaseBean;;
 import org.mxchange.jcontacts.contact.utils.ContactUtils;
@@ -46,6 +49,39 @@ public class PizzaContactSessionBean extends BasePizzaDatabaseBean implements Co
 	public PizzaContactSessionBean () {
 	}
 
+	@Override
+	public Contact addContact (final Contact contact) throws ContactAlreadyAddedException {
+		// Log trace message
+		this.getLoggerBeanLocal().logTrace(MessageFormat.format("addContact: 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() 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);
+		}
+
+		// Set created timestamp
+		contact.setContactCreated(new GregorianCalendar());
+
+		// Set all created timestamps in cellphone, land-line and fax number(s)
+		this.setAllContactPhoneEntriesCreated(contact);
+
+		// Persist it
+		this.getEntityManager().persist(contact);
+
+		// Flush it
+		this.getEntityManager().flush();
+
+		// Return updated instance
+		return contact;
+	}
+
 	@Override
 	public Contact findContactById (final Long contactId) throws ContactNotFoundException {
 		// Log trace message
-- 
2.39.5