]> 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, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaapplication.beans.contact;
18
19 import 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.ContactUtils;
38 import org.mxchange.jcontacts.contact.UserContact;
39 import org.mxchange.jcontacts.contact.title.PersonalTitle;
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.user.add.ObservableAdminAddedUserEvent;
52 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
55 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
56 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
57 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
58 import org.mxchange.pizzaapplication.beans.BasePizzaController;
59 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
60 import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionController;
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          * Academic academicTitle
78          */
79         private String academicTitle;
80
81         /**
82          * Birth day
83          */
84         private Date birthday;
85
86         /**
87          * City
88          */
89         private String city;
90
91         /**
92          * Optional comments
93          */
94         private String comment;
95
96         /**
97          * Remote contact bean
98          */
99         private ContactSessionBeanRemote contactBean;
100
101         /**
102          * Contact list
103          */
104         private final List<Contact> contactList;
105
106         /**
107          * Country instance
108          */
109         private Country country;
110
111         /**
112          * Email address
113          */
114         private String emailAddress;
115
116         /**
117          * Email address list
118          */
119         private final List<String> emailAddressList;
120
121         /**
122          * Email address repeated
123          */
124         private String emailAddressRepeat;
125
126         /**
127          * Family name
128          */
129         private String familyName;
130
131         /**
132          * Fax number's area code
133          */
134         private Integer faxAreaCode;
135
136         /**
137          * Country instance for fax number
138          */
139         private Country faxCountry;
140
141         /**
142          * Fax number
143          */
144         private Long faxNumber;
145
146         /**
147          * First name
148          */
149         private String firstName;
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          * Personal academicTitle
203          */
204         private PersonalTitle personalTitle;
205
206         /**
207          * A list of all selectable contacts
208          */
209         private List<Contact> selectableContacts;
210
211         /**
212          * Street
213          */
214         private String street;
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 administrator
279          * <p>
280          * @param event Event being fired
281          */
282         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
283                 // event should not be null
284                 if (null == event) {
285                         // Throw NPE
286                         throw new NullPointerException("event is null"); //NOI18N
287                 } else if (event.getAddedUser() == null) {
288                         // Throw NPE again
289                         throw new NullPointerException("event.addedUser is null"); //NOI18N
290                 } else if (event.getAddedUser().getUserId() == null) {
291                         // userId is null
292                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
293                 } else if (event.getAddedUser().getUserId() < 1) {
294                         // Not avalid id
295                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
296                 }
297
298                 // Clear all data
299                 this.clear();
300         }
301
302         /**
303          * Observes events being fired when an administrator has linked a new user
304          * with existing contact data.
305          * <p>
306          * @param event Event being fired
307          */
308         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
309                 // event should not be null
310                 if (null == event) {
311                         // Throw NPE
312                         throw new NullPointerException("event is null"); //NOI18N
313                 } else if (event.getLinkedUser() == null) {
314                         // Throw NPE again
315                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
316                 } else if (event.getLinkedUser().getUserContact() == null) {
317                         // Throw NPE again
318                         throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
319                 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
320                         // userId is null
321                         throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
322                 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
323                         // Not avalid id
324                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
325                 }
326
327                 // Remove contact from list available contacts list
328                 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
329
330                 // Clear all data
331                 this.clear();
332         }
333
334         /**
335          * Event observer for updated contact data by administrators
336          * <p>
337          * @param event Updated contact data event
338          */
339         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
340                 // event should not be null
341                 if (null == event) {
342                         // Throw NPE
343                         throw new NullPointerException("event is null"); //NOI18N
344                 } else if (event.getUpdatedContact() == null) {
345                         // Throw NPE again
346                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
347                 } else if (event.getUpdatedContact().getContactId() == null) {
348                         // userId is null
349                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
350                 } else if (event.getUpdatedContact().getContactId() < 1) {
351                         // Not avalid id
352                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
353                 }
354
355                 // Add contact instance only once
356                 this.uniqueAddContact(event.getUpdatedContact());
357
358                 // Add email address to list
359                 this.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 void clearEmailAddresses () {
458                 // Clear both
459                 this.setEmailAddress(null);
460                 this.setEmailAddressRepeat(null);
461         }
462
463         @Override
464         public Contact createContactInstance () {
465                 // Is all required data set?
466                 if (!this.isRequiredPersonalDataSet()) {
467                         // No, then abort here
468                         throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
469                 }
470
471                 // Required personal data must be set
472                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
473
474                 // Generate phone number
475                 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
476                 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
477                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
478
479                 // Create new contact
480                 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
481                 contact.setContactStreet(this.getStreet());
482                 contact.setContactHouseNumber(this.getHouseNumber());
483                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
484                 contact.setContactZipCode(this.getZipCode());
485                 contact.setContactCity(this.getCity());
486                 contact.setContactCountry(this.getCountry());
487                 contact.setContactEmailAddress(this.getEmailAddress());
488                 contact.setContactBirthday(this.getBirthday());
489                 contact.setContactComment(this.getComment());
490
491                 // Don't set null or wrong references
492                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
493                         // Now the number must be given
494                         if (phone.getPhoneAreaCode() == null) {
495                                 // Is null
496                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
497                         } else if (phone.getPhoneAreaCode() < 1) {
498                                 // Abort here
499                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
500                         } else if (phone.getPhoneNumber() == null) {
501                                 // Is null
502                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
503                         } else if (phone.getPhoneNumber() < 1) {
504                                 // Abort here
505                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
506                         }
507
508                         // Set phone number
509                         contact.setContactLandLineNumber(phone);
510                 }
511
512                 // Don't set null or wrong references
513                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
514                         // Now the number must be given
515                         if (fax.getPhoneAreaCode() == null) {
516                                 // Is null
517                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
518                         } else if (fax.getPhoneAreaCode() < 1) {
519                                 // Abort here
520                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
521                         } else if (fax.getPhoneNumber() == null) {
522                                 // Is null
523                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
524                         } else if (fax.getPhoneNumber() < 1) {
525                                 // Abort here
526                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
527                         }
528
529                         // Set fax number
530                         contact.setContactFaxNumber(fax);
531                 }
532
533                 // Is the provider set?
534                 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
535                         // Is the number set?
536                         if (mobile.getPhoneNumber() == null) {
537                                 // Is null
538                                 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
539                         } else if (mobile.getPhoneNumber() < 1) {
540                                 // Abort here
541                                 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
542                         }
543
544                         // Set mobile number
545                         contact.setContactMobileNumber(mobile);
546                 }
547
548                 // Return it
549                 return contact;
550         }
551
552         @Override
553         public String doChangePersonalContactData () {
554                 // This method shall only be called if the user is logged-in
555                 if (!this.userLoginController.isUserLoggedIn()) {
556                         // Not logged-in
557                         throw new IllegalStateException("User is not logged-in"); //NOI18N
558                 } else if (!this.isRequiredChangePersonalDataSet()) {
559                         // Not all required fields are set
560                         throw new FaceletException("Not all required fields are set."); //NOI18N
561                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
562                         // Password not matching
563                         this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
564                         return ""; //NOI18N
565                 }
566
567                 // Get contact instance
568                 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
569
570                 // It should be there, so run some tests on it
571                 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
572                 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
573                 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
574
575                 // Update all fields
576                 contact.setContactPersonalTitle(this.getPersonalTitle());
577                 contact.setContactFirstName(this.getFirstName());
578                 contact.setContactFamilyName(this.getFamilyName());
579                 contact.setContactStreet(this.getStreet());
580                 contact.setContactHouseNumber(this.getHouseNumber());
581                 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
582                 contact.setContactZipCode(this.getZipCode());
583                 contact.setContactCity(this.getCity());
584                 contact.setContactCountry(this.getCountry());
585
586                 // Update contact's mobile number
587                 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
588
589                 // Update contact's land-line number
590                 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
591
592                 // Update contact's fax number
593                 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
594
595                 // Send it to the EJB
596                 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
597
598                 // All fine
599                 return "contact_data_saved"; //NOI18N
600         }
601
602         /**
603          * Getter for academic title
604          * <p>
605          * @return Academic title
606          */
607         public String getAcademicTitle () {
608                 return this.academicTitle;
609         }
610
611         /**
612          * Setter for academic title
613          * <p>
614          * @param academicTitle Academic title
615          */
616         public void setAcademicTitle (final String academicTitle) {
617                 this.academicTitle = academicTitle;
618         }
619
620         /**
621          * Getter for birth day
622          * <p>
623          * @return Birth day
624          */
625         @SuppressWarnings ("ReturnOfDateField")
626         public Date getBirthday () {
627                 return this.birthday;
628         }
629
630         /**
631          * Setter for birth day
632          * <p>
633          * @param birthday Birth day
634          */
635         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
636         public void setBirthday (final Date birthday) {
637                 this.birthday = birthday;
638         }
639
640         /**
641          * Getter for city name
642          * <p>
643          * @return City name
644          */
645         public String getCity () {
646                 return this.city;
647         }
648
649         /**
650          * Setter for city name
651          * <p>
652          * @param city City name
653          */
654         public void setCity (final String city) {
655                 this.city = city;
656         }
657
658         /**
659          * Getter for comments
660          * <p>
661          * @return Comments
662          */
663         public String getComment () {
664                 return this.comment;
665         }
666
667         /**
668          * Setter for comment
669          * <p>
670          * @param comment Comments
671          */
672         public void setComment (final String comment) {
673                 this.comment = comment;
674         }
675
676         @Override
677         public String getControllerType () {
678                 return "general"; //NOI18N
679         }
680
681         @Override
682         @Deprecated
683         public void setControllerType (final String controllerType) {
684                 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
685         }
686
687         /**
688          * Getter for country instance
689          * <p>
690          * @return Country instance
691          */
692         public Country getCountry () {
693                 return this.country;
694         }
695
696         /**
697          * Setter for country instance
698          * <p>
699          * @param country Country instance
700          */
701         public void setCountry (final Country country) {
702                 this.country = country;
703         }
704
705         @Override
706         public String getEmailAddress () {
707                 return this.emailAddress;
708         }
709
710         /**
711          * Setter for email address
712          * <p>
713          * @param emailAddress Email address
714          */
715         public void setEmailAddress (final String emailAddress) {
716                 this.emailAddress = emailAddress;
717         }
718
719         /**
720          * Getter for email address, repeated
721          * <p>
722          * @return the emailAddress, repeated
723          */
724         public String getEmailAddressRepeat () {
725                 return this.emailAddressRepeat;
726         }
727
728         /**
729          * Setter for email address repeated
730          * <p>
731          * @param emailAddressRepeat the emailAddress to set
732          */
733         public void setEmailAddressRepeat (final String emailAddressRepeat) {
734                 this.emailAddressRepeat = emailAddressRepeat;
735         }
736
737         /**
738          * Family name
739          * <p>
740          * @return the familyName
741          */
742         public String getFamilyName () {
743                 return this.familyName;
744         }
745
746         /**
747          * Family name
748          * <p>
749          * @param familyName the familyName to set
750          */
751         public void setFamilyName (final String familyName) {
752                 this.familyName = familyName;
753         }
754
755         /**
756          * Getter for fax number's area code
757          * <p>
758          * @return Fax number's area code
759          */
760         public Integer getFaxAreaCode () {
761                 return this.faxAreaCode;
762         }
763
764         /**
765          * Setter for fax number's area code
766          * <p>
767          * @param faxAreaCode Fax number's area code
768          */
769         public void setFaxAreaCode (final Integer faxAreaCode) {
770                 this.faxAreaCode = faxAreaCode;
771         }
772
773         /**
774          * Getter for fax's country instance
775          * <p>
776          * @return Fax' country instance
777          */
778         public Country getFaxCountry () {
779                 return this.faxCountry;
780         }
781
782         /**
783          * Setter for fax's country instance
784          * <p>
785          * @param faxCountry Fax' country instance
786          */
787         public void setFaxCountry (final Country faxCountry) {
788                 this.faxCountry = faxCountry;
789         }
790
791         /**
792          * Getter for fax number
793          * <p>
794          * @return Fax number
795          */
796         public Long getFaxNumber () {
797                 return this.faxNumber;
798         }
799
800         /**
801          * Setter for fax number
802          * <p>
803          * @param faxNumber Fax number
804          */
805         public void setFaxNumber (final Long faxNumber) {
806                 this.faxNumber = faxNumber;
807         }
808
809         /**
810          * First name
811          * <p>
812          * @return the first name
813          */
814         public String getFirstName () {
815                 return this.firstName;
816         }
817
818         /**
819          * First name
820          * <p>
821          * @param firstName the first name to set
822          */
823         public void setFirstName (final String firstName) {
824                 this.firstName = firstName;
825         }
826
827         /**
828          * House number
829          * <p>
830          * @return the houseNumber
831          */
832         public Short getHouseNumber () {
833                 return this.houseNumber;
834         }
835
836         /**
837          * House number
838          * <p>
839          * @param houseNumber the houseNumber to set
840          */
841         public void setHouseNumber (final Short houseNumber) {
842                 this.houseNumber = houseNumber;
843         }
844
845         /**
846          * Getter for house number extension, example: 123a 'a' is then the
847          * extension and 123 is the house number.
848          * <p>
849          * @return House number extension
850          */
851         public String getHouseNumberExtension () {
852                 return this.houseNumberExtension;
853         }
854
855         /**
856          * Setter for house number extension
857          * <p>
858          * @param houseNumberExtension House number extension
859          */
860         public void setHouseNumberExtension (final String houseNumberExtension) {
861                 this.houseNumberExtension = houseNumberExtension;
862         }
863
864         /**
865          * Getter for land-line number's area code
866          * <p>
867          * @return Land-line number's area code
868          */
869         public Integer getLandLineAreaCode () {
870                 return this.landLineAreaCode;
871         }
872
873         /**
874          * Setter for land-line number's area code
875          * <p>
876          * @param landLineAreaCode Land-line number's area code
877          */
878         public void setLandLineAreaCode (final Integer landLineAreaCode) {
879                 this.landLineAreaCode = landLineAreaCode;
880         }
881
882         /**
883          * Getter for land-line number's country instance
884          * <p>
885          * @return Land-line number's country instance
886          */
887         public Country getLandLineCountry () {
888                 return this.landLineCountry;
889         }
890
891         /**
892          * Setter for land-line number's country instance
893          * <p>
894          * @param landLineCountry Land-line number's country instance
895          */
896         public void setLandLineCountry (final Country landLineCountry) {
897                 this.landLineCountry = landLineCountry;
898         }
899
900         /**
901          * Getter for land-line number
902          * <p>
903          * @return Land-line number
904          */
905         public Long getLandLineNumber () {
906                 return this.landLineNumber;
907         }
908
909         /**
910          * Setter for land-line number
911          * <p>
912          * @param landLineNumber Land-line number
913          */
914         public void setLandLineNumber (final Long landLineNumber) {
915                 this.landLineNumber = landLineNumber;
916         }
917
918         /**
919          * Getter for mobile number
920          * <p>
921          * @return Mobile number
922          */
923         public Long getMobileNumber () {
924                 return this.mobileNumber;
925         }
926
927         /**
928          * Setter for mobile number
929          * <p>
930          * @param mobileNumber Mobile number
931          */
932         public void setMobileNumber (final Long mobileNumber) {
933                 this.mobileNumber = mobileNumber;
934         }
935
936         /**
937          * Getter for mobile number's carrier
938          * <p>
939          * @return Mobile number's carrier
940          */
941         public MobileProvider getMobileProvider () {
942                 return this.mobileProvider;
943         }
944
945         /**
946          * Setter for mobile number's provider
947          * <p>
948          * @param mobileProvider Mobile number's provider
949          */
950         public void setMobileProvider (final MobileProvider mobileProvider) {
951                 this.mobileProvider = mobileProvider;
952         }
953
954         /**
955          * Getter for personal title
956          * <p>
957          * @return Personal title
958          */
959         public PersonalTitle getPersonalTitle () {
960                 return this.personalTitle;
961         }
962
963         /**
964          * Setter for personal title
965          * <p>
966          * @param personalTitle Personal title
967          */
968         public void setPersonalTitle (final PersonalTitle personalTitle) {
969                 this.personalTitle = personalTitle;
970         }
971
972         /**
973          * Getter for street
974          * <p>
975          * @return Street
976          */
977         public String getStreet () {
978                 return this.street;
979         }
980
981         /**
982          * Setter for street
983          * <p>
984          * @param street Street
985          */
986         public void setStreet (final String street) {
987                 this.street = street;
988         }
989
990         /**
991          * Getter for ZIP code
992          * <p>
993          * @return ZIP code
994          */
995         public Integer getZipCode () {
996                 return this.zipCode;
997         }
998
999         /**
1000          * Setter for ZIP code
1001          * <p>
1002          * @param zipCode ZIP code
1003          */
1004         public void setZipCode (final Integer zipCode) {
1005                 this.zipCode = zipCode;
1006         }
1007
1008         /**
1009          * Post-construction method
1010          */
1011         @PostConstruct
1012         public void init () {
1013                 // Try it
1014                 try {
1015                         // Get initial context
1016                         Context context = new InitialContext();
1017
1018                         // Try to lookup
1019                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
1020                 } catch (final NamingException e) {
1021                         // Throw again
1022                         throw new FaceletException(e);
1023                 }
1024
1025                 // Get full email address list for reducing EJB calls
1026                 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
1027
1028                 // Get all contacts
1029                 List<Contact> allContacts = this.contactBean.getAllContacts();
1030
1031                 // Get full contact list
1032                 this.contactList.addAll(allContacts);
1033
1034                 // Get all users
1035                 List<User> allUsers = this.userController.allUsers();
1036
1037                 // Get iterator
1038                 Iterator<Contact> iterator = allContacts.iterator();
1039
1040                 // Loop through it
1041                 while (iterator.hasNext()) {
1042                         // Get next element
1043                         Contact next = iterator.next();
1044
1045                         // Get iterator
1046                         Iterator<User> userIterator = allUsers.iterator();
1047
1048                         // Loop through all users
1049                         while (userIterator.hasNext()) {
1050                                 // Get user instance
1051                                 User nextUser = userIterator.next();
1052
1053                                 // Is contact same?
1054                                 if (Objects.equals(next, nextUser.getUserContact())) {
1055                                         // Found same
1056                                         iterator.remove();
1057                                         break;
1058                                 }
1059                         }
1060                 }
1061
1062                 // Set contact list
1063                 this.selectableContacts = allContacts;
1064         }
1065
1066         @Override
1067         public boolean isEmailAddressRegistered (final Contact contact) {
1068                 // Cherck parameter
1069                 if (null == contact) {
1070                         // Throw NPE
1071                         throw new NullPointerException("contact is null"); //NOI18N
1072                 } else if (contact.getContactEmailAddress() == null) {
1073                         // Throw again
1074                         throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1075                 } else if (contact.getContactEmailAddress().isEmpty()) {
1076                         // Is empty
1077                         throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1078                 }
1079
1080                 // Determine it
1081                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
1082         }
1083
1084         @Override
1085         public boolean isRequiredChangePersonalDataSet () {
1086                 return ((this.getPersonalTitle() != null) &&
1087                                 (this.getFirstName() != null) &&
1088                                 (this.getFamilyName() != null) &&
1089                                 (this.getStreet() != null) &&
1090                                 (this.getHouseNumber() != null) &&
1091                                 (this.getZipCode() != null) &&
1092                                 (this.getCity() != null));
1093         }
1094
1095         @Override
1096         public boolean isRequiredPersonalDataSet () {
1097                 return ((this.getPersonalTitle() != null) &&
1098                                 (this.getFirstName() != null) &&
1099                                 (this.getFamilyName() != null) &&
1100                                 (this.getStreet() != null) &&
1101                                 (this.getHouseNumber() != null) &&
1102                                 (this.getZipCode() != null) &&
1103                                 (this.getCity() != null) &&
1104                                 (this.getEmailAddress() != null) &&
1105                                 (this.getEmailAddressRepeat() != null));
1106         }
1107
1108         @Override
1109         public boolean isSameEmailAddressEntered () {
1110                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1111         }
1112
1113         @Override
1114         public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
1115                 // Init variable
1116                 Contact localContact = null;
1117
1118                 // Clear this bean
1119                 this.clear();
1120
1121                 // Try to lookup it in visible user list
1122                 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
1123                         // Get next user
1124                         Contact next = iterator.next();
1125
1126                         // Is the user id found?
1127                         if (Objects.equals(next.getContactId(), contactId)) {
1128                                 // Copy to other variable
1129                                 localContact = next;
1130                                 break;
1131                         }
1132                 }
1133
1134                 // Is it still null?
1135                 if (null == localContact) {
1136                         // Not visible for the current user
1137                         throw new ContactNotFoundException(contactId);
1138                 }
1139
1140                 // Copy all data to this bean
1141                 this.copyContact(localContact);
1142
1143                 // Return it
1144                 return localContact;
1145         }
1146
1147         @Override
1148         public List<Contact> selectableContacts () {
1149                 return Collections.unmodifiableList(this.selectableContacts);
1150         }
1151
1152         @Override
1153         public void updateContactDataFromController (final Contact contact) {
1154                 // Is the instance valid?
1155                 if (null == contact) {
1156                         // Throw NPE
1157                         throw new NullPointerException("contact is null"); //NOI18N
1158                 } else if (contact.getContactId() == null) {
1159                         // Throw NPE
1160                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1161                 } else if (contact.getContactId() < 1) {
1162                         // Not valid id number
1163                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1164                 }
1165
1166                 // Set all
1167                 this.copyContact(contact);
1168         }
1169
1170         /**
1171          * Adds email address to bean's internal list.
1172          * <p>
1173          * @param contact Contact instance
1174          */
1175         private void addUserNameEmailAddress (final Contact contact) {
1176                 // Make sure the entry is not added yet
1177                 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
1178                         // Already added
1179                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1180                 }
1181
1182                 // Add email addres
1183                 this.emailAddressList.add(contact.getContactEmailAddress());
1184         }
1185
1186         /**
1187          * Clears this bean
1188          */
1189         private void clear () {
1190                 // Clear all data
1191                 // - personal data
1192                 this.setPersonalTitle(null);
1193                 this.setAcademicTitle(null);
1194                 this.setFirstName(null);
1195                 this.setFamilyName(null);
1196                 this.setStreet(null);
1197                 this.setHouseNumber(null);
1198                 this.setHouseNumberExtension(null);
1199                 this.setZipCode(null);
1200                 this.setCity(null);
1201                 this.setCountry(null);
1202
1203                 // - contact data
1204                 this.clearEmailAddresses();
1205                 this.setLandLineAreaCode(null);
1206                 this.setLandLineCountry(null);
1207                 this.setLandLineNumber(null);
1208                 this.setMobileProvider(null);
1209                 this.setMobileNumber(null);
1210                 this.setFaxAreaCode(null);
1211                 this.setFaxCountry(null);
1212                 this.setFaxNumber(null);
1213
1214                 // - other data
1215                 this.setBirthday(null);
1216                 this.setComment(null);
1217         }
1218
1219         /**
1220          * Copies given contact into the controller
1221          * <p>
1222          * @param contact Contact instance
1223          */
1224         private void copyContact (final Contact contact) {
1225                 // Is the instance valid?
1226                 if (null == contact) {
1227                         // Throw NPE
1228                         throw new NullPointerException("contact is null"); //NOI18N
1229                 } else if (contact.getContactId() == null) {
1230                         // Throw NPE
1231                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1232                 } else if (contact.getContactId() < 1) {
1233                         // Not valid id number
1234                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1235                 }
1236
1237                 // Copy all fields:
1238                 // - base data
1239                 this.setPersonalTitle(contact.getContactPersonalTitle());
1240                 this.setAcademicTitle(contact.getContactTitle());
1241                 this.setFirstName(contact.getContactFirstName());
1242                 this.setFamilyName(contact.getContactFamilyName());
1243                 this.setStreet(contact.getContactStreet());
1244                 this.setHouseNumber(contact.getContactHouseNumber());
1245                 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1246                 this.setZipCode(contact.getContactZipCode());
1247                 this.setCity(contact.getContactCity());
1248                 this.setCountry(contact.getContactCountry());
1249                 this.setEmailAddress(contact.getContactEmailAddress());
1250                 this.setBirthday(contact.getContactBirthday());
1251                 this.setComment(contact.getContactComment());
1252
1253                 // Get mobile, phone and fax instance
1254                 DialableMobileNumber mobile = contact.getContactMobileNumber();
1255                 DialableFaxNumber fax = contact.getContactFaxNumber();
1256                 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1257
1258                 // - contact data
1259                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1260                         this.setLandLineCountry(phone.getPhoneCountry());
1261                         this.setLandLineAreaCode(phone.getPhoneAreaCode());
1262                         this.setLandLineNumber(phone.getPhoneNumber());
1263                 }
1264
1265                 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1266                         this.setMobileProvider(mobile.getMobileProvider());
1267                         this.setMobileNumber(mobile.getPhoneNumber());
1268                 }
1269
1270                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1271                         this.setFaxCountry(fax.getPhoneCountry());
1272                         this.setFaxAreaCode(fax.getPhoneAreaCode());
1273                         this.setFaxNumber(fax.getPhoneNumber());
1274                 }
1275         }
1276
1277         /**
1278          * Removes given contact from all lists
1279          * <p>
1280          * @param contact Contact instance to remove
1281          */
1282         private void removeContact (final Contact contact) {
1283                 // Is the instance valid?
1284                 if (null == contact) {
1285                         // Throw NPE
1286                         throw new NullPointerException("contact is null"); //NOI18N
1287                 } else if (contact.getContactId() == null) {
1288                         // Throw NPE
1289                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1290                 } else if (contact.getContactId() < 1) {
1291                         // Not valid id number
1292                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1293                 }
1294
1295                 // Remove from general list
1296                 if (!this.contactList.remove(contact)) {
1297                         // Did not remove contact
1298                         throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1299                 }
1300
1301                 // Remove from other lists
1302                 this.emailAddressList.remove(contact.getContactEmailAddress());
1303         }
1304
1305         /**
1306          * Adds unique instance to contact list. First any existing instance is
1307          * being removed, then the new instance is added.
1308          * <p>
1309          * @param contact Contact instance to add uniquely
1310          */
1311         private void uniqueAddContact (final Contact contact) {
1312                 // Is the instance valid?
1313                 if (null == contact) {
1314                         // Throw NPE
1315                         throw new NullPointerException("contact is null"); //NOI18N
1316                 } else if (contact.getContactId() == null) {
1317                         // Throw NPE
1318                         throw new NullPointerException("contact.contactId is null"); //NOI18N
1319                 } else if (contact.getContactId() < 1) {
1320                         // Not valid id number
1321                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1322                 }
1323
1324                 // Get iterator from list
1325                 Iterator<Contact> iterator = this.contactList.iterator();
1326
1327                 // "Walk" through all entries
1328                 while (iterator.hasNext()) {
1329                         // Get next element
1330                         Contact next = iterator.next();
1331
1332                         // Is id number the same?
1333                         if (Objects.equals(contact.getContactId(), next.getContactId())) {
1334                                 // Found entry, so remove it and abort
1335                                 this.removeContact(next);
1336                                 break;
1337                         }
1338                 }
1339
1340                 // Add contact to list
1341                 this.contactList.add(contact);
1342         }
1343
1344 }