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