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