]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontacts/model/phone/JobsAdminContactPhoneSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jcontacts / model / phone / JobsAdminContactPhoneSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.model.phone;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
25 import org.mxchange.jcontacts.model.contact.Contact;
26 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
27 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
28 import org.mxchange.jphone.exceptions.phone.PhoneNumberAlreadyLinkedException;
29 import org.mxchange.jphone.exceptions.phone.PhoneNumberNotLinkedException;
30 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
31 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
32
33 /**
34  * A session EJB for administrative contact's phone number purposes
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Stateless (name = "adminContactPhone", description = "An administrative bean handling contact's phone (fax, land-line and mobile) data")
39 public class JobsAdminContactPhoneSessionBean extends BaseJobsEnterpriseBean implements AdminContactsPhoneSessionBeanRemote {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 189_217_561_460_237_108L;
45
46         /**
47          * Contact EJB
48          */
49         @EJB (lookup = "java:global/jjobs-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
50         private ContactSessionBeanRemote contactBean;
51
52         /**
53          * Default constructor
54          */
55         public JobsAdminContactPhoneSessionBean () {
56                 // Call super constructor
57                 super();
58         }
59
60         @Override
61         public Contact linkExistingFaxNumberWithContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberAlreadyLinkedException, ContactNotFoundException {
62                 // Trace message
63                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N
64
65                 // Is the contact set?
66                 if (null == contact) {
67                         // Throw NPE
68                         throw new NullPointerException("contact is null"); //NOI18N
69                 } else if (contact.getContactId() == null) {
70                         // ... and throw again
71                         throw new NullPointerException("contact.contactId is null"); //NOI18N
72                 } else if (contact.getContactId() < 1) {
73                         // Invalid id number
74                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
75                 } else if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
76                         // Not set cell phone instance
77                         throw new PhoneNumberAlreadyLinkedException(faxNumber);
78                 } else if (null == faxNumber) {
79                         // Throw NPE
80                         throw new NullPointerException("faxNumber is null"); //NOI18N
81                 } else if (faxNumber.getPhoneId() == null) {
82                         // Throw it again
83                         throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N
84                 } else if (faxNumber.getPhoneId() < 1) {
85                         // Invalid id
86                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N
87                 } else if (faxNumber.getPhoneCountry() == null) {
88                         // ... and again
89                         throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N
90                 } else if (faxNumber.getPhoneAreaCode() == null) {
91                         // Throw it again
92                         throw new NullPointerException("faxNumber.phoneAreaCode is null"); //NOI18N
93                 } else if (faxNumber.getPhoneAreaCode() < 1) {
94                         // Invalid id
95                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneAreaCode={0} is not valid", faxNumber.getPhoneAreaCode())); //NOI18N
96                 } else if (faxNumber.getPhoneNumber() == null) {
97                         // Throw it again
98                         throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N
99                 } else if (faxNumber.getPhoneNumber() < 1) {
100                         // Invalid id
101                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N
102                 }
103
104                 // Set fax number in contact
105                 contact.setContactFaxNumber(faxNumber);
106
107                 // Update database
108                 final Contact updatedContact = this.contactBean.updateContactData(contact);
109
110                 // Trace message
111                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
112
113                 // Return it
114                 return updatedContact;
115         }
116
117         @Override
118         public Contact linkExistingLandLineNumberWithContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberAlreadyLinkedException, ContactNotFoundException {
119                 // Trace message
120                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N
121
122                 // Is the contact set?
123                 if (null == contact) {
124                         // Throw NPE
125                         throw new NullPointerException("contact is null"); //NOI18N
126                 } else if (contact.getContactId() == null) {
127                         // ... and throw again
128                         throw new NullPointerException("contact.contactId is null"); //NOI18N
129                 } else if (contact.getContactId() < 1) {
130                         // Invalid id number
131                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
132                 } else if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
133                         // Not set cell phone instance
134                         throw new PhoneNumberAlreadyLinkedException(landLineNumber);
135                 } else if (null == landLineNumber) {
136                         // Throw NPE
137                         throw new NullPointerException("landLineNumber is null"); //NOI18N
138                 } else if (landLineNumber.getPhoneId() == null) {
139                         // Throw it again
140                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
141                 } else if (landLineNumber.getPhoneId() < 1) {
142                         // Invalid id
143                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N
144                 } else if (landLineNumber.getPhoneCountry() == null) {
145                         // ... and again
146                         throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N
147                 } else if (landLineNumber.getPhoneAreaCode() == null) {
148                         // Throw it again
149                         throw new NullPointerException("landLineNumber.phoneAreaCode is null"); //NOI18N
150                 } else if (landLineNumber.getPhoneAreaCode() < 1) {
151                         // Invalid id
152                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneAreaCode={0} is not valid", landLineNumber.getPhoneAreaCode())); //NOI18N
153                 } else if (landLineNumber.getPhoneNumber() == null) {
154                         // Throw it again
155                         throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N
156                 } else if (landLineNumber.getPhoneNumber() < 1) {
157                         // Invalid id
158                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N
159                 }
160
161                 // Set land-line number in contact
162                 contact.setContactLandLineNumber(landLineNumber);
163
164                 // Update database
165                 final Contact updatedContact = this.contactBean.updateContactData(contact);
166
167                 // Trace message
168                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
169
170                 // Return it
171                 return updatedContact;
172         }
173
174         @Override
175         public Contact linkNewFaxNumberWithContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberAlreadyLinkedException, ContactNotFoundException {
176                 // Trace message
177                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N
178
179                 // Is the contact set?
180                 if (null == contact) {
181                         // Throw NPE
182                         throw new NullPointerException("contact is null"); //NOI18N
183                 } else if (contact.getContactId() == null) {
184                         // ... and throw again
185                         throw new NullPointerException("contact.contactId is null"); //NOI18N
186                 } else if (contact.getContactId() < 1) {
187                         // Invalid id number
188                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
189                 } else if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
190                         // Not set cell phone instance
191                         throw new PhoneNumberAlreadyLinkedException(faxNumber);
192                 } else if (null == faxNumber) {
193                         // Throw NPE
194                         throw new NullPointerException("faxNumber is null"); //NOI18N
195                 } else if (faxNumber.getPhoneId() instanceof Long) {
196                         // Throw it again
197                         throw new IllegalStateException(MessageFormat.format("faxNumber.phoneId={0} is not null", faxNumber.getPhoneId())); //NOI18N
198                 } else if (faxNumber.getPhoneCountry() == null) {
199                         // ... and again
200                         throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N
201                 } else if (faxNumber.getPhoneAreaCode() == null) {
202                         // Throw it again
203                         throw new NullPointerException("faxNumber.phoneAreaCode is null"); //NOI18N
204                 } else if (faxNumber.getPhoneAreaCode() < 1) {
205                         // Invalid id
206                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneAreaCode={0} is not valid", faxNumber.getPhoneAreaCode())); //NOI18N
207                 } else if (faxNumber.getPhoneNumber() == null) {
208                         // Throw it again
209                         throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N
210                 } else if (faxNumber.getPhoneNumber() < 1) {
211                         // Invalid id
212                         throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N
213                 }
214
215                 // Set created instance
216                 faxNumber.setPhoneEntryCreated(new Date());
217
218                 // Set fax number in contact
219                 contact.setContactFaxNumber(faxNumber);
220
221                 // Update database
222                 final Contact updatedContact = this.contactBean.updateContactData(contact);
223
224                 // Trace message
225                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
226
227                 // Return it
228                 return updatedContact;
229         }
230
231         @Override
232         public Contact linkNewLandLineNumberWithContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberAlreadyLinkedException, ContactNotFoundException {
233                 // Trace message
234                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N
235
236                 // Is the contact set?
237                 if (null == contact) {
238                         // Throw NPE
239                         throw new NullPointerException("contact is null"); //NOI18N
240                 } else if (contact.getContactId() == null) {
241                         // ... and throw again
242                         throw new NullPointerException("contact.contactId is null"); //NOI18N
243                 } else if (contact.getContactId() < 1) {
244                         // Invalid id number
245                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
246                 } else if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
247                         // Not set cell phone instance
248                         throw new PhoneNumberAlreadyLinkedException(landLineNumber);
249                 } else if (null == landLineNumber) {
250                         // Throw NPE
251                         throw new NullPointerException("landLineNumber is null"); //NOI18N
252                 } else if (landLineNumber.getPhoneId() instanceof Long) {
253                         // Throw it again
254                         throw new IllegalStateException(MessageFormat.format("landLineNumber.phoneId={0} is not null", landLineNumber.getPhoneId())); //NOI18N
255                 } else if (landLineNumber.getPhoneCountry() == null) {
256                         // ... and again
257                         throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N
258                 } else if (landLineNumber.getPhoneAreaCode() == null) {
259                         // Throw it again
260                         throw new NullPointerException("landLineNumber.phoneAreaCode is null"); //NOI18N
261                 } else if (landLineNumber.getPhoneAreaCode() < 1) {
262                         // Invalid id
263                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneAreaCode={0} is not valid", landLineNumber.getPhoneAreaCode())); //NOI18N
264                 } else if (landLineNumber.getPhoneNumber() == null) {
265                         // Throw it again
266                         throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N
267                 } else if (landLineNumber.getPhoneNumber() < 1) {
268                         // Invalid id
269                         throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N
270                 }
271
272                 // Set created instance
273                 landLineNumber.setPhoneEntryCreated(new Date());
274
275                 // Set landLine number in contact
276                 contact.setContactLandLineNumber(landLineNumber);
277
278                 // Update database
279                 final Contact updatedContact = this.contactBean.updateContactData(contact);
280
281                 // Trace message
282                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
283
284                 // Return it
285                 return updatedContact;
286         }
287
288         @Override
289         public Contact unlinkFaxDataFromContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberNotLinkedException, ContactNotFoundException {
290                 // Trace message
291                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N
292
293                 // Is the contact set?
294                 if (null == contact) {
295                         // Throw NPE
296                         throw new NullPointerException("contact is null"); //NOI18N
297                 } else if (contact.getContactId() == null) {
298                         // ... and throw again
299                         throw new NullPointerException("contact.contactId is null"); //NOI18N
300                 } else if (contact.getContactId() < 1) {
301                         // Invalid id number
302                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
303                 } else if (contact.getContactFaxNumber() == null) {
304                         // Not set cell phone instance
305                         throw new PhoneNumberNotLinkedException(faxNumber);
306                 } else if (contact.getContactFaxNumber().getPhoneId() == null) {
307                         // Throw NPE again
308                         throw new NullPointerException("contact.contactFaxNumber.phoneId is null"); //NOI18N
309                 } else if (contact.getContactFaxNumber().getPhoneId() < 1) {
310                         // Invalid id number
311                         throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} is invalid.", contact.getContactFaxNumber().getPhoneId())); //NOI18N
312                 } else if (!Objects.equals(faxNumber.getPhoneId(), contact.getContactFaxNumber().getPhoneId())) {
313                         // Not same object
314                         throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} and faxNumber.phoneId={1} are not the same.", contact.getContactFaxNumber().getPhoneId(), faxNumber.getPhoneId())); //NOI18N
315                 }
316
317                 // Remove it from contact
318                 contact.setContactFaxNumber(null);
319
320                 // Update database
321                 final Contact updatedContact = this.contactBean.updateContactData(contact);
322
323                 // Trace message
324                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
325
326                 // Return it
327                 return updatedContact;
328         }
329
330         @Override
331         public Contact unlinkLandLineDataFromContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberNotLinkedException, ContactNotFoundException {
332                 // Trace message
333                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N
334
335                 // Is the contact set?
336                 if (null == contact) {
337                         // Throw NPE
338                         throw new NullPointerException("contact is null"); //NOI18N
339                 } else if (contact.getContactId() == null) {
340                         // ... and throw again
341                         throw new NullPointerException("contact.contactId is null"); //NOI18N
342                 } else if (contact.getContactId() < 1) {
343                         // Invalid id number
344                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
345                 } else if (contact.getContactLandLineNumber() == null) {
346                         // Not set cell phone instance
347                         throw new PhoneNumberNotLinkedException(landLineNumber);
348                 } else if (contact.getContactLandLineNumber().getPhoneId() == null) {
349                         // Throw NPE again
350                         throw new NullPointerException("contact.contactLandLineNumber.phoneId is null"); //NOI18N
351                 } else if (contact.getContactLandLineNumber().getPhoneId() < 1) {
352                         // Invalid id number
353                         throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} is invalid.", contact.getContactLandLineNumber().getPhoneId())); //NOI18N
354                 } else if (!Objects.equals(landLineNumber.getPhoneId(), contact.getContactLandLineNumber().getPhoneId())) {
355                         // Not same object
356                         throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} and landLineNumber.phoneId={1} are not the same.", contact.getContactLandLineNumber().getPhoneId(), landLineNumber.getPhoneId())); //NOI18N
357                 }
358
359                 // Remove it from contact
360                 contact.setContactLandLineNumber(null);
361
362                 // Update database
363                 final Contact updatedContact = this.contactBean.updateContactData(contact);
364
365                 // Trace message
366                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N
367
368                 // Return it
369                 return updatedContact;
370         }
371
372 }