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