From: Roland Häder <roland@mxchange.org>
Date: Thu, 21 Apr 2016 20:15:05 +0000 (+0200)
Subject: Continued (similar to juser-core):
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9baaca96e922e7da7b9e0f04402d48e3915ace63;p=jcontacts-core.git

Continued (similar to juser-core):
- moved added-contact event to own package
- added updated-contact event
- added method updateLandLineNumber() for updating land-line number in contact
---

diff --git a/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java b/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
index db2d6cc..11ae89e 100644
--- a/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
+++ b/src/org/mxchange/jcontacts/contact/utils/ContactUtils.java
@@ -16,11 +16,13 @@
  */
 package org.mxchange.jcontacts.contact.utils;
 
-import java.text.MessageFormat;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jcountry.data.Country;
 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
+import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
 
 /**
@@ -30,16 +32,37 @@ import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
  */
 public class ContactUtils extends BaseFrameworkSystem {
 
-	public static void updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {
+	/**
+	 * Updates cellphone data in contact instance. This method also removes the
+	 * cellphone instance if no provider is selected. A bean (mostly EJB) should
+	 * then make sure that the cellphone entry is being unlinked from contact
+	 * instance or being removed, if no longer used.
+	 * <p>
+	 * @param contact           Contact instance to update
+	 * @param cellphoneProvider New cellphone provider (or old)
+	 * @param cellphoneNumber   New cellphone number (or old)
+	 * <p>
+	 * @return Whether the cellphone has been unlinked in contact object
+	 */
+	public static boolean updateCellPhoneNumber (final Contact contact, final MobileProvider cellphoneProvider, final Long cellphoneNumber) {
+		// At least contact must be valid
+		if (null == contact) {
+			// Throw NPE
+			throw new NullPointerException("contact is null"); //NOI18N
+		}
+
+		// Default is not unlinked
+		boolean isUnlinked = false;
+
 		// Is there a cellphone number?
 		if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
-			// Debug message
-			System.out.println(MessageFormat.format("updateCellPhoneNumber: cellPhoneId={0}", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N
-
 			// Is provider null?
-			if (null == cellphoneProvider) {
+			if ((null == cellphoneProvider) || (null == cellphoneNumber) || (cellphoneNumber == 0)) {
 				// Remove instance
 				contact.setContactCellphoneNumber(null);
+
+				// Mark as unlinked
+				isUnlinked = true;
 			} else {
 				// Yes, then update as well
 				contact.getContactCellphoneNumber().setCellphoneProvider(cellphoneProvider);
@@ -49,6 +72,59 @@ public class ContactUtils extends BaseFrameworkSystem {
 			// Create new instance
 			DialableCellphoneNumber cellphone = new CellphoneNumber(cellphoneProvider, cellphoneNumber);
 		}
+
+		// Return status
+		return isUnlinked;
+	}
+
+	/**
+	 * Updates land-line data in contact instance. This method also removes the
+	 * land-line instance if no country is selected. A bean (mostly EJB) should
+	 * then make sure that the land-line entry is being unlinked from contact
+	 * instance or being removed, if no longer used.
+	 * <p>
+	 * @param contact       Contact instance being updated
+	 * @param phoneCountry  New phone country or old or null
+	 * @param phoneAreaCode New phone's area code (or old)
+	 * @param phoneNumber   New phone number (or old)
+	 * <p>
+	 * @return Whether the land-line has been unlinked in contact object
+	 */
+	public static boolean updateLandLineNumber (final Contact contact, final Country phoneCountry, final Integer phoneAreaCode, final Long phoneNumber) {
+		// At least contact must be valid
+		if (null == contact) {
+			// Throw NPE
+			throw new NullPointerException("contact is null"); //NOI18N
+		}
+
+		// Default is not unlinked
+		boolean isUnlinked = false;
+
+		// Is there a land-line instance?
+		if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
+			// Found existing land-line number, remove it?
+			if ((null == phoneCountry) || (null == phoneAreaCode) || (null == phoneNumber)) {
+				// Remove existing instance
+				contact.setContactLandLineNumber(null);
+
+				// Mark it as being removed
+				isUnlinked = true;
+			} else {
+				// Set all data
+				contact.getContactLandLineNumber().setPhoneCountry(phoneCountry);
+				contact.getContactLandLineNumber().setPhoneAreaCode(phoneAreaCode);
+				contact.getContactLandLineNumber().setPhoneNumber(phoneNumber);
+			}
+		} else if ((phoneCountry instanceof Country) && (phoneAreaCode > 0) && (phoneNumber > 0)) {
+			// Set new land-line number
+			DialableLandLineNumber landLineNumber = new LandLineNumber(phoneCountry, phoneAreaCode, phoneNumber);
+
+			// Set it in contact
+			contact.setContactLandLineNumber(landLineNumber);
+		}
+
+		// Return status
+		return isUnlinked;
 	}
 
 	/**