]> git.mxchange.org Git - addressbook-mailer-ejb.git/blobdiff - src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java
Renaming season has started:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jcontacts / phone / AddressbookAdminContactPhoneSessionBean.java
index bb50f62f5a7e14601fb1973b4c619085b5a9e49b..8e4d9b91b985aaad8a78422e43aeeb946f211f1b 100644 (file)
  */
 package org.mxchange.jcontacts.phone;
 
-import javax.ejb.Stateless;
 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+import java.text.MessageFormat;
+import java.util.Objects;
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
+import org.mxchange.jphone.exceptions.MobileNumberNotLinkedException;
+import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
 
 /**
  * A session EJB for administrative contact's phone number purposes
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
-@Stateless (name = "admincontactphone", description = "An administrative bean handling contact's phone data")
+@Stateless (name = "adminContactPhone", description = "An administrative bean handling contact's phone data")
 public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminContactsPhoneSessionBeanRemote {
 
        /**
@@ -32,4 +39,52 @@ public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookData
         */
        private static final long serialVersionUID = 189_217_561_460_237_108L;
 
+       /**
+        * Contact EJB
+        */
+       @EJB
+       private ContactSessionBeanRemote contactBean;
+
+       @Override
+       public Contact unlinkMobileDataFromContact (final Contact contact, final DialableMobileNumber mobileNumber) throws MobileNumberNotLinkedException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N
+
+               // Is the contact set?
+               if (null == contact) {
+                       // Throw NPE
+                       throw new NullPointerException("contact is null"); //NOI18N
+               } else if (contact.getContactId() == null) {
+                       // ... and throw again
+                       throw new NullPointerException("contact.contactId is null"); //NOI18N
+               } else if (contact.getContactId() < 1) {
+                       // Invalid id number
+                       throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+               } else if (contact.getContactMobileNumber() == null) {
+                       // Not set cell phone instance
+                       throw new MobileNumberNotLinkedException(mobileNumber);
+               } else if (contact.getContactMobileNumber().getPhoneId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("contact.contactCellphoneNumber.phoneId is null"); //NOI18N
+               } else if (contact.getContactMobileNumber().getPhoneId() < 1) {
+                       // Invalid id number
+                       throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} is invalid.", contact.getContactMobileNumber().getPhoneId())); //NOI18N
+               } else if (!Objects.equals(mobileNumber.getPhoneId(), contact.getContactMobileNumber().getPhoneId())) {
+                       // Not same object
+                       throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} and mobileNumber.phoneId={1} are not the same.", contact.getContactMobileNumber().getPhoneId(), mobileNumber.getPhoneId())); //NOI18N
+               }
+
+               // Remove it from contact
+               contact.setContactMobileNumber(null);
+
+               // Update database
+               Contact updatedContact = this.contactBean.updateContactData(contact);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
+
+               // Return it
+               return updatedContact;
+       }
+
 }