]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java
Continued with fixing: (please cherry-pick)
[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.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcontacts.contact.UserContact;
37 import org.mxchange.jcontacts.contact.gender.Gender;
38 import org.mxchange.jcontacts.contact.utils.ContactUtils;
39 import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent;
40 import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent;
41 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
42 import org.mxchange.jcountry.data.Country;
43 import org.mxchange.jjobs.beans.BaseJobsController;
44 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
45 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
46 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
47 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
48 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
49 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
50 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
52 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
53 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
54 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
55 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
56 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
57
58 /**
59  * A general contact bean (controller)
60  * <p>
61  * @author Roland Haeder<roland@mxchange.org>
62  */
63 @Named ("contactController")
64 @SessionScoped
65 public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController {
66
67         /**
68          * Serial number
69          */
70         private static final long serialVersionUID = 542_145_347_916L;
71
72         /**
73          * Birth day
74          */
75         private Date birthday;
76
77         /**
78          * Cellphone number's carrier
79          */
80         private MobileProvider cellphoneCarrier;
81
82         /**
83          * Cellphone number
84          */
85         private Long cellphoneNumber;
86
87         /**
88          * City
89          */
90         private String city;
91
92         /**
93          * Optional comments
94          */
95         private String comment;
96
97         /**
98          * Remote contact bean
99          */
100         private final ContactSessionBeanRemote contactBean;
101
102         /**
103          * Contact list
104          */
105         private List<Contact> contactList;
106
107         /**
108          * Country instance
109          */
110         private Country country;
111
112         /**
113          * Email address
114          */
115         private String emailAddress;
116
117         /**
118          * Email address list
119          */
120         private List<String> emailAddressList;
121
122         /**
123          * Email address repeated
124          */
125         private String emailAddressRepeat;
126
127         /**
128          * Family name
129          */
130         private String familyName;
131
132         /**
133          * Fax number's area code
134          */
135         private Integer faxAreaCode;
136
137         /**
138          * Country instance for fax number
139          */
140         private Country faxCountry;
141
142         /**
143          * Fax number
144          */
145         private Long faxNumber;
146
147         /**
148          * First name
149          */
150         private String firstName;
151
152         /**
153          * Gender instance
154          */
155         private Gender gender;
156
157         /**
158          * House number
159          */
160         private Short houseNumber;
161
162         /**
163          * Whether a cellphone entry has been unlinked
164          */
165         private boolean isCellphoneUnlinked;
166
167         /**
168          * Whether a fax entry has been unlinked
169          */
170         private boolean isFaxUnlinked;
171
172         /**
173          * Whether a land-line number has been unlinked
174          */
175         private boolean isLandLineUnlinked;
176
177         /**
178          * Phone number area code
179          */
180         private Integer phoneAreaCode;
181
182         /**
183          * Country instance for phone number
184          */
185         private Country phoneCountry;
186
187         /**
188          * Phone number
189          */
190         private Long phoneNumber;
191
192         /**
193          * Street
194          */
195         private String street;
196
197         /**
198          * Login bean (controller)
199          */
200         @Inject
201         private JobsUserLoginWebSessionController userLoginController;
202
203         /**
204          * ZIP code
205          */
206         private Integer zipCode;
207
208         /**
209          * Default constructor
210          */
211         public JobsContactWebSessionBean () {
212                 // Try it
213                 try {
214                         // Get initial context
215                         Context context = new InitialContext();
216
217                         // Try to lookup
218                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
219                 } catch (final NamingException e) {
220                         // Throw again
221                         throw new FaceletException(e);
222                 }
223         }
224
225         @Override
226         public void afterAdminAddedContact (@Observes final AdminAddedContactEvent event) {
227                 // The event must be valid
228                 if (null == event) {
229                         // Throw NPE
230                         throw new NullPointerException("event is null"); //NOI18N
231                 } else if (event.getAddedContact() == null) {
232                         // Throw again ...
233                         throw new NullPointerException("event.addedContact is null"); //NOI18N
234                 } else if (event.getAddedContact().getContactId() == null) {
235                         // ... and again
236                         throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
237                 } else if (event.getAddedContact().getContactId() < 1) {
238                         // Not valid
239                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
240                 }
241
242                 // Clear this bean
243                 this.clear();
244
245                 // Call other method
246                 this.uniqueAddContact(event.getAddedContact());
247
248                 // Add to selectable contacts
249                 this.selectableContacts.add(event.getAddedContact());
250         }
251
252         @Override
253         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
254                 // Trace message
255                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
256
257                 // event should not be null
258                 if (null == event) {
259                         // Throw NPE
260                         throw new NullPointerException("event is null"); //NOI18N
261                 } else if (event.getAddedUser() == null) {
262                         // Throw NPE again
263                         throw new NullPointerException("event.addedUser is null"); //NOI18N
264                 } else if (event.getAddedUser().getUserId() == null) {
265                         // userId is null
266                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
267                 } else if (event.getAddedUser().getUserId() < 1) {
268                         // Not avalid id
269                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
270                 }
271
272                 // Clear all data
273                 this.clear();
274
275                 // Trace message
276                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
277         }
278
279         @Override
280         public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) {
281                 // Trace message
282                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
283
284                 // event should not be null
285                 if (null == event) {
286                         // Throw NPE
287                         throw new NullPointerException("event is null"); //NOI18N
288                 } else if (event.getUpdatedContact() == null) {
289                         // Throw NPE again
290                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
291                 } else if (event.getUpdatedContact().getContactId() == null) {
292                         // userId is null
293                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
294                 } else if (event.getUpdatedContact().getContactId() < 1) {
295                         // Not avalid id
296                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
297                 }
298
299                 // Add contact instance only once
300                 this.uniqueAddContact(event.getUpdatedContact());
301
302                 // Add email address to list
303                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
304         }
305
306         @Override
307         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
308                 // Trace message
309                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
310
311                 // event should not be null
312                 if (null == event) {
313                         // Throw NPE
314                         throw new NullPointerException("event is null"); //NOI18N
315                 } else if (event.getRegisteredUser() == null) {
316                         // Throw NPE again
317                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
318                 } else if (event.getRegisteredUser().getUserId() == null) {
319                         // userId is null
320                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
321                 } else if (event.getRegisteredUser().getUserId() < 1) {
322                         // Not avalid id
323                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
324                 }
325
326                 // Get user instance
327                 Contact registeredContact = event.getRegisteredUser().getUserContact();
328
329                 // Debug message
330                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N
331
332                 // Copy all data from registered->user
333                 this.copyContact(registeredContact);
334
335                 // Add contact instance only once
336                 this.uniqueAddContact(registeredContact);
337
338                 // Add user name and email address
339                 this.addUserNameEmailAddress(registeredContact);
340
341                 // Clear all data
342                 this.clear();
343
344                 // Trace message
345                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N
346         }
347
348         @Override
349         public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
350                 // Trace message
351                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
352
353                 // event should not be null
354                 if (null == event) {
355                         // Throw NPE
356                         throw new NullPointerException("event is null"); //NOI18N
357                 } else if (event.getConfirmedUser() == null) {
358                         // Throw NPE again
359                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
360                 } else if (event.getConfirmedUser().getUserId() == null) {
361                         // userId is null
362                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
363                 } else if (event.getConfirmedUser().getUserId() < 1) {
364                         // Not avalid id
365                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
366                 }
367
368                 // Add contact instance only once
369                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
370         }
371
372         @Override
373         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
374                 // Trace message
375                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
376
377                 // event should not be null
378                 if (null == event) {
379                         // Throw NPE
380                         throw new NullPointerException("event is null"); //NOI18N
381                 } else if (event.getLoggedInUser() == null) {
382                         // Throw NPE again
383                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
384                 } else if (event.getLoggedInUser().getUserId() == null) {
385                         // userId is null
386                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
387                 } else if (event.getLoggedInUser().getUserId() < 1) {
388                         // Not avalid id
389                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
390                 }
391
392                 // Copy all data to this bean
393                 this.copyContact(event.getLoggedInUser().getUserContact());
394
395                 // Trace message
396                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
397         }
398
399         @Override
400         public List<Contact> allContacts () {
401                 // Debug message
402                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size()));
403
404                 // Return un-modified list
405                 return Collections.unmodifiableList(this.contactList);
406         }
407
408         @Override
409         public Contact createContactInstance () {
410                 // User message
411                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName()));
412
413                 // Is all required data set?
414                 if (!this.isRequiredPersonalDataSet()) {
415                         // No, then abort here
416                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
417                 }
418
419                 // Generate phone number
420                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
421                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
422                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
423
424                 // Create new contact
425                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
426                 contact.setContactStreet(this.getStreet());
427                 contact.setContactHouseNumber(this.getHouseNumber());
428                 contact.setContactZipCode(this.getZipCode());
429                 contact.setContactCity(this.getCity());
430                 contact.setContactCountry(this.getCountry());
431                 contact.setContactEmailAddress(this.getEmailAddress());
432                 contact.setContactBirthday(this.getBirthday());
433                 contact.setContactComment(this.getComment());
434
435                 // Debug message
436                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress()));
437
438                 // Don't set null or wrong references
439                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
440                         // Now the number must be given
441                         if (phone.getPhoneAreaCode() == null) {
442                                 // Is null
443                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
444                         } else if (phone.getPhoneAreaCode() < 1) {
445                                 // Abort here
446                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
447                         } else if (phone.getPhoneNumber() == null) {
448                                 // Is null
449                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
450                         } else if (phone.getPhoneNumber() < 1) {
451                                 // Abort here
452                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
453                         }
454
455                         // Set phone number
456                         contact.setContactLandLineNumber(phone);
457                 }
458
459                 // Don't set null or wrong references
460                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
461                         // Now the number must be given
462                         if (fax.getPhoneAreaCode() == null) {
463                                 // Is null
464                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
465                         } else if (fax.getPhoneAreaCode() < 1) {
466                                 // Abort here
467                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
468                         } else if (fax.getPhoneNumber() == null) {
469                                 // Is null
470                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
471                         } else if (fax.getPhoneNumber() < 1) {
472                                 // Abort here
473                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
474                         }
475
476                         // Set fax number
477                         contact.setContactFaxNumber(fax);
478                 }
479
480                 // Is the provider set?
481                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
482                         // Is the number set?
483                         if (cellphone.getPhoneNumber() == null) {
484                                 // Is null
485                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
486                         } else if (cellphone.getPhoneNumber() < 1) {
487                                 // Abort here
488                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
489                         }
490
491                         // Set cellphone number
492                         contact.setContactCellphoneNumber(cellphone);
493                 }
494
495                 // Trace message
496                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact));
497
498                 // Return it
499                 return contact;
500         }
501
502         @Override
503         public String doChangePersonalContactData () {
504                 // This method shall only be called if the user is logged-in
505                 if (!this.userLoginController.isUserLoggedIn()) {
506                         // Not logged-in
507                         throw new IllegalStateException("User is not logged-in"); //NOI18N
508                 } else if (!this.isRequiredChangePersonalDataSet()) {
509                         // Not all required fields are set
510                         throw new FaceletException("Not all required fields are set."); //NOI18N
511                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
512                         // Password not matching
513                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
514                         return ""; //NOI18N
515                 }
516
517                 // Get contact instance
518                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
519
520                 // It should be there, so run some tests on it
521                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
522                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
523                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
524
525                 // Update all fields
526                 contact.setContactGender(this.getGender());
527                 contact.setContactFirstName(this.getFirstName());
528                 contact.setContactFamilyName(this.getFamilyName());
529                 contact.setContactStreet(this.getStreet());
530                 contact.setContactHouseNumber(this.getHouseNumber());
531                 contact.setContactZipCode(this.getZipCode());
532                 contact.setContactCity(this.getCity());
533                 contact.setContactCountry(this.getCountry());
534
535                 // Update contact's cellphone number
536                 this.isCellphoneUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getCellphoneCarrier(), this.getCellphoneNumber());
537
538                 // Update contact's land-line number
539                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
540
541                 // Update contact's fax number
542                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
543
544                 // Send it to the EJB
545                 this.contactBean.updateContactData(contact, this.isCellphoneUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
546
547                 // All fine
548                 return "contact_data_saved"; //NOI18N
549         }
550
551         @Override
552         @SuppressWarnings ("ReturnOfDateField")
553         public Date getBirthday () {
554                 return this.birthday;
555         }
556
557         @Override
558         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
559         public void setBirthday (final Date birthday) {
560                 this.birthday = birthday;
561         }
562
563         @Override
564         public MobileProvider getCellphoneCarrier () {
565                 return this.cellphoneCarrier;
566         }
567
568         @Override
569         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
570                 this.cellphoneCarrier = cellphoneCarrier;
571         }
572
573         @Override
574         public Long getCellphoneNumber () {
575                 return this.cellphoneNumber;
576         }
577
578         @Override
579         public void setCellphoneNumber (Long cellphoneNumber) {
580                 this.cellphoneNumber = cellphoneNumber;
581         }
582
583         @Override
584         public String getCity () {
585                 return this.city;
586         }
587
588         @Override
589         public void setCity (final String city) {
590                 this.city = city;
591         }
592
593         @Override
594         public String getComment () {
595                 return this.comment;
596         }
597
598         @Override
599         public void setComment (final String comment) {
600                 this.comment = comment;
601         }
602
603         @Override
604         public Country getCountry () {
605                 return this.country;
606         }
607
608         @Override
609         public void setCountry (final Country country) {
610                 this.country = country;
611         }
612
613         @Override
614         public String getEmailAddress () {
615                 return this.emailAddress;
616         }
617
618         @Override
619         public void setEmailAddress (final String emailAddress) {
620                 this.emailAddress = emailAddress;
621         }
622
623         @Override
624         public String getEmailAddressRepeat () {
625                 return this.emailAddressRepeat;
626         }
627
628         @Override
629         public void setEmailAddressRepeat (final String emailAddressRepeat) {
630                 this.emailAddressRepeat = emailAddressRepeat;
631         }
632
633         @Override
634         public String getFamilyName () {
635                 return this.familyName;
636         }
637
638         @Override
639         public void setFamilyName (final String familyName) {
640                 this.familyName = familyName;
641         }
642
643         @Override
644         public Integer getFaxAreaCode () {
645                 return this.faxAreaCode;
646         }
647
648         @Override
649         public void setFaxAreaCode (final Integer faxAreaCode) {
650                 this.faxAreaCode = faxAreaCode;
651         }
652
653         @Override
654         public Country getFaxCountry () {
655                 return this.faxCountry;
656         }
657
658         @Override
659         public void setFaxCountry (final Country faxCountry) {
660                 this.faxCountry = faxCountry;
661         }
662
663         @Override
664         public Long getFaxNumber () {
665                 return this.faxNumber;
666         }
667
668         @Override
669         public void setFaxNumber (final Long faxNumber) {
670                 this.faxNumber = faxNumber;
671         }
672
673         @Override
674         public String getFirstName () {
675                 return this.firstName;
676         }
677
678         @Override
679         public void setFirstName (final String firstName) {
680                 this.firstName = firstName;
681         }
682
683         @Override
684         public Gender getGender () {
685                 return this.gender;
686         }
687
688         @Override
689         public void setGender (final Gender gender) {
690                 this.gender = gender;
691         }
692
693         @Override
694         public Short getHouseNumber () {
695                 return this.houseNumber;
696         }
697
698         @Override
699         public void setHouseNumber (final Short houseNumber) {
700                 this.houseNumber = houseNumber;
701         }
702
703         @Override
704         public Integer getPhoneAreaCode () {
705                 return this.phoneAreaCode;
706         }
707
708         @Override
709         public void setPhoneAreaCode (final Integer phoneAreaCode) {
710                 this.phoneAreaCode = phoneAreaCode;
711         }
712
713         @Override
714         public Country getPhoneCountry () {
715                 return this.phoneCountry;
716         }
717
718         @Override
719         public void setPhoneCountry (final Country phoneCountry) {
720                 this.phoneCountry = phoneCountry;
721         }
722
723         @Override
724         public Long getPhoneNumber () {
725                 return this.phoneNumber;
726         }
727
728         @Override
729         public void setPhoneNumber (final Long phoneNumber) {
730                 this.phoneNumber = phoneNumber;
731         }
732
733         @Override
734         public String getStreet () {
735                 return this.street;
736         }
737
738         @Override
739         public void setStreet (final String street) {
740                 this.street = street;
741         }
742
743         @Override
744         public Integer getZipCode () {
745                 return this.zipCode;
746         }
747
748         @Override
749         public void setZipCode (final Integer zipCode) {
750                 this.zipCode = zipCode;
751         }
752
753         @Override
754         public boolean hasContacts () {
755                 return (!this.contactList.isEmpty());
756         }
757
758         /**
759          * Post-initialization of this class
760          */
761         @PostConstruct
762         public void init () {
763                 // Get full email address list for reducing EJB calls
764                 this.emailAddressList = this.contactBean.getEmailAddressList();
765
766                 // Get full contact list
767                 this.contactList = this.contactBean.getAllContacts();
768         }
769
770         @Override
771         public boolean isEmailAddressRegistered (final Contact contact) {
772                 // Cherck parameter
773                 if (null == contact) {
774                         // Throw NPE
775                         throw new NullPointerException("contact is null"); //NOI18N
776                 } else if (contact.getContactEmailAddress() == null) {
777                         // Throw again
778                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
779                 } else if (contact.getContactEmailAddress().isEmpty()) {
780                         // Is empty
781                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
782                 }
783
784                 // Determine it
785                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
786         }
787
788         @Override
789         public boolean isRequiredChangePersonalDataSet () {
790                 return ((this.getGender() != null) &&
791                                 (this.getFirstName() != null) &&
792                                 (this.getFamilyName() != null) &&
793                                 (this.getStreet() != null) &&
794                                 (this.getHouseNumber() != null) &&
795                                 (this.getZipCode() != null) &&
796                                 (this.getCity() != null));
797         }
798
799         @Override
800         public boolean isRequiredPersonalDataSet () {
801                 return ((this.getGender() != null) &&
802                                 (this.getFirstName() != null) &&
803                                 (this.getFamilyName() != null) &&
804                                 (this.getStreet() != null) &&
805                                 (this.getHouseNumber() != null) &&
806                                 (this.getZipCode() != null) &&
807                                 (this.getCity() != null) &&
808                                 (this.getEmailAddress() != null) &&
809                                 (this.getEmailAddressRepeat() != null));
810         }
811
812         @Override
813         public boolean isSameEmailAddressEntered () {
814                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
815         }
816
817         @Override
818         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
819                 // Init variable
820                 Contact localContact = null;
821
822                 // Clear this bean
823                 this.clear();
824
825                 // Try to lookup it in visible user list
826                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
827                         // Get next user
828                         Contact next = iterator.next();
829
830                         // Is the user id found?
831                         if (Objects.equals(next.getContactId(), contactId)) {
832                                 // Copy to other variable
833                                 localContact = next;
834                                 break;
835                         }
836                 }
837
838                 // Is it still null?
839                 if (null == localContact) {
840                         // Not visible for the current user
841                         throw new ContactNotFoundException(contactId);
842                 }
843
844                 // Copy all data to this bean
845                 this.copyContact(localContact);
846
847                 // Return it
848                 return localContact;
849         }
850
851         @Override
852         public void updateContactDataFromController (final Contact contact) {
853                 // Is the instance valid?
854                 if (null == contact) {
855                         // Throw NPE
856                         throw new NullPointerException("contact is null"); //NOI18N
857                 } else if (contact.getContactId() == null) {
858                         // Throw NPE
859                         throw new NullPointerException("contact.contactId is null"); //NOI18N
860                 } else if (contact.getContactId() < 1) {
861                         // Not valid id number
862                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
863                 }
864
865                 // Set all
866                 this.copyContact(contact);
867         }
868
869         /**
870          * Adds email address to bean's internal list.
871          * <p>
872          * @param contact Contact instance
873          */
874         private void addUserNameEmailAddress (final Contact contact) {
875                 // Make sure the entry is not added yet
876                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
877                         // Already added
878                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
879                 }
880
881                 // Add email addres
882                 this.emailAddressList.add(contact.getContactEmailAddress());
883         }
884
885         /**
886          * Clears this bean
887          */
888         private void clear () {
889                 // Clear all data
890                 // - personal data
891                 this.setGender(Gender.UNKNOWN);
892                 this.setFirstName(null);
893                 this.setFamilyName(null);
894                 this.setStreet(null);
895                 this.setHouseNumber(null);
896                 this.setZipCode(null);
897                 this.setCity(null);
898                 this.setCountry(null);
899
900                 // - contact data
901                 this.setEmailAddress(null);
902                 this.setEmailAddressRepeat(null);
903                 this.setPhoneAreaCode(null);
904                 this.setPhoneCountry(null);
905                 this.setPhoneNumber(null);
906                 this.setCellphoneCarrier(null);
907                 this.setCellphoneNumber(null);
908                 this.setFaxAreaCode(null);
909                 this.setFaxCountry(null);
910                 this.setFaxNumber(null);
911
912                 // - other data
913                 this.setBirthday(null);
914                 this.setComment(null);
915         }
916
917         /**
918          * Copies given contact into the controller
919          * <p>
920          * @param contact Contact instance
921          */
922         private void copyContact (final Contact contact) {
923                 // Is the instance valid?
924                 if (null == contact) {
925                         // Throw NPE
926                         throw new NullPointerException("contact is null"); //NOI18N
927                 } else if (contact.getContactId() == null) {
928                         // Throw NPE
929                         throw new NullPointerException("contact.contactId is null"); //NOI18N
930                 } else if (contact.getContactId() < 1) {
931                         // Not valid id number
932                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
933                 }
934
935                 // Copy all fields:
936                 // - base data
937                 this.setGender(contact.getContactGender());
938                 this.setFirstName(contact.getContactFirstName());
939                 this.setFamilyName(contact.getContactFamilyName());
940                 this.setStreet(contact.getContactStreet());
941                 this.setHouseNumber(contact.getContactHouseNumber());
942                 this.setZipCode(contact.getContactZipCode());
943                 this.setCity(contact.getContactCity());
944                 this.setCountry(contact.getContactCountry());
945                 this.setEmailAddress(contact.getContactEmailAddress());
946                 this.setBirthday(contact.getContactBirthday());
947                 this.setComment(contact.getContactComment());
948
949                 // Get cellphone, phone and fax instance
950                 DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber();
951                 DialableFaxNumber fax = contact.getContactFaxNumber();
952                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
953
954                 // - contact data
955                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
956                         this.setPhoneCountry(phone.getPhoneCountry());
957                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
958                         this.setPhoneNumber(phone.getPhoneNumber());
959                 }
960
961                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
962                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
963                         this.setCellphoneNumber(cellphone.getPhoneNumber());
964                 }
965
966                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
967                         this.setFaxCountry(fax.getPhoneCountry());
968                         this.setFaxAreaCode(fax.getPhoneAreaCode());
969                         this.setFaxNumber(fax.getPhoneNumber());
970                 }
971         }
972
973         /**
974          * Removes given contact from all lists
975          * <p>
976          * @param contact Contact instance to remove
977          */
978         private void removeContact (final Contact contact) {
979                 // Is the instance valid?
980                 if (null == contact) {
981                         // Throw NPE
982                         throw new NullPointerException("contact is null"); //NOI18N
983                 } else if (contact.getContactId() == null) {
984                         // Throw NPE
985                         throw new NullPointerException("contact.contactId is null"); //NOI18N
986                 } else if (contact.getContactId() < 1) {
987                         // Not valid id number
988                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
989                 }
990
991                 // Remove from general list
992                 if (!this.contactList.remove(contact)) {
993                         // Did not remove contact
994                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
995                 }
996
997                 // Remove from other lists
998                 this.emailAddressList.remove(contact.getContactEmailAddress());
999         }
1000
1001         /**
1002          * Adds unique instance to contact list. First any existing instance is
1003          * being removed, then the new instance is added.
1004          * <p>
1005          * @param contact Contact instance to add uniquely
1006          */
1007         private void uniqueAddContact (final Contact contact) {
1008                 // Is the instance valid?
1009                 if (null == contact) {
1010                         // Throw NPE
1011                         throw new NullPointerException("contact is null"); //NOI18N
1012                 } else if (contact.getContactId() == null) {
1013                         // Throw NPE
1014                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1015                 } else if (contact.getContactId() < 1) {
1016                         // Not valid id number
1017                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1018                 }
1019
1020                 // Get iterator from list
1021                 Iterator<Contact> iterator = this.contactList.iterator();
1022
1023                 // "Walk" through all entries
1024                 while (iterator.hasNext()) {
1025                         // Get next element
1026                         Contact next = iterator.next();
1027
1028                         // Is id number the same?
1029                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1030                                 // Found entry, so remove it and abort
1031                                 this.removeContact(next);
1032                                 break;
1033                         }
1034                 }
1035
1036                 // Add contact to list
1037                 this.contactList.add(contact);
1038         }
1039
1040 }