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 java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
28 import javax.enterprise.context.RequestScoped;
29 import javax.enterprise.event.Observes;
30 import javax.faces.view.facelets.FaceletException;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.addressbook.beans.BaseAddressbookController;
34 import org.mxchange.addressbook.beans.user.AddressbookUserWebRequestController;
35 import org.mxchange.addressbook.beans.user.login.AddressbookUserLoginWebSessionController;
36 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
37 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
38 import org.mxchange.jcontacts.model.contact.Contact;
39 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
40 import org.mxchange.jcontacts.model.contact.ContactUtils;
41 import org.mxchange.jcontacts.model.contact.UserContact;
42 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
43 import org.mxchange.jcountry.model.data.Country;
44 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
46 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
47 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
48 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
49 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
50 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
51 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
52 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
53 import org.mxchange.jusercore.model.user.User;
54 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
55 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
56 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
57 import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
60 * A general contact bean (controller)
62 * @author Roland Häder<roland@mxchange.org>
64 @Named ("contactController")
66 public class AddressbookContactWebRequestBean extends BaseAddressbookController implements AddressbookContactWebRequestController {
71 private static final long serialVersionUID = 542_145_347_916L;
74 * Academic academicTitle
76 private String academicTitle;
81 private Date birthday;
91 private String comment;
94 * EJB for general contact purposes
96 @EJB (lookup = "java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote")
97 private ContactSessionBeanRemote contactBean;
103 @Cached (cacheName = "contactsCache")
104 private transient Cache<Long, Contact> contactsCache;
109 private Country country;
114 private String emailAddress;
120 @Cached (cacheName = "emailAddressCache")
121 private transient Cache<Long, String> emailAddressCache;
124 * Email address repeated
126 private String emailAddressRepeat;
131 private String familyName;
134 * Fax number's area code
136 private Integer faxAreaCode;
139 * Country instance for fax number
141 private Country faxCountry;
146 private Long faxNumber;
151 private String firstName;
156 private Short houseNumber;
159 * House number extension
161 private String houseNumberExtension;
164 * Whether a fax entry has been unlinked
166 private boolean isFaxUnlinked;
169 * Whether a land-line number has been unlinked
171 private boolean isLandLineUnlinked;
174 * Whether a mobile entry has been unlinked
176 private boolean isMobileUnlinked;
179 * Phone number area code
181 private Integer landLineAreaCode;
184 * Country instance for phone number
186 private Country landLineCountry;
191 private Long landLineNumber;
196 private Long mobileNumber;
199 * Mobile number's carrier
201 private MobileProvider mobileProvider;
204 * Personal academicTitle
206 private PersonalTitle personalTitle;
209 * A list of all selectable contacts
211 private List<Contact> selectableContacts;
216 private String street;
219 * Regular user controller
222 private AddressbookUserWebRequestController userController;
225 * Login bean (controller)
228 private AddressbookUserLoginWebSessionController userLoginController;
233 private Integer zipCode;
236 * Default constructor
238 public AddressbookContactWebRequestBean () {
239 // Call super constructor
244 * Observes events being fired when an administrator has added a new
247 * @param event Event being fired
249 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
250 // The event must be valid
253 throw new NullPointerException("event is null"); //NOI18N
254 } else if (event.getAddedContact() == null) {
256 throw new NullPointerException("event.addedContact is null"); //NOI18N
257 } else if (event.getAddedContact().getContactId() == null) {
259 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
260 } else if (event.getAddedContact().getContactId() < 1) {
262 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
269 this.uniqueAddContact(event.getAddedContact());
271 // Add to selectable contacts
272 this.selectableContacts.add(event.getAddedContact());
276 * Event observer for newly added users by administrator
278 * @param event Event being fired
280 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
281 // event should not be null
284 throw new NullPointerException("event is null"); //NOI18N
285 } else if (event.getAddedUser() == null) {
287 throw new NullPointerException("event.addedUser is null"); //NOI18N
288 } else if (event.getAddedUser().getUserId() == null) {
290 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
291 } else if (event.getAddedUser().getUserId() < 1) {
293 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
301 * Observes events being fired when an administrator has linked a new user
302 * with existing contact data.
304 * @param event Event being fired
306 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
307 // event should not be null
310 throw new NullPointerException("event is null"); //NOI18N
311 } else if (event.getLinkedUser() == null) {
313 throw new NullPointerException("event.linkedUser is null"); //NOI18N
314 } else if (event.getLinkedUser().getUserContact() == null) {
316 throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
317 } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
319 throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
320 } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
322 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
325 // Remove contact from list available contacts list
326 this.selectableContacts.remove(event.getLinkedUser().getUserContact());
333 * Event observer for updated contact data by administrators
335 * @param event Updated contact data event
337 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
338 // event should not be null
341 throw new NullPointerException("event is null"); //NOI18N
342 } else if (event.getUpdatedContact() == null) {
344 throw new NullPointerException("event.updatedContact is null"); //NOI18N
345 } else if (event.getUpdatedContact().getContactId() == null) {
347 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
348 } else if (event.getUpdatedContact().getContactId() < 1) {
350 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
353 // Add contact instance only once
354 this.uniqueAddContact(event.getUpdatedContact());
356 // Add email address to list
357 this.emailAddressCache.put(event.getUpdatedContact().getContactId(), event.getUpdatedContact().getContactEmailAddress());
361 * Event observer when user confirmed account.
363 * @param event Event being fired
365 public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
366 // event should not be null
369 throw new NullPointerException("event is null"); //NOI18N
370 } else if (event.getConfirmedUser() == null) {
372 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
373 } else if (event.getConfirmedUser().getUserId() == null) {
375 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
376 } else if (event.getConfirmedUser().getUserId() < 1) {
378 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
381 // Add contact instance only once
382 this.uniqueAddContact(event.getConfirmedUser().getUserContact());
386 * Event observer for logged-in user
388 * @param event Event instance
390 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
391 // event should not be null
394 throw new NullPointerException("event is null"); //NOI18N
395 } else if (event.getLoggedInUser() == null) {
397 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
398 } else if (event.getLoggedInUser().getUserId() == null) {
400 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
401 } else if (event.getLoggedInUser().getUserId() < 1) {
403 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
406 // Copy all data to this bean
407 this.copyContact(event.getLoggedInUser().getUserContact());
411 * Event observer for new user registrations
413 * @param event User registration event
415 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
416 // event should not be null
419 throw new NullPointerException("event is null"); //NOI18N
420 } else if (event.getRegisteredUser() == null) {
422 throw new NullPointerException("event.registeredUser is null"); //NOI18N
423 } else if (event.getRegisteredUser().getUserId() == null) {
425 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
426 } else if (event.getRegisteredUser().getUserId() < 1) {
428 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
432 Contact registeredContact = event.getRegisteredUser().getUserContact();
434 // Copy all data from registered->user
435 this.copyContact(registeredContact);
437 // Add contact instance only once
438 this.uniqueAddContact(registeredContact);
440 // Add user name and email address
441 this.addUserNameEmailAddress(registeredContact);
448 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
449 public List<Contact> allContacts () {
451 List<Contact> list = new LinkedList<>();
454 Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
457 while (iterator.hasNext()) {
459 final Cache.Entry<Long, Contact> next = iterator.next();
462 list.add(next.getValue());
470 public void clearEmailAddresses () {
472 this.setEmailAddress(null);
473 this.setEmailAddressRepeat(null);
477 public Contact createContactInstance () {
478 // Is all required data set?
479 if (!this.isRequiredPersonalDataSet()) {
480 // No, then abort here
481 throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
484 // Required personal data must be set
485 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
487 // Generate phone number
488 DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
489 DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
490 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
492 // Create new contact
493 Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
494 contact.setContactStreet(this.getStreet());
495 contact.setContactHouseNumber(this.getHouseNumber());
496 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
497 contact.setContactZipCode(this.getZipCode());
498 contact.setContactCity(this.getCity());
499 contact.setContactCountry(this.getCountry());
500 contact.setContactEmailAddress(this.getEmailAddress());
501 contact.setContactBirthday(this.getBirthday());
502 contact.setContactComment(this.getComment());
504 // Don't set null or wrong references
505 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
506 // Now the number must be given
507 if (phone.getPhoneAreaCode() == null) {
509 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
510 } else if (phone.getPhoneAreaCode() < 1) {
512 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
513 } else if (phone.getPhoneNumber() == null) {
515 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
516 } else if (phone.getPhoneNumber() < 1) {
518 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
522 contact.setContactLandLineNumber(phone);
525 // Don't set null or wrong references
526 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
527 // Now the number must be given
528 if (fax.getPhoneAreaCode() == null) {
530 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
531 } else if (fax.getPhoneAreaCode() < 1) {
533 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
534 } else if (fax.getPhoneNumber() == null) {
536 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
537 } else if (fax.getPhoneNumber() < 1) {
539 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
543 contact.setContactFaxNumber(fax);
546 // Is the provider set?
547 if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
548 // Is the number set?
549 if (mobile.getPhoneNumber() == null) {
551 throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
552 } else if (mobile.getPhoneNumber() < 1) {
554 throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
558 contact.setContactMobileNumber(mobile);
566 public String doChangePersonalContactData () {
567 // This method shall only be called if the user is logged-in
568 if (!this.userLoginController.isUserLoggedIn()) {
570 throw new IllegalStateException("User is not logged-in"); //NOI18N
571 } else if (!this.isRequiredChangePersonalDataSet()) {
572 // Not all required fields are set
573 throw new FaceletException("Not all required fields are set."); //NOI18N
574 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
575 // Password not matching
576 this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
580 // Get contact instance
581 Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
583 // It should be there, so run some tests on it
584 assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
585 assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
586 assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
589 contact.setContactPersonalTitle(this.getPersonalTitle());
590 contact.setContactFirstName(this.getFirstName());
591 contact.setContactFamilyName(this.getFamilyName());
592 contact.setContactStreet(this.getStreet());
593 contact.setContactHouseNumber(this.getHouseNumber());
594 contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
595 contact.setContactZipCode(this.getZipCode());
596 contact.setContactCity(this.getCity());
597 contact.setContactCountry(this.getCountry());
599 // Update contact's mobile number
600 this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
602 // Update contact's land-line number
603 this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
605 // Update contact's fax number
606 this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
608 // Send it to the EJB
609 this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
612 return "contact_data_saved"; //NOI18N
616 * Getter for academic title
618 * @return Academic title
620 public String getAcademicTitle () {
621 return this.academicTitle;
625 * Setter for academic title
627 * @param academicTitle Academic title
629 public void setAcademicTitle (final String academicTitle) {
630 this.academicTitle = academicTitle;
634 * Getter for birth day
638 @SuppressWarnings ("ReturnOfDateField")
639 public Date getBirthday () {
640 return this.birthday;
644 * Setter for birth day
646 * @param birthday Birth day
648 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
649 public void setBirthday (final Date birthday) {
650 this.birthday = birthday;
654 * Getter for city name
658 public String getCity () {
663 * Setter for city name
665 * @param city City name
667 public void setCity (final String city) {
672 * Getter for comments
676 public String getComment () {
683 * @param comment Comments
685 public void setComment (final String comment) {
686 this.comment = comment;
690 public String getControllerType () {
691 return "general"; //NOI18N
696 public void setControllerType (final String controllerType) {
697 throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
701 * Getter for country instance
703 * @return Country instance
705 public Country getCountry () {
710 * Setter for country instance
712 * @param country Country instance
714 public void setCountry (final Country country) {
715 this.country = country;
719 public String getEmailAddress () {
720 return this.emailAddress;
724 * Setter for email address
726 * @param emailAddress Email address
728 public void setEmailAddress (final String emailAddress) {
729 this.emailAddress = emailAddress;
733 * Getter for email address, repeated
735 * @return the emailAddress, repeated
737 public String getEmailAddressRepeat () {
738 return this.emailAddressRepeat;
742 * Setter for email address repeated
744 * @param emailAddressRepeat the emailAddress to set
746 public void setEmailAddressRepeat (final String emailAddressRepeat) {
747 this.emailAddressRepeat = emailAddressRepeat;
753 * @return the familyName
755 public String getFamilyName () {
756 return this.familyName;
762 * @param familyName the familyName to set
764 public void setFamilyName (final String familyName) {
765 this.familyName = familyName;
769 * Getter for fax number's area code
771 * @return Fax number's area code
773 public Integer getFaxAreaCode () {
774 return this.faxAreaCode;
778 * Setter for fax number's area code
780 * @param faxAreaCode Fax number's area code
782 public void setFaxAreaCode (final Integer faxAreaCode) {
783 this.faxAreaCode = faxAreaCode;
787 * Getter for fax's country instance
789 * @return Fax' country instance
791 public Country getFaxCountry () {
792 return this.faxCountry;
796 * Setter for fax's country instance
798 * @param faxCountry Fax' country instance
800 public void setFaxCountry (final Country faxCountry) {
801 this.faxCountry = faxCountry;
805 * Getter for fax number
809 public Long getFaxNumber () {
810 return this.faxNumber;
814 * Setter for fax number
816 * @param faxNumber Fax number
818 public void setFaxNumber (final Long faxNumber) {
819 this.faxNumber = faxNumber;
825 * @return the first name
827 public String getFirstName () {
828 return this.firstName;
834 * @param firstName the first name to set
836 public void setFirstName (final String firstName) {
837 this.firstName = firstName;
843 * @return the houseNumber
845 public Short getHouseNumber () {
846 return this.houseNumber;
852 * @param houseNumber the houseNumber to set
854 public void setHouseNumber (final Short houseNumber) {
855 this.houseNumber = houseNumber;
859 * Getter for house number extension, example: 123a 'a' is then the
860 * extension and 123 is the house number.
862 * @return House number extension
864 public String getHouseNumberExtension () {
865 return this.houseNumberExtension;
869 * Setter for house number extension
871 * @param houseNumberExtension House number extension
873 public void setHouseNumberExtension (final String houseNumberExtension) {
874 this.houseNumberExtension = houseNumberExtension;
878 * Getter for land-line number's area code
880 * @return Land-line number's area code
882 public Integer getLandLineAreaCode () {
883 return this.landLineAreaCode;
887 * Setter for land-line number's area code
889 * @param landLineAreaCode Land-line number's area code
891 public void setLandLineAreaCode (final Integer landLineAreaCode) {
892 this.landLineAreaCode = landLineAreaCode;
896 * Getter for land-line number's country instance
898 * @return Land-line number's country instance
900 public Country getLandLineCountry () {
901 return this.landLineCountry;
905 * Setter for land-line number's country instance
907 * @param landLineCountry Land-line number's country instance
909 public void setLandLineCountry (final Country landLineCountry) {
910 this.landLineCountry = landLineCountry;
914 * Getter for land-line number
916 * @return Land-line number
918 public Long getLandLineNumber () {
919 return this.landLineNumber;
923 * Setter for land-line number
925 * @param landLineNumber Land-line number
927 public void setLandLineNumber (final Long landLineNumber) {
928 this.landLineNumber = landLineNumber;
932 * Getter for mobile number
934 * @return Mobile number
936 public Long getMobileNumber () {
937 return this.mobileNumber;
941 * Setter for mobile number
943 * @param mobileNumber Mobile number
945 public void setMobileNumber (final Long mobileNumber) {
946 this.mobileNumber = mobileNumber;
950 * Getter for mobile number's carrier
952 * @return Mobile number's carrier
954 public MobileProvider getMobileProvider () {
955 return this.mobileProvider;
959 * Setter for mobile number's provider
961 * @param mobileProvider Mobile number's provider
963 public void setMobileProvider (final MobileProvider mobileProvider) {
964 this.mobileProvider = mobileProvider;
968 * Getter for personal title
970 * @return Personal title
972 public PersonalTitle getPersonalTitle () {
973 return this.personalTitle;
977 * Setter for personal title
979 * @param personalTitle Personal title
981 public void setPersonalTitle (final PersonalTitle personalTitle) {
982 this.personalTitle = personalTitle;
990 public String getStreet () {
997 * @param street Street
999 public void setStreet (final String street) {
1000 this.street = street;
1004 * Getter for ZIP code
1008 public Integer getZipCode () {
1009 return this.zipCode;
1013 * Setter for ZIP code
1015 * @param zipCode ZIP code
1017 public void setZipCode (final Integer zipCode) {
1018 this.zipCode = zipCode;
1022 * Post-construction method
1025 public void init () {
1027 final List<Contact> selectable = new LinkedList<>();
1030 if (!this.contactsCache.iterator().hasNext()) {
1032 List<Contact> list = this.contactBean.allContacts();
1035 for (final Iterator<Contact> iterator = list.iterator(); iterator.hasNext();) {
1037 final Contact next = iterator.next();
1040 this.contactsCache.put(next.getContactId(), next);
1041 this.emailAddressCache.put(next.getContactId(), next.getContactEmailAddress());
1042 selectable.add(next);
1047 List<User> allUsers = this.userController.allUsers();
1050 Iterator<Contact> iterator = selectable.iterator();
1053 while (iterator.hasNext()) {
1055 Contact next = iterator.next();
1058 Iterator<User> userIterator = allUsers.iterator();
1060 // Loop through all users
1061 while (userIterator.hasNext()) {
1062 // Get user instance
1063 User nextUser = userIterator.next();
1066 if (Objects.equals(next, nextUser.getUserContact())) {
1075 this.selectableContacts = selectable;
1079 public boolean isEmailAddressRegistered (final Contact contact) {
1081 if (null == contact) {
1083 throw new NullPointerException("contact is null"); //NOI18N
1084 } else if (contact.getContactEmailAddress() == null) {
1086 throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
1087 } else if (contact.getContactEmailAddress().isEmpty()) {
1089 throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
1093 return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId())));
1097 public boolean isRequiredChangePersonalDataSet () {
1098 return ((this.getPersonalTitle() != null) &&
1099 (this.getFirstName() != null) &&
1100 (this.getFamilyName() != null) &&
1101 (this.getStreet() != null) &&
1102 (this.getHouseNumber() != null) &&
1103 (this.getZipCode() != null) &&
1104 (this.getCity() != null));
1108 public boolean isRequiredPersonalDataSet () {
1109 return ((this.getPersonalTitle() != null) &&
1110 (this.getFirstName() != null) &&
1111 (this.getFamilyName() != null) &&
1112 (this.getStreet() != null) &&
1113 (this.getHouseNumber() != null) &&
1114 (this.getZipCode() != null) &&
1115 (this.getCity() != null) &&
1116 (this.getEmailAddress() != null) &&
1117 (this.getEmailAddressRepeat() != null));
1121 public boolean isSameEmailAddressEntered () {
1122 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
1126 * Returns a list of all selectable contacts for user creation. Contacts
1127 * from already existing users are excluded in this list.
1129 * @return A list of all selectable contacts
1131 public List<Contact> selectableContacts () {
1132 return Collections.unmodifiableList(this.selectableContacts);
1136 public void updateContactDataFromController (final Contact contact) {
1137 // Is the instance valid?
1138 if (null == contact) {
1140 throw new NullPointerException("contact is null"); //NOI18N
1141 } else if (contact.getContactId() == null) {
1143 throw new NullPointerException("contact.contactId is null"); //NOI18N
1144 } else if (contact.getContactId() < 1) {
1145 // Not valid id number
1146 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1150 this.copyContact(contact);
1154 * Adds email address to bean's internal list.
1156 * @param contact Contact instance
1158 private void addUserNameEmailAddress (final Contact contact) {
1159 // Make sure the entry is not added yet
1160 if (this.emailAddressCache.containsKey(contact.getContactId())) {
1162 throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
1166 this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
1172 private void clear () {
1175 this.setPersonalTitle(null);
1176 this.setAcademicTitle(null);
1177 this.setFirstName(null);
1178 this.setFamilyName(null);
1179 this.setStreet(null);
1180 this.setHouseNumber(null);
1181 this.setHouseNumberExtension(null);
1182 this.setZipCode(null);
1184 this.setCountry(null);
1187 this.clearEmailAddresses();
1188 this.setLandLineAreaCode(null);
1189 this.setLandLineCountry(null);
1190 this.setLandLineNumber(null);
1191 this.setMobileProvider(null);
1192 this.setMobileNumber(null);
1193 this.setFaxAreaCode(null);
1194 this.setFaxCountry(null);
1195 this.setFaxNumber(null);
1198 this.setBirthday(null);
1199 this.setComment(null);
1203 * Copies given contact into the controller
1205 * @param contact Contact instance
1207 private void copyContact (final Contact contact) {
1208 // Is the instance valid?
1209 if (null == contact) {
1211 throw new NullPointerException("contact is null"); //NOI18N
1212 } else if (contact.getContactId() == null) {
1214 throw new NullPointerException("contact.contactId is null"); //NOI18N
1215 } else if (contact.getContactId() < 1) {
1216 // Not valid id number
1217 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1222 this.setPersonalTitle(contact.getContactPersonalTitle());
1223 this.setAcademicTitle(contact.getContactTitle());
1224 this.setFirstName(contact.getContactFirstName());
1225 this.setFamilyName(contact.getContactFamilyName());
1226 this.setStreet(contact.getContactStreet());
1227 this.setHouseNumber(contact.getContactHouseNumber());
1228 this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
1229 this.setZipCode(contact.getContactZipCode());
1230 this.setCity(contact.getContactCity());
1231 this.setCountry(contact.getContactCountry());
1232 this.setEmailAddress(contact.getContactEmailAddress());
1233 this.setBirthday(contact.getContactBirthday());
1234 this.setComment(contact.getContactComment());
1236 // Get mobile, phone and fax instance
1237 DialableMobileNumber mobile = contact.getContactMobileNumber();
1238 DialableFaxNumber fax = contact.getContactFaxNumber();
1239 DialableLandLineNumber phone = contact.getContactLandLineNumber();
1242 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
1243 this.setLandLineCountry(phone.getPhoneCountry());
1244 this.setLandLineAreaCode(phone.getPhoneAreaCode());
1245 this.setLandLineNumber(phone.getPhoneNumber());
1248 if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
1249 this.setMobileProvider(mobile.getMobileProvider());
1250 this.setMobileNumber(mobile.getPhoneNumber());
1253 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
1254 this.setFaxCountry(fax.getPhoneCountry());
1255 this.setFaxAreaCode(fax.getPhoneAreaCode());
1256 this.setFaxNumber(fax.getPhoneNumber());
1261 * Removes given contact from all lists
1263 * @param contact Contact instance to remove
1265 private void removeContact (final Contact contact) {
1266 // Is the instance valid?
1267 if (null == contact) {
1269 throw new NullPointerException("contact is null"); //NOI18N
1270 } else if (contact.getContactId() == null) {
1272 throw new NullPointerException("contact.contactId is null"); //NOI18N
1273 } else if (contact.getContactId() < 1) {
1274 // Not valid id number
1275 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1278 // Remove from general list
1279 if (!this.contactsCache.remove(contact.getContactId())) {
1280 // Did not remove contact
1281 throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
1284 // Remove from other lists
1285 this.emailAddressCache.remove(contact.getContactId());
1289 * Adds unique instance to contact list. First any existing instance is
1290 * being removed, then the new instance is added.
1292 * @param contact Contact instance to add uniquely
1294 private void uniqueAddContact (final Contact contact) {
1295 // Is the instance valid?
1296 if (null == contact) {
1298 throw new NullPointerException("contact is null"); //NOI18N
1299 } else if (contact.getContactId() == null) {
1301 throw new NullPointerException("contact.contactId is null"); //NOI18N
1302 } else if (contact.getContactId() < 1) {
1303 // Not valid id number
1304 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
1307 // Get iterator from list
1308 Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
1310 // "Walk" through all entries
1311 while (iterator.hasNext()) {
1313 Cache.Entry<Long, Contact> next = iterator.next();
1315 // Is id number the same?
1316 if (Objects.equals(contact.getContactId(), next.getKey())) {
1317 // Found entry, so remove it and abort
1318 this.removeContact(next.getValue());
1323 // Add contact to list
1324 this.contactsCache.put(contact.getContactId(), contact);