]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
1f4ee396cbd9bf4b003f005e64adac801e3eb1c0
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / JobsContactWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.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.events.contact.add.AdminAddedContactEvent;
41 import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent;
42 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
43 import org.mxchange.jcontacts.events.fax.unlinked.AdminUnlinkedFaxNumberEvent;
44 import org.mxchange.jcontacts.events.landline.unlinked.AdminUnlinkedLandLineNumberEvent;
45 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
46 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
47 import org.mxchange.jcountry.data.Country;
48 import org.mxchange.jjobs.beans.BaseJobsController;
49 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
50 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
51 import org.mxchange.jphone.events.mobile.remove.AdminRemoveMobileNumberFromListEvent;
52 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
53 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
54 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
55 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
56 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
57 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
58 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
59 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
60 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
61 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
62 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
63 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
64 import org.mxchange.jusercore.model.user.User;
65
66 /**
67  * A general contact controller (bean)
68  * <p>
69  * @author Roland Haeder<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/jratecalc-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 afterAdminAddedContact (@Observes final AdminAddedContactEvent 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 AdminAddedUserEvent 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 AdminDeletedContactEvent 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 afterAdminLinkedUser (@Observes final AdminLinkedUserEvent 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 AdminUnlinkedFaxNumberEvent 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 AdminUnlinkedLandLineNumberEvent 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 AdminUnlinkedMobileNumberEvent 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 AdminUpdatedContactEvent 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 afterRegistrationEvent (@Observes final UserRegisteredEvent 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.getRegisteredUser() == null) {
461                         // Throw NPE again
462                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
463                 } else if (event.getRegisteredUser().getUserId() == null) {
464                         // userId is null
465                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
466                 } else if (event.getRegisteredUser().getUserId() < 1) {
467                         // Not avalid id
468                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
469                 }
470
471                 // Get user instance
472                 Contact registeredContact = event.getRegisteredUser().getUserContact();
473
474                 // Copy all data from registered->user
475                 this.copyContact(registeredContact);
476
477                 // Add contact instance only once
478                 this.uniqueAddContact(registeredContact);
479
480                 // Add user name and email address
481                 this.addUserNameEmailAddress(registeredContact);
482
483                 // Clear all data
484                 this.clear();
485         }
486
487         @Override
488         public void afterUserConfirmedAccountEvent (@Observes final UserConfirmedAccountEvent event) {
489                 // event should not be null
490                 if (null == event) {
491                         // Throw NPE
492                         throw new NullPointerException("event is null"); //NOI18N
493                 } else if (event.getConfirmedUser() == null) {
494                         // Throw NPE again
495                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
496                 } else if (event.getConfirmedUser().getUserId() == null) {
497                         // userId is null
498                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
499                 } else if (event.getConfirmedUser().getUserId() < 1) {
500                         // Not avalid id
501                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
502                 }
503
504                 // Add contact instance only once
505                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
506         }
507
508         @Override
509         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
510                 // event should not be null
511                 if (null == event) {
512                         // Throw NPE
513                         throw new NullPointerException("event is null"); //NOI18N
514                 } else if (event.getLoggedInUser() == null) {
515                         // Throw NPE again
516                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
517                 } else if (event.getLoggedInUser().getUserId() == null) {
518                         // userId is null
519                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
520                 } else if (event.getLoggedInUser().getUserId() < 1) {
521                         // Not avalid id
522                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
523                 }
524
525                 // Copy all data to this bean
526                 this.copyContact(event.getLoggedInUser().getUserContact());
527         }
528
529         @Override
530         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
531         public List<Contact> allContacts () {
532                 // Debug message
533                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
534
535                 // Return un-modified list
536                 return this.contacts;
537         }
538
539         @Override
540         public Contact createContactInstance () {
541                 // User message
542                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
543
544                 // Is all required data set?
545                 if (!this.isRequiredPersonalDataSet()) {
546                         // No, then abort here
547                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
548                 }
549
550                 // Generate phone number
551                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
552                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
553                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
554
555                 // Create new contact
556                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
557                 contact.setContactStreet(this.getStreet());
558                 contact.setContactHouseNumber(this.getHouseNumber());
559                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
560                 contact.setContactZipCode(this.getZipCode());
561                 contact.setContactCity(this.getCity());
562                 contact.setContactCountry(this.getCountry());
563                 contact.setContactEmailAddress(this.getEmailAddress());
564                 contact.setContactBirthday(this.getBirthday());
565                 contact.setContactComment(this.getComment());
566
567                 // Debug message
568                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
569                 // Don't set null or wrong references
570                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
571                         // Now the number must be given
572                         if (phone.getPhoneAreaCode() == null) {
573                                 // Is null
574                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
575                         } else if (phone.getPhoneAreaCode() < 1) {
576                                 // Abort here
577                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
578                         } else if (phone.getPhoneNumber() == null) {
579                                 // Is null
580                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
581                         } else if (phone.getPhoneNumber() < 1) {
582                                 // Abort here
583                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
584                         }
585
586                         // Set phone number
587                         contact.setContactLandLineNumber(phone);
588                 }
589
590                 // Don't set null or wrong references
591                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
592                         // Now the number must be given
593                         if (fax.getPhoneAreaCode() == null) {
594                                 // Is null
595                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
596                         } else if (fax.getPhoneAreaCode() < 1) {
597                                 // Abort here
598                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
599                         } else if (fax.getPhoneNumber() == null) {
600                                 // Is null
601                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
602                         } else if (fax.getPhoneNumber() < 1) {
603                                 // Abort here
604                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
605                         }
606
607                         // Set fax number
608                         contact.setContactFaxNumber(fax);
609                 }
610
611                 // Is the provider set?
612                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
613                         // Is the number set?
614                         if (mobile.getPhoneNumber() == null) {
615                                 // Is null
616                                 throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N
617                         } else if (mobile.getPhoneNumber() < 1) {
618                                 // Abort here
619                                 throw new IllegalArgumentException("mobileNumber.phoneNumber is zero or below."); //NOI18N
620                         }
621
622                         // Set mobile number
623                         contact.setContactMobileNumber(mobile);
624                 }
625
626                 // Trace message
627                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
628                 // Return it
629                 return contact;
630         }
631
632         @Override
633         public String doChangePersonalContactData () {
634                 // This method shall only be called if the user is logged-in
635                 if (!this.userLoginController.isUserLoggedIn()) {
636                         // Not logged-in
637                         throw new IllegalStateException("User is not logged-in"); //NOI18N
638                 } else if (!this.isRequiredChangePersonalDataSet()) {
639                         // Not all required fields are set
640                         throw new FaceletException("Not all required fields are set."); //NOI18N
641                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
642                         // Password not matching
643                         this.showFacesMessage("form_login_change_personal:currentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N
644                         return ""; //NOI18N
645                 }
646
647                 // Get contact instance
648                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
649
650                 // It should be there, so run some tests on it
651                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
652                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
653                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
654
655                 // Update all fields
656                 contact.setContactGender(this.getGender());
657                 contact.setContactFirstName(this.getFirstName());
658                 contact.setContactFamilyName(this.getFamilyName());
659                 contact.setContactStreet(this.getStreet());
660                 contact.setContactHouseNumber(this.getHouseNumber());
661                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
662                 contact.setContactZipCode(this.getZipCode());
663                 contact.setContactCity(this.getCity());
664                 contact.setContactCountry(this.getCountry());
665
666                 // Update contact's mobile number
667                 this.isMobileUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getMobileProvider(), this.getMobileNumber());
668
669                 // Update contact's land-line number
670                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
671
672                 // Update contact's fax number
673                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
674
675                 // Send it to the EJB
676                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
677
678                 // All fine
679                 return "contact_data_saved"; //NOI18N
680         }
681
682         @Override
683         @SuppressWarnings ("ReturnOfDateField")
684         public Date getBirthday () {
685                 return this.birthday;
686         }
687
688         @Override
689         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
690         public void setBirthday (final Date birthday) {
691                 this.birthday = birthday;
692         }
693
694         @Override
695         public String getCity () {
696                 return this.city;
697         }
698
699         @Override
700         public void setCity (final String city) {
701                 this.city = city;
702         }
703
704         @Override
705         public String getComment () {
706                 return this.comment;
707         }
708
709         @Override
710         public void setComment (final String comment) {
711                 this.comment = comment;
712         }
713
714         @Override
715         public String getControllerType () {
716                 return "general"; //NOI18N
717         }
718
719         @Override
720         @Deprecated
721         public void setControllerType (final String controllerType) {
722                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
723         }
724
725         @Override
726         public Country getCountry () {
727                 return this.country;
728         }
729
730         @Override
731         public void setCountry (final Country country) {
732                 this.country = country;
733         }
734
735         @Override
736         public String getEmailAddress () {
737                 return this.emailAddress;
738         }
739
740         @Override
741         public void setEmailAddress (final String emailAddress) {
742                 this.emailAddress = emailAddress;
743         }
744
745         @Override
746         public String getEmailAddressRepeat () {
747                 return this.emailAddressRepeat;
748         }
749
750         @Override
751         public void setEmailAddressRepeat (final String emailAddressRepeat) {
752                 this.emailAddressRepeat = emailAddressRepeat;
753         }
754
755         @Override
756         public String getFamilyName () {
757                 return this.familyName;
758         }
759
760         @Override
761         public void setFamilyName (final String familyName) {
762                 this.familyName = familyName;
763         }
764
765         @Override
766         public Integer getFaxAreaCode () {
767                 return this.faxAreaCode;
768         }
769
770         @Override
771         public void setFaxAreaCode (final Integer faxAreaCode) {
772                 this.faxAreaCode = faxAreaCode;
773         }
774
775         @Override
776         public Country getFaxCountry () {
777                 return this.faxCountry;
778         }
779
780         @Override
781         public void setFaxCountry (final Country faxCountry) {
782                 this.faxCountry = faxCountry;
783         }
784
785         @Override
786         public Long getFaxNumber () {
787                 return this.faxNumber;
788         }
789
790         @Override
791         public void setFaxNumber (final Long faxNumber) {
792                 this.faxNumber = faxNumber;
793         }
794
795         @Override
796         public String getFirstName () {
797                 return this.firstName;
798         }
799
800         @Override
801         public void setFirstName (final String firstName) {
802                 this.firstName = firstName;
803         }
804
805         @Override
806         public Gender getGender () {
807                 return this.gender;
808         }
809
810         @Override
811         public void setGender (final Gender gender) {
812                 this.gender = gender;
813         }
814
815         @Override
816         public Short getHouseNumber () {
817                 return this.houseNumber;
818         }
819
820         @Override
821         public void setHouseNumber (final Short houseNumber) {
822                 this.houseNumber = houseNumber;
823         }
824
825         @Override
826         public String getHouseNumberExtension () {
827                 return this.houseNumberExtension;
828         }
829
830         @Override
831         public void setHouseNumberExtension (final String houseNumberExtension) {
832                 this.houseNumberExtension = houseNumberExtension;
833         }
834
835         @Override
836         public Integer getLandLineAreaCode () {
837                 return this.landLineAreaCode;
838         }
839
840         @Override
841         public void setLandLineAreaCode (final Integer landLineAreaCode) {
842                 this.landLineAreaCode = landLineAreaCode;
843         }
844
845         @Override
846         public Country getLandLineCountry () {
847                 return this.landLineCountry;
848         }
849
850         @Override
851         public void setLandLineCountry (final Country landLineCountry) {
852                 this.landLineCountry = landLineCountry;
853         }
854
855         @Override
856         public Long getLandLineNumber () {
857                 return this.landLineNumber;
858         }
859
860         @Override
861         public void setLandLineNumber (final Long landLineNumber) {
862                 this.landLineNumber = landLineNumber;
863         }
864
865         @Override
866         public Long getMobileNumber () {
867                 return this.mobileNumber;
868         }
869
870         @Override
871         public void setMobileNumber (Long mobileNumber) {
872                 this.mobileNumber = mobileNumber;
873         }
874
875         @Override
876         public MobileProvider getMobileProvider () {
877                 return this.mobileProvider;
878         }
879
880         @Override
881         public void setMobileProvider (final MobileProvider mobileProvider) {
882                 this.mobileProvider = mobileProvider;
883         }
884
885         @Override
886         public String getStreet () {
887                 return this.street;
888         }
889
890         @Override
891         public void setStreet (final String street) {
892                 this.street = street;
893         }
894
895         @Override
896         public String getTitle () {
897                 return this.title;
898         }
899
900         @Override
901         public void setTitle (final String title) {
902                 this.title = title;
903         }
904
905         @Override
906         public Integer getZipCode () {
907                 return this.zipCode;
908         }
909
910         @Override
911         public void setZipCode (final Integer zipCode) {
912                 this.zipCode = zipCode;
913         }
914
915         /**
916          * Post-initialization of this class
917          */
918         @PostConstruct
919         public void init () {
920                 // Get full email address list for reducing EJB calls
921                 this.emailAddresses.addAll(this.contactBean.getEmailAddressList());
922
923                 // Get full contact list
924                 this.contacts.addAll(this.contactBean.getAllContacts());
925
926                 // Get all users
927                 List<User> allUsers = this.userController.allUsers();
928
929                 // Get all contacts
930                 List<Contact> allContacts = this.contactBean.getAllContacts();
931
932                 // Get iterator
933                 Iterator<Contact> iterator = allContacts.iterator();
934
935                 // Loop through it
936                 while (iterator.hasNext()) {
937                         // Get next element
938                         Contact next = iterator.next();
939
940                         // Get iterator
941                         Iterator<User> userIterator = allUsers.iterator();
942
943                         // Loop through all users
944                         while (userIterator.hasNext()) {
945                                 // Get user instance
946                                 User nextUser = userIterator.next();
947
948                                 // Is contact same?
949                                 if (Objects.equals(next, nextUser.getUserContact())) {
950                                         // Found same
951                                         iterator.remove();
952                                         break;
953                                 }
954                         }
955                 }
956
957                 // Set contact list
958                 this.selectableContacts = allContacts;
959         }
960
961         @Override
962         public boolean isEmailAddressRegistered (final Contact contact) {
963                 // Cherck parameter
964                 if (null == contact) {
965                         // Throw NPE
966                         throw new NullPointerException("contact is null"); //NOI18N
967                 } else if (contact.getContactEmailAddress() == null) {
968                         // Throw again
969                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
970                 } else if (contact.getContactEmailAddress().isEmpty()) {
971                         // Is empty
972                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
973                 }
974
975                 // Determine it
976                 return ((this.emailAddresses instanceof List) && (this.emailAddresses.contains(contact.getContactEmailAddress())));
977         }
978
979         @Override
980         public boolean isRequiredChangePersonalDataSet () {
981                 return ((this.getGender() != null) &&
982                                 (this.getFirstName() != null) &&
983                                 (this.getFamilyName() != null) &&
984                                 (this.getStreet() != null) &&
985                                 (this.getHouseNumber() != null) &&
986                                 (this.getZipCode() != null) &&
987                                 (this.getCity() != null));
988         }
989
990         @Override
991         public boolean isRequiredPersonalDataSet () {
992                 return ((this.getGender() != null) &&
993                                 (this.getFirstName() != null) &&
994                                 (this.getFamilyName() != null) &&
995                                 (this.getStreet() != null) &&
996                                 (this.getHouseNumber() != null) &&
997                                 (this.getZipCode() != null) &&
998                                 (this.getCity() != null) &&
999                                 (this.getEmailAddress() != null) &&
1000                                 (this.getEmailAddressRepeat() != null));
1001         }
1002
1003         @Override
1004         public boolean isSameEmailAddressEntered () {
1005                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1006         }
1007
1008         @Override
1009         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
1010                 // Init variable
1011                 Contact localContact = null;
1012
1013                 // Clear this bean
1014                 this.clear();
1015
1016                 // Try to lookup it in visible user list
1017                 for (final Iterator<Contact> iterator = this.contacts.iterator(); iterator.hasNext();) {
1018                         // Get next user
1019                         Contact next = iterator.next();
1020
1021                         // Is the user id found?
1022                         if (Objects.equals(next.getContactId(), contactId)) {
1023                                 // Copy to other variable
1024                                 localContact = next;
1025                                 break;
1026                         }
1027                 }
1028
1029                 // Is it still null?
1030                 if (null == localContact) {
1031                         // Not visible for the current user
1032                         throw new ContactNotFoundException(contactId);
1033                 }
1034
1035                 // Copy all data to this bean
1036                 this.copyContact(localContact);
1037
1038                 // Return it
1039                 return localContact;
1040         }
1041
1042         @Override
1043         public void removeMobileNumberFromListEvent (@Observes final AdminRemoveMobileNumberFromListEvent event) {
1044                 // event should not be null
1045                 if (null == event) {
1046                         // Throw NPE
1047                         throw new NullPointerException("event is null"); //NOI18N
1048                 } else if (event.getMobileNumberList() == null) {
1049                         // Throw NPE again
1050                         throw new NullPointerException("event.mobileList is null"); //NOI18N
1051                 } else if (event.getMobileNumberList().isEmpty()) {
1052                         // List is empty, no need to check
1053                         return;
1054                 }
1055
1056                 // Check all entries
1057                 for (final Contact contact : this.contacts) {
1058                         // Is the mobile instance set and in list?
1059                         if ((contact.getContactMobileNumber() instanceof DialableMobileNumber) && (event.getMobileNumberList().contains(contact.getContactMobileNumber()))) {
1060                                 // Found it, so remvoe it from list
1061                                 event.getMobileNumberList().remove(contact.getContactMobileNumber());
1062                         }
1063                 }
1064         }
1065
1066         @Override
1067         public List<Contact> selectableContacts () {
1068                 return Collections.unmodifiableList(this.selectableContacts);
1069         }
1070
1071         @Override
1072         public void updateContactDataFromController (final Contact contact) {
1073                 // Is the instance valid?
1074                 if (null == contact) {
1075                         // Throw NPE
1076                         throw new NullPointerException("contact is null"); //NOI18N
1077                 } else if (contact.getContactId() == null) {
1078                         // Throw NPE
1079                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1080                 } else if (contact.getContactId() < 1) {
1081                         // Not valid id number
1082                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1083                 }
1084
1085                 // Set all
1086                 this.copyContact(contact);
1087         }
1088
1089         /**
1090          * Adds email address to bean's internal list.
1091          * <p>
1092          * @param contact Contact instance
1093          */
1094         private void addUserNameEmailAddress (final Contact contact) {
1095                 // Make sure the entry is not added yet
1096                 if (this.emailAddresses.contains(contact.getContactEmailAddress())) {
1097                         // Already added
1098                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1099                 }
1100
1101                 // Add email addres
1102                 this.emailAddresses.add(contact.getContactEmailAddress());
1103         }
1104
1105         /**
1106          * Clears this bean
1107          */
1108         private void clear () {
1109                 // Clear all data
1110                 // - personal data
1111                 this.setGender(null);
1112                 this.setTitle(null);
1113                 this.setFirstName(null);
1114                 this.setFamilyName(null);
1115                 this.setStreet(null);
1116                 this.setHouseNumber(null);
1117                 this.setHouseNumberExtension(null);
1118                 this.setZipCode(null);
1119                 this.setCity(null);
1120                 this.setCountry(null);
1121
1122                 // - contact data
1123                 this.setEmailAddress(null);
1124                 this.setEmailAddressRepeat(null);
1125                 this.setLandLineAreaCode(null);
1126                 this.setLandLineCountry(null);
1127                 this.setLandLineNumber(null);
1128                 this.setMobileProvider(null);
1129                 this.setMobileNumber(null);
1130                 this.setFaxAreaCode(null);
1131                 this.setFaxCountry(null);
1132                 this.setFaxNumber(null);
1133
1134                 // - other data
1135                 this.setBirthday(null);
1136                 this.setComment(null);
1137         }
1138
1139         /**
1140          * Copies given contact into the controller
1141          * <p>
1142          * @param contact Contact instance
1143          */
1144         private void copyContact (final Contact contact) {
1145                 // Is the instance valid?
1146                 if (null == contact) {
1147                         // Throw NPE
1148                         throw new NullPointerException("contact is null"); //NOI18N
1149                 } else if (contact.getContactId() == null) {
1150                         // Throw NPE
1151                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1152                 } else if (contact.getContactId() < 1) {
1153                         // Not valid id number
1154                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1155                 }
1156
1157                 // Copy all fields:
1158                 // - base data
1159                 this.setGender(contact.getContactGender());
1160                 this.setTitle(contact.getContactTitle());
1161                 this.setFirstName(contact.getContactFirstName());
1162                 this.setFamilyName(contact.getContactFamilyName());
1163                 this.setStreet(contact.getContactStreet());
1164                 this.setHouseNumber(contact.getContactHouseNumber());
1165                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1166                 this.setZipCode(contact.getContactZipCode());
1167                 this.setCity(contact.getContactCity());
1168                 this.setCountry(contact.getContactCountry());
1169                 this.setEmailAddress(contact.getContactEmailAddress());
1170                 this.setBirthday(contact.getContactBirthday());
1171                 this.setComment(contact.getContactComment());
1172
1173                 // Get mobile, phone and fax instance
1174                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1175                 DialableFaxNumber fax = contact.getContactFaxNumber();
1176                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1177
1178                 // - contact data
1179                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1180                         this.setLandLineCountry(phone.getPhoneCountry());
1181                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1182                         this.setLandLineNumber(phone.getPhoneNumber());
1183                 }
1184
1185                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1186                         this.setMobileProvider(mobile.getMobileProvider());
1187                         this.setMobileNumber(mobile.getPhoneNumber());
1188                 }
1189
1190                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1191                         this.setFaxCountry(fax.getPhoneCountry());
1192                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1193                         this.setFaxNumber(fax.getPhoneNumber());
1194                 }
1195         }
1196
1197         /**
1198          * Removes given contact from all lists
1199          * <p>
1200          * @param contact Contact instance to remove
1201          */
1202         private void removeContact (final Contact contact) {
1203                 // Is the instance valid?
1204                 if (null == contact) {
1205                         // Throw NPE
1206                         throw new NullPointerException("contact is null"); //NOI18N
1207                 } else if (contact.getContactId() == null) {
1208                         // Throw NPE
1209                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1210                 } else if (contact.getContactId() < 1) {
1211                         // Not valid id number
1212                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1213                 }
1214
1215                 // Remove from general list
1216                 if (!this.contacts.remove(contact)) {
1217                         // Did not remove contact
1218                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1219                 }
1220
1221                 // Remove from other lists
1222                 this.emailAddresses.remove(contact.getContactEmailAddress());
1223         }
1224
1225         /**
1226          * Removes given fax number from all contacts
1227          * <p>
1228          * @param faxNumber Fax number to remove
1229          */
1230         private void removeFromContacts (final DialableFaxNumber faxNumber) {
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                         // Is the number set?
1246                         if (Objects.equals(contact.getContactFaxNumber(), faxNumber)) {
1247                                 // Found it
1248                                 contact.setContactFaxNumber(null);
1249                         }
1250                 }
1251         }
1252
1253         /**
1254          * Removes given land-line number from all contacts
1255          * <p>
1256          * @param landLineNumber Land-line number to remove
1257          */
1258         private void removeFromContacts (final DialableLandLineNumber landLineNumber) {
1259                 // Is it valid?
1260                 if (null == landLineNumber) {
1261                         // Throw NPE
1262                         throw new NullPointerException("landLineNumber is null"); //NOI18N
1263                 } else if (landLineNumber.getPhoneId() == null) {
1264                         // Throw NPE again
1265                         throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N
1266                 } else if (landLineNumber.getPhoneId() < 1) {
1267                         // Throw NPE again
1268                         throw new NullPointerException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N
1269                 }
1270
1271                 // Loop through all contacts
1272                 for (final Contact contact : this.contacts) {
1273                         // Is the number set?
1274                         if (Objects.equals(contact.getContactLandLineNumber(), landLineNumber)) {
1275                                 // Found it
1276                                 contact.setContactLandLineNumber(null);
1277                         }
1278                 }
1279         }
1280
1281         /**
1282          * Removes given mobile number from all contacts
1283          * <p>
1284          * @param mobileNumber Mobile number to remove
1285          */
1286         private void removeFromContacts (final DialableMobileNumber mobileNumber) {
1287                 // Log message
1288                 System.out.println(MessageFormat.format("{0}.removeFromContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N
1289
1290                 // Is it valid?
1291                 if (null == mobileNumber) {
1292                         // Throw NPE
1293                         throw new NullPointerException("mobileNumber is null"); //NOI18N
1294                 } else if (mobileNumber.getPhoneId() == null) {
1295                         // Throw NPE again
1296                         throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N
1297                 } else if (mobileNumber.getPhoneId() < 1) {
1298                         // Throw NPE again
1299                         throw new NullPointerException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N
1300                 }
1301
1302                 // Loop through all contacts
1303                 for (final Contact contact : this.contacts) {
1304                         // Log message
1305                         System.out.println(MessageFormat.format("{0}.removeFromContacts: contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N
1306
1307                         // Is the number set?
1308                         if (Objects.equals(contact.getContactMobileNumber(), mobileNumber)) {
1309                                 // Log message
1310                                 System.out.println(MessageFormat.format("{0}.removeFromContacts: Unsetting ...", this.getClass().getSimpleName())); //NOI18N
1311
1312                                 // Unset it
1313                                 contact.setContactMobileNumber(null);
1314                         }
1315                 }
1316         }
1317
1318         /**
1319          * Adds unique instance to contact list. First any existing instance is
1320          * being removed, then the new instance is added.
1321          * <p>
1322          * @param contact Contact instance to add uniquely
1323          */
1324         private void uniqueAddContact (final Contact contact) {
1325                 // Is the instance valid?
1326                 if (null == contact) {
1327                         // Throw NPE
1328                         throw new NullPointerException("contact is null"); //NOI18N
1329                 } else if (contact.getContactId() == null) {
1330                         // Throw NPE
1331                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1332                 } else if (contact.getContactId() < 1) {
1333                         // Not valid id number
1334                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1335                 }
1336
1337                 // Get iterator from list
1338                 Iterator<Contact> iterator = this.contacts.iterator();
1339
1340                 // "Walk" through all entries
1341                 while (iterator.hasNext()) {
1342                         // Get next element
1343                         Contact next = iterator.next();
1344
1345                         // Is id number the same?
1346                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1347                                 // Found entry, so remove it and abort
1348                                 this.removeContact(next);
1349                                 break;
1350                         }
1351                 }
1352
1353                 // Add contact to list
1354                 this.contacts.add(contact);
1355         }
1356
1357 }