]> 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                                           Boolean.FALSE
502                           );
503
504                 // Don't set null or wrong references
505                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
506                         // Now the number must be given
507                         if (fax.getPhoneAreaCode() == null) {
508                                 // Is null
509                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
510                         } else if (fax.getPhoneAreaCode() < 1) {
511                                 // Abort here
512                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
513                         } else if (fax.getPhoneNumber() == null) {
514                                 // Is null
515                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
516                         } else if (fax.getPhoneNumber() < 1) {
517                                 // Abort here
518                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
519                         }
520
521                         // Set fax number
522                         contact.setContactFaxNumber(fax);
523                 }
524
525                 // Is the provider set?
526                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
527                         // Is the number set?
528                         if (mobile.getPhoneNumber() == null) {
529                                 // Is null
530                                 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
531                         } else if (mobile.getPhoneNumber() < 1) {
532                                 // Abort here
533                                 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
534                         }
535
536                         // Set mobile number
537                         contact.setContactMobileNumber(mobile);
538                 }
539
540                 // Return it
541                 return contact;
542         }
543
544         @Override
545         public String doChangePersonalContactData () {
546                 // This method shall only be called if the user is logged-in
547                 if (!this.userLoginController.isUserLoggedIn()) {
548                         // Not logged-in
549                         throw new IllegalStateException("User is not logged-in"); //NOI18N
550                 } else if (!this.isRequiredChangePersonalDataSet()) {
551                         // Not all required fields are set
552                         throw new FaceletException("Not all required fields are set."); //NOI18N
553                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
554                         // Password not matching
555                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
556                         return ""; //NOI18N
557                 }
558
559                 // Get contact instance
560                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
561
562                 // It should be there, so run some tests on it
563                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
564                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
565                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
566
567                 // Update all fields
568                 contact.setContactPersonalTitle(this.getPersonalTitle());
569                 contact.setContactFirstName(this.getFirstName());
570                 contact.setContactFamilyName(this.getFamilyName());
571                 contact.setContactStreet(this.getStreet());
572                 contact.setContactHouseNumber(this.getHouseNumber());
573                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
574                 contact.setContactZipCode(this.getZipCode());
575                 contact.setContactCity(this.getCity());
576                 contact.setContactCountry(this.getCountry());
577
578                 // Update contact's mobile number
579                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
580
581                 // Update contact's land-line number
582                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
583
584                 // Update contact's fax number
585                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
586
587                 // Send it to the EJB
588                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
589
590                 // All fine
591                 return "contact_data_saved"; //NOI18N
592         }
593
594         /**
595          * Getter for academic title
596          * <p>
597          * @return Academic title
598          */
599         public String getAcademicTitle () {
600                 return this.academicTitle;
601         }
602
603         /**
604          * Setter for academic title
605          * <p>
606          * @param academicTitle Academic title
607          */
608         public void setAcademicTitle (final String academicTitle) {
609                 this.academicTitle = academicTitle;
610         }
611
612         /**
613          * Getter for birth day
614          * <p>
615          * @return Birth day
616          */
617         @SuppressWarnings ("ReturnOfDateField")
618         public Date getBirthday () {
619                 return this.birthday;
620         }
621
622         /**
623          * Setter for birth day
624          * <p>
625          * @param birthday Birth day
626          */
627         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
628         public void setBirthday (final Date birthday) {
629                 this.birthday = birthday;
630         }
631
632         /**
633          * Getter for city name
634          * <p>
635          * @return City name
636          */
637         public String getCity () {
638                 return this.city;
639         }
640
641         /**
642          * Setter for city name
643          * <p>
644          * @param city City name
645          */
646         public void setCity (final String city) {
647                 this.city = city;
648         }
649
650         /**
651          * Getter for comments
652          * <p>
653          * @return Comments
654          */
655         public String getComment () {
656                 return this.comment;
657         }
658
659         /**
660          * Setter for comment
661          * <p>
662          * @param comment Comments
663          */
664         public void setComment (final String comment) {
665                 this.comment = comment;
666         }
667
668         @Override
669         public String getControllerType () {
670                 return "general"; //NOI18N
671         }
672
673         @Override
674         @Deprecated
675         public void setControllerType (final String controllerType) {
676                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
677         }
678
679         /**
680          * Getter for country instance
681          * <p>
682          * @return Country instance
683          */
684         public Country getCountry () {
685                 return this.country;
686         }
687
688         /**
689          * Setter for country instance
690          * <p>
691          * @param country Country instance
692          */
693         public void setCountry (final Country country) {
694                 this.country = country;
695         }
696
697         @Override
698         public String getEmailAddress () {
699                 return this.emailAddress;
700         }
701
702         /**
703          * Setter for email address
704          * <p>
705          * @param emailAddress Email address
706          */
707         public void setEmailAddress (final String emailAddress) {
708                 this.emailAddress = emailAddress;
709         }
710
711         /**
712          * Getter for email address, repeated
713          * <p>
714          * @return the emailAddress, repeated
715          */
716         public String getEmailAddressRepeat () {
717                 return this.emailAddressRepeat;
718         }
719
720         /**
721          * Setter for email address repeated
722          * <p>
723          * @param emailAddressRepeat the emailAddress to set
724          */
725         public void setEmailAddressRepeat (final String emailAddressRepeat) {
726                 this.emailAddressRepeat = emailAddressRepeat;
727         }
728
729         /**
730          * Family name
731          * <p>
732          * @return the familyName
733          */
734         public String getFamilyName () {
735                 return this.familyName;
736         }
737
738         /**
739          * Family name
740          * <p>
741          * @param familyName the familyName to set
742          */
743         public void setFamilyName (final String familyName) {
744                 this.familyName = familyName;
745         }
746
747         /**
748          * Getter for fax number's area code
749          * <p>
750          * @return Fax number's area code
751          */
752         public Integer getFaxAreaCode () {
753                 return this.faxAreaCode;
754         }
755
756         /**
757          * Setter for fax number's area code
758          * <p>
759          * @param faxAreaCode Fax number's area code
760          */
761         public void setFaxAreaCode (final Integer faxAreaCode) {
762                 this.faxAreaCode = faxAreaCode;
763         }
764
765         /**
766          * Getter for fax's country instance
767          * <p>
768          * @return Fax' country instance
769          */
770         public Country getFaxCountry () {
771                 return this.faxCountry;
772         }
773
774         /**
775          * Setter for fax's country instance
776          * <p>
777          * @param faxCountry Fax' country instance
778          */
779         public void setFaxCountry (final Country faxCountry) {
780                 this.faxCountry = faxCountry;
781         }
782
783         /**
784          * Getter for fax number
785          * <p>
786          * @return Fax number
787          */
788         public Long getFaxNumber () {
789                 return this.faxNumber;
790         }
791
792         /**
793          * Setter for fax number
794          * <p>
795          * @param faxNumber Fax number
796          */
797         public void setFaxNumber (final Long faxNumber) {
798                 this.faxNumber = faxNumber;
799         }
800
801         /**
802          * First name
803          * <p>
804          * @return the first name
805          */
806         public String getFirstName () {
807                 return this.firstName;
808         }
809
810         /**
811          * First name
812          * <p>
813          * @param firstName the first name to set
814          */
815         public void setFirstName (final String firstName) {
816                 this.firstName = firstName;
817         }
818
819         /**
820          * House number
821          * <p>
822          * @return the houseNumber
823          */
824         public Short getHouseNumber () {
825                 return this.houseNumber;
826         }
827
828         /**
829          * House number
830          * <p>
831          * @param houseNumber the houseNumber to set
832          */
833         public void setHouseNumber (final Short houseNumber) {
834                 this.houseNumber = houseNumber;
835         }
836
837         /**
838          * Getter for house number extension, example: 123a 'a' is then the
839          * extension and 123 is the house number.
840          * <p>
841          * @return House number extension
842          */
843         public String getHouseNumberExtension () {
844                 return this.houseNumberExtension;
845         }
846
847         /**
848          * Setter for house number extension
849          * <p>
850          * @param houseNumberExtension House number extension
851          */
852         public void setHouseNumberExtension (final String houseNumberExtension) {
853                 this.houseNumberExtension = houseNumberExtension;
854         }
855
856         /**
857          * Getter for land-line number's area code
858          * <p>
859          * @return Land-line number's area code
860          */
861         public Integer getLandLineAreaCode () {
862                 return this.landLineAreaCode;
863         }
864
865         /**
866          * Setter for land-line number's area code
867          * <p>
868          * @param landLineAreaCode Land-line number's area code
869          */
870         public void setLandLineAreaCode (final Integer landLineAreaCode) {
871                 this.landLineAreaCode = landLineAreaCode;
872         }
873
874         /**
875          * Getter for land-line number's country instance
876          * <p>
877          * @return Land-line number's country instance
878          */
879         public Country getLandLineCountry () {
880                 return this.landLineCountry;
881         }
882
883         /**
884          * Setter for land-line number's country instance
885          * <p>
886          * @param landLineCountry Land-line number's country instance
887          */
888         public void setLandLineCountry (final Country landLineCountry) {
889                 this.landLineCountry = landLineCountry;
890         }
891
892         /**
893          * Getter for land-line number
894          * <p>
895          * @return Land-line number
896          */
897         public Long getLandLineNumber () {
898                 return this.landLineNumber;
899         }
900
901         /**
902          * Setter for land-line number
903          * <p>
904          * @param landLineNumber Land-line number
905          */
906         public void setLandLineNumber (final Long landLineNumber) {
907                 this.landLineNumber = landLineNumber;
908         }
909
910         /**
911          * Getter for mobile number
912          * <p>
913          * @return Mobile number
914          */
915         public Long getMobileNumber () {
916                 return this.mobileNumber;
917         }
918
919         /**
920          * Setter for mobile number
921          * <p>
922          * @param mobileNumber Mobile number
923          */
924         public void setMobileNumber (final Long mobileNumber) {
925                 this.mobileNumber = mobileNumber;
926         }
927
928         /**
929          * Getter for mobile number's carrier
930          * <p>
931          * @return Mobile number's carrier
932          */
933         public MobileProvider getMobileProvider () {
934                 return this.mobileProvider;
935         }
936
937         /**
938          * Setter for mobile number's provider
939          * <p>
940          * @param mobileProvider Mobile number's provider
941          */
942         public void setMobileProvider (final MobileProvider mobileProvider) {
943                 this.mobileProvider = mobileProvider;
944         }
945
946         /**
947          * Getter for personal title
948          * <p>
949          * @return Personal title
950          */
951         public PersonalTitle getPersonalTitle () {
952                 return this.personalTitle;
953         }
954
955         /**
956          * Setter for personal title
957          * <p>
958          * @param personalTitle Personal title
959          */
960         public void setPersonalTitle (final PersonalTitle personalTitle) {
961                 this.personalTitle = personalTitle;
962         }
963
964         /**
965          * Getter for street
966          * <p>
967          * @return Street
968          */
969         public String getStreet () {
970                 return this.street;
971         }
972
973         /**
974          * Setter for street
975          * <p>
976          * @param street Street
977          */
978         public void setStreet (final String street) {
979                 this.street = street;
980         }
981
982         /**
983          * Getter for ZIP code
984          * <p>
985          * @return ZIP code
986          */
987         public Integer getZipCode () {
988                 return this.zipCode;
989         }
990
991         /**
992          * Setter for ZIP code
993          * <p>
994          * @param zipCode ZIP code
995          */
996         public void setZipCode (final Integer zipCode) {
997                 this.zipCode = zipCode;
998         }
999
1000         /**
1001          * Post-construction method
1002          */
1003         @PostConstruct
1004         public void init () {
1005                 // Is cache there?
1006                 if (!this.contactsCache.iterator().hasNext()) {
1007                         // Get whole list
1008                         final List<Contact> contacts = this.contactBean.allContacts();
1009
1010                         // Add all
1011                         for (final Contact contact : contacts) {
1012                                 // Add it to cache
1013                                 this.contactsCache.put(contact.getContactId(), contact);
1014                                 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1015                         }
1016                 } else if (this.selectableContactsCache.iterator().hasNext()) {
1017                         // Has already entries, avoid executing below code
1018                         return;
1019                 }
1020
1021                 // Get all users
1022                 final List<User> allUsers = this.userController.allUsers();
1023
1024                 // Get iterator from contacts cache
1025                 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1026
1027                 // Loop through all contacts
1028                 while (iterator.hasNext()) {
1029                         // Get next element
1030                         final Cache.Entry<Long, Contact> next = iterator.next();
1031
1032                         // Default is not found
1033                         boolean isFound = false;
1034
1035                         // User list is not empty, check each entry, if contact is found
1036                         for (final User user : allUsers) {
1037                                 // Is the contact the same?
1038                                 if (Objects.equals(user.getUserContact(), next.getValue())) {
1039                                         // Found one
1040                                         isFound = true;
1041                                         break;
1042                                 }
1043                         }
1044
1045                         // Is contact not found?
1046                         if (!isFound) {
1047                                 // Add it as selectable
1048                                 this.selectableContactsCache.put(next.getKey(), next.getValue());
1049                         }
1050                 }
1051         }
1052
1053         @Override
1054         public boolean isEmailAddressRegistered (final Contact contact) {
1055                 // Cherck parameter
1056                 if (null == contact) {
1057                         // Throw NPE
1058                         throw new NullPointerException("contact is null"); //NOI18N
1059                 } else if (contact.getContactEmailAddress() == null) {
1060                         // Throw again
1061                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1062                 } else if (contact.getContactEmailAddress().isEmpty()) {
1063                         // Is empty
1064                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1065                 }
1066
1067                 // Determine it
1068                 return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId())));
1069         }
1070
1071         @Override
1072         public boolean isRequiredChangePersonalDataSet () {
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         }
1081
1082         @Override
1083         public boolean isRequiredPersonalDataSet () {
1084                 return ((this.getPersonalTitle() != null) &&
1085                                 (this.getFirstName() != null) &&
1086                                 (this.getFamilyName() != null) &&
1087                                 (this.getStreet() != null) &&
1088                                 (this.getHouseNumber() != null) &&
1089                                 (this.getZipCode() != null) &&
1090                                 (this.getCity() != null) &&
1091                                 (this.getEmailAddress() != null) &&
1092                                 (this.getEmailAddressRepeat() != null));
1093         }
1094
1095         @Override
1096         public boolean isSameEmailAddressEntered () {
1097                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1098         }
1099
1100         /**
1101          * Returns a list of all selectable contacts for user creation. Contacts
1102          * from already existing users are excluded in this list.
1103          * <p>
1104          * @return A list of all selectable contacts
1105          */
1106         public List<Contact> selectableContacts () {
1107                 // Init list
1108                 final List<Contact> selectableContacts = new LinkedList<>();
1109
1110                 // Get iterator from cache
1111                 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1112
1113                 // Loop through all contacts
1114                 while (iterator.hasNext()) {
1115                         // Get next element
1116                         final Cache.Entry<Long, Contact> next = iterator.next();
1117
1118                         // Add entry's value to list
1119                         selectableContacts.add(next.getValue());
1120                 }
1121
1122                 // Return list
1123                 return selectableContacts;
1124         }
1125
1126         @Override
1127         public void updateContactDataFromController (final Contact contact) {
1128                 // Is the instance valid?
1129                 if (null == contact) {
1130                         // Throw NPE
1131                         throw new NullPointerException("contact is null"); //NOI18N
1132                 } else if (contact.getContactId() == null) {
1133                         // Throw NPE
1134                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1135                 } else if (contact.getContactId() < 1) {
1136                         // Not valid id number
1137                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1138                 }
1139
1140                 // Set all
1141                 this.copyContact(contact);
1142         }
1143
1144         /**
1145          * Adds email address to bean's internal list.
1146          * <p>
1147          * @param contact Contact instance
1148          */
1149         private void addUserNameEmailAddress (final Contact contact) {
1150                 // Make sure the entry is not added yet
1151                 if (this.emailAddressCache.containsKey(contact.getContactId())) {
1152                         // Already added
1153                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1154                 }
1155
1156                 // Add email addres
1157                 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1158         }
1159
1160         /**
1161          * Clears this bean
1162          */
1163         private void clear () {
1164                 // Clear all data
1165                 // - personal data
1166                 this.setPersonalTitle(null);
1167                 this.setAcademicTitle(null);
1168                 this.setFirstName(null);
1169                 this.setFamilyName(null);
1170                 this.setStreet(null);
1171                 this.setHouseNumber(null);
1172                 this.setHouseNumberExtension(null);
1173                 this.setZipCode(null);
1174                 this.setCity(null);
1175                 this.setCountry(null);
1176
1177                 // - contact data
1178                 this.clearEmailAddresses();
1179                 this.setLandLineAreaCode(null);
1180                 this.setLandLineCountry(null);
1181                 this.setLandLineNumber(null);
1182                 this.setMobileProvider(null);
1183                 this.setMobileNumber(null);
1184                 this.setFaxAreaCode(null);
1185                 this.setFaxCountry(null);
1186                 this.setFaxNumber(null);
1187
1188                 // - other data
1189                 this.setBirthday(null);
1190                 this.setComment(null);
1191         }
1192
1193         /**
1194          * Copies given contact into the controller
1195          * <p>
1196          * @param contact Contact instance
1197          */
1198         private void copyContact (final Contact contact) {
1199                 // Is the instance valid?
1200                 if (null == contact) {
1201                         // Throw NPE
1202                         throw new NullPointerException("contact is null"); //NOI18N
1203                 } else if (contact.getContactId() == null) {
1204                         // Throw NPE
1205                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1206                 } else if (contact.getContactId() < 1) {
1207                         // Not valid id number
1208                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1209                 }
1210
1211                 // Copy all fields:
1212                 // - base data
1213                 this.setPersonalTitle(contact.getContactPersonalTitle());
1214                 this.setAcademicTitle(contact.getContactTitle());
1215                 this.setFirstName(contact.getContactFirstName());
1216                 this.setFamilyName(contact.getContactFamilyName());
1217                 this.setStreet(contact.getContactStreet());
1218                 this.setHouseNumber(contact.getContactHouseNumber());
1219                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1220                 this.setZipCode(contact.getContactZipCode());
1221                 this.setCity(contact.getContactCity());
1222                 this.setCountry(contact.getContactCountry());
1223                 this.setEmailAddress(contact.getContactEmailAddress());
1224                 this.setBirthday(contact.getContactBirthday());
1225                 this.setComment(contact.getContactComment());
1226
1227                 // Get mobile, phone and fax instance
1228                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1229                 DialableFaxNumber fax = contact.getContactFaxNumber();
1230                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1231
1232                 // - contact data
1233                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1234                         this.setLandLineCountry(phone.getPhoneCountry());
1235                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1236                         this.setLandLineNumber(phone.getPhoneNumber());
1237                 }
1238
1239                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1240                         this.setMobileProvider(mobile.getMobileProvider());
1241                         this.setMobileNumber(mobile.getPhoneNumber());
1242                 }
1243
1244                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1245                         this.setFaxCountry(fax.getPhoneCountry());
1246                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1247                         this.setFaxNumber(fax.getPhoneNumber());
1248                 }
1249         }
1250
1251         /**
1252          * Removes given contact from all lists
1253          * <p>
1254          * @param contact Contact instance to remove
1255          */
1256         private void removeContact (final Contact contact) {
1257                 // Is the instance valid?
1258                 if (null == contact) {
1259                         // Throw NPE
1260                         throw new NullPointerException("contact is null"); //NOI18N
1261                 } else if (contact.getContactId() == null) {
1262                         // Throw NPE
1263                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1264                 } else if (contact.getContactId() < 1) {
1265                         // Not valid id number
1266                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1267                 }
1268
1269                 // Remove from general list
1270                 if (!this.contactsCache.remove(contact.getContactId())) {
1271                         // Did not remove contact
1272                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1273                 }
1274
1275                 // Remove from other lists
1276                 this.emailAddressCache.remove(contact.getContactId());
1277         }
1278
1279         /**
1280          * Adds unique instance to contact list. First any existing instance is
1281          * being removed, then the new instance is added.
1282          * <p>
1283          * @param contact Contact instance to add uniquely
1284          */
1285         private void uniqueAddContact (final Contact contact) {
1286                 // Is the instance valid?
1287                 if (null == contact) {
1288                         // Throw NPE
1289                         throw new NullPointerException("contact is null"); //NOI18N
1290                 } else if (contact.getContactId() == null) {
1291                         // Throw NPE
1292                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1293                 } else if (contact.getContactId() < 1) {
1294                         // Not valid id number
1295                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1296                 }
1297
1298                 // Get iterator from list
1299                 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1300
1301                 // "Walk" through all entries
1302                 while (iterator.hasNext()) {
1303                         // Get next element
1304                         final Cache.Entry<Long, Contact> next = iterator.next();
1305
1306                         // Is id number the same?
1307                         if (Objects.equals(contact.getContactId(), next.getKey())) {
1308                                 // Found entry, so remove it and abort
1309                                 this.removeContact(next.getValue());
1310                                 break;
1311                         }
1312                 }
1313
1314                 // Add contact to list
1315                 this.contactsCache.put(contact.getContactId(), contact);
1316         }
1317
1318 }