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