2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.contact;
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;
63 * A general contact bean (controller)
65 * @author Roland Häder<roland@mxchange.org>
67 @Named ("contactController")
69 public class PizzaContactWebSessionBean extends BasePizzaController implements PizzaContactWebSessionController {
74 private static final long serialVersionUID = 542_145_347_916L;
77 * Academic academicTitle
79 private String academicTitle;
84 private Date birthday;
94 private String comment;
99 private ContactSessionBeanRemote contactBean;
104 private final List<Contact> contactList;
109 private Country country;
114 private String emailAddress;
119 private final List<String> emailAddressList;
122 * Email address repeated
124 private String emailAddressRepeat;
129 private String familyName;
132 * Fax number's area code
134 private Integer faxAreaCode;
137 * Country instance for fax number
139 private Country faxCountry;
144 private Long faxNumber;
149 private String firstName;
154 private Short houseNumber;
157 * House number extension
159 private String houseNumberExtension;
162 * Whether a fax entry has been unlinked
164 private boolean isFaxUnlinked;
167 * Whether a land-line number has been unlinked
169 private boolean isLandLineUnlinked;
172 * Whether a mobile entry has been unlinked
174 private boolean isMobileUnlinked;
177 * Phone number area code
179 private Integer landLineAreaCode;
182 * Country instance for phone number
184 private Country landLineCountry;
189 private Long landLineNumber;
194 private Long mobileNumber;
197 * Mobile number's carrier
199 private MobileProvider mobileProvider;
202 * Personal academicTitle
204 private PersonalTitle personalTitle;
207 * A list of all selectable contacts
209 private List<Contact> selectableContacts;
214 private String street;
217 * Regular user controller
220 private PizzaUserWebSessionController userController;
223 * Login bean (controller)
226 private PizzaUserLoginWebSessionController userLoginController;
231 private Integer zipCode;
234 * Default constructor
236 public PizzaContactWebSessionBean () {
237 // Call super constructor
241 this.contactList = new LinkedList<>();
242 this.emailAddressList = new LinkedList<>();
246 * Observes events being fired when an administrator has added a new
249 * @param event Event being fired
251 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
252 // The event must be valid
255 throw new NullPointerException("event is null"); //NOI18N
256 } else if (event.getAddedContact() == null) {
258 throw new NullPointerException("event.addedContact is null"); //NOI18N
259 } else if (event.getAddedContact().getContactId() == null) {
261 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
262 } else if (event.getAddedContact().getContactId() < 1) {
264 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
271 this.uniqueAddContact(event.getAddedContact());
273 // Add to selectable contacts
274 this.selectableContacts.add(event.getAddedContact());
278 * Event observer for newly added users by administrator
280 * @param event Event being fired
282 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
283 // event should not be null
286 throw new NullPointerException("event is null"); //NOI18N
287 } else if (event.getAddedUser() == null) {
289 throw new NullPointerException("event.addedUser is null"); //NOI18N
290 } else if (event.getAddedUser().getUserId() == null) {
292 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
293 } else if (event.getAddedUser().getUserId() < 1) {
295 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
303 * Observes events being fired when an administrator has linked a new user
304 * with existing contact data.
306 * @param event Event being fired
308 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
309 // event should not be null
312 throw new NullPointerException("event is null"); //NOI18N
313 } else if (event.getLinkedUser() == null) {
315 throw new NullPointerException("event.linkedUser is null"); //NOI18N
316 } else if (event.getLinkedUser().getUserContact() == null) {
318 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
319 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
321 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
322 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
324 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
327 // Remove contact from list available contacts list
328 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
335 * Event observer for updated contact data by administrators
337 * @param event Updated contact data event
339 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
340 // event should not be null
343 throw new NullPointerException("event is null"); //NOI18N
344 } else if (event.getUpdatedContact() == null) {
346 throw new NullPointerException("event.updatedContact is null"); //NOI18N
347 } else if (event.getUpdatedContact().getContactId() == null) {
349 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
350 } else if (event.getUpdatedContact().getContactId() < 1) {
352 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
355 // Add contact instance only once
356 this.uniqueAddContact(event.getUpdatedContact());
358 // Add email address to list
359 this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress());
363 * Event observer when user confirmed account.
365 * @param event Event being fired
367 public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
368 // event should not be null
371 throw new NullPointerException("event is null"); //NOI18N
372 } else if (event.getConfirmedUser() == null) {
374 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
375 } else if (event.getConfirmedUser().getUserId() == null) {
377 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
378 } else if (event.getConfirmedUser().getUserId() < 1) {
380 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
383 // Add contact instance only once
384 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
388 * Event observer for logged-in user
390 * @param event Event instance
392 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
393 // event should not be null
396 throw new NullPointerException("event is null"); //NOI18N
397 } else if (event.getLoggedInUser() == null) {
399 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
400 } else if (event.getLoggedInUser().getUserId() == null) {
402 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
403 } else if (event.getLoggedInUser().getUserId() < 1) {
405 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
408 // Copy all data to this bean
409 this.copyContact(event.getLoggedInUser().getUserContact());
413 * Event observer for new user registrations
415 * @param event User registration event
417 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
418 // event should not be null
421 throw new NullPointerException("event is null"); //NOI18N
422 } else if (event.getRegisteredUser() == null) {
424 throw new NullPointerException("event.registeredUser is null"); //NOI18N
425 } else if (event.getRegisteredUser().getUserId() == null) {
427 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
428 } else if (event.getRegisteredUser().getUserId() < 1) {
430 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
434 Contact registeredContact = event.getRegisteredUser().getUserContact();
436 // Copy all data from registered->user
437 this.copyContact(registeredContact);
439 // Add contact instance only once
440 this.uniqueAddContact(registeredContact);
442 // Add user name and email address
443 this.addUserNameEmailAddress(registeredContact);
450 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
451 public List<Contact> allContacts () {
452 // Return un-modified list
453 return this.contactList;
457 public void clearEmailAddresses () {
459 this.setEmailAddress(null);
460 this.setEmailAddressRepeat(null);
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
471 // Required personal data must be set
472 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
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());
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());
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) {
496 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
497 } else if (phone.getPhoneAreaCode() < 1) {
499 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
500 } else if (phone.getPhoneNumber() == null) {
502 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
503 } else if (phone.getPhoneNumber() < 1) {
505 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
509 contact.setContactLandLineNumber(phone);
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) {
517 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
518 } else if (fax.getPhoneAreaCode() < 1) {
520 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
521 } else if (fax.getPhoneNumber() == null) {
523 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
524 } else if (fax.getPhoneNumber() < 1) {
526 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
530 contact.setContactFaxNumber(fax);
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) {
538 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
539 } else if (mobile.getPhoneNumber() < 1) {
541 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
545 contact.setContactMobileNumber(mobile);
553 public String doChangePersonalContactData () {
554 // This method shall only be called if the user is logged-in
555 if (!this.userLoginController.isUserLoggedIn()) {
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
567 // Get contact instance
568 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
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
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());
586 // Update contact's mobile number
587 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
589 // Update contact's land-line number
590 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
592 // Update contact's fax number
593 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
595 // Send it to the EJB
596 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
599 return "contact_data_saved"; //NOI18N
603 * Getter for academic title
605 * @return Academic title
607 public String getAcademicTitle () {
608 return this.academicTitle;
612 * Setter for academic title
614 * @param academicTitle Academic title
616 public void setAcademicTitle (final String academicTitle) {
617 this.academicTitle = academicTitle;
621 * Getter for birth day
625 @SuppressWarnings ("ReturnOfDateField")
626 public Date getBirthday () {
627 return this.birthday;
631 * Setter for birth day
633 * @param birthday Birth day
635 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
636 public void setBirthday (final Date birthday) {
637 this.birthday = birthday;
641 * Getter for city name
645 public String getCity () {
650 * Setter for city name
652 * @param city City name
654 public void setCity (final String city) {
659 * Getter for comments
663 public String getComment () {
670 * @param comment Comments
672 public void setComment (final String comment) {
673 this.comment = comment;
677 public String getControllerType () {
678 return "general"; //NOI18N
683 public void setControllerType (final String controllerType) {
684 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
688 * Getter for country instance
690 * @return Country instance
692 public Country getCountry () {
697 * Setter for country instance
699 * @param country Country instance
701 public void setCountry (final Country country) {
702 this.country = country;
706 public String getEmailAddress () {
707 return this.emailAddress;
711 * Setter for email address
713 * @param emailAddress Email address
715 public void setEmailAddress (final String emailAddress) {
716 this.emailAddress = emailAddress;
720 * Getter for email address, repeated
722 * @return the emailAddress, repeated
724 public String getEmailAddressRepeat () {
725 return this.emailAddressRepeat;
729 * Setter for email address repeated
731 * @param emailAddressRepeat the emailAddress to set
733 public void setEmailAddressRepeat (final String emailAddressRepeat) {
734 this.emailAddressRepeat = emailAddressRepeat;
740 * @return the familyName
742 public String getFamilyName () {
743 return this.familyName;
749 * @param familyName the familyName to set
751 public void setFamilyName (final String familyName) {
752 this.familyName = familyName;
756 * Getter for fax number's area code
758 * @return Fax number's area code
760 public Integer getFaxAreaCode () {
761 return this.faxAreaCode;
765 * Setter for fax number's area code
767 * @param faxAreaCode Fax number's area code
769 public void setFaxAreaCode (final Integer faxAreaCode) {
770 this.faxAreaCode = faxAreaCode;
774 * Getter for fax's country instance
776 * @return Fax' country instance
778 public Country getFaxCountry () {
779 return this.faxCountry;
783 * Setter for fax's country instance
785 * @param faxCountry Fax' country instance
787 public void setFaxCountry (final Country faxCountry) {
788 this.faxCountry = faxCountry;
792 * Getter for fax number
796 public Long getFaxNumber () {
797 return this.faxNumber;
801 * Setter for fax number
803 * @param faxNumber Fax number
805 public void setFaxNumber (final Long faxNumber) {
806 this.faxNumber = faxNumber;
812 * @return the first name
814 public String getFirstName () {
815 return this.firstName;
821 * @param firstName the first name to set
823 public void setFirstName (final String firstName) {
824 this.firstName = firstName;
830 * @return the houseNumber
832 public Short getHouseNumber () {
833 return this.houseNumber;
839 * @param houseNumber the houseNumber to set
841 public void setHouseNumber (final Short houseNumber) {
842 this.houseNumber = houseNumber;
846 * Getter for house number extension, example: 123a 'a' is then the
847 * extension and 123 is the house number.
849 * @return House number extension
851 public String getHouseNumberExtension () {
852 return this.houseNumberExtension;
856 * Setter for house number extension
858 * @param houseNumberExtension House number extension
860 public void setHouseNumberExtension (final String houseNumberExtension) {
861 this.houseNumberExtension = houseNumberExtension;
865 * Getter for land-line number's area code
867 * @return Land-line number's area code
869 public Integer getLandLineAreaCode () {
870 return this.landLineAreaCode;
874 * Setter for land-line number's area code
876 * @param landLineAreaCode Land-line number's area code
878 public void setLandLineAreaCode (final Integer landLineAreaCode) {
879 this.landLineAreaCode = landLineAreaCode;
883 * Getter for land-line number's country instance
885 * @return Land-line number's country instance
887 public Country getLandLineCountry () {
888 return this.landLineCountry;
892 * Setter for land-line number's country instance
894 * @param landLineCountry Land-line number's country instance
896 public void setLandLineCountry (final Country landLineCountry) {
897 this.landLineCountry = landLineCountry;
901 * Getter for land-line number
903 * @return Land-line number
905 public Long getLandLineNumber () {
906 return this.landLineNumber;
910 * Setter for land-line number
912 * @param landLineNumber Land-line number
914 public void setLandLineNumber (final Long landLineNumber) {
915 this.landLineNumber = landLineNumber;
919 * Getter for mobile number
921 * @return Mobile number
923 public Long getMobileNumber () {
924 return this.mobileNumber;
928 * Setter for mobile number
930 * @param mobileNumber Mobile number
932 public void setMobileNumber (final Long mobileNumber) {
933 this.mobileNumber = mobileNumber;
937 * Getter for mobile number's carrier
939 * @return Mobile number's carrier
941 public MobileProvider getMobileProvider () {
942 return this.mobileProvider;
946 * Setter for mobile number's provider
948 * @param mobileProvider Mobile number's provider
950 public void setMobileProvider (final MobileProvider mobileProvider) {
951 this.mobileProvider = mobileProvider;
955 * Getter for personal title
957 * @return Personal title
959 public PersonalTitle getPersonalTitle () {
960 return this.personalTitle;
964 * Setter for personal title
966 * @param personalTitle Personal title
968 public void setPersonalTitle (final PersonalTitle personalTitle) {
969 this.personalTitle = personalTitle;
977 public String getStreet () {
984 * @param street Street
986 public void setStreet (final String street) {
987 this.street = street;
991 * Getter for ZIP code
995 public Integer getZipCode () {
1000 * Setter for ZIP code
1002 * @param zipCode ZIP code
1004 public void setZipCode (final Integer zipCode) {
1005 this.zipCode = zipCode;
1009 * Post-construction method
1012 public void init () {
1015 // Get initial context
1016 Context context = new InitialContext();
1019 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
1020 } catch (final NamingException e) {
1022 throw new FaceletException(e);
1025 // Get full email address list for reducing EJB calls
1026 this.emailAddressList.addAll(this.contactBean.getEmailAddressList());
1029 List<Contact> allContacts = this.contactBean.getAllContacts();
1031 // Get full contact list
1032 this.contactList.addAll(allContacts);
1035 List<User> allUsers = this.userController.allUsers();
1038 Iterator<Contact> iterator = allContacts.iterator();
1041 while (iterator.hasNext()) {
1043 Contact next = iterator.next();
1046 Iterator<User> userIterator = allUsers.iterator();
1048 // Loop through all users
1049 while (userIterator.hasNext()) {
1050 // Get user instance
1051 User nextUser = userIterator.next();
1054 if (Objects.equals(next, nextUser.getUserContact())) {
1063 this.selectableContacts = allContacts;
1067 public boolean isEmailAddressRegistered (final Contact contact) {
1069 if (null == contact) {
1071 throw new NullPointerException("contact is null"); //NOI18N
1072 } else if (contact.getContactEmailAddress() == null) {
1074 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1075 } else if (contact.getContactEmailAddress().isEmpty()) {
1077 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1081 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress())));
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));
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));
1109 public boolean isSameEmailAddressEntered () {
1110 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1114 public Contact lookupContactById (final Long contactId) throws ContactNotFoundException {
1116 Contact localContact = null;
1121 // Try to lookup it in visible user list
1122 for (final Iterator<Contact> iterator = this.contactList.iterator(); iterator.hasNext();) {
1124 Contact next = iterator.next();
1126 // Is the user id found?
1127 if (Objects.equals(next.getContactId(), contactId)) {
1128 // Copy to other variable
1129 localContact = next;
1134 // Is it still null?
1135 if (null == localContact) {
1136 // Not visible for the current user
1137 throw new ContactNotFoundException(contactId);
1140 // Copy all data to this bean
1141 this.copyContact(localContact);
1144 return localContact;
1148 public List<Contact> selectableContacts () {
1149 return Collections.unmodifiableList(this.selectableContacts);
1153 public void updateContactDataFromController (final Contact contact) {
1154 // Is the instance valid?
1155 if (null == contact) {
1157 throw new NullPointerException("contact is null"); //NOI18N
1158 } else if (contact.getContactId() == null) {
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
1167 this.copyContact(contact);
1171 * Adds email address to bean's internal list.
1173 * @param contact Contact instance
1175 private void addUserNameEmailAddress (final Contact contact) {
1176 // Make sure the entry is not added yet
1177 if (this.emailAddressList.contains(contact.getContactEmailAddress())) {
1179 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1183 this.emailAddressList.add(contact.getContactEmailAddress());
1189 private void clear () {
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);
1201 this.setCountry(null);
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);
1215 this.setBirthday(null);
1216 this.setComment(null);
1220 * Copies given contact into the controller
1222 * @param contact Contact instance
1224 private void copyContact (final Contact contact) {
1225 // Is the instance valid?
1226 if (null == contact) {
1228 throw new NullPointerException("contact is null"); //NOI18N
1229 } else if (contact.getContactId() == null) {
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
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());
1253 // Get mobile, phone and fax instance
1254 DialableMobileNumber mobile = contact.getContactMobileNumber();
1255 DialableFaxNumber fax = contact.getContactFaxNumber();
1256 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1259 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1260 this.setLandLineCountry(phone.getPhoneCountry());
1261 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1262 this.setLandLineNumber(phone.getPhoneNumber());
1265 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1266 this.setMobileProvider(mobile.getMobileProvider());
1267 this.setMobileNumber(mobile.getPhoneNumber());
1270 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1271 this.setFaxCountry(fax.getPhoneCountry());
1272 this.setFaxAreaCode(fax.getPhoneAreaCode());
1273 this.setFaxNumber(fax.getPhoneNumber());
1278 * Removes given contact from all lists
1280 * @param contact Contact instance to remove
1282 private void removeContact (final Contact contact) {
1283 // Is the instance valid?
1284 if (null == contact) {
1286 throw new NullPointerException("contact is null"); //NOI18N
1287 } else if (contact.getContactId() == null) {
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
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
1301 // Remove from other lists
1302 this.emailAddressList.remove(contact.getContactEmailAddress());
1306 * Adds unique instance to contact list. First any existing instance is
1307 * being removed, then the new instance is added.
1309 * @param contact Contact instance to add uniquely
1311 private void uniqueAddContact (final Contact contact) {
1312 // Is the instance valid?
1313 if (null == contact) {
1315 throw new NullPointerException("contact is null"); //NOI18N
1316 } else if (contact.getContactId() == null) {
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
1324 // Get iterator from list
1325 Iterator<Contact> iterator = this.contactList.iterator();
1327 // "Walk" through all entries
1328 while (iterator.hasNext()) {
1330 Contact next = iterator.next();
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);
1340 // Add contact to list
1341 this.contactList.add(contact);