]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java
Renaming season has started:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jcontacts / phone / AddressbookAdminContactPhoneSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcontacts.phone;
18
19 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import javax.ejb.EJB;
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;
28
29 /**
30  * A session EJB for administrative contact's phone number purposes
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 @Stateless (name = "adminContactPhone", description = "An administrative bean handling contact's phone data")
35 public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminContactsPhoneSessionBeanRemote {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 189_217_561_460_237_108L;
41
42         /**
43          * Contact EJB
44          */
45         @EJB
46         private ContactSessionBeanRemote contactBean;
47
48         @Override
49         public Contact unlinkMobileDataFromContact (final Contact contact, final DialableMobileNumber mobileNumber) throws MobileNumberNotLinkedException {
50                 // Trace message
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N
52
53                 // Is the contact set?
54                 if (null == contact) {
55                         // Throw NPE
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) {
61                         // Invalid id number
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) {
67                         // Throw NPE again
68                         throw new NullPointerException("contact.contactCellphoneNumber.phoneId is null"); //NOI18N
69                 } else if (contact.getContactMobileNumber().getPhoneId() < 1) {
70                         // Invalid id number
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())) {
73                         // Not same object
74                         throw new IllegalArgumentException(MessageFormat.format("contact.contactCellphoneNumber.phoneId={0} and mobileNumber.phoneId={1} are not the same.", contact.getContactMobileNumber().getPhoneId(), mobileNumber.getPhoneId())); //NOI18N
75                 }
76
77                 // Remove it from contact
78                 contact.setContactMobileNumber(null);
79
80                 // Update database
81                 Contact updatedContact = this.contactBean.updateContactData(contact);
82
83                 // Trace message
84                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
85
86                 // Return it
87                 return updatedContact;
88         }
89
90 }