]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java
Continued: (please cherry-pick)
[jfinancials-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 java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import javax.ejb.Stateless;
22 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
23 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
24
25 /**
26  * A session EJB for administrative contact's phone number purposes
27  * <p>
28  * @author Roland Haeder<roland@mxchange.org>
29  */
30 @Stateless (name = "admincontactphone", description = "An administrative bean handling contact's phone data")
31 public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminContactsPhoneSessionBeanRemote {
32
33         /**
34          * Serial number
35          */
36         private static final long serialVersionUID = 189_217_561_460_237_108L;
37
38         @Override
39         public DialableCellphoneNumber updateCellphoneData (final DialableCellphoneNumber cellPhoneNumber) {
40                 // Trace message
41                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateCellphoneData: cellPhoneNumber={1} - CALLED!", this.getClass().getSimpleName(), cellPhoneNumber));
42
43                 // Is all data set
44                 if (null == cellPhoneNumber) {
45                         // Not set, throw NPE
46                         throw new NullPointerException("cellphoneNumber is null"); //NOI18N
47                 } else if (cellPhoneNumber.getPhoneId() == null) {
48                         // Throw NPE again
49                         throw new NullPointerException("cellphoneNumber.phoneId is null"); //NOI18N
50                 } else if (cellPhoneNumber.getPhoneId() < 1) {
51                         // Invalid number
52                         throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.phoneId={0} is not valid", cellPhoneNumber.getPhoneId())); //NOI18N
53                 } else if (cellPhoneNumber.getCellphoneProvider() == null) {
54                         // Throw NPE
55                         throw new NullPointerException("cellphoneNumber.cellphoneProvider is null"); //NOI18N
56                 } else if (cellPhoneNumber.getCellphoneProvider().getProviderId() == null) {
57                         // ... throw again
58                         throw new NullPointerException("cellphoneNumber.cellphoneProvider.providerId is null"); //NOI18N
59                 } else if (cellPhoneNumber.getCellphoneProvider().getProviderId() < 1) {
60                         // Id not valid
61                         throw new IllegalArgumentException(MessageFormat.format("cellphoneNumber.cellphoneProvider.providerId={0} is not valid.", cellPhoneNumber.getCellphoneProvider().getProviderId())); //NOI18N
62                 } else if (cellPhoneNumber.getPhoneNumber() == null) {
63                         // Throw NPE again
64                         throw new NullPointerException("cellphoneNumber.phoneNumber is null"); //NOI18N
65                 } else if (cellPhoneNumber.getPhoneNumber() < 1) {
66                         // Throw NPE again
67                         throw new NullPointerException(MessageFormat.format("cellphoneNumber.phoneNumber={0} is not valid.", cellPhoneNumber.getPhoneNumber())); //NOI18N
68                 }
69
70                 // Set updated timestamp
71                 cellPhoneNumber.setPhoneEntryUpdated(new GregorianCalendar());
72
73                 // Get contact from it and find it
74                 DialableCellphoneNumber foundNumber = this.getEntityManager().find(cellPhoneNumber.getClass(), cellPhoneNumber.getPhoneId());
75
76                 // Should be found
77                 assert (foundNumber instanceof DialableCellphoneNumber) : MessageFormat.format("Cell phone number with id {0} not found, but should be.", cellPhoneNumber.getPhoneId()); //NOI18N
78
79                 // Debug message
80                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateCellphoneData: foundNumber.phoneId={0}", foundNumber.getPhoneId())); //NOI18N
81
82                 // Merge contact instance
83                 DialableCellphoneNumber detachedNumber = this.getEntityManager().merge(foundNumber);
84
85                 // Trace message
86                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateCellphoneData: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
87
88                 // Return it
89                 return detachedNumber;
90         }
91
92 }