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