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.addressbook.beans.contact;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Objects;
27 import javax.annotation.PostConstruct;
28 import javax.cache.Cache;
30 import javax.enterprise.context.RequestScoped;
31 import javax.enterprise.event.Observes;
32 import javax.faces.view.facelets.FaceletException;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import org.mxchange.addressbook.beans.BaseAddressbookController;
36 import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController;
37 import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController;
38 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
39 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
40 import org.mxchange.jcontacts.model.contact.Contact;
41 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
42 import org.mxchange.jcontacts.model.contact.ContactUtils;
43 import org.mxchange.jcontacts.model.contact.UserContact;
44 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
45 import org.mxchange.jcountry.model.data.Country;
46 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
51 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
52 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
53 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
54 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
55 import org.mxchange.jusercore.model.user.User;
56 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
57 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
58 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
59 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
62 * A general contact bean (controller)
64 * @author Roland Häder<roland@mxchange.org>
66 @Named ("contactController")
68 public class AddressbookContactWebRequestBean extends BaseAddressbookController implements AddressbookContactWebRequestController {
73 private static final long serialVersionUID = 542_145_347_916L;
76 * Academic academicTitle
78 private String academicTitle;
83 private Date birthday;
93 private String comment;
96 * EJB for general contact purposes
98 @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote")
99 private ContactSessionBeanRemote contactBean;
105 @NamedCache (cacheName = "contactsCache")
106 private Cache<Long, Contact> contactsCache;
111 private Country country;
116 private String emailAddress;
122 @NamedCache (cacheName = "emailAddressCache")
123 private Cache<Long, String> emailAddressCache;
126 * Email address repeated
128 private String emailAddressRepeat;
133 private String familyName;
136 * Fax number's area code
138 private Integer faxAreaCode;
141 * Country instance for fax number
143 private Country faxCountry;
148 private Long faxNumber;
153 private String firstName;
158 private Short houseNumber;
161 * House number extension
163 private String houseNumberExtension;
166 * Whether a fax entry has been unlinked
168 private boolean isFaxUnlinked;
171 * Whether a land-line number has been unlinked
173 private boolean isLandLineUnlinked;
176 * Whether a mobile entry has been unlinked
178 private boolean isMobileUnlinked;
181 * Phone number area code
183 private Integer landLineAreaCode;
186 * Country instance for phone number
188 private Country landLineCountry;
193 private Long landLineNumber;
198 private Long mobileNumber;
201 * Mobile number's carrier
203 private MobileProvider mobileProvider;
206 * Personal academicTitle
208 private PersonalTitle personalTitle;
211 * A list of all selectable contacts
213 private List<Contact> selectableContacts;
218 private String street;
221 * Regular user controller
224 private AddressbookUserWebRequestController userController;
227 * Login bean (controller)
230 private AddressbookUserLoginWebSessionController userLoginController;
235 private Integer zipCode;
238 * Default constructor
240 public AddressbookContactWebRequestBean () {
241 // Call super constructor
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.emailAddressCache.put(event.getUpdatedContact().getContactId(), 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 final 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 () {
453 final List<Contact> list = new LinkedList<>();
456 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
459 while (iterator.hasNext()) {
461 final Cache.Entry<Long, Contact> next = iterator.next();
464 list.add(next.getValue());
472 public void clearEmailAddresses () {
474 this.setEmailAddress(null);
475 this.setEmailAddressRepeat(null);
479 public Contact createContactInstance () {
480 // Is all required data set?
481 if (!this.isRequiredPersonalDataSet()) {
482 // No, then abort here
483 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
486 // Required personal data must be set
487 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
489 // Generate phone number
490 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
491 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
492 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
494 // Create new contact
495 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
496 contact.setContactStreet(this.getStreet());
497 contact.setContactHouseNumber(this.getHouseNumber());
498 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
499 contact.setContactZipCode(this.getZipCode());
500 contact.setContactCity(this.getCity());
501 contact.setContactCountry(this.getCountry());
502 contact.setContactEmailAddress(this.getEmailAddress());
503 contact.setContactBirthday(this.getBirthday());
504 contact.setContactComment(this.getComment());
506 // Don't set null or wrong references
507 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
508 // Now the number must be given
509 if (phone.getPhoneAreaCode() == null) {
511 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
512 } else if (phone.getPhoneAreaCode() < 1) {
514 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
515 } else if (phone.getPhoneNumber() == null) {
517 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
518 } else if (phone.getPhoneNumber() < 1) {
520 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
524 contact.setContactLandLineNumber(phone);
527 // Don't set null or wrong references
528 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
529 // Now the number must be given
530 if (fax.getPhoneAreaCode() == null) {
532 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
533 } else if (fax.getPhoneAreaCode() < 1) {
535 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
536 } else if (fax.getPhoneNumber() == null) {
538 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
539 } else if (fax.getPhoneNumber() < 1) {
541 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
545 contact.setContactFaxNumber(fax);
548 // Is the provider set?
549 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
550 // Is the number set?
551 if (mobile.getPhoneNumber() == null) {
553 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
554 } else if (mobile.getPhoneNumber() < 1) {
556 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
560 contact.setContactMobileNumber(mobile);
568 public String doChangePersonalContactData () {
569 // This method shall only be called if the user is logged-in
570 if (!this.userLoginController.isUserLoggedIn()) {
572 throw new IllegalStateException("User is not logged-in"); //NOI18N
573 } else if (!this.isRequiredChangePersonalDataSet()) {
574 // Not all required fields are set
575 throw new FaceletException("Not all required fields are set."); //NOI18N
576 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
577 // Password not matching
578 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
582 // Get contact instance
583 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
585 // It should be there, so run some tests on it
586 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
587 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
588 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
591 contact.setContactPersonalTitle(this.getPersonalTitle());
592 contact.setContactFirstName(this.getFirstName());
593 contact.setContactFamilyName(this.getFamilyName());
594 contact.setContactStreet(this.getStreet());
595 contact.setContactHouseNumber(this.getHouseNumber());
596 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
597 contact.setContactZipCode(this.getZipCode());
598 contact.setContactCity(this.getCity());
599 contact.setContactCountry(this.getCountry());
601 // Update contact's mobile number
602 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
604 // Update contact's land-line number
605 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
607 // Update contact's fax number
608 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
610 // Send it to the EJB
611 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
614 return "contact_data_saved"; //NOI18N
618 * Getter for academic title
620 * @return Academic title
622 public String getAcademicTitle () {
623 return this.academicTitle;
627 * Setter for academic title
629 * @param academicTitle Academic title
631 public void setAcademicTitle (final String academicTitle) {
632 this.academicTitle = academicTitle;
636 * Getter for birth day
640 @SuppressWarnings ("ReturnOfDateField")
641 public Date getBirthday () {
642 return this.birthday;
646 * Setter for birth day
648 * @param birthday Birth day
650 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
651 public void setBirthday (final Date birthday) {
652 this.birthday = birthday;
656 * Getter for city name
660 public String getCity () {
665 * Setter for city name
667 * @param city City name
669 public void setCity (final String city) {
674 * Getter for comments
678 public String getComment () {
685 * @param comment Comments
687 public void setComment (final String comment) {
688 this.comment = comment;
692 public String getControllerType () {
693 return "general"; //NOI18N
698 public void setControllerType (final String controllerType) {
699 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
703 * Getter for country instance
705 * @return Country instance
707 public Country getCountry () {
712 * Setter for country instance
714 * @param country Country instance
716 public void setCountry (final Country country) {
717 this.country = country;
721 public String getEmailAddress () {
722 return this.emailAddress;
726 * Setter for email address
728 * @param emailAddress Email address
730 public void setEmailAddress (final String emailAddress) {
731 this.emailAddress = emailAddress;
735 * Getter for email address, repeated
737 * @return the emailAddress, repeated
739 public String getEmailAddressRepeat () {
740 return this.emailAddressRepeat;
744 * Setter for email address repeated
746 * @param emailAddressRepeat the emailAddress to set
748 public void setEmailAddressRepeat (final String emailAddressRepeat) {
749 this.emailAddressRepeat = emailAddressRepeat;
755 * @return the familyName
757 public String getFamilyName () {
758 return this.familyName;
764 * @param familyName the familyName to set
766 public void setFamilyName (final String familyName) {
767 this.familyName = familyName;
771 * Getter for fax number's area code
773 * @return Fax number's area code
775 public Integer getFaxAreaCode () {
776 return this.faxAreaCode;
780 * Setter for fax number's area code
782 * @param faxAreaCode Fax number's area code
784 public void setFaxAreaCode (final Integer faxAreaCode) {
785 this.faxAreaCode = faxAreaCode;
789 * Getter for fax's country instance
791 * @return Fax' country instance
793 public Country getFaxCountry () {
794 return this.faxCountry;
798 * Setter for fax's country instance
800 * @param faxCountry Fax' country instance
802 public void setFaxCountry (final Country faxCountry) {
803 this.faxCountry = faxCountry;
807 * Getter for fax number
811 public Long getFaxNumber () {
812 return this.faxNumber;
816 * Setter for fax number
818 * @param faxNumber Fax number
820 public void setFaxNumber (final Long faxNumber) {
821 this.faxNumber = faxNumber;
827 * @return the first name
829 public String getFirstName () {
830 return this.firstName;
836 * @param firstName the first name to set
838 public void setFirstName (final String firstName) {
839 this.firstName = firstName;
845 * @return the houseNumber
847 public Short getHouseNumber () {
848 return this.houseNumber;
854 * @param houseNumber the houseNumber to set
856 public void setHouseNumber (final Short houseNumber) {
857 this.houseNumber = houseNumber;
861 * Getter for house number extension, example: 123a 'a' is then the
862 * extension and 123 is the house number.
864 * @return House number extension
866 public String getHouseNumberExtension () {
867 return this.houseNumberExtension;
871 * Setter for house number extension
873 * @param houseNumberExtension House number extension
875 public void setHouseNumberExtension (final String houseNumberExtension) {
876 this.houseNumberExtension = houseNumberExtension;
880 * Getter for land-line number's area code
882 * @return Land-line number's area code
884 public Integer getLandLineAreaCode () {
885 return this.landLineAreaCode;
889 * Setter for land-line number's area code
891 * @param landLineAreaCode Land-line number's area code
893 public void setLandLineAreaCode (final Integer landLineAreaCode) {
894 this.landLineAreaCode = landLineAreaCode;
898 * Getter for land-line number's country instance
900 * @return Land-line number's country instance
902 public Country getLandLineCountry () {
903 return this.landLineCountry;
907 * Setter for land-line number's country instance
909 * @param landLineCountry Land-line number's country instance
911 public void setLandLineCountry (final Country landLineCountry) {
912 this.landLineCountry = landLineCountry;
916 * Getter for land-line number
918 * @return Land-line number
920 public Long getLandLineNumber () {
921 return this.landLineNumber;
925 * Setter for land-line number
927 * @param landLineNumber Land-line number
929 public void setLandLineNumber (final Long landLineNumber) {
930 this.landLineNumber = landLineNumber;
934 * Getter for mobile number
936 * @return Mobile number
938 public Long getMobileNumber () {
939 return this.mobileNumber;
943 * Setter for mobile number
945 * @param mobileNumber Mobile number
947 public void setMobileNumber (final Long mobileNumber) {
948 this.mobileNumber = mobileNumber;
952 * Getter for mobile number's carrier
954 * @return Mobile number's carrier
956 public MobileProvider getMobileProvider () {
957 return this.mobileProvider;
961 * Setter for mobile number's provider
963 * @param mobileProvider Mobile number's provider
965 public void setMobileProvider (final MobileProvider mobileProvider) {
966 this.mobileProvider = mobileProvider;
970 * Getter for personal title
972 * @return Personal title
974 public PersonalTitle getPersonalTitle () {
975 return this.personalTitle;
979 * Setter for personal title
981 * @param personalTitle Personal title
983 public void setPersonalTitle (final PersonalTitle personalTitle) {
984 this.personalTitle = personalTitle;
992 public String getStreet () {
999 * @param street Street
1001 public void setStreet (final String street) {
1002 this.street = street;
1006 * Getter for ZIP code
1010 public Integer getZipCode () {
1011 return this.zipCode;
1015 * Setter for ZIP code
1017 * @param zipCode ZIP code
1019 public void setZipCode (final Integer zipCode) {
1020 this.zipCode = zipCode;
1024 * Post-construction method
1027 public void init () {
1029 final List<Contact> selectable = new LinkedList<>();
1032 if (!this.contactsCache.iterator().hasNext()) {
1034 List<Contact> list = this.contactBean.allContacts();
1037 for (final Iterator<Contact> iterator = list.iterator(); iterator.hasNext();) {
1039 final Contact next = iterator.next();
1042 this.contactsCache.put(next.getContactId(), next);
1043 this.emailAddressCache.put(next.getContactId(), next.getContactEmailAddress());
1044 selectable.add(next);
1049 List<User> allUsers = this.userController.allUsers();
1052 Iterator<Contact> iterator = selectable.iterator();
1055 while (iterator.hasNext()) {
1057 Contact next = iterator.next();
1060 Iterator<User> userIterator = allUsers.iterator();
1062 // Loop through all users
1063 while (userIterator.hasNext()) {
1064 // Get user instance
1065 User nextUser = userIterator.next();
1068 if (Objects.equals(next, nextUser.getUserContact())) {
1077 this.selectableContacts = selectable;
1081 public boolean isEmailAddressRegistered (final Contact contact) {
1083 if (null == contact) {
1085 throw new NullPointerException("contact is null"); //NOI18N
1086 } else if (contact.getContactEmailAddress() == null) {
1088 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1089 } else if (contact.getContactEmailAddress().isEmpty()) {
1091 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1095 return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId())));
1099 public boolean isRequiredChangePersonalDataSet () {
1100 return ((this.getPersonalTitle() != null) &&
1101 (this.getFirstName() != null) &&
1102 (this.getFamilyName() != null) &&
1103 (this.getStreet() != null) &&
1104 (this.getHouseNumber() != null) &&
1105 (this.getZipCode() != null) &&
1106 (this.getCity() != null));
1110 public boolean isRequiredPersonalDataSet () {
1111 return ((this.getPersonalTitle() != null) &&
1112 (this.getFirstName() != null) &&
1113 (this.getFamilyName() != null) &&
1114 (this.getStreet() != null) &&
1115 (this.getHouseNumber() != null) &&
1116 (this.getZipCode() != null) &&
1117 (this.getCity() != null) &&
1118 (this.getEmailAddress() != null) &&
1119 (this.getEmailAddressRepeat() != null));
1123 public boolean isSameEmailAddressEntered () {
1124 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1128 * Returns a list of all selectable contacts for user creation. Contacts
1129 * from already existing users are excluded in this list.
1131 * @return A list of all selectable contacts
1133 public List<Contact> selectableContacts () {
1134 return Collections.unmodifiableList(this.selectableContacts);
1138 public void updateContactDataFromController (final Contact contact) {
1139 // Is the instance valid?
1140 if (null == contact) {
1142 throw new NullPointerException("contact is null"); //NOI18N
1143 } else if (contact.getContactId() == null) {
1145 throw new NullPointerException("contact.contactId is null"); //NOI18N
1146 } else if (contact.getContactId() < 1) {
1147 // Not valid id number
1148 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1152 this.copyContact(contact);
1156 * Adds email address to bean's internal list.
1158 * @param contact Contact instance
1160 private void addUserNameEmailAddress (final Contact contact) {
1161 // Make sure the entry is not added yet
1162 if (this.emailAddressCache.containsKey(contact.getContactId())) {
1164 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1168 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1174 private void clear () {
1177 this.setPersonalTitle(null);
1178 this.setAcademicTitle(null);
1179 this.setFirstName(null);
1180 this.setFamilyName(null);
1181 this.setStreet(null);
1182 this.setHouseNumber(null);
1183 this.setHouseNumberExtension(null);
1184 this.setZipCode(null);
1186 this.setCountry(null);
1189 this.clearEmailAddresses();
1190 this.setLandLineAreaCode(null);
1191 this.setLandLineCountry(null);
1192 this.setLandLineNumber(null);
1193 this.setMobileProvider(null);
1194 this.setMobileNumber(null);
1195 this.setFaxAreaCode(null);
1196 this.setFaxCountry(null);
1197 this.setFaxNumber(null);
1200 this.setBirthday(null);
1201 this.setComment(null);
1205 * Copies given contact into the controller
1207 * @param contact Contact instance
1209 private void copyContact (final Contact contact) {
1210 // Is the instance valid?
1211 if (null == contact) {
1213 throw new NullPointerException("contact is null"); //NOI18N
1214 } else if (contact.getContactId() == null) {
1216 throw new NullPointerException("contact.contactId is null"); //NOI18N
1217 } else if (contact.getContactId() < 1) {
1218 // Not valid id number
1219 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1224 this.setPersonalTitle(contact.getContactPersonalTitle());
1225 this.setAcademicTitle(contact.getContactTitle());
1226 this.setFirstName(contact.getContactFirstName());
1227 this.setFamilyName(contact.getContactFamilyName());
1228 this.setStreet(contact.getContactStreet());
1229 this.setHouseNumber(contact.getContactHouseNumber());
1230 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1231 this.setZipCode(contact.getContactZipCode());
1232 this.setCity(contact.getContactCity());
1233 this.setCountry(contact.getContactCountry());
1234 this.setEmailAddress(contact.getContactEmailAddress());
1235 this.setBirthday(contact.getContactBirthday());
1236 this.setComment(contact.getContactComment());
1238 // Get mobile, phone and fax instance
1239 DialableMobileNumber mobile = contact.getContactMobileNumber();
1240 DialableFaxNumber fax = contact.getContactFaxNumber();
1241 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1244 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1245 this.setLandLineCountry(phone.getPhoneCountry());
1246 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1247 this.setLandLineNumber(phone.getPhoneNumber());
1250 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1251 this.setMobileProvider(mobile.getMobileProvider());
1252 this.setMobileNumber(mobile.getPhoneNumber());
1255 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1256 this.setFaxCountry(fax.getPhoneCountry());
1257 this.setFaxAreaCode(fax.getPhoneAreaCode());
1258 this.setFaxNumber(fax.getPhoneNumber());
1263 * Removes given contact from all lists
1265 * @param contact Contact instance to remove
1267 private void removeContact (final Contact contact) {
1268 // Is the instance valid?
1269 if (null == contact) {
1271 throw new NullPointerException("contact is null"); //NOI18N
1272 } else if (contact.getContactId() == null) {
1274 throw new NullPointerException("contact.contactId is null"); //NOI18N
1275 } else if (contact.getContactId() < 1) {
1276 // Not valid id number
1277 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1280 // Remove from general list
1281 if (!this.contactsCache.remove(contact.getContactId())) {
1282 // Did not remove contact
1283 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1286 // Remove from other lists
1287 this.emailAddressCache.remove(contact.getContactId());
1291 * Adds unique instance to contact list. First any existing instance is
1292 * being removed, then the new instance is added.
1294 * @param contact Contact instance to add uniquely
1296 private void uniqueAddContact (final Contact contact) {
1297 // Is the instance valid?
1298 if (null == contact) {
1300 throw new NullPointerException("contact is null"); //NOI18N
1301 } else if (contact.getContactId() == null) {
1303 throw new NullPointerException("contact.contactId is null"); //NOI18N
1304 } else if (contact.getContactId() < 1) {
1305 // Not valid id number
1306 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1309 // Get iterator from list
1310 final Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1312 // "Walk" through all entries
1313 while (iterator.hasNext()) {
1315 final Cache.Entry<Long, Contact> next = iterator.next();
1317 // Is id number the same?
1318 if (Objects.equals(contact.getContactId(), next.getKey())) {
1319 // Found entry, so remove it and abort
1320 this.removeContact(next.getValue());
1325 // Add contact to list
1326 this.contactsCache.put(contact.getContactId(), contact);