]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
7f6a66a963a72f1d253651e258b5f7298e3d54c4
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 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.jjobs.beans.contact;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.SessionScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcontacts.contact.UserContact;
38 import org.mxchange.jcontacts.contact.gender.Gender;
39 import org.mxchange.jcontacts.contact.utils.ContactUtils;
40 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
41 import org.mxchange.jcountry.data.Country;
42 import org.mxchange.jjobs.beans.BaseJobsController;
43 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
44 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
45 import org.mxchange.jphone.events.mobile.remove.AdminRemoveMobileNumberFromListEvent;
46 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
51 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
52 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent;
55 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
56 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
57 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
58 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
59 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
60 import org.mxchange.jcontacts.events.contact.deleted.ObservableAdminDeletedContactEvent;
61 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
62 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
63 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
64 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
65
66 /**
67  * A general contact controller (bean)
68  * <p>
69  * @author Roland Häder<roland@mxchange.org>
70  */
71 @Named ("contactController")
72 @SessionScoped
73 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
74
75         /**
76          * Serial number
77          */
78         private static final long serialVersionUID = 542_145_347_916L;
79
80         /**
81          * Birth day
82          */
83         private Date birthday;
84
85         /**
86          * City
87          */
88         private String city;
89
90         /**
91          * Optional comments
92          */
93         private String comment;
94
95         /**
96          * Remote contact bean
97          */
98         private final ContactSessionBeanRemote contactBean;
99
100         /**
101          * Contact list
102          */
103         private final List<Contact> contacts;
104
105         /**
106          * Country instance
107          */
108         private Country country;
109
110         /**
111          * Email address
112          */
113         private String emailAddress;
114
115         /**
116          * Email address repeated
117          */
118         private String emailAddressRepeat;
119
120         /**
121          * Email address list
122          */
123         private final List<String> emailAddresses;
124
125         /**
126          * Family name
127          */
128         private String familyName;
129
130         /**
131          * Fax number's area code
132          */
133         private Integer faxAreaCode;
134
135         /**
136          * Country instance for fax number
137          */
138         private Country faxCountry;
139
140         /**
141          * Fax number
142          */
143         private Long faxNumber;
144
145         /**
146          * First name
147          */
148         private String firstName;
149
150         /**
151          * Gender instance
152          */
153         private Gender gender;
154
155         /**
156          * House number
157          */
158         private Short houseNumber;
159
160         /**
161          * House number extension
162          */
163         private String houseNumberExtension;
164
165         /**
166          * Whether a fax entry has been unlinked
167          */
168         private boolean isFaxUnlinked;
169
170         /**
171          * Whether a land-line number has been unlinked
172          */
173         private boolean isLandLineUnlinked;
174
175         /**
176          * Whether a mobile entry has been unlinked
177          */
178         private boolean isMobileUnlinked;
179
180         /**
181          * Land-line number area code
182          */
183         private Integer landLineAreaCode;
184
185         /**
186          * Country instance for land-line number
187          */
188         private Country landLineCountry;
189
190         /**
191          * Land-line number
192          */
193         private Long landLineNumber;
194
195         /**
196          * Mobile number
197          */
198         private Long mobileNumber;
199
200         /**
201          * Mobile provider
202          */
203         private MobileProvider mobileProvider;
204
205         /**
206          * A list of all selectable contacts
207          */
208         private List<Contact> selectableContacts;
209
210         /**
211          * Street
212          */
213         private String street;
214
215         /**
216          * Title
217          */
218         private String title;
219
220         /**
221          * Regular user controller
222          */
223         @Inject
224         private JobsUserWebSessionController userController;
225
226         /**
227          * Login controller (bean)
228          */
229         @Inject
230         private JobsUserLoginWebSessionController userLoginController;
231
232         /**
233          * ZIP code
234          */
235         private Integer zipCode;
236
237         /**
238          * Default constructor
239          */
240         public JobsContactWebSessionBean () {
241                 // Try it
242                 try {
243                         // Get initial context
244                         Context context = new InitialContext();
245
246                         // Try to lookup
247                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
248                 } catch (final NamingException e) {
249                         // Throw again
250                         throw new FaceletException(e);
251                 }
252
253                 // Init lists/maps
254                 this.contacts = new LinkedList<>();
255                 this.emailAddresses = new LinkedList<>();
256         }
257
258         @Override
259         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
260                 // The event must be valid
261                 if (null == event) {
262                         // Throw NPE
263                         throw new NullPointerException("event is null"); //NOI18N
264                 } else if (event.getAddedContact() == null) {
265                         // Throw again ...
266                         throw new NullPointerException("event.addedContact is null"); //NOI18N
267                 } else if (event.getAddedContact().getContactId() == null) {
268                         // ... and again
269                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
270                 } else if (event.getAddedContact().getContactId() < 1) {
271                         // Not valid
272                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
273                 }
274
275                 // Clear this bean
276                 this.clear();
277
278                 // Call other method
279                 this.uniqueAddContact(event.getAddedContact());
280
281                 // Add to selectable contacts
282                 this.selectableContacts.add(event.getAddedContact());
283         }
284
285         @Override
286         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
287                 // event should not be null
288                 if (null == event) {
289                         // Throw NPE
290                         throw new NullPointerException("event is null"); //NOI18N
291                 } else if (event.getAddedUser() == null) {
292                         // Throw NPE again
293                         throw new NullPointerException("event.addedUser is null"); //NOI18N
294                 } else if (event.getAddedUser().getUserId() == null) {
295                         // userId is null
296                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
297                 } else if (event.getAddedUser().getUserId() < 1) {
298                         // Not avalid id
299                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
300                 }
301
302                 // Clear all data
303                 this.clear();
304         }
305
306         @Override
307         public void afterAdminDeletedContactEvent (@Observes final ObservableAdminDeletedContactEvent event) {
308                 // event should not be null
309                 if (null == event) {
310                         // Throw NPE
311                         throw new NullPointerException("event is null"); //NOI18N
312                 } else if (event.getDeletedContact() == null) {
313                         // Throw NPE again
314                         throw new NullPointerException("event.deletedContact is null"); //NOI18N
315                 } else if (event.getDeletedContact().getContactId() == null) {
316                         // userId is null
317                         throw new NullPointerException("event.deletedContact.contactId is null"); //NOI18N
318                 } else if (event.getDeletedContact().getContactId() < 1) {
319                         // Not avalid id
320                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedContact(), event.getDeletedContact().getContactId())); //NOI18N
321                 }
322
323                 // Remove from both lists
324                 this.contacts.remove(event.getDeletedContact());
325                 this.selectableContacts.remove(event.getDeletedContact());
326
327                 // Clear all data
328                 this.clear();
329         }
330
331         @Override
332         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
333                 // event should not be null
334                 if (null == event) {
335                         // Throw NPE
336                         throw new NullPointerException("event is null"); //NOI18N
337                 } else if (event.getLinkedUser() == null) {
338                         // Throw NPE again
339                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
340                 } else if (event.getLinkedUser().getUserContact() == null) {
341                         // Throw NPE again
342                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
343                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
344                         // userId is null
345                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
346                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
347                         // Not avalid id
348                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
349                 }
350
351                 // Remove contact from list available contacts list
352                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
353
354                 // Clear all data
355                 this.clear();
356         }
357
358         @Override
359         public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
360                 // event should not be null
361                 if (null == event) {
362                         // Throw NPE
363                         throw new NullPointerException("event is null"); //NOI18N
364                 } else if (event.getUnlinkedFaxNumber() == null) {
365                         // Throw NPE again
366                         throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
367                 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
368                         // userId is null
369                         throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
370                 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
371                         // Not avalid id
372                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
373                 }
374
375                 // Remove it from list
376                 this.removeFromContacts(event.getUnlinkedFaxNumber());
377
378                 // Clear all data
379                 this.clear();
380         }
381
382         @Override
383         public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
384                 // event should not be null
385                 if (null == event) {
386                         // Throw NPE
387                         throw new NullPointerException("event is null"); //NOI18N
388                 } else if (event.getUnlinkedLandLineNumber() == null) {
389                         // Throw NPE again
390                         throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
391                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
392                         // userId is null
393                         throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
394                 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
395                         // Not avalid id
396                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
397                 }
398
399                 // Remove it from list
400                 this.removeFromContacts(event.getUnlinkedLandLineNumber());
401
402                 // Clear all data
403                 this.clear();
404         }
405
406         @Override
407         public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
408                 // event should not be null
409                 if (null == event) {
410                         // Throw NPE
411                         throw new NullPointerException("event is null"); //NOI18N
412                 } else if (event.getUnlinkedMobileNumber() == null) {
413                         // Throw NPE again
414                         throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
415                 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
416                         // userId is null
417                         throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
418                 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
419                         // Not avalid id
420                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
421                 }
422
423                 // Remove it from list
424                 this.removeFromContacts(event.getUnlinkedMobileNumber());
425
426                 // Clear all data
427                 this.clear();
428         }
429
430         @Override
431         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
432                 // event should not be null
433                 if (null == event) {
434                         // Throw NPE
435                         throw new NullPointerException("event is null"); //NOI18N
436                 } else if (event.getUpdatedContact() == null) {
437                         // Throw NPE again
438                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
439                 } else if (event.getUpdatedContact().getContactId() == null) {
440                         // userId is null
441                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
442                 } else if (event.getUpdatedContact().getContactId() < 1) {
443                         // Not avalid id
444                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
445                 }
446
447                 // Add contact instance only once
448                 this.uniqueAddContact(event.getUpdatedContact());
449
450                 // Add email address to list
451                 this.emailAddresses.add(event.getUpdatedContact().getContactEmailAddress());
452         }
453
454         @Override
455         public void afterUserConfirmedAccountEventEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
456                 // event should not be null
457                 if (null == event) {
458                         // Throw NPE
459                         throw new NullPointerException("event is null"); //NOI18N
460                 } else if (event.getConfirmedUser() == null) {
461                         // Throw NPE again
462                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
463                 } else if (event.getConfirmedUser().getUserId() == null) {
464                         // userId is null
465                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
466                 } else if (event.getConfirmedUser().getUserId() < 1) {
467                         // Not avalid id
468                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
469                 }
470
471                 // Add contact instance only once
472                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
473         }
474
475         @Override
476         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
477                 // event should not be null
478                 if (null == event) {
479                         // Throw NPE
480                         throw new NullPointerException("event is null"); //NOI18N
481                 } else if (event.getLoggedInUser() == null) {
482                         // Throw NPE again
483                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
484                 } else if (event.getLoggedInUser().getUserId() == null) {
485                         // userId is null
486                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
487                 } else if (event.getLoggedInUser().getUserId() < 1) {
488                         // Not avalid id
489                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
490                 }
491
492                 // Copy all data to this bean
493                 this.copyContact(event.getLoggedInUser().getUserContact());
494         }
495
496         @Override
497         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
498                 // event should not be null
499                 if (null == event) {
500                         // Throw NPE
501                         throw new NullPointerException("event is null"); //NOI18N
502                 } else if (event.getRegisteredUser() == null) {
503                         // Throw NPE again
504                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
505                 } else if (event.getRegisteredUser().getUserId() == null) {
506                         // userId is null
507                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
508                 } else if (event.getRegisteredUser().getUserId() < 1) {
509                         // Not avalid id
510                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
511                 }
512
513                 // Get user instance
514                 Contact registeredContact = event.getRegisteredUser().getUserContact();
515
516                 // Copy all data from registered->user
517                 this.copyContact(registeredContact);
518
519                 // Add user name and email address
520                 this.addUserNameEmailAddress(registeredContact);
521
522                 // Clear all data
523                 this.clear();
524         }
525
526         @Override
527         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
528         public List<Contact> allContacts () {
529                 // Debug message
530                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
531
532                 // Return un-modified list
533                 return this.contacts;
534         }
535
536         @Override
537         public Contact createContactInstance () {
538                 // User message
539                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
540
541                 // Is all required data set?
542                 if (!this.isRequiredPersonalDataSet()) {
543                         // No, then abort here
544                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
545                 }
546
547                 // Generate phone number
548                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
549                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
550                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
551
552                 // Create new contact
553                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
554                 contact.setContactStreet(this.getStreet());
555                 contact.setContactHouseNumber(this.getHouseNumber());
556                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
557                 contact.setContactZipCode(this.getZipCode());
558                 contact.setContactCity(this.getCity());
559                 contact.setContactCountry(this.getCountry());
560                 contact.setContactEmailAddress(this.getEmailAddress());
561                 contact.setContactBirthday(this.getBirthday());
562                 contact.setContactComment(this.getComment());
563
564                 // Debug message
565                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
566                 // Don't set null or wrong references
567                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
568                         // Now the number must be given
569                         if (phone.getPhoneAreaCode() == null) {
570                                 // Is null
571                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
572                         } else if (phone.getPhoneAreaCode() < 1) {
573                                 // Abort here
574                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
575                         } else if (phone.getPhoneNumber() == null) {
576                                 // Is null
577                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
578                         } else if (phone.getPhoneNumber() < 1) {
579                                 // Abort here
580                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
581                         }
582
583                         // Set phone number
584                         contact.setContactLandLineNumber(phone);
585                 }
586
587                 // Don't set null or wrong references
588                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
589                         // Now the number must be given
590                         if (fax.getPhoneAreaCode() == null) {
591                                 // Is null
592                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
593                         } else if (fax.getPhoneAreaCode() < 1) {
594                                 // Abort here
595                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
596                         } else if (fax.getPhoneNumber() == null) {
597                                 // Is null
598                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
599                         } else if (fax.getPhoneNumber() < 1) {
600                                 // Abort here
601                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
602                         }
603
604                         // Set fax number
605                         contact.setContactFaxNumber(fax);
606                 }
607
608                 // Is the provider set?
609                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
610                         // Is the number set?
611                         if (mobile.getPhoneNumber() == null) {
612                                 // Is null
613                                 throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
614                         } else if (mobile.getPhoneNumber() < 1) {
615                                 // Abort here
616                                 throw new IllegalArgumentException("mobileNumber.phoneNumber is zero or below."); //NOI18N
617                         }
618
619                         // Set mobile number
620                         contact.setContactMobileNumber(mobile);
621                 }
622
623                 // Trace message
624                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
625                 // Return it
626                 return contact;
627         }
628
629         @Override
630         public String doChangePersonalContactData () {
631                 // This method shall only be called if the user is logged-in
632                 if (!this.userLoginController.isUserLoggedIn()) {
633                         // Not logged-in
634                         throw new IllegalStateException("User is not logged-in"); //NOI18N
635                 } else if (!this.isRequiredChangePersonalDataSet()) {
636                         // Not all required fields are set
637                         throw new FaceletException("Not all required fields are set."); //NOI18N
638                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
639                         // Password not matching
640                         this.showFacesMessage("form_login_change_personal:currentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N
641                         return ""; //NOI18N
642                 }
643
644                 // Get contact instance
645                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
646
647                 // It should be there, so run some tests on it
648                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
649                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
650                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
651
652                 // Update all fields
653                 contact.setContactGender(this.getGender());
654                 contact.setContactFirstName(this.getFirstName());
655                 contact.setContactFamilyName(this.getFamilyName());
656                 contact.setContactStreet(this.getStreet());
657                 contact.setContactHouseNumber(this.getHouseNumber());
658                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
659                 contact.setContactZipCode(this.getZipCode());
660                 contact.setContactCity(this.getCity());
661                 contact.setContactCountry(this.getCountry());
662
663                 // Update contact's mobile number
664                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
665
666                 // Update contact's land-line number
667                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
668
669                 // Update contact's fax number
670                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
671
672                 // Send it to the EJB
673                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
674
675                 // All fine
676                 return "user_contact_data_saved"; //NOI18N
677         }
678
679         @Override
680         @SuppressWarnings ("ReturnOfDateField")
681         public Date getBirthday () {
682                 return this.birthday;
683         }
684
685         @Override
686         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
687         public void setBirthday (final Date birthday) {
688                 this.birthday = birthday;
689         }
690
691         @Override
692         public String getCity () {
693                 return this.city;
694         }
695
696         @Override
697         public void setCity (final String city) {
698                 this.city = city;
699         }
700
701         @Override
702         public String getComment () {
703                 return this.comment;
704         }
705
706         @Override
707         public void setComment (final String comment) {
708                 this.comment = comment;
709         }
710
711         @Override
712         public String getControllerType () {
713                 return "general"; //NOI18N
714         }
715
716         @Override
717         @Deprecated
718         public void setControllerType (final String controllerType) {
719                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
720         }
721
722         @Override
723         public Country getCountry () {
724                 return this.country;
725         }
726
727         @Override
728         public void setCountry (final Country country) {
729                 this.country = country;
730         }
731
732         @Override
733         public String getEmailAddress () {
734                 return this.emailAddress;
735         }
736
737         @Override
738         public void setEmailAddress (final String emailAddress) {
739                 this.emailAddress = emailAddress;
740         }
741
742         @Override
743         public String getEmailAddressRepeat () {
744                 return this.emailAddressRepeat;
745         }
746
747         @Override
748         public void setEmailAddressRepeat (final String emailAddressRepeat) {
749                 this.emailAddressRepeat = emailAddressRepeat;
750         }
751
752         @Override
753         public String getFamilyName () {
754                 return this.familyName;
755         }
756
757         @Override
758         public void setFamilyName (final String familyName) {
759                 this.familyName = familyName;
760         }
761
762         @Override
763         public Integer getFaxAreaCode () {
764                 return this.faxAreaCode;
765         }
766
767         @Override
768         public void setFaxAreaCode (final Integer faxAreaCode) {
769                 this.faxAreaCode = faxAreaCode;
770         }
771
772         @Override
773         public Country getFaxCountry () {
774                 return this.faxCountry;
775         }
776
777         @Override
778         public void setFaxCountry (final Country faxCountry) {
779                 this.faxCountry = faxCountry;
780         }
781
782         @Override
783         public Long getFaxNumber () {
784                 return this.faxNumber;
785         }
786
787         @Override
788         public void setFaxNumber (final Long faxNumber) {
789                 this.faxNumber = faxNumber;
790         }
791
792         @Override
793         public String getFirstName () {
794                 return this.firstName;
795         }
796
797         @Override
798         public void setFirstName (final String firstName) {
799                 this.firstName = firstName;
800         }
801
802         @Override
803         public Gender getGender () {
804                 return this.gender;
805         }
806
807         @Override
808         public void setGender (final Gender gender) {
809                 this.gender = gender;
810         }
811
812         @Override
813         public Short getHouseNumber () {
814                 return this.houseNumber;
815         }
816
817         @Override
818         public void setHouseNumber (final Short houseNumber) {
819                 this.houseNumber = houseNumber;
820         }
821
822         @Override
823         public String getHouseNumberExtension () {
824                 return this.houseNumberExtension;
825         }
826
827         @Override
828         public void setHouseNumberExtension (final String houseNumberExtension) {
829                 this.houseNumberExtension = houseNumberExtension;
830         }
831
832         @Override
833         public Integer getLandLineAreaCode () {
834                 return this.landLineAreaCode;
835         }
836
837         @Override
838         public void setLandLineAreaCode (final Integer landLineAreaCode) {
839                 this.landLineAreaCode = landLineAreaCode;
840         }
841
842         @Override
843         public Country getLandLineCountry () {
844                 return this.landLineCountry;
845         }
846
847         @Override
848         public void setLandLineCountry (final Country landLineCountry) {
849                 this.landLineCountry = landLineCountry;
850         }
851
852         @Override
853         public Long getLandLineNumber () {
854                 return this.landLineNumber;
855         }
856
857         @Override
858         public void setLandLineNumber (final Long landLineNumber) {
859                 this.landLineNumber = landLineNumber;
860         }
861
862         @Override
863         public Long getMobileNumber () {
864                 return this.mobileNumber;
865         }
866
867         @Override
868         public void setMobileNumber (Long mobileNumber) {
869                 this.mobileNumber = mobileNumber;
870         }
871
872         @Override
873         public MobileProvider getMobileProvider () {
874                 return this.mobileProvider;
875         }
876
877         @Override
878         public void setMobileProvider (final MobileProvider mobileProvider) {
879                 this.mobileProvider = mobileProvider;
880         }
881
882         @Override
883         public String getStreet () {
884                 return this.street;
885         }
886
887         @Override
888         public void setStreet (final String street) {
889                 this.street = street;
890         }
891
892         @Override
893         public String getTitle () {
894                 return this.title;
895         }
896
897         @Override
898         public void setTitle (final String title) {
899                 this.title = title;
900         }
901
902         @Override
903         public Integer getZipCode () {
904                 return this.zipCode;
905         }
906
907         @Override
908         public void setZipCode (final Integer zipCode) {
909                 this.zipCode = zipCode;
910         }
911
912         /**
913          * Post-initialization of this class
914          */
915         @PostConstruct
916         public void init () {
917                 // Get full email address list for reducing EJB calls
918                 this.emailAddresses.addAll(this.contactBean.getEmailAddressList());
919
920                 // Get full contact list
921                 this.contacts.addAll(this.contactBean.getAllContacts());
922
923                 // Get all users
924                 List<User> allUsers = this.userController.allUsers();
925
926                 // Get all contacts
927                 List<Contact> allContacts = this.contactBean.getAllContacts();
928
929                 // Get iterator
930                 Iterator<Contact> iterator = allContacts.iterator();
931
932                 // Loop through it
933                 while (iterator.hasNext()) {
934                         // Get next element
935                         Contact next = iterator.next();
936
937                         // Get iterator
938                         Iterator<User> userIterator = allUsers.iterator();
939
940                         // Loop through all users
941                         while (userIterator.hasNext()) {
942                                 // Get user instance
943                                 User nextUser = userIterator.next();
944
945                                 // Is contact same?
946                                 if (Objects.equals(next, nextUser.getUserContact())) {
947                                         // Found same
948                                         iterator.remove();
949                                         break;
950                                 }
951                         }
952                 }
953
954                 // Set contact list
955                 this.selectableContacts = allContacts;
956         }
957
958         @Override
959         public boolean isEmailAddressRegistered (final Contact contact) {
960                 // Cherck parameter
961                 if (null == contact) {
962                         // Throw NPE
963                         throw new NullPointerException("contact is null"); //NOI18N
964                 } else if (contact.getContactEmailAddress() == null) {
965                         // Throw again
966                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
967                 } else if (contact.getContactEmailAddress().isEmpty()) {
968                         // Is empty
969                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
970                 }
971
972                 // Determine it
973                 return ((this.emailAddresses instanceof List) && (this.emailAddresses.contains(contact.getContactEmailAddress())));
974         }
975
976         @Override
977         public boolean isRequiredChangePersonalDataSet () {
978                 return ((this.getGender() != null) &&
979                                 (this.getFirstName() != null) &&
980                                 (this.getFamilyName() != null) &&
981                                 (this.getStreet() != null) &&
982                                 (this.getHouseNumber() != null) &&
983                                 (this.getZipCode() != null) &&
984                                 (this.getCity() != null));
985         }
986
987         @Override
988         public boolean isRequiredPersonalDataSet () {
989                 return ((this.getGender() != null) &&
990                                 (this.getFirstName() != null) &&
991                                 (this.getFamilyName() != null) &&
992                                 (this.getStreet() != null) &&
993                                 (this.getHouseNumber() != null) &&
994                                 (this.getZipCode() != null) &&
995                                 (this.getCity() != null) &&
996                                 (this.getEmailAddress() != null) &&
997                                 (this.getEmailAddressRepeat() != null));
998         }
999
1000         @Override
1001         public boolean isSameEmailAddressEntered () {
1002                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1003         }
1004
1005         @Override
1006         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
1007                 // Init variable
1008                 Contact localContact = null;
1009
1010                 // Clear this bean
1011                 this.clear();
1012
1013                 // Try to lookup it in visible user list
1014                 for (final Iterator<Contact> iterator = this.contacts.iterator(); iterator.hasNext();) {
1015                         // Get next user
1016                         Contact next = iterator.next();
1017
1018                         // Is the user id found?
1019                         if (Objects.equals(next.getContactId(), contactId)) {
1020                                 // Copy to other variable
1021                                 localContact = next;
1022                                 break;
1023                         }
1024                 }
1025
1026                 // Is it still null?
1027                 if (null == localContact) {
1028                         // Not visible for the current user
1029                         throw new ContactNotFoundException(contactId);
1030                 }
1031
1032                 // Copy all data to this bean
1033                 this.copyContact(localContact);
1034
1035                 // Return it
1036                 return localContact;
1037         }
1038
1039         @Override
1040         public void removeMobileNumberFromListEvent (@Observes final AdminRemoveMobileNumberFromListEvent event) {
1041                 // event should not be null
1042                 if (null == event) {
1043                         // Throw NPE
1044                         throw new NullPointerException("event is null"); //NOI18N
1045                 } else if (event.getMobileNumberList() == null) {
1046                         // Throw NPE again
1047                         throw new NullPointerException("event.mobileList is null"); //NOI18N
1048                 } else if (event.getMobileNumberList().isEmpty()) {
1049                         // List is empty, no need to check
1050                         return;
1051                 }
1052
1053                 // Check all entries
1054                 for (final Contact contact : this.contacts) {
1055                         // Is the mobile instance set and in list?
1056                         if ((contact.getContactMobileNumber() instanceof DialableMobileNumber) && (event.getMobileNumberList().contains(contact.getContactMobileNumber()))) {
1057                                 // Found it, so remvoe it from list
1058                                 event.getMobileNumberList().remove(contact.getContactMobileNumber());
1059                         }
1060                 }
1061         }
1062
1063         @Override
1064         public List<Contact> selectableContacts () {
1065                 return Collections.unmodifiableList(this.selectableContacts);
1066         }
1067
1068         @Override
1069         public void updateContactDataFromController (final Contact contact) {
1070                 // Is the instance valid?
1071                 if (null == contact) {
1072                         // Throw NPE
1073                         throw new NullPointerException("contact is null"); //NOI18N
1074                 } else if (contact.getContactId() == null) {
1075                         // Throw NPE
1076                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1077                 } else if (contact.getContactId() < 1) {
1078                         // Not valid id number
1079                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1080                 }
1081
1082                 // Set all
1083                 this.copyContact(contact);
1084         }
1085
1086         /**
1087          * Adds email address to bean's internal list.
1088          * <p>
1089          * @param contact Contact instance
1090          */
1091         private void addUserNameEmailAddress (final Contact contact) {
1092                 // Make sure the entry is not added yet
1093                 if (this.emailAddresses.contains(contact.getContactEmailAddress())) {
1094                         // Already added
1095                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1096                 }
1097
1098                 // Add email addres
1099                 this.emailAddresses.add(contact.getContactEmailAddress());
1100         }
1101
1102         /**
1103          * Clears this bean
1104          */
1105         private void clear () {
1106                 // Clear all data
1107                 // - personal data
1108                 this.setGender(null);
1109                 this.setTitle(null);
1110                 this.setFirstName(null);
1111                 this.setFamilyName(null);
1112                 this.setStreet(null);
1113                 this.setHouseNumber(null);
1114                 this.setHouseNumberExtension(null);
1115                 this.setZipCode(null);
1116                 this.setCity(null);
1117                 this.setCountry(null);
1118
1119                 // - contact data
1120                 this.setEmailAddress(null);
1121                 this.setEmailAddressRepeat(null);
1122                 this.setLandLineAreaCode(null);
1123                 this.setLandLineCountry(null);
1124                 this.setLandLineNumber(null);
1125                 this.setMobileProvider(null);
1126                 this.setMobileNumber(null);
1127                 this.setFaxAreaCode(null);
1128                 this.setFaxCountry(null);
1129                 this.setFaxNumber(null);
1130
1131                 // - other data
1132                 this.setBirthday(null);
1133                 this.setComment(null);
1134         }
1135
1136         /**
1137          * Copies given contact into the controller
1138          * <p>
1139          * @param contact Contact instance
1140          */
1141         private void copyContact (final Contact contact) {
1142                 // Is the instance valid?
1143                 if (null == contact) {
1144                         // Throw NPE
1145                         throw new NullPointerException("contact is null"); //NOI18N
1146                 } else if (contact.getContactId() == null) {
1147                         // Throw NPE
1148                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1149                 } else if (contact.getContactId() < 1) {
1150                         // Not valid id number
1151                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1152                 }
1153
1154                 // Copy all fields:
1155                 // - base data
1156                 this.setGender(contact.getContactGender());
1157                 this.setTitle(contact.getContactTitle());
1158                 this.setFirstName(contact.getContactFirstName());
1159                 this.setFamilyName(contact.getContactFamilyName());
1160                 this.setStreet(contact.getContactStreet());
1161                 this.setHouseNumber(contact.getContactHouseNumber());
1162                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1163                 this.setZipCode(contact.getContactZipCode());
1164                 this.setCity(contact.getContactCity());
1165                 this.setCountry(contact.getContactCountry());
1166                 this.setEmailAddress(contact.getContactEmailAddress());
1167                 this.setBirthday(contact.getContactBirthday());
1168                 this.setComment(contact.getContactComment());
1169
1170                 // Get mobile, phone and fax instance
1171                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1172                 DialableFaxNumber fax = contact.getContactFaxNumber();
1173                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1174
1175                 // - contact data
1176                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1177                         this.setLandLineCountry(phone.getPhoneCountry());
1178                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1179                         this.setLandLineNumber(phone.getPhoneNumber());
1180                 }
1181
1182                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1183                         this.setMobileProvider(mobile.getMobileProvider());
1184                         this.setMobileNumber(mobile.getPhoneNumber());
1185                 }
1186
1187                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1188                         this.setFaxCountry(fax.getPhoneCountry());
1189                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1190                         this.setFaxNumber(fax.getPhoneNumber());
1191                 }
1192         }
1193
1194         /**
1195          * Removes given contact from all lists
1196          * <p>
1197          * @param contact Contact instance to remove
1198          */
1199         private void removeContact (final Contact contact) {
1200                 // Is the instance valid?
1201                 if (null == contact) {
1202                         // Throw NPE
1203                         throw new NullPointerException("contact is null"); //NOI18N
1204                 } else if (contact.getContactId() == null) {
1205                         // Throw NPE
1206                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1207                 } else if (contact.getContactId() < 1) {
1208                         // Not valid id number
1209                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1210                 }
1211
1212                 // Remove from general list
1213                 if (!this.contacts.remove(contact)) {
1214                         // Did not remove contact
1215                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1216                 }
1217
1218                 // Remove from other lists
1219                 this.emailAddresses.remove(contact.getContactEmailAddress());
1220         }
1221
1222         /**
1223          * Removes given fax number from all contacts
1224          * <p>
1225          * @param faxNumber Fax number to remove
1226          */
1227         private void removeFromContacts (final DialableFaxNumber faxNumber) {
1228                 // Log message
1229                 System.out.println(MessageFormat.format("{0}.removeFromContacts: faxNumber={1} - CALLED!", this.getClass().getSimpleName(), faxNumber)); //NOI18N
1230
1231                 // Is it valid?
1232                 if (null == faxNumber) {
1233                         // Throw NPE
1234                         throw new NullPointerException("faxNumber is null"); //NOI18N
1235                 } else if (faxNumber.getPhoneId() == null) {
1236                         // Throw NPE again
1237                         throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N
1238                 } else if (faxNumber.getPhoneId() < 1) {
1239                         // Throw NPE again
1240                         throw new NullPointerException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N
1241                 }
1242
1243                 // Loop through all contacts
1244                 for (final Contact contact : this.contacts) {
1245                         // Log message
1246                         System.out.println(MessageFormat.format("{0}.removeFromContacts: contact={1},contact.contactFaxNumber={2}", this.getClass().getSimpleName(), contact, contact.getContactFaxNumber())); //NOI18N
1247
1248                         // Is the number set?
1249                         if (Objects.equals(contact.getContactFaxNumber(), faxNumber)) {
1250                                 // Log message
1251                                 System.out.println(MessageFormat.format("{0}.removeFromContacts: Unsetting faxNumber={1} ...", this.getClass().getSimpleName(), faxNumber)); //NOI18N
1252
1253                                 // Found it
1254                                 contact.setContactFaxNumber(null);
1255                         }
1256                 }
1257         }
1258
1259         /**
1260          * Removes given land-line number from all contacts
1261          * <p>
1262          * @param landLineNumber Land-line number to remove
1263          */
1264         private void removeFromContacts (final DialableLandLineNumber landLineNumber) {
1265                 // Log message
1266                 System.out.println(MessageFormat.format("{0}.removeFromContacts: landLineNumber={1} - CALLED!", this.getClass().getSimpleName(), landLineNumber)); //NOI18N
1267
1268                 // Is it valid?
1269                 if (null == landLineNumber) {
1270                         // Throw NPE
1271                         throw new NullPointerException("landLineNumber is null"); //NOI18N
1272                 } else if (landLineNumber.getPhoneId() == null) {
1273                         // Throw NPE again
1274                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
1275                 } else if (landLineNumber.getPhoneId() < 1) {
1276                         // Throw NPE again
1277                         throw new NullPointerException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N
1278                 }
1279
1280                 // Loop through all contacts
1281                 for (final Contact contact : this.contacts) {
1282                         // Log message
1283                         System.out.println(MessageFormat.format("{0}.removeFromContacts: contact={1},contact.contactLandLineNumber={2}", this.getClass().getSimpleName(), contact, contact.getContactLandLineNumber())); //NOI18N
1284
1285                         // Is the number set?
1286                         if (Objects.equals(contact.getContactLandLineNumber(), landLineNumber)) {
1287                                 // Log message
1288                                 System.out.println(MessageFormat.format("{0}.removeFromContacts: Unsetting landLineNumber={1} ...", this.getClass().getSimpleName(), landLineNumber)); //NOI18N
1289
1290                                 // Found it
1291                                 contact.setContactLandLineNumber(null);
1292                         }
1293                 }
1294         }
1295
1296         /**
1297          * Removes given mobile number from all contacts
1298          * <p>
1299          * @param mobileNumber Mobile number to remove
1300          */
1301         private void removeFromContacts (final DialableMobileNumber mobileNumber) {
1302                 // Log message
1303                 System.out.println(MessageFormat.format("{0}.removeFromContacts: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber)); //NOI18N
1304
1305                 // Is it valid?
1306                 if (null == mobileNumber) {
1307                         // Throw NPE
1308                         throw new NullPointerException("mobileNumber is null"); //NOI18N
1309                 } else if (mobileNumber.getPhoneId() == null) {
1310                         // Throw NPE again
1311                         throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N
1312                 } else if (mobileNumber.getPhoneId() < 1) {
1313                         // Throw NPE again
1314                         throw new NullPointerException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N
1315                 }
1316
1317                 // Loop through all contacts
1318                 for (final Contact contact : this.contacts) {
1319                         // Log message
1320                         System.out.println(MessageFormat.format("{0}.removeFromContacts: contact={1},contact.contactMobileNumber={2}", this.getClass().getSimpleName(), contact, contact.getContactMobileNumber())); //NOI18N
1321
1322                         // Is the number set?
1323                         if (Objects.equals(contact.getContactMobileNumber(), mobileNumber)) {
1324                                 // Log message
1325                                 System.out.println(MessageFormat.format("{0}.removeFromContacts: Unsetting mobileNumber={1} ...", this.getClass().getSimpleName(), mobileNumber)); //NOI18N
1326
1327                                 // Unset it
1328                                 contact.setContactMobileNumber(null);
1329                         }
1330                 }
1331         }
1332
1333         /**
1334          * Adds unique instance to contact list. First any existing instance is
1335          * being removed, then the new instance is added.
1336          * <p>
1337          * @param contact Contact instance to add uniquely
1338          */
1339         private void uniqueAddContact (final Contact contact) {
1340                 // Is the instance valid?
1341                 if (null == contact) {
1342                         // Throw NPE
1343                         throw new NullPointerException("contact is null"); //NOI18N
1344                 } else if (contact.getContactId() == null) {
1345                         // Throw NPE
1346                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1347                 } else if (contact.getContactId() < 1) {
1348                         // Not valid id number
1349                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1350                 }
1351
1352                 // Get iterator from list
1353                 Iterator<Contact> iterator = this.contacts.iterator();
1354
1355                 // "Walk" through all entries
1356                 while (iterator.hasNext()) {
1357                         // Get next element
1358                         Contact next = iterator.next();
1359
1360                         // Is id number the same?
1361                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1362                                 // Found entry, so remove it and abort
1363                                 this.removeContact(next);
1364                                 break;
1365                         }
1366                 }
1367
1368                 // Add contact to list
1369                 this.contacts.add(contact);
1370         }
1371
1372 }