2 * Copyright (C) 2016 - 2022 Free Software Foundation
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 fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.context.RequestScoped;
30 import javax.enterprise.event.Observes;
31 import javax.faces.view.facelets.FaceletException;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
35 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
36 import org.mxchange.jcontacts.model.contact.Contact;
37 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
38 import org.mxchange.jcontacts.model.contact.UserContact;
39 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
40 import org.mxchange.jcontacts.model.utils.ContactUtils;
41 import org.mxchange.jcountry.model.data.Country;
42 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
43 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
44 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
45 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
46 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
47 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
48 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
49 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
50 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
51 import org.mxchange.jusercore.model.user.User;
52 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
53 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
54 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
55 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
56 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
57 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebRequestController;
58 import org.mxchange.pizzaapplication.beans.user.login.PizzaUserLoginWebSessionController;
61 * A general contact bean (controller)
63 * @author Roland Häder<roland@mxchange.org>
65 @Named ("contactController")
67 public class PizzaContactWebRequestBean extends BasePizzaBean implements PizzaContactWebRequestController {
72 private static final long serialVersionUID = 542_145_347_916L;
75 * Academic academicTitle
77 private String academicTitle;
82 private Date birthday;
92 private String comment;
95 * EJB for general contact purposes
97 @EJB (lookup = "java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
98 private ContactSessionBeanRemote contactBean;
104 @NamedCache (cacheName = "contactsCache")
105 private Cache<Long, Contact> contactsCache;
110 private Country country;
115 private String emailAddress;
121 @NamedCache (cacheName = "emailAddressCache")
122 private Cache<Long, String> emailAddressCache;
125 * Email address repeated
127 private String emailAddressRepeat;
132 private String familyName;
135 * Fax number's area code
137 private Integer faxAreaCode;
140 * Country instance for fax number
142 private Country faxCountry;
147 private Long faxNumber;
152 private String firstName;
157 private Short houseNumber;
160 * House number extension
162 private String houseNumberExtension;
165 * Whether a fax entry has been unlinked
167 private boolean isFaxUnlinked;
170 * Whether a land-line number has been unlinked
172 private boolean isLandLineUnlinked;
175 * Whether a mobile entry has been unlinked
177 private boolean isMobileUnlinked;
180 * Phone number area code
182 private Integer landLineAreaCode;
185 * Country instance for phone number
187 private Country landLineCountry;
192 private Long landLineNumber;
197 private Long mobileNumber;
200 * Mobile number's carrier
202 private MobileProvider mobileProvider;
205 * Personal academicTitle
207 private PersonalTitle personalTitle;
210 * A list of all selectable contacts
213 @NamedCache (cacheName = "selectableContactsCache")
214 private Cache<Long, Contact> selectableContactsCache;
219 private String street;
222 * Regular user controller
225 private PizzaUserWebRequestController userController;
228 * Login bean (controller)
231 private PizzaUserLoginWebSessionController userLoginController;
236 private Integer zipCode;
239 * Default constructor
241 public PizzaContactWebRequestBean () {
242 // Call super constructor
247 * Observes events being fired when an administrator has added a new
250 * @param event Event being fired
252 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
253 // The event must be valid
256 throw new NullPointerException("event is null"); //NOI18N
257 } else if (event.getAddedContact() == null) {
259 throw new NullPointerException("event.addedContact is null"); //NOI18N
260 } else if (event.getAddedContact().getContactId() == null) {
262 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
263 } else if (event.getAddedContact().getContactId() < 1) {
265 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
272 this.uniqueAddContact(event.getAddedContact());
274 // Add to selectable contacts
275 this.selectableContactsCache.put(event.getAddedContact().getContactId(), event.getAddedContact());
279 * Event observer for newly added users by administrator
281 * @param event Event being fired
283 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
284 // event should not be null
287 throw new NullPointerException("event is null"); //NOI18N
288 } else if (event.getAddedUser() == null) {
290 throw new NullPointerException("event.addedUser is null"); //NOI18N
291 } else if (event.getAddedUser().getUserId() == null) {
293 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
294 } else if (event.getAddedUser().getUserId() < 1) {
296 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
304 * Observes events being fired when an administrator has linked a new user
305 * with existing contact data.
307 * @param event Event being fired
309 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
310 // event should not be null
313 throw new NullPointerException("event is null"); //NOI18N
314 } else if (event.getLinkedUser() == null) {
316 throw new NullPointerException("event.linkedUser is null"); //NOI18N
317 } else if (event.getLinkedUser().getUserContact() == null) {
319 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
320 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
322 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
323 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
325 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
328 // Remove contact from list available contacts list
329 this.selectableContactsCache.remove(event.getLinkedUser().getUserContact().getContactId());
336 * Event observer for updated contact data by administrators
338 * @param event Updated contact data event
340 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
341 // event should not be null
344 throw new NullPointerException("event is null"); //NOI18N
345 } else if (event.getUpdatedContact() == null) {
347 throw new NullPointerException("event.updatedContact is null"); //NOI18N
348 } else if (event.getUpdatedContact().getContactId() == null) {
350 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
351 } else if (event.getUpdatedContact().getContactId() < 1) {
353 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
356 // Add contact instance only once
357 this.uniqueAddContact(event.getUpdatedContact());
359 // Add email address to list
360 this.emailAddressCache.put(event.getUpdatedContact().getContactId(), event.getUpdatedContact().getContactEmailAddress());
364 * Event observer when user confirmed account.
366 * @param event Event being fired
368 public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
369 // event should not be null
372 throw new NullPointerException("event is null"); //NOI18N
373 } else if (event.getConfirmedUser() == null) {
375 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
376 } else if (event.getConfirmedUser().getUserId() == null) {
378 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
379 } else if (event.getConfirmedUser().getUserId() < 1) {
381 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
384 // Add contact instance only once
385 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
389 * Event observer for logged-in user
391 * @param event Event instance
393 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
394 // event should not be null
397 throw new NullPointerException("event is null"); //NOI18N
398 } else if (event.getLoggedInUser() == null) {
400 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
401 } else if (event.getLoggedInUser().getUserId() == null) {
403 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
404 } else if (event.getLoggedInUser().getUserId() < 1) {
406 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
409 // Copy all data to this bean
410 this.copyContact(event.getLoggedInUser().getUserContact());
414 * Event observer for new user registrations
416 * @param event User registration event
418 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
419 // event should not be null
422 throw new NullPointerException("event is null"); //NOI18N
423 } else if (event.getRegisteredUser() == null) {
425 throw new NullPointerException("event.registeredUser is null"); //NOI18N
426 } else if (event.getRegisteredUser().getUserId() == null) {
428 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
429 } else if (event.getRegisteredUser().getUserId() < 1) {
431 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
435 final Contact registeredContact = event.getRegisteredUser().getUserContact();
437 // Copy all data from registered->user
438 this.copyContact(registeredContact);
440 // Add contact instance only once
441 this.uniqueAddContact(registeredContact);
443 // Add user name and email address
444 this.addUserNameEmailAddress(registeredContact);
451 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
452 public List<Contact> allContacts () {
454 final List<Contact> list = new LinkedList<>();
457 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
460 while (iterator.hasNext()) {
462 final Cache.Entry<Long, Contact> next = iterator.next();
465 list.add(next.getValue());
473 public void clearEmailAddresses () {
475 this.setEmailAddress(null);
476 this.setEmailAddressRepeat(null);
480 public Contact createContactInstance () {
481 // Is all required data set?
482 if (!this.isRequiredPersonalDataSet()) {
483 // No, then abort here
484 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
487 // Required personal data must be set
488 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
490 // Generate phone number
491 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
492 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
493 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
495 // Create new contact
496 final Contact contact = new UserContact(
497 this.getPersonalTitle(),
499 this.getFamilyName(),
503 // Don't set null or wrong references
504 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
505 // Now the number must be given
506 if (fax.getPhoneAreaCode() == null) {
508 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
509 } else if (fax.getPhoneAreaCode() < 1) {
511 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
512 } else if (fax.getPhoneNumber() == null) {
514 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
515 } else if (fax.getPhoneNumber() < 1) {
517 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
521 contact.setContactFaxNumber(fax);
524 // Is the provider set?
525 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
526 // Is the number set?
527 if (mobile.getPhoneNumber() == null) {
529 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
530 } else if (mobile.getPhoneNumber() < 1) {
532 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
536 contact.setContactMobileNumber(mobile);
544 public String doChangePersonalContactData () {
545 // This method shall only be called if the user is logged-in
546 if (!this.userLoginController.isUserLoggedIn()) {
548 throw new IllegalStateException("User is not logged-in"); //NOI18N
549 } else if (!this.isRequiredChangePersonalDataSet()) {
550 // Not all required fields are set
551 throw new FaceletException("Not all required fields are set."); //NOI18N
552 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
553 // Password not matching
554 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
558 // Get contact instance
559 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
561 // It should be there, so run some tests on it
562 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
563 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
564 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
567 contact.setContactPersonalTitle(this.getPersonalTitle());
568 contact.setContactFirstName(this.getFirstName());
569 contact.setContactFamilyName(this.getFamilyName());
570 contact.setContactStreet(this.getStreet());
571 contact.setContactHouseNumber(this.getHouseNumber());
572 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
573 contact.setContactZipCode(this.getZipCode());
574 contact.setContactCity(this.getCity());
575 contact.setContactCountry(this.getCountry());
577 // Update contact's mobile number
578 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
580 // Update contact's land-line number
581 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
583 // Update contact's fax number
584 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
586 // Send it to the EJB
587 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
590 return "contact_data_saved"; //NOI18N
594 * Getter for academic title
596 * @return Academic title
598 public String getAcademicTitle () {
599 return this.academicTitle;
603 * Setter for academic title
605 * @param academicTitle Academic title
607 public void setAcademicTitle (final String academicTitle) {
608 this.academicTitle = academicTitle;
612 * Getter for birth day
616 @SuppressWarnings ("ReturnOfDateField")
617 public Date getBirthday () {
618 return this.birthday;
622 * Setter for birth day
624 * @param birthday Birth day
626 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
627 public void setBirthday (final Date birthday) {
628 this.birthday = birthday;
632 * Getter for city name
636 public String getCity () {
641 * Setter for city name
643 * @param city City name
645 public void setCity (final String city) {
650 * Getter for comments
654 public String getComment () {
661 * @param comment Comments
663 public void setComment (final String comment) {
664 this.comment = comment;
668 public String getControllerType () {
669 return "general"; //NOI18N
674 public void setControllerType (final String controllerType) {
675 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
679 * Getter for country instance
681 * @return Country instance
683 public Country getCountry () {
688 * Setter for country instance
690 * @param country Country instance
692 public void setCountry (final Country country) {
693 this.country = country;
697 public String getEmailAddress () {
698 return this.emailAddress;
702 * Setter for email address
704 * @param emailAddress Email address
706 public void setEmailAddress (final String emailAddress) {
707 this.emailAddress = emailAddress;
711 * Getter for email address, repeated
713 * @return the emailAddress, repeated
715 public String getEmailAddressRepeat () {
716 return this.emailAddressRepeat;
720 * Setter for email address repeated
722 * @param emailAddressRepeat the emailAddress to set
724 public void setEmailAddressRepeat (final String emailAddressRepeat) {
725 this.emailAddressRepeat = emailAddressRepeat;
731 * @return the familyName
733 public String getFamilyName () {
734 return this.familyName;
740 * @param familyName the familyName to set
742 public void setFamilyName (final String familyName) {
743 this.familyName = familyName;
747 * Getter for fax number's area code
749 * @return Fax number's area code
751 public Integer getFaxAreaCode () {
752 return this.faxAreaCode;
756 * Setter for fax number's area code
758 * @param faxAreaCode Fax number's area code
760 public void setFaxAreaCode (final Integer faxAreaCode) {
761 this.faxAreaCode = faxAreaCode;
765 * Getter for fax's country instance
767 * @return Fax' country instance
769 public Country getFaxCountry () {
770 return this.faxCountry;
774 * Setter for fax's country instance
776 * @param faxCountry Fax' country instance
778 public void setFaxCountry (final Country faxCountry) {
779 this.faxCountry = faxCountry;
783 * Getter for fax number
787 public Long getFaxNumber () {
788 return this.faxNumber;
792 * Setter for fax number
794 * @param faxNumber Fax number
796 public void setFaxNumber (final Long faxNumber) {
797 this.faxNumber = faxNumber;
803 * @return the first name
805 public String getFirstName () {
806 return this.firstName;
812 * @param firstName the first name to set
814 public void setFirstName (final String firstName) {
815 this.firstName = firstName;
821 * @return the houseNumber
823 public Short getHouseNumber () {
824 return this.houseNumber;
830 * @param houseNumber the houseNumber to set
832 public void setHouseNumber (final Short houseNumber) {
833 this.houseNumber = houseNumber;
837 * Getter for house number extension, example: 123a 'a' is then the
838 * extension and 123 is the house number.
840 * @return House number extension
842 public String getHouseNumberExtension () {
843 return this.houseNumberExtension;
847 * Setter for house number extension
849 * @param houseNumberExtension House number extension
851 public void setHouseNumberExtension (final String houseNumberExtension) {
852 this.houseNumberExtension = houseNumberExtension;
856 * Getter for land-line number's area code
858 * @return Land-line number's area code
860 public Integer getLandLineAreaCode () {
861 return this.landLineAreaCode;
865 * Setter for land-line number's area code
867 * @param landLineAreaCode Land-line number's area code
869 public void setLandLineAreaCode (final Integer landLineAreaCode) {
870 this.landLineAreaCode = landLineAreaCode;
874 * Getter for land-line number's country instance
876 * @return Land-line number's country instance
878 public Country getLandLineCountry () {
879 return this.landLineCountry;
883 * Setter for land-line number's country instance
885 * @param landLineCountry Land-line number's country instance
887 public void setLandLineCountry (final Country landLineCountry) {
888 this.landLineCountry = landLineCountry;
892 * Getter for land-line number
894 * @return Land-line number
896 public Long getLandLineNumber () {
897 return this.landLineNumber;
901 * Setter for land-line number
903 * @param landLineNumber Land-line number
905 public void setLandLineNumber (final Long landLineNumber) {
906 this.landLineNumber = landLineNumber;
910 * Getter for mobile number
912 * @return Mobile number
914 public Long getMobileNumber () {
915 return this.mobileNumber;
919 * Setter for mobile number
921 * @param mobileNumber Mobile number
923 public void setMobileNumber (final Long mobileNumber) {
924 this.mobileNumber = mobileNumber;
928 * Getter for mobile number's carrier
930 * @return Mobile number's carrier
932 public MobileProvider getMobileProvider () {
933 return this.mobileProvider;
937 * Setter for mobile number's provider
939 * @param mobileProvider Mobile number's provider
941 public void setMobileProvider (final MobileProvider mobileProvider) {
942 this.mobileProvider = mobileProvider;
946 * Getter for personal title
948 * @return Personal title
950 public PersonalTitle getPersonalTitle () {
951 return this.personalTitle;
955 * Setter for personal title
957 * @param personalTitle Personal title
959 public void setPersonalTitle (final PersonalTitle personalTitle) {
960 this.personalTitle = personalTitle;
968 public String getStreet () {
975 * @param street Street
977 public void setStreet (final String street) {
978 this.street = street;
982 * Getter for ZIP code
986 public Integer getZipCode () {
991 * Setter for ZIP code
993 * @param zipCode ZIP code
995 public void setZipCode (final Integer zipCode) {
996 this.zipCode = zipCode;
1000 * Post-construction method
1003 public void init () {
1005 if (!this.contactsCache.iterator().hasNext()) {
1007 final List<Contact> contacts = this.contactBean.allContacts();
1010 for (final Contact contact : contacts) {
1012 this.contactsCache.put(contact.getContactId(), contact);
1013 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1015 } else if (this.selectableContactsCache.iterator().hasNext()) {
1016 // Has already entries, avoid executing below code
1021 final List<User> allUsers = this.userController.allUsers();
1023 // Get iterator from contacts cache
1024 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1026 // Loop through all contacts
1027 while (iterator.hasNext()) {
1029 final Cache.Entry<Long, Contact> next = iterator.next();
1031 // Default is not found
1032 boolean isFound = false;
1034 // User list is not empty, check each entry, if contact is found
1035 for (final User user : allUsers) {
1036 // Is the contact the same?
1037 if (Objects.equals(user.getUserContact(), next.getValue())) {
1044 // Is contact not found?
1046 // Add it as selectable
1047 this.selectableContactsCache.put(next.getKey(), next.getValue());
1053 public boolean isEmailAddressRegistered (final Contact contact) {
1055 if (null == contact) {
1057 throw new NullPointerException("contact is null"); //NOI18N
1058 } else if (contact.getContactEmailAddress() == null) {
1060 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1061 } else if (contact.getContactEmailAddress().isEmpty()) {
1063 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1067 return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId())));
1071 public boolean isRequiredChangePersonalDataSet () {
1072 return ((this.getPersonalTitle() != null) &&
1073 (this.getFirstName() != null) &&
1074 (this.getFamilyName() != null) &&
1075 (this.getStreet() != null) &&
1076 (this.getHouseNumber() != null) &&
1077 (this.getZipCode() != null) &&
1078 (this.getCity() != null));
1082 public boolean isRequiredPersonalDataSet () {
1083 return ((this.getPersonalTitle() != null) &&
1084 (this.getFirstName() != null) &&
1085 (this.getFamilyName() != null) &&
1086 (this.getStreet() != null) &&
1087 (this.getHouseNumber() != null) &&
1088 (this.getZipCode() != null) &&
1089 (this.getCity() != null) &&
1090 (this.getEmailAddress() != null) &&
1091 (this.getEmailAddressRepeat() != null));
1095 public boolean isSameEmailAddressEntered () {
1096 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1100 * Returns a list of all selectable contacts for user creation. Contacts
1101 * from already existing users are excluded in this list.
1103 * @return A list of all selectable contacts
1105 public List<Contact> selectableContacts () {
1107 final List<Contact> selectableContacts = new LinkedList<>();
1109 // Get iterator from cache
1110 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1112 // Loop through all contacts
1113 while (iterator.hasNext()) {
1115 final Cache.Entry<Long, Contact> next = iterator.next();
1117 // Add entry's value to list
1118 selectableContacts.add(next.getValue());
1122 return selectableContacts;
1126 public void updateContactDataFromController (final Contact contact) {
1127 // Is the instance valid?
1128 if (null == contact) {
1130 throw new NullPointerException("contact is null"); //NOI18N
1131 } else if (contact.getContactId() == null) {
1133 throw new NullPointerException("contact.contactId is null"); //NOI18N
1134 } else if (contact.getContactId() < 1) {
1135 // Not valid id number
1136 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1140 this.copyContact(contact);
1144 * Adds email address to bean's internal list.
1146 * @param contact Contact instance
1148 private void addUserNameEmailAddress (final Contact contact) {
1149 // Make sure the entry is not added yet
1150 if (this.emailAddressCache.containsKey(contact.getContactId())) {
1152 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1156 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1162 private void clear () {
1165 this.setPersonalTitle(null);
1166 this.setAcademicTitle(null);
1167 this.setFirstName(null);
1168 this.setFamilyName(null);
1169 this.setStreet(null);
1170 this.setHouseNumber(null);
1171 this.setHouseNumberExtension(null);
1172 this.setZipCode(null);
1174 this.setCountry(null);
1177 this.clearEmailAddresses();
1178 this.setLandLineAreaCode(null);
1179 this.setLandLineCountry(null);
1180 this.setLandLineNumber(null);
1181 this.setMobileProvider(null);
1182 this.setMobileNumber(null);
1183 this.setFaxAreaCode(null);
1184 this.setFaxCountry(null);
1185 this.setFaxNumber(null);
1188 this.setBirthday(null);
1189 this.setComment(null);
1193 * Copies given contact into the controller
1195 * @param contact Contact instance
1197 private void copyContact (final Contact contact) {
1198 // Is the instance valid?
1199 if (null == contact) {
1201 throw new NullPointerException("contact is null"); //NOI18N
1202 } else if (contact.getContactId() == null) {
1204 throw new NullPointerException("contact.contactId is null"); //NOI18N
1205 } else if (contact.getContactId() < 1) {
1206 // Not valid id number
1207 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1212 this.setPersonalTitle(contact.getContactPersonalTitle());
1213 this.setAcademicTitle(contact.getContactTitle());
1214 this.setFirstName(contact.getContactFirstName());
1215 this.setFamilyName(contact.getContactFamilyName());
1216 this.setStreet(contact.getContactStreet());
1217 this.setHouseNumber(contact.getContactHouseNumber());
1218 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1219 this.setZipCode(contact.getContactZipCode());
1220 this.setCity(contact.getContactCity());
1221 this.setCountry(contact.getContactCountry());
1222 this.setEmailAddress(contact.getContactEmailAddress());
1223 this.setBirthday(contact.getContactBirthday());
1224 this.setComment(contact.getContactComment());
1226 // Get mobile, phone and fax instance
1227 DialableMobileNumber mobile = contact.getContactMobileNumber();
1228 DialableFaxNumber fax = contact.getContactFaxNumber();
1229 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1232 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1233 this.setLandLineCountry(phone.getPhoneCountry());
1234 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1235 this.setLandLineNumber(phone.getPhoneNumber());
1238 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1239 this.setMobileProvider(mobile.getMobileProvider());
1240 this.setMobileNumber(mobile.getPhoneNumber());
1243 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1244 this.setFaxCountry(fax.getPhoneCountry());
1245 this.setFaxAreaCode(fax.getPhoneAreaCode());
1246 this.setFaxNumber(fax.getPhoneNumber());
1251 * Removes given contact from all lists
1253 * @param contact Contact instance to remove
1255 private void removeContact (final Contact contact) {
1256 // Is the instance valid?
1257 if (null == contact) {
1259 throw new NullPointerException("contact is null"); //NOI18N
1260 } else if (contact.getContactId() == null) {
1262 throw new NullPointerException("contact.contactId is null"); //NOI18N
1263 } else if (contact.getContactId() < 1) {
1264 // Not valid id number
1265 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1268 // Remove from general list
1269 if (!this.contactsCache.remove(contact.getContactId())) {
1270 // Did not remove contact
1271 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1274 // Remove from other lists
1275 this.emailAddressCache.remove(contact.getContactId());
1279 * Adds unique instance to contact list. First any existing instance is
1280 * being removed, then the new instance is added.
1282 * @param contact Contact instance to add uniquely
1284 private void uniqueAddContact (final Contact contact) {
1285 // Is the instance valid?
1286 if (null == contact) {
1288 throw new NullPointerException("contact is null"); //NOI18N
1289 } else if (contact.getContactId() == null) {
1291 throw new NullPointerException("contact.contactId is null"); //NOI18N
1292 } else if (contact.getContactId() < 1) {
1293 // Not valid id number
1294 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1297 // Get iterator from list
1298 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1300 // "Walk" through all entries
1301 while (iterator.hasNext()) {
1303 final Cache.Entry<Long, Contact> next = iterator.next();
1305 // Is id number the same?
1306 if (Objects.equals(contact.getContactId(), next.getKey())) {
1307 // Found entry, so remove it and abort
1308 this.removeContact(next.getValue());
1313 // Add contact to list
1314 this.contactsCache.put(contact.getContactId(), contact);