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