]> 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.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          * Mobile number's carrier
178          */
179         private MobileProvider mobileCarrier;
180
181         /**
182          * Mobile number
183          */
184         private Long mobileNumber;
185
186         /**
187          * Phone number area code
188          */
189         private Integer phoneAreaCode;
190
191         /**
192          * Country instance for phone number
193          */
194         private Country phoneCountry;
195
196         /**
197          * Phone number
198          */
199         private Long phoneNumber;
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                 // Init lists/maps
238                 this.contactList = new LinkedList<>();
239                 this.emailAddressList = new LinkedList<>();
240         }
241
242         /**
243          * Observes events being fired when an administrator has added a new
244          * contact.
245          * <p>
246          * @param event Event being fired
247          */
248         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
249                 // The event must be valid
250                 if (null == event) {
251                         // Throw NPE
252                         throw new NullPointerException("event is null"); //NOI18N
253                 } else if (event.getAddedContact() == null) {
254                         // Throw again ...
255                         throw new NullPointerException("event.addedContact is null"); //NOI18N
256                 } else if (event.getAddedContact().getContactId() == null) {
257                         // ... and again
258                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
259                 } else if (event.getAddedContact().getContactId() < 1) {
260                         // Not valid
261                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
262                 }
263
264                 // Clear this bean
265                 this.clear();
266
267                 // Call other method
268                 this.uniqueAddContact(event.getAddedContact());
269
270                 // Add to selectable contacts
271                 this.selectableContacts.add(event.getAddedContact());
272         }
273
274         /**
275          * Event observer for newly added users by adminstrator
276          * <p>
277          * @param event Event being fired
278          */
279         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
280                 // event should not be null
281                 if (null == event) {
282                         // Throw NPE
283                         throw new NullPointerException("event is null"); //NOI18N
284                 } else if (event.getAddedUser() == null) {
285                         // Throw NPE again
286                         throw new NullPointerException("event.addedUser is null"); //NOI18N
287                 } else if (event.getAddedUser().getUserId() == null) {
288                         // userId is null
289                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
290                 } else if (event.getAddedUser().getUserId() < 1) {
291                         // Not avalid id
292                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
293                 }
294
295                 // Clear all data
296                 this.clear();
297         }
298
299         /**
300          * Observes events being fired when an administrator has linked a new user
301          * with existing contact data.
302          * <p>
303          * @param event Event being fired
304          */
305         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
306                 // event should not be null
307                 if (null == event) {
308                         // Throw NPE
309                         throw new NullPointerException("event is null"); //NOI18N
310                 } else if (event.getLinkedUser() == null) {
311                         // Throw NPE again
312                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
313                 } else if (event.getLinkedUser().getUserContact() == null) {
314                         // Throw NPE again
315                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
316                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
317                         // userId is null
318                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
319                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
320                         // Not avalid id
321                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
322                 }
323
324                 // Remove contact from list available contacts list
325                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
326
327                 // Clear all data
328                 this.clear();
329         }
330
331         /**
332          * Event observer for updated contact data by administrators
333          * <p>
334          * @param event Updated contact data event
335          */
336         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
337                 // event should not be null
338                 if (null == event) {
339                         // Throw NPE
340                         throw new NullPointerException("event is null"); //NOI18N
341                 } else if (event.getUpdatedContact() == null) {
342                         // Throw NPE again
343                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
344                 } else if (event.getUpdatedContact().getContactId() == null) {
345                         // userId is null
346                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
347                 } else if (event.getUpdatedContact().getContactId() < 1) {
348                         // Not avalid id
349                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
350                 }
351
352                 // Add contact instance only once
353                 this.uniqueAddContact(event.getUpdatedContact());
354
355                 // Add email address to list
356                 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
357         }
358
359         /**
360          * Event observer when user confirmed account.
361          * <p>
362          * @param event Event being fired
363          */
364         public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
365                 // event should not be null
366                 if (null == event) {
367                         // Throw NPE
368                         throw new NullPointerException("event is null"); //NOI18N
369                 } else if (event.getConfirmedUser() == null) {
370                         // Throw NPE again
371                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
372                 } else if (event.getConfirmedUser().getUserId() == null) {
373                         // userId is null
374                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
375                 } else if (event.getConfirmedUser().getUserId() < 1) {
376                         // Not avalid id
377                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
378                 }
379
380                 // Add contact instance only once
381                 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
382         }
383
384         /**
385          * Event observer for logged-in user
386          * <p>
387          * @param event Event instance
388          */
389         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
390                 // event should not be null
391                 if (null == event) {
392                         // Throw NPE
393                         throw new NullPointerException("event is null"); //NOI18N
394                 } else if (event.getLoggedInUser() == null) {
395                         // Throw NPE again
396                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
397                 } else if (event.getLoggedInUser().getUserId() == null) {
398                         // userId is null
399                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
400                 } else if (event.getLoggedInUser().getUserId() < 1) {
401                         // Not avalid id
402                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
403                 }
404
405                 // Copy all data to this bean
406                 this.copyContact(event.getLoggedInUser().getUserContact());
407         }
408
409         /**
410          * Event observer for new user registrations
411          * <p>
412          * @param event User registration event
413          */
414         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
415                 // event should not be null
416                 if (null == event) {
417                         // Throw NPE
418                         throw new NullPointerException("event is null"); //NOI18N
419                 } else if (event.getRegisteredUser() == null) {
420                         // Throw NPE again
421                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
422                 } else if (event.getRegisteredUser().getUserId() == null) {
423                         // userId is null
424                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
425                 } else if (event.getRegisteredUser().getUserId() < 1) {
426                         // Not avalid id
427                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
428                 }
429
430                 // Get user instance
431                 Contact registeredContact = event.getRegisteredUser().getUserContact();
432
433                 // Copy all data from registered->user
434                 this.copyContact(registeredContact);
435
436                 // Add contact instance only once
437                 this.uniqueAddContact(registeredContact);
438
439                 // Add user name and email address
440                 this.addUserNameEmailAddress(registeredContact);
441
442                 // Clear all data
443                 this.clear();
444         }
445
446         @Override
447         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
448         public List<Contact> allContacts () {
449                 // Return un-modified list
450                 return this.contactList;
451         }
452
453         @Override
454         public Contact createContactInstance () {
455                 // Is all required data set?
456                 if (!this.isRequiredPersonalDataSet()) {
457                         // No, then abort here
458                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
459                 }
460
461                 // Required personal data must be set
462                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
463
464                 // Generate phone number
465                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
466                 DialableMobileNumber mobile = new MobileNumber(this.getMobileCarrier(), this.getMobileNumber());
467                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
468
469                 // Create new contact
470                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
471                 contact.setContactStreet(this.getStreet());
472                 contact.setContactHouseNumber(this.getHouseNumber());
473                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
474                 contact.setContactZipCode(this.getZipCode());
475                 contact.setContactCity(this.getCity());
476                 contact.setContactCountry(this.getCountry());
477                 contact.setContactEmailAddress(this.getEmailAddress());
478                 contact.setContactBirthday(this.getBirthday());
479                 contact.setContactComment(this.getComment());
480
481                 // Don't set null or wrong references
482                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
483                         // Now the number must be given
484                         if (phone.getPhoneAreaCode() == null) {
485                                 // Is null
486                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
487                         } else if (phone.getPhoneAreaCode() < 1) {
488                                 // Abort here
489                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
490                         } else if (phone.getPhoneNumber() == null) {
491                                 // Is null
492                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
493                         } else if (phone.getPhoneNumber() < 1) {
494                                 // Abort here
495                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
496                         }
497
498                         // Set phone number
499                         contact.setContactLandLineNumber(phone);
500                 }
501
502                 // Don't set null or wrong references
503                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
504                         // Now the number must be given
505                         if (fax.getPhoneAreaCode() == null) {
506                                 // Is null
507                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
508                         } else if (fax.getPhoneAreaCode() < 1) {
509                                 // Abort here
510                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
511                         } else if (fax.getPhoneNumber() == null) {
512                                 // Is null
513                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
514                         } else if (fax.getPhoneNumber() < 1) {
515                                 // Abort here
516                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
517                         }
518
519                         // Set fax number
520                         contact.setContactFaxNumber(fax);
521                 }
522
523                 // Is the provider set?
524                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileCarrier() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
525                         // Is the number set?
526                         if (mobile.getPhoneNumber() == null) {
527                                 // Is null
528                                 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
529                         } else if (mobile.getPhoneNumber() < 1) {
530                                 // Abort here
531                                 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
532                         }
533
534                         // Set mobile number
535                         contact.setContactMobileNumber(mobile);
536                 }
537
538                 // Return it
539                 return contact;
540         }
541
542         @Override
543         public String doChangePersonalContactData () {
544                 // This method shall only be called if the user is logged-in
545                 if (!this.userLoginController.isUserLoggedIn()) {
546                         // Not logged-in
547                         throw new IllegalStateException("User is not logged-in"); //NOI18N
548                 } else if (!this.isRequiredChangePersonalDataSet()) {
549                         // Not all required fields are set
550                         throw new FaceletException("Not all required fields are set."); //NOI18N
551                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
552                         // Password not matching
553                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
554                         return ""; //NOI18N
555                 }
556
557                 // Get contact instance
558                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
559
560                 // It should be there, so run some tests on it
561                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
562                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
563                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
564
565                 // Update all fields
566                 contact.setContactGender(this.getGender());
567                 contact.setContactFirstName(this.getFirstName());
568                 contact.setContactFamilyName(this.getFamilyName());
569                 contact.setContactStreet(this.getStreet());
570                 contact.setContactHouseNumber(this.getHouseNumber());
571                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
572                 contact.setContactZipCode(this.getZipCode());
573                 contact.setContactCity(this.getCity());
574                 contact.setContactCountry(this.getCountry());
575
576                 // Update contact's mobile number
577                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileCarrier(), this.getMobileNumber());
578
579                 // Update contact's land-line number
580                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
581
582                 // Update contact's fax number
583                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
584
585                 // Send it to the EJB
586                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
587
588                 // All fine
589                 return "contact_data_saved"; //NOI18N
590         }
591
592         @Override
593         @SuppressWarnings ("ReturnOfDateField")
594         public Date getBirthday () {
595                 return this.birthday;
596         }
597
598         @Override
599         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
600         public void setBirthday (final Date birthday) {
601                 this.birthday = birthday;
602         }
603
604         @Override
605         public String getCity () {
606                 return this.city;
607         }
608
609         @Override
610         public void setCity (final String city) {
611                 this.city = city;
612         }
613
614         @Override
615         public String getComment () {
616                 return this.comment;
617         }
618
619         @Override
620         public void setComment (final String comment) {
621                 this.comment = comment;
622         }
623
624         @Override
625         public String getControllerType () {
626                 return "general"; //NOI18N
627         }
628
629         @Override
630         @Deprecated
631         public void setControllerType (final String controllerType) {
632                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
633         }
634
635         @Override
636         public Country getCountry () {
637                 return this.country;
638         }
639
640         @Override
641         public void setCountry (final Country country) {
642                 this.country = country;
643         }
644
645         @Override
646         public String getEmailAddress () {
647                 return this.emailAddress;
648         }
649
650         @Override
651         public void setEmailAddress (final String emailAddress) {
652                 this.emailAddress = emailAddress;
653         }
654
655         @Override
656         public String getEmailAddressRepeat () {
657                 return this.emailAddressRepeat;
658         }
659
660         @Override
661         public void setEmailAddressRepeat (final String emailAddressRepeat) {
662                 this.emailAddressRepeat = emailAddressRepeat;
663         }
664
665         @Override
666         public String getFamilyName () {
667                 return this.familyName;
668         }
669
670         @Override
671         public void setFamilyName (final String familyName) {
672                 this.familyName = familyName;
673         }
674
675         @Override
676         public Integer getFaxAreaCode () {
677                 return this.faxAreaCode;
678         }
679
680         @Override
681         public void setFaxAreaCode (final Integer faxAreaCode) {
682                 this.faxAreaCode = faxAreaCode;
683         }
684
685         @Override
686         public Country getFaxCountry () {
687                 return this.faxCountry;
688         }
689
690         @Override
691         public void setFaxCountry (final Country faxCountry) {
692                 this.faxCountry = faxCountry;
693         }
694
695         @Override
696         public Long getFaxNumber () {
697                 return this.faxNumber;
698         }
699
700         @Override
701         public void setFaxNumber (final Long faxNumber) {
702                 this.faxNumber = faxNumber;
703         }
704
705         @Override
706         public String getFirstName () {
707                 return this.firstName;
708         }
709
710         @Override
711         public void setFirstName (final String firstName) {
712                 this.firstName = firstName;
713         }
714
715         @Override
716         public Gender getGender () {
717                 return this.gender;
718         }
719
720         @Override
721         public void setGender (final Gender gender) {
722                 this.gender = gender;
723         }
724
725         @Override
726         public Short getHouseNumber () {
727                 return this.houseNumber;
728         }
729
730         @Override
731         public void setHouseNumber (final Short houseNumber) {
732                 this.houseNumber = houseNumber;
733         }
734
735         @Override
736         public String getHouseNumberExtension () {
737                 return this.houseNumberExtension;
738         }
739
740         @Override
741         public void setHouseNumberExtension (final String houseNumberExtension) {
742                 this.houseNumberExtension = houseNumberExtension;
743         }
744
745         @Override
746         public MobileProvider getMobileCarrier () {
747                 return this.mobileCarrier;
748         }
749
750         @Override
751         public void setMobileCarrier (final MobileProvider mobileCarrier) {
752                 this.mobileCarrier = mobileCarrier;
753         }
754
755         @Override
756         public Long getMobileNumber () {
757                 return this.mobileNumber;
758         }
759
760         @Override
761         public void setMobileNumber (final Long mobileNumber) {
762                 this.mobileNumber = mobileNumber;
763         }
764
765         @Override
766         public Integer getPhoneAreaCode () {
767                 return this.phoneAreaCode;
768         }
769
770         @Override
771         public void setPhoneAreaCode (final Integer phoneAreaCode) {
772                 this.phoneAreaCode = phoneAreaCode;
773         }
774
775         @Override
776         public Country getPhoneCountry () {
777                 return this.phoneCountry;
778         }
779
780         @Override
781         public void setPhoneCountry (final Country phoneCountry) {
782                 this.phoneCountry = phoneCountry;
783         }
784
785         @Override
786         public Long getPhoneNumber () {
787                 return this.phoneNumber;
788         }
789
790         @Override
791         public void setPhoneNumber (final Long phoneNumber) {
792                 this.phoneNumber = phoneNumber;
793         }
794
795         @Override
796         public String getStreet () {
797                 return this.street;
798         }
799
800         @Override
801         public void setStreet (final String street) {
802                 this.street = street;
803         }
804
805         @Override
806         public String getTitle () {
807                 return this.title;
808         }
809
810         @Override
811         public void setTitle (final String title) {
812                 this.title = title;
813         }
814
815         @Override
816         public Integer getZipCode () {
817                 return this.zipCode;
818         }
819
820         @Override
821         public void setZipCode (final Integer zipCode) {
822                 this.zipCode = zipCode;
823         }
824
825         /**
826          * Post-construction method
827          */
828         @PostConstruct
829         public void init () {
830                 // Try it
831                 try {
832                         // Get initial context
833                         Context context = new InitialContext();
834
835                         // Try to lookup
836                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
837                 } catch (final NamingException e) {
838                         // Throw again
839                         throw new FaceletException(e);
840                 }
841
842                 // Get full email address list for reducing EJB calls
843                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
844
845                 // Get all contacts
846                 List<Contact> allContacts = this.contactBean.getAllContacts();
847
848                 // Get full contact list
849                 this.contactList.addAll(allContacts);
850
851                 // Get all users
852                 List<User> allUsers = this.userController.allUsers();
853
854                 // Get iterator
855                 Iterator<Contact> iterator = allContacts.iterator();
856
857                 // Loop through it
858                 while (iterator.hasNext()) {
859                         // Get next element
860                         Contact next = iterator.next();
861
862                         // Get iterator
863                         Iterator<User> userIterator = allUsers.iterator();
864
865                         // Loop through all users
866                         while (userIterator.hasNext()) {
867                                 // Get user instance
868                                 User nextUser = userIterator.next();
869
870                                 // Is contact same?
871                                 if (Objects.equals(next, nextUser.getUserContact())) {
872                                         // Found same
873                                         iterator.remove();
874                                         break;
875                                 }
876                         }
877                 }
878
879                 // Set contact list
880                 this.selectableContacts = allContacts;
881         }
882
883         @Override
884         public boolean isEmailAddressRegistered (final Contact contact) {
885                 // Cherck parameter
886                 if (null == contact) {
887                         // Throw NPE
888                         throw new NullPointerException("contact is null"); //NOI18N
889                 } else if (contact.getContactEmailAddress() == null) {
890                         // Throw again
891                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
892                 } else if (contact.getContactEmailAddress().isEmpty()) {
893                         // Is empty
894                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
895                 }
896
897                 // Determine it
898                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
899         }
900
901         @Override
902         public boolean isRequiredChangePersonalDataSet () {
903                 return ((this.getGender() != null) &&
904                                 (this.getFirstName() != null) &&
905                                 (this.getFamilyName() != null) &&
906                                 (this.getStreet() != null) &&
907                                 (this.getHouseNumber() != null) &&
908                                 (this.getZipCode() != null) &&
909                                 (this.getCity() != null));
910         }
911
912         @Override
913         public boolean isRequiredPersonalDataSet () {
914                 return ((this.getGender() != null) &&
915                                 (this.getFirstName() != null) &&
916                                 (this.getFamilyName() != null) &&
917                                 (this.getStreet() != null) &&
918                                 (this.getHouseNumber() != null) &&
919                                 (this.getZipCode() != null) &&
920                                 (this.getCity() != null) &&
921                                 (this.getEmailAddress() != null) &&
922                                 (this.getEmailAddressRepeat() != null));
923         }
924
925         @Override
926         public boolean isSameEmailAddressEntered () {
927                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
928         }
929
930         @Override
931         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
932                 // Init variable
933                 Contact localContact = null;
934
935                 // Clear this bean
936                 this.clear();
937
938                 // Try to lookup it in visible user list
939                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
940                         // Get next user
941                         Contact next = iterator.next();
942
943                         // Is the user id found?
944                         if (Objects.equals(next.getContactId(), contactId)) {
945                                 // Copy to other variable
946                                 localContact = next;
947                                 break;
948                         }
949                 }
950
951                 // Is it still null?
952                 if (null == localContact) {
953                         // Not visible for the current user
954                         throw new ContactNotFoundException(contactId);
955                 }
956
957                 // Copy all data to this bean
958                 this.copyContact(localContact);
959
960                 // Return it
961                 return localContact;
962         }
963
964         @Override
965         public List<Contact> selectableContacts () {
966                 return Collections.unmodifiableList(this.selectableContacts);
967         }
968
969         @Override
970         public void updateContactDataFromController (final Contact contact) {
971                 // Is the instance valid?
972                 if (null == contact) {
973                         // Throw NPE
974                         throw new NullPointerException("contact is null"); //NOI18N
975                 } else if (contact.getContactId() == null) {
976                         // Throw NPE
977                         throw new NullPointerException("contact.contactId is null"); //NOI18N
978                 } else if (contact.getContactId() < 1) {
979                         // Not valid id number
980                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
981                 }
982
983                 // Set all
984                 this.copyContact(contact);
985         }
986
987         /**
988          * Adds email address to bean's internal list.
989          * <p>
990          * @param contact Contact instance
991          */
992         private void addUserNameEmailAddress (final Contact contact) {
993                 // Make sure the entry is not added yet
994                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
995                         // Already added
996                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
997                 }
998
999                 // Add email addres
1000                 this.emailAddressList.add(contact.getContactEmailAddress());
1001         }
1002
1003         /**
1004          * Clears this bean
1005          */
1006         private void clear () {
1007                 // Clear all data
1008                 // - personal data
1009                 this.setGender(null);
1010                 this.setTitle(null);
1011                 this.setFirstName(null);
1012                 this.setFamilyName(null);
1013                 this.setStreet(null);
1014                 this.setHouseNumber(null);
1015                 this.setHouseNumberExtension(null);
1016                 this.setZipCode(null);
1017                 this.setCity(null);
1018                 this.setCountry(null);
1019
1020                 // - contact data
1021                 this.setEmailAddress(null);
1022                 this.setEmailAddressRepeat(null);
1023                 this.setPhoneAreaCode(null);
1024                 this.setPhoneCountry(null);
1025                 this.setPhoneNumber(null);
1026                 this.setMobileCarrier(null);
1027                 this.setMobileNumber(null);
1028                 this.setFaxAreaCode(null);
1029                 this.setFaxCountry(null);
1030                 this.setFaxNumber(null);
1031
1032                 // - other data
1033                 this.setBirthday(null);
1034                 this.setComment(null);
1035         }
1036
1037         /**
1038          * Copies given contact into the controller
1039          * <p>
1040          * @param contact Contact instance
1041          */
1042         private void copyContact (final Contact contact) {
1043                 // Is the instance valid?
1044                 if (null == contact) {
1045                         // Throw NPE
1046                         throw new NullPointerException("contact is null"); //NOI18N
1047                 } else if (contact.getContactId() == null) {
1048                         // Throw NPE
1049                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1050                 } else if (contact.getContactId() < 1) {
1051                         // Not valid id number
1052                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1053                 }
1054
1055                 // Copy all fields:
1056                 // - base data
1057                 this.setGender(contact.getContactGender());
1058                 this.setTitle(contact.getContactTitle());
1059                 this.setFirstName(contact.getContactFirstName());
1060                 this.setFamilyName(contact.getContactFamilyName());
1061                 this.setStreet(contact.getContactStreet());
1062                 this.setHouseNumber(contact.getContactHouseNumber());
1063                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1064                 this.setZipCode(contact.getContactZipCode());
1065                 this.setCity(contact.getContactCity());
1066                 this.setCountry(contact.getContactCountry());
1067                 this.setEmailAddress(contact.getContactEmailAddress());
1068                 this.setBirthday(contact.getContactBirthday());
1069                 this.setComment(contact.getContactComment());
1070
1071                 // Get mobile, phone and fax instance
1072                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1073                 DialableFaxNumber fax = contact.getContactFaxNumber();
1074                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1075
1076                 // - contact data
1077                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1078                         this.setPhoneCountry(phone.getPhoneCountry());
1079                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
1080                         this.setPhoneNumber(phone.getPhoneNumber());
1081                 }
1082
1083                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1084                         this.setMobileCarrier(mobile.getMobileProvider());
1085                         this.setMobileNumber(mobile.getPhoneNumber());
1086                 }
1087
1088                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1089                         this.setFaxCountry(fax.getPhoneCountry());
1090                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1091                         this.setFaxNumber(fax.getPhoneNumber());
1092                 }
1093         }
1094
1095         /**
1096          * Removes given contact from all lists
1097          * <p>
1098          * @param contact Contact instance to remove
1099          */
1100         private void removeContact (final Contact contact) {
1101                 // Is the instance valid?
1102                 if (null == contact) {
1103                         // Throw NPE
1104                         throw new NullPointerException("contact is null"); //NOI18N
1105                 } else if (contact.getContactId() == null) {
1106                         // Throw NPE
1107                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1108                 } else if (contact.getContactId() < 1) {
1109                         // Not valid id number
1110                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1111                 }
1112
1113                 // Remove from general list
1114                 if (!this.contactList.remove(contact)) {
1115                         // Did not remove contact
1116                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1117                 }
1118
1119                 // Remove from other lists
1120                 this.emailAddressList.remove(contact.getContactEmailAddress());
1121         }
1122
1123         /**
1124          * Adds unique instance to contact list. First any existing instance is
1125          * being removed, then the new instance is added.
1126          * <p>
1127          * @param contact Contact instance to add uniquely
1128          */
1129         private void uniqueAddContact (final Contact contact) {
1130                 // Is the instance valid?
1131                 if (null == contact) {
1132                         // Throw NPE
1133                         throw new NullPointerException("contact is null"); //NOI18N
1134                 } else if (contact.getContactId() == null) {
1135                         // Throw NPE
1136                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1137                 } else if (contact.getContactId() < 1) {
1138                         // Not valid id number
1139                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1140                 }
1141
1142                 // Get iterator from list
1143                 Iterator<Contact> iterator = this.contactList.iterator();
1144
1145                 // "Walk" through all entries
1146                 while (iterator.hasNext()) {
1147                         // Get next element
1148                         Contact next = iterator.next();
1149
1150                         // Is id number the same?
1151                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1152                                 // Found entry, so remove it and abort
1153                                 this.removeContact(next);
1154                                 break;
1155                         }
1156                 }
1157
1158                 // Add contact to list
1159                 this.contactList.add(contact);
1160         }
1161
1162 }