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 java.text.MessageFormat;
20 import java.util.Objects;
22 import javax.ejb.Stateless;
23 import org.mxchange.jcontacts.contact.Contact;
24 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
25 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
26 import org.mxchange.jphone.exceptions.CellphoneNotLinkedException;
27 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
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 JobsAdminContactPhoneSessionBean extends BaseJobsDatabaseBean implements AdminContactsPhoneSessionBeanRemote {
40 private static final long serialVersionUID = 189_217_561_460_237_108L;
46 private ContactSessionBeanRemote contactBean;
49 public Contact unlinkCellphoneDataFromContact (final Contact contact, final DialableCellphoneNumber cellphoneNumber) throws CellphoneNotLinkedException {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkCellphoneDataFromContact: contact={1},cellphoneNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, cellphoneNumber)); //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.getContactCellphoneNumber() == null) {
64 // Not set cell phone instance
65 throw new CellphoneNotLinkedException(cellphoneNumber);
66 } else if (contact.getContactCellphoneNumber().getPhoneId() == null) {
68 throw new NullPointerException("contact.contactCellphoneNumber.phoneId is null"); //NOI18N
69 } else if (contact.getContactCellphoneNumber().getPhoneId() < 1) {
71 throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} is invalid.", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N
72 } else if (!Objects.equals(cellphoneNumber.getPhoneId(), contact.getContactCellphoneNumber().getPhoneId())) {
74 throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} and cellphoneNumber.phoneId={1} are not the same.", contact.getContactCellphoneNumber().getPhoneId(), cellphoneNumber.getPhoneId())); //NOI18N
77 // Remove it from contact
78 contact.setContactCellphoneNumber(null);
81 Contact updatedContact = this.contactBean.updateContactData(contact);
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkCellphoneDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
87 return updatedContact;