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(),
504 // Don't set null or wrong references
505 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
506 // Now the number must be given
507 if (fax.getPhoneAreaCode() == null) {
509 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
510 } else if (fax.getPhoneAreaCode() < 1) {
512 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
513 } else if (fax.getPhoneNumber() == null) {
515 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
516 } else if (fax.getPhoneNumber() < 1) {
518 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
522 contact.setContactFaxNumber(fax);
525 // Is the provider set?
526 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
527 // Is the number set?
528 if (mobile.getPhoneNumber() == null) {
530 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
531 } else if (mobile.getPhoneNumber() < 1) {
533 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
537 contact.setContactMobileNumber(mobile);
545 public String doChangePersonalContactData () {
546 // This method shall only be called if the user is logged-in
547 if (!this.userLoginController.isUserLoggedIn()) {
549 throw new IllegalStateException("User is not logged-in"); //NOI18N
550 } else if (!this.isRequiredChangePersonalDataSet()) {
551 // Not all required fields are set
552 throw new FaceletException("Not all required fields are set."); //NOI18N
553 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
554 // Password not matching
555 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
559 // Get contact instance
560 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
562 // It should be there, so run some tests on it
563 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
564 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
565 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
568 contact.setContactPersonalTitle(this.getPersonalTitle());
569 contact.setContactFirstName(this.getFirstName());
570 contact.setContactFamilyName(this.getFamilyName());
571 contact.setContactStreet(this.getStreet());
572 contact.setContactHouseNumber(this.getHouseNumber());
573 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
574 contact.setContactZipCode(this.getZipCode());
575 contact.setContactCity(this.getCity());
576 contact.setContactCountry(this.getCountry());
578 // Update contact's mobile number
579 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
581 // Update contact's land-line number
582 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
584 // Update contact's fax number
585 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
587 // Send it to the EJB
588 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
591 return "contact_data_saved"; //NOI18N
595 * Getter for academic title
597 * @return Academic title
599 public String getAcademicTitle () {
600 return this.academicTitle;
604 * Setter for academic title
606 * @param academicTitle Academic title
608 public void setAcademicTitle (final String academicTitle) {
609 this.academicTitle = academicTitle;
613 * Getter for birth day
617 @SuppressWarnings ("ReturnOfDateField")
618 public Date getBirthday () {
619 return this.birthday;
623 * Setter for birth day
625 * @param birthday Birth day
627 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
628 public void setBirthday (final Date birthday) {
629 this.birthday = birthday;
633 * Getter for city name
637 public String getCity () {
642 * Setter for city name
644 * @param city City name
646 public void setCity (final String city) {
651 * Getter for comments
655 public String getComment () {
662 * @param comment Comments
664 public void setComment (final String comment) {
665 this.comment = comment;
669 public String getControllerType () {
670 return "general"; //NOI18N
675 public void setControllerType (final String controllerType) {
676 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
680 * Getter for country instance
682 * @return Country instance
684 public Country getCountry () {
689 * Setter for country instance
691 * @param country Country instance
693 public void setCountry (final Country country) {
694 this.country = country;
698 public String getEmailAddress () {
699 return this.emailAddress;
703 * Setter for email address
705 * @param emailAddress Email address
707 public void setEmailAddress (final String emailAddress) {
708 this.emailAddress = emailAddress;
712 * Getter for email address, repeated
714 * @return the emailAddress, repeated
716 public String getEmailAddressRepeat () {
717 return this.emailAddressRepeat;
721 * Setter for email address repeated
723 * @param emailAddressRepeat the emailAddress to set
725 public void setEmailAddressRepeat (final String emailAddressRepeat) {
726 this.emailAddressRepeat = emailAddressRepeat;
732 * @return the familyName
734 public String getFamilyName () {
735 return this.familyName;
741 * @param familyName the familyName to set
743 public void setFamilyName (final String familyName) {
744 this.familyName = familyName;
748 * Getter for fax number's area code
750 * @return Fax number's area code
752 public Integer getFaxAreaCode () {
753 return this.faxAreaCode;
757 * Setter for fax number's area code
759 * @param faxAreaCode Fax number's area code
761 public void setFaxAreaCode (final Integer faxAreaCode) {
762 this.faxAreaCode = faxAreaCode;
766 * Getter for fax's country instance
768 * @return Fax' country instance
770 public Country getFaxCountry () {
771 return this.faxCountry;
775 * Setter for fax's country instance
777 * @param faxCountry Fax' country instance
779 public void setFaxCountry (final Country faxCountry) {
780 this.faxCountry = faxCountry;
784 * Getter for fax number
788 public Long getFaxNumber () {
789 return this.faxNumber;
793 * Setter for fax number
795 * @param faxNumber Fax number
797 public void setFaxNumber (final Long faxNumber) {
798 this.faxNumber = faxNumber;
804 * @return the first name
806 public String getFirstName () {
807 return this.firstName;
813 * @param firstName the first name to set
815 public void setFirstName (final String firstName) {
816 this.firstName = firstName;
822 * @return the houseNumber
824 public Short getHouseNumber () {
825 return this.houseNumber;
831 * @param houseNumber the houseNumber to set
833 public void setHouseNumber (final Short houseNumber) {
834 this.houseNumber = houseNumber;
838 * Getter for house number extension, example: 123a 'a' is then the
839 * extension and 123 is the house number.
841 * @return House number extension
843 public String getHouseNumberExtension () {
844 return this.houseNumberExtension;
848 * Setter for house number extension
850 * @param houseNumberExtension House number extension
852 public void setHouseNumberExtension (final String houseNumberExtension) {
853 this.houseNumberExtension = houseNumberExtension;
857 * Getter for land-line number's area code
859 * @return Land-line number's area code
861 public Integer getLandLineAreaCode () {
862 return this.landLineAreaCode;
866 * Setter for land-line number's area code
868 * @param landLineAreaCode Land-line number's area code
870 public void setLandLineAreaCode (final Integer landLineAreaCode) {
871 this.landLineAreaCode = landLineAreaCode;
875 * Getter for land-line number's country instance
877 * @return Land-line number's country instance
879 public Country getLandLineCountry () {
880 return this.landLineCountry;
884 * Setter for land-line number's country instance
886 * @param landLineCountry Land-line number's country instance
888 public void setLandLineCountry (final Country landLineCountry) {
889 this.landLineCountry = landLineCountry;
893 * Getter for land-line number
895 * @return Land-line number
897 public Long getLandLineNumber () {
898 return this.landLineNumber;
902 * Setter for land-line number
904 * @param landLineNumber Land-line number
906 public void setLandLineNumber (final Long landLineNumber) {
907 this.landLineNumber = landLineNumber;
911 * Getter for mobile number
913 * @return Mobile number
915 public Long getMobileNumber () {
916 return this.mobileNumber;
920 * Setter for mobile number
922 * @param mobileNumber Mobile number
924 public void setMobileNumber (final Long mobileNumber) {
925 this.mobileNumber = mobileNumber;
929 * Getter for mobile number's carrier
931 * @return Mobile number's carrier
933 public MobileProvider getMobileProvider () {
934 return this.mobileProvider;
938 * Setter for mobile number's provider
940 * @param mobileProvider Mobile number's provider
942 public void setMobileProvider (final MobileProvider mobileProvider) {
943 this.mobileProvider = mobileProvider;
947 * Getter for personal title
949 * @return Personal title
951 public PersonalTitle getPersonalTitle () {
952 return this.personalTitle;
956 * Setter for personal title
958 * @param personalTitle Personal title
960 public void setPersonalTitle (final PersonalTitle personalTitle) {
961 this.personalTitle = personalTitle;
969 public String getStreet () {
976 * @param street Street
978 public void setStreet (final String street) {
979 this.street = street;
983 * Getter for ZIP code
987 public Integer getZipCode () {
992 * Setter for ZIP code
994 * @param zipCode ZIP code
996 public void setZipCode (final Integer zipCode) {
997 this.zipCode = zipCode;
1001 * Post-construction method
1004 public void init () {
1006 if (!this.contactsCache.iterator().hasNext()) {
1008 final List<Contact> contacts = this.contactBean.allContacts();
1011 for (final Contact contact : contacts) {
1013 this.contactsCache.put(contact.getContactId(), contact);
1014 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1016 } else if (this.selectableContactsCache.iterator().hasNext()) {
1017 // Has already entries, avoid executing below code
1022 final List<User> allUsers = this.userController.allUsers();
1024 // Get iterator from contacts cache
1025 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1027 // Loop through all contacts
1028 while (iterator.hasNext()) {
1030 final Cache.Entry<Long, Contact> next = iterator.next();
1032 // Default is not found
1033 boolean isFound = false;
1035 // User list is not empty, check each entry, if contact is found
1036 for (final User user : allUsers) {
1037 // Is the contact the same?
1038 if (Objects.equals(user.getUserContact(), next.getValue())) {
1045 // Is contact not found?
1047 // Add it as selectable
1048 this.selectableContactsCache.put(next.getKey(), next.getValue());
1054 public boolean isEmailAddressRegistered (final Contact contact) {
1056 if (null == contact) {
1058 throw new NullPointerException("contact is null"); //NOI18N
1059 } else if (contact.getContactEmailAddress() == null) {
1061 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1062 } else if (contact.getContactEmailAddress().isEmpty()) {
1064 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1068 return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId())));
1072 public boolean isRequiredChangePersonalDataSet () {
1073 return ((this.getPersonalTitle() != null) &&
1074 (this.getFirstName() != null) &&
1075 (this.getFamilyName() != null) &&
1076 (this.getStreet() != null) &&
1077 (this.getHouseNumber() != null) &&
1078 (this.getZipCode() != null) &&
1079 (this.getCity() != null));
1083 public boolean isRequiredPersonalDataSet () {
1084 return ((this.getPersonalTitle() != null) &&
1085 (this.getFirstName() != null) &&
1086 (this.getFamilyName() != null) &&
1087 (this.getStreet() != null) &&
1088 (this.getHouseNumber() != null) &&
1089 (this.getZipCode() != null) &&
1090 (this.getCity() != null) &&
1091 (this.getEmailAddress() != null) &&
1092 (this.getEmailAddressRepeat() != null));
1096 public boolean isSameEmailAddressEntered () {
1097 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1101 * Returns a list of all selectable contacts for user creation. Contacts
1102 * from already existing users are excluded in this list.
1104 * @return A list of all selectable contacts
1106 public List<Contact> selectableContacts () {
1108 final List<Contact> selectableContacts = new LinkedList<>();
1110 // Get iterator from cache
1111 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1113 // Loop through all contacts
1114 while (iterator.hasNext()) {
1116 final Cache.Entry<Long, Contact> next = iterator.next();
1118 // Add entry's value to list
1119 selectableContacts.add(next.getValue());
1123 return selectableContacts;
1127 public void updateContactDataFromController (final Contact contact) {
1128 // Is the instance valid?
1129 if (null == contact) {
1131 throw new NullPointerException("contact is null"); //NOI18N
1132 } else if (contact.getContactId() == null) {
1134 throw new NullPointerException("contact.contactId is null"); //NOI18N
1135 } else if (contact.getContactId() < 1) {
1136 // Not valid id number
1137 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1141 this.copyContact(contact);
1145 * Adds email address to bean's internal list.
1147 * @param contact Contact instance
1149 private void addUserNameEmailAddress (final Contact contact) {
1150 // Make sure the entry is not added yet
1151 if (this.emailAddressCache.containsKey(contact.getContactId())) {
1153 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1157 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1163 private void clear () {
1166 this.setPersonalTitle(null);
1167 this.setAcademicTitle(null);
1168 this.setFirstName(null);
1169 this.setFamilyName(null);
1170 this.setStreet(null);
1171 this.setHouseNumber(null);
1172 this.setHouseNumberExtension(null);
1173 this.setZipCode(null);
1175 this.setCountry(null);
1178 this.clearEmailAddresses();
1179 this.setLandLineAreaCode(null);
1180 this.setLandLineCountry(null);
1181 this.setLandLineNumber(null);
1182 this.setMobileProvider(null);
1183 this.setMobileNumber(null);
1184 this.setFaxAreaCode(null);
1185 this.setFaxCountry(null);
1186 this.setFaxNumber(null);
1189 this.setBirthday(null);
1190 this.setComment(null);
1194 * Copies given contact into the controller
1196 * @param contact Contact instance
1198 private void copyContact (final Contact contact) {
1199 // Is the instance valid?
1200 if (null == contact) {
1202 throw new NullPointerException("contact is null"); //NOI18N
1203 } else if (contact.getContactId() == null) {
1205 throw new NullPointerException("contact.contactId is null"); //NOI18N
1206 } else if (contact.getContactId() < 1) {
1207 // Not valid id number
1208 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1213 this.setPersonalTitle(contact.getContactPersonalTitle());
1214 this.setAcademicTitle(contact.getContactTitle());
1215 this.setFirstName(contact.getContactFirstName());
1216 this.setFamilyName(contact.getContactFamilyName());
1217 this.setStreet(contact.getContactStreet());
1218 this.setHouseNumber(contact.getContactHouseNumber());
1219 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1220 this.setZipCode(contact.getContactZipCode());
1221 this.setCity(contact.getContactCity());
1222 this.setCountry(contact.getContactCountry());
1223 this.setEmailAddress(contact.getContactEmailAddress());
1224 this.setBirthday(contact.getContactBirthday());
1225 this.setComment(contact.getContactComment());
1227 // Get mobile, phone and fax instance
1228 DialableMobileNumber mobile = contact.getContactMobileNumber();
1229 DialableFaxNumber fax = contact.getContactFaxNumber();
1230 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1233 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1234 this.setLandLineCountry(phone.getPhoneCountry());
1235 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1236 this.setLandLineNumber(phone.getPhoneNumber());
1239 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1240 this.setMobileProvider(mobile.getMobileProvider());
1241 this.setMobileNumber(mobile.getPhoneNumber());
1244 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1245 this.setFaxCountry(fax.getPhoneCountry());
1246 this.setFaxAreaCode(fax.getPhoneAreaCode());
1247 this.setFaxNumber(fax.getPhoneNumber());
1252 * Removes given contact from all lists
1254 * @param contact Contact instance to remove
1256 private void removeContact (final Contact contact) {
1257 // Is the instance valid?
1258 if (null == contact) {
1260 throw new NullPointerException("contact is null"); //NOI18N
1261 } else if (contact.getContactId() == null) {
1263 throw new NullPointerException("contact.contactId is null"); //NOI18N
1264 } else if (contact.getContactId() < 1) {
1265 // Not valid id number
1266 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1269 // Remove from general list
1270 if (!this.contactsCache.remove(contact.getContactId())) {
1271 // Did not remove contact
1272 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1275 // Remove from other lists
1276 this.emailAddressCache.remove(contact.getContactId());
1280 * Adds unique instance to contact list. First any existing instance is
1281 * being removed, then the new instance is added.
1283 * @param contact Contact instance to add uniquely
1285 private void uniqueAddContact (final Contact contact) {
1286 // Is the instance valid?
1287 if (null == contact) {
1289 throw new NullPointerException("contact is null"); //NOI18N
1290 } else if (contact.getContactId() == null) {
1292 throw new NullPointerException("contact.contactId is null"); //NOI18N
1293 } else if (contact.getContactId() < 1) {
1294 // Not valid id number
1295 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1298 // Get iterator from list
1299 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1301 // "Walk" through all entries
1302 while (iterator.hasNext()) {
1304 final Cache.Entry<Long, Contact> next = iterator.next();
1306 // Is id number the same?
1307 if (Objects.equals(contact.getContactId(), next.getKey())) {
1308 // Found entry, so remove it and abort
1309 this.removeContact(next.getValue());
1314 // Add contact to list
1315 this.contactsCache.put(contact.getContactId(), contact);