2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcontacts.phone;
19 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
20 import java.text.MessageFormat;
21 import java.util.Objects;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontacts.contact.Contact;
25 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
26 import org.mxchange.jphone.exceptions.MobileNumberNotLinkedException;
27 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
30 * A session EJB for administrative contact's phone number purposes
32 * @author Roland Haeder<roland@mxchange.org>
34 @Stateless (name = "adminContactPhone", description = "An administrative bean handling contact's phone data")
35 public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminContactsPhoneSessionBeanRemote {
40 private static final long serialVersionUID = 189_217_561_460_237_108L;
46 private ContactSessionBeanRemote contactBean;
49 public Contact unlinkMobileDataFromContact (final Contact contact, final DialableMobileNumber mobileNumber) throws MobileNumberNotLinkedException {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N
53 // Is the contact set?
54 if (null == contact) {
56 throw new NullPointerException("contact is null"); //NOI18N
57 } else if (contact.getContactId() == null) {
58 // ... and throw again
59 throw new NullPointerException("contact.contactId is null"); //NOI18N
60 } else if (contact.getContactId() < 1) {
62 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
63 } else if (contact.getContactMobileNumber() == null) {
64 // Not set cell phone instance
65 throw new MobileNumberNotLinkedException(mobileNumber);
66 } else if (contact.getContactMobileNumber().getPhoneId() == null) {
68 throw new NullPointerException("contact.contactCellphoneNumber.phoneId is null"); //NOI18N
69 } else if (contact.getContactMobileNumber().getPhoneId() < 1) {
71 throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} is invalid.", contact.getContactMobileNumber().getPhoneId())); //NOI18N
72 } else if (!Objects.equals(mobileNumber.getPhoneId(), contact.getContactMobileNumber().getPhoneId())) {
74 throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} and mobileNumber.phoneId={1} are not the same.", contact.getContactMobileNumber().getPhoneId(), mobileNumber.getPhoneId())); //NOI18N
77 // Remove it from contact
78 contact.setContactMobileNumber(null);
81 Contact updatedContact = this.contactBean.updateContactData(contact);
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
87 return updatedContact;