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