]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookAdminPhoneSessionBean.java
Please cherry-pick:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jphone / phonenumbers / phone / AddressbookAdminPhoneSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.jphone.phonenumbers.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.fax.DialableFaxNumber;
24 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
25 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
26
27 /**
28  * An EJB for administrative phone purposes
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "adminPhone", description = "An administrative bean handling phone data")
33 public class AddressbookAdminPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminPhoneSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 18_597_165_817_401_853L;
39
40         @Override
41         public void deleteFaxData (final DialableFaxNumber faxNumber) {
42                 // Trace message
43                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: faxNumber={1} - CALLED!", this.getClass().getSimpleName(), faxNumber));
44
45                 // Is all data set
46                 if (null == faxNumber) {
47                         // Not set, throw NPE
48                         throw new NullPointerException("faxNumber is null"); //NOI18N
49                 } else if (faxNumber.getPhoneId() == null) {
50                         // Throw NPE again
51                         throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N
52                 } else if (faxNumber.getPhoneId() < 1) {
53                         // Invalid number
54                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N
55                 } else if (faxNumber.getPhoneCountry()== null) {
56                         // Throw NPE
57                         throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N
58                 } else if (faxNumber.getPhoneCountry().getCountryId()== null) {
59                         // ... throw again
60                         throw new NullPointerException("faxNumber.phoneCountry.countryId is null"); //NOI18N
61                 } else if (faxNumber.getPhoneCountry().getCountryId() < 1) {
62                         // Id not valid
63                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneCountry.countryId={0} is not valid.", faxNumber.getPhoneCountry().getCountryId())); //NOI18N
64                 } else if (faxNumber.getPhoneNumber() == null) {
65                         // Throw NPE again
66                         throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N
67                 } else if (faxNumber.getPhoneNumber() < 1) {
68                         // Throw NPE again
69                         throw new NullPointerException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid.", faxNumber.getPhoneNumber())); //NOI18N
70                 }
71
72                 // Merge it to get a managed entity back
73                 DialableFaxNumber managedNumber = this.getEntityManager().getReference(faxNumber.getClass(), faxNumber.getPhoneId());
74
75                 // Remove it from database
76                 this.getEntityManager().remove(managedNumber);
77
78                 // Trace message
79                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName()));
80         }
81
82         @Override
83         public void deleteLandLineData (final DialableLandLineNumber landLineNumber) {
84                 // Trace message
85                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: landLineNumber={1} - CALLED!", this.getClass().getSimpleName(), landLineNumber));
86
87                 // Is all data set
88                 if (null == landLineNumber) {
89                         // Not set, throw NPE
90                         throw new NullPointerException("landLineNumber is null"); //NOI18N
91                 } else if (landLineNumber.getPhoneId() == null) {
92                         // Throw NPE again
93                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
94                 } else if (landLineNumber.getPhoneId() < 1) {
95                         // Invalid number
96                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N
97                 } else if (landLineNumber.getPhoneCountry()== null) {
98                         // Throw NPE
99                         throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N
100                 } else if (landLineNumber.getPhoneCountry().getCountryId()== null) {
101                         // ... throw again
102                         throw new NullPointerException("landLineNumber.phoneCountry.countryId is null"); //NOI18N
103                 } else if (landLineNumber.getPhoneCountry().getCountryId() < 1) {
104                         // Id not valid
105                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneCountry.countryId={0} is not valid.", landLineNumber.getPhoneCountry().getCountryId())); //NOI18N
106                 } else if (landLineNumber.getPhoneNumber() == null) {
107                         // Throw NPE again
108                         throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N
109                 } else if (landLineNumber.getPhoneNumber() < 1) {
110                         // Throw NPE again
111                         throw new NullPointerException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid.", landLineNumber.getPhoneNumber())); //NOI18N
112                 }
113
114                 // Merge it to get a managed entity back
115                 DialableLandLineNumber managedNumber = this.getEntityManager().getReference(landLineNumber.getClass(), landLineNumber.getPhoneId());
116
117                 // Remove it from database
118                 this.getEntityManager().remove(managedNumber);
119
120                 // Trace message
121                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName()));
122         }
123
124         @Override
125         public void deleteMobileData (final DialableMobileNumber mobileNumber) {
126                 // Trace message
127                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber));
128
129                 // Is all data set
130                 if (null == mobileNumber) {
131                         // Not set, throw NPE
132                         throw new NullPointerException("mobileNumber is null"); //NOI18N
133                 } else if (mobileNumber.getPhoneId() == null) {
134                         // Throw NPE again
135                         throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N
136                 } else if (mobileNumber.getPhoneId() < 1) {
137                         // Invalid number
138                         throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N
139                 } else if (mobileNumber.getMobileProvider() == null) {
140                         // Throw NPE
141                         throw new NullPointerException("mobileNumber.cellphoneProvider is null"); //NOI18N
142                 } else if (mobileNumber.getMobileProvider().getProviderId() == null) {
143                         // ... throw again
144                         throw new NullPointerException("mobileNumber.cellphoneProvider.providerId is null"); //NOI18N
145                 } else if (mobileNumber.getMobileProvider().getProviderId() < 1) {
146                         // Id not valid
147                         throw new IllegalArgumentException(MessageFormat.format("mobileNumber.cellphoneProvider.providerId={0} is not valid.", mobileNumber.getMobileProvider().getProviderId())); //NOI18N
148                 } else if (mobileNumber.getPhoneNumber() == null) {
149                         // Throw NPE again
150                         throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
151                 } else if (mobileNumber.getPhoneNumber() < 1) {
152                         // Throw NPE again
153                         throw new NullPointerException(MessageFormat.format("mobileNumber.phoneNumber={0} is not valid.", mobileNumber.getPhoneNumber())); //NOI18N
154                 }
155
156                 // Merge it to get a managed entity back
157                 DialableMobileNumber managedNumber = this.getEntityManager().getReference(mobileNumber.getClass(), mobileNumber.getPhoneId());
158
159                 // Remove it from database
160                 this.getEntityManager().remove(managedNumber);
161
162                 // Trace message
163                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName()));
164         }
165
166         @Override
167         public DialableFaxNumber updateFaxData (DialableFaxNumber faxNumber) {
168                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
169         }
170
171         @Override
172         public DialableLandLineNumber updateLandLineData (DialableLandLineNumber landLineNumber) {
173                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
174         }
175
176         @Override
177         public DialableMobileNumber updateMobileData (final DialableMobileNumber mobileNumber) {
178                 // Trace message
179                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateMobileData: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber));
180
181                 // Is all data set
182                 if (null == mobileNumber) {
183                         // Not set, throw NPE
184                         throw new NullPointerException("mobileNumber is null"); //NOI18N
185                 } else if (mobileNumber.getPhoneId() == null) {
186                         // Throw NPE again
187                         throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N
188                 } else if (mobileNumber.getPhoneId() < 1) {
189                         // Invalid number
190                         throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N
191                 } else if (mobileNumber.getMobileProvider() == null) {
192                         // Throw NPE
193                         throw new NullPointerException("mobileNumber.cellphoneProvider is null"); //NOI18N
194                 } else if (mobileNumber.getMobileProvider().getProviderId() == null) {
195                         // ... throw again
196                         throw new NullPointerException("mobileNumber.cellphoneProvider.providerId is null"); //NOI18N
197                 } else if (mobileNumber.getMobileProvider().getProviderId() < 1) {
198                         // Id not valid
199                         throw new IllegalArgumentException(MessageFormat.format("mobileNumber.cellphoneProvider.providerId={0} is not valid.", mobileNumber.getMobileProvider().getProviderId())); //NOI18N
200                 } else if (mobileNumber.getPhoneNumber() == null) {
201                         // Throw NPE again
202                         throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
203                 } else if (mobileNumber.getPhoneNumber() < 1) {
204                         // Throw NPE again
205                         throw new NullPointerException(MessageFormat.format("mobileNumber.phoneNumber={0} is not valid.", mobileNumber.getPhoneNumber())); //NOI18N
206                 }
207
208                 // Set updated timestamp
209                 mobileNumber.setPhoneEntryUpdated(new GregorianCalendar());
210
211                 // Get contact from it and find it
212                 DialableMobileNumber foundNumber = this.getEntityManager().getReference(mobileNumber.getClass(), mobileNumber.getPhoneId());
213
214                 // Should be found
215                 assert (foundNumber instanceof DialableMobileNumber) : MessageFormat.format("Cell phone number with id {0} not found, but should be.", mobileNumber.getPhoneId()); //NOI18N
216
217                 // Debug message
218                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateMobileData: foundNumber.phoneId={0}", foundNumber.getPhoneId())); //NOI18N
219
220                 // Merge contact instance
221                 DialableMobileNumber detachedNumber = this.getEntityManager().merge(foundNumber);
222
223                 // Trace message
224                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateMobileData: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N
225
226                 // Return it
227                 return detachedNumber;
228         }
229
230 }