+/*
+ * Copyright (C) 2016, 2017 Roland Häder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jjobs.beans.contact;
+
+import java.text.MessageFormat;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import javax.annotation.PostConstruct;
+import javax.ejb.EJB;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Observes;
+import javax.faces.view.facelets.FaceletException;
+import javax.inject.Inject;
+import javax.inject.Named;
+import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
+import org.mxchange.jcontacts.contact.ContactUtils;
+import org.mxchange.jcontacts.contact.UserContact;
+import org.mxchange.jcontacts.contact.title.PersonalTitle;
+import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
+import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
+import org.mxchange.jcoreee.jcache.Cached;
+import org.mxchange.jcountry.data.Country;
+import org.mxchange.jjobs.beans.BaseJobsController;
+import org.mxchange.jjobs.beans.user.JobsUserWebRequestController;
+import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
+import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
+import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
+import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
+import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
+import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
+import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
+import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
+import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
+import org.mxchange.jusercore.model.user.User;
+import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
+import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
+import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
+import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException;
+
+/**
+ * A general contact controller (bean)
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@Named ("contactController")
+@RequestScoped
+public class JobsContactWebRequestBean extends BaseJobsController implements JobsContactWebRequestController {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 542_145_347_916L;
+
+ /**
+ * Academic academicTitle
+ */
+ private String academicTitle;
+
+ /**
+ * Birth day
+ */
+ private Date birthday;
+
+ /**
+ * City
+ */
+ private String city;
+
+ /**
+ * Optional comments
+ */
+ private String comment;
+
+ /**
+ * EJB for general contact purposes
+ */
+ @EJB (lookup = "java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote")
+ private ContactSessionBeanRemote contactBean;
+
+ /**
+ * Contact list
+ */
+ @Inject
+ @Cached (cacheName = "contactsCache")
+ private transient Cache<Long, Contact> contactsCache;
+
+ /**
+ * Country instance
+ */
+ private Country country;
+
+ /**
+ * Email address
+ */
+ private String emailAddress;
+
+ /**
+ * Email address list
+ */
+ @Inject
+ @Cached (cacheName = "emailAddressCache")
+ private transient Cache<Long, String> emailAddressCache;
+
+ /**
+ * Email address repeated
+ */
+ private String emailAddressRepeat;
+
+ /**
+ * Family name
+ */
+ private String familyName;
+
+ /**
+ * Fax number's area code
+ */
+ private Integer faxAreaCode;
+
+ /**
+ * Country instance for fax number
+ */
+ private Country faxCountry;
+
+ /**
+ * Fax number
+ */
+ private Long faxNumber;
+
+ /**
+ * First name
+ */
+ private String firstName;
+
+ /**
+ * House number
+ */
+ private Short houseNumber;
+
+ /**
+ * House number extension
+ */
+ private String houseNumberExtension;
+
+ /**
+ * Whether a fax entry has been unlinked
+ */
+ private boolean isFaxUnlinked;
+
+ /**
+ * Whether a land-line number has been unlinked
+ */
+ private boolean isLandLineUnlinked;
+
+ /**
+ * Whether a mobile entry has been unlinked
+ */
+ private boolean isMobileUnlinked;
+
+ /**
+ * Phone number area code
+ */
+ private Integer landLineAreaCode;
+
+ /**
+ * Country instance for phone number
+ */
+ private Country landLineCountry;
+
+ /**
+ * Phone number
+ */
+ private Long landLineNumber;
+
+ /**
+ * Mobile number
+ */
+ private Long mobileNumber;
+
+ /**
+ * Mobile number's carrier
+ */
+ private MobileProvider mobileProvider;
+
+ /**
+ * Personal academicTitle
+ */
+ private PersonalTitle personalTitle;
+
+ /**
+ * A list of all selectable contacts
+ */
+ private List<Contact> selectableContacts;
+
+ /**
+ * Street
+ */
+ private String street;
+
+ /**
+ * Regular user controller
+ */
+ @Inject
+ private JobsUserWebRequestController userController;
+
+ /**
+ * Login bean (controller)
+ */
+ @Inject
+ private JobsUserLoginWebSessionController userLoginController;
+
+ /**
+ * ZIP code
+ */
+ private Integer zipCode;
+
+ /**
+ * Default constructor
+ */
+ public JobsContactWebRequestBean () {
+ // Call super constructor
+ super();
+ }
+
+ /**
+ * Observes events being fired when an administrator has added a new
+ * contact.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
+ // The event must be valid
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedContact() == null) {
+ // Throw again ...
+ throw new NullPointerException("event.addedContact is null"); //NOI18N
+ } else if (event.getAddedContact().getContactId() == null) {
+ // ... and again
+ throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
+ } else if (event.getAddedContact().getContactId() < 1) {
+ // Not valid
+ throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
+ }
+
+ // Clear this bean
+ this.clear();
+
+ // Call other method
+ this.uniqueAddContact(event.getAddedContact());
+
+ // Add to selectable contacts
+ this.selectableContacts.add(event.getAddedContact());
+ }
+
+ /**
+ * Event observer for newly added users by administrator
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.addedUser is null"); //NOI18N
+ } else if (event.getAddedUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
+ } else if (event.getAddedUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
+ }
+
+ // Clear all data
+ this.clear();
+ }
+
+ /**
+ * Observes events being fired when an administrator has linked a new user
+ * with existing contact data.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getLinkedUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.linkedUser is null"); //NOI18N
+ } else if (event.getLinkedUser().getUserContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.linkedUser.userContact is null"); //NOI18N
+ } else if (event.getLinkedUser().getUserContact().getContactId() == null) {
+ // userId is null
+ throw new NullPointerException("event.linkedUser.userContact.contactId is null"); //NOI18N
+ } else if (event.getLinkedUser().getUserContact().getContactId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserContact().getContactId())); //NOI18N
+ }
+
+ // Remove contact from list available contacts list
+ this.selectableContacts.remove(event.getLinkedUser().getUserContact());
+
+ // Clear all data
+ this.clear();
+ }
+
+ /**
+ * Event observer for updated contact data by administrators
+ * <p>
+ * @param event Updated contact data event
+ */
+ public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedContact() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.updatedContact is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() == null) {
+ // userId is null
+ throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
+ } else if (event.getUpdatedContact().getContactId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
+ }
+
+ // Add contact instance only once
+ this.uniqueAddContact(event.getUpdatedContact());
+
+ // Add email address to list
+ this.emailAddressCache.put(event.getUpdatedContact().getContactId(), event.getUpdatedContact().getContactEmailAddress());
+ }
+
+ /**
+ * Event observer when user confirmed account.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterUserConfirmedAccount (@Observes final ObservableUserConfirmedAccountEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getConfirmedUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.confirmedUser is null"); //NOI18N
+ } else if (event.getConfirmedUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
+ } else if (event.getConfirmedUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
+ }
+
+ // Add contact instance only once
+ this.uniqueAddContact(event.getConfirmedUser().getUserContact());
+ }
+
+ /**
+ * Event observer for logged-in user
+ * <p>
+ * @param event Event instance
+ */
+ public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getLoggedInUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.loggedInUser is null"); //NOI18N
+ } else if (event.getLoggedInUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
+ } else if (event.getLoggedInUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
+ }
+
+ // Copy all data to this bean
+ this.copyContact(event.getLoggedInUser().getUserContact());
+ }
+
+ /**
+ * Event observer for new user registrations
+ * <p>
+ * @param event User registration event
+ */
+ public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getRegisteredUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.registeredUser is null"); //NOI18N
+ } else if (event.getRegisteredUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
+ } else if (event.getRegisteredUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
+ }
+
+ // Get user instance
+ Contact registeredContact = event.getRegisteredUser().getUserContact();
+
+ // Copy all data from registered->user
+ this.copyContact(registeredContact);
+
+ // Add contact instance only once
+ this.uniqueAddContact(registeredContact);
+
+ // Add user name and email address
+ this.addUserNameEmailAddress(registeredContact);
+
+ // Clear all data
+ this.clear();
+ }
+
+ @Override
+ @SuppressWarnings ("ReturnOfCollectionOrArrayField")
+ public List<Contact> allContacts () {
+ // Init list
+ List<Contact> list = new LinkedList<>();
+
+ // Get iterator
+ Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
+
+ // Loop over all
+ while (iterator.hasNext()) {
+ // Get next entry
+ final Cache.Entry<Long, Contact> next = iterator.next();
+
+ // Add value to list
+ list.add(next.getValue());
+ }
+
+ // Return it
+ return list;
+ }
+
+ @Override
+ public void clearEmailAddresses () {
+ // Clear both
+ this.setEmailAddress(null);
+ this.setEmailAddressRepeat(null);
+ }
+
+ @Override
+ public Contact createContactInstance () {
+ // Is all required data set?
+ if (!this.isRequiredPersonalDataSet()) {
+ // No, then abort here
+ throw new FaceletException(new IllegalArgumentException("Not all personal data is set, but createContactInstance() is called.")); //NOI18N
+ }
+
+ // Required personal data must be set
+ assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
+
+ // Generate phone number
+ DialableLandLineNumber phone = new LandLineNumber(this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+ DialableMobileNumber mobile = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
+ DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+ // Create new contact
+ Contact contact = new UserContact(this.getPersonalTitle(), this.getFirstName(), this.getFamilyName());
+ contact.setContactStreet(this.getStreet());
+ contact.setContactHouseNumber(this.getHouseNumber());
+ contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
+ contact.setContactZipCode(this.getZipCode());
+ contact.setContactCity(this.getCity());
+ contact.setContactCountry(this.getCountry());
+ contact.setContactEmailAddress(this.getEmailAddress());
+ contact.setContactBirthday(this.getBirthday());
+ contact.setContactComment(this.getComment());
+
+ // Don't set null or wrong references
+ if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getLandLineAreaCode() != null) && (this.getLandLineNumber() != null) && (this.getLandLineAreaCode() > 0) && (this.getLandLineNumber() > 0)) {
+ // Now the number must be given
+ if (phone.getPhoneAreaCode() == null) {
+ // Is null
+ throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
+ } else if (phone.getPhoneAreaCode() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
+ } else if (phone.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
+ } else if (phone.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set phone number
+ contact.setContactLandLineNumber(phone);
+ }
+
+ // Don't set null or wrong references
+ if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
+ // Now the number must be given
+ if (fax.getPhoneAreaCode() == null) {
+ // Is null
+ throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
+ } else if (fax.getPhoneAreaCode() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
+ } else if (fax.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
+ } else if (fax.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set fax number
+ contact.setContactFaxNumber(fax);
+ }
+
+ // Is the provider set?
+ if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
+ // Is the number set?
+ if (mobile.getPhoneNumber() == null) {
+ // Is null
+ throw new NullPointerException("mobile.phoneNumber is null"); //NOI18N
+ } else if (mobile.getPhoneNumber() < 1) {
+ // Abort here
+ throw new IllegalArgumentException("mobile.phoneNumber is zero or below."); //NOI18N
+ }
+
+ // Set mobile number
+ contact.setContactMobileNumber(mobile);
+ }
+
+ // Return it
+ return contact;
+ }
+
+ @Override
+ public String doChangePersonalContactData () {
+ // This method shall only be called if the user is logged-in
+ if (!this.userLoginController.isUserLoggedIn()) {
+ // Not logged-in
+ throw new IllegalStateException("User is not logged-in"); //NOI18N
+ } else if (!this.isRequiredChangePersonalDataSet()) {
+ // Not all required fields are set
+ throw new FaceletException("Not all required fields are set."); //NOI18N
+ } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
+ // Password not matching
+ this.showFacesMessage("form_login_change_personal:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
+ return ""; //NOI18N
+ }
+
+ // Get contact instance
+ Contact contact = this.userLoginController.getLoggedInUser().getUserContact();
+
+ // It should be there, so run some tests on it
+ assert (contact instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
+ assert (contact.getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
+ assert (contact.getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N
+
+ // Update all fields
+ contact.setContactPersonalTitle(this.getPersonalTitle());
+ contact.setContactFirstName(this.getFirstName());
+ contact.setContactFamilyName(this.getFamilyName());
+ contact.setContactStreet(this.getStreet());
+ contact.setContactHouseNumber(this.getHouseNumber());
+ contact.setContactHouseNumberExtension(this.getHouseNumberExtension());
+ contact.setContactZipCode(this.getZipCode());
+ contact.setContactCity(this.getCity());
+ contact.setContactCountry(this.getCountry());
+
+ // Update contact's mobile number
+ this.isMobileUnlinked = ContactUtils.updateMobileNumber(contact, this.getMobileProvider(), this.getMobileNumber());
+
+ // Update contact's land-line number
+ this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber());
+
+ // Update contact's fax number
+ this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
+
+ // Send it to the EJB
+ this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked);
+
+ // All fine
+ return "contact_data_saved"; //NOI18N
+ }
+
+ /**
+ * Getter for academic title
+ * <p>
+ * @return Academic title
+ */
+ public String getAcademicTitle () {
+ return this.academicTitle;
+ }
+
+ /**
+ * Setter for academic title
+ * <p>
+ * @param academicTitle Academic title
+ */
+ public void setAcademicTitle (final String academicTitle) {
+ this.academicTitle = academicTitle;
+ }
+
+ /**
+ * Getter for birth day
+ * <p>
+ * @return Birth day
+ */
+ @SuppressWarnings ("ReturnOfDateField")
+ public Date getBirthday () {
+ return this.birthday;
+ }
+
+ /**
+ * Setter for birth day
+ * <p>
+ * @param birthday Birth day
+ */
+ @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+ public void setBirthday (final Date birthday) {
+ this.birthday = birthday;
+ }
+
+ /**
+ * Getter for city name
+ * <p>
+ * @return City name
+ */
+ public String getCity () {
+ return this.city;
+ }
+
+ /**
+ * Setter for city name
+ * <p>
+ * @param city City name
+ */
+ public void setCity (final String city) {
+ this.city = city;
+ }
+
+ /**
+ * Getter for comments
+ * <p>
+ * @return Comments
+ */
+ public String getComment () {
+ return this.comment;
+ }
+
+ /**
+ * Setter for comment
+ * <p>
+ * @param comment Comments
+ */
+ public void setComment (final String comment) {
+ this.comment = comment;
+ }
+
+ @Override
+ public String getControllerType () {
+ return "general"; //NOI18N
+ }
+
+ @Override
+ @Deprecated
+ public void setControllerType (final String controllerType) {
+ throw new UnsupportedOperationException("Setting controller type is not supported."); //NOI18N
+ }
+
+ /**
+ * Getter for country instance
+ * <p>
+ * @return Country instance
+ */
+ public Country getCountry () {
+ return this.country;
+ }
+
+ /**
+ * Setter for country instance
+ * <p>
+ * @param country Country instance
+ */
+ public void setCountry (final Country country) {
+ this.country = country;
+ }
+
+ @Override
+ public String getEmailAddress () {
+ return this.emailAddress;
+ }
+
+ /**
+ * Setter for email address
+ * <p>
+ * @param emailAddress Email address
+ */
+ public void setEmailAddress (final String emailAddress) {
+ this.emailAddress = emailAddress;
+ }
+
+ /**
+ * Getter for email address, repeated
+ * <p>
+ * @return the emailAddress, repeated
+ */
+ public String getEmailAddressRepeat () {
+ return this.emailAddressRepeat;
+ }
+
+ /**
+ * Setter for email address repeated
+ * <p>
+ * @param emailAddressRepeat the emailAddress to set
+ */
+ public void setEmailAddressRepeat (final String emailAddressRepeat) {
+ this.emailAddressRepeat = emailAddressRepeat;
+ }
+
+ /**
+ * Family name
+ * <p>
+ * @return the familyName
+ */
+ public String getFamilyName () {
+ return this.familyName;
+ }
+
+ /**
+ * Family name
+ * <p>
+ * @param familyName the familyName to set
+ */
+ public void setFamilyName (final String familyName) {
+ this.familyName = familyName;
+ }
+
+ /**
+ * Getter for fax number's area code
+ * <p>
+ * @return Fax number's area code
+ */
+ public Integer getFaxAreaCode () {
+ return this.faxAreaCode;
+ }
+
+ /**
+ * Setter for fax number's area code
+ * <p>
+ * @param faxAreaCode Fax number's area code
+ */
+ public void setFaxAreaCode (final Integer faxAreaCode) {
+ this.faxAreaCode = faxAreaCode;
+ }
+
+ /**
+ * Getter for fax's country instance
+ * <p>
+ * @return Fax' country instance
+ */
+ public Country getFaxCountry () {
+ return this.faxCountry;
+ }
+
+ /**
+ * Setter for fax's country instance
+ * <p>
+ * @param faxCountry Fax' country instance
+ */
+ public void setFaxCountry (final Country faxCountry) {
+ this.faxCountry = faxCountry;
+ }
+
+ /**
+ * Getter for fax number
+ * <p>
+ * @return Fax number
+ */
+ public Long getFaxNumber () {
+ return this.faxNumber;
+ }
+
+ /**
+ * Setter for fax number
+ * <p>
+ * @param faxNumber Fax number
+ */
+ public void setFaxNumber (final Long faxNumber) {
+ this.faxNumber = faxNumber;
+ }
+
+ /**
+ * First name
+ * <p>
+ * @return the first name
+ */
+ public String getFirstName () {
+ return this.firstName;
+ }
+
+ /**
+ * First name
+ * <p>
+ * @param firstName the first name to set
+ */
+ public void setFirstName (final String firstName) {
+ this.firstName = firstName;
+ }
+
+ /**
+ * House number
+ * <p>
+ * @return the houseNumber
+ */
+ public Short getHouseNumber () {
+ return this.houseNumber;
+ }
+
+ /**
+ * House number
+ * <p>
+ * @param houseNumber the houseNumber to set
+ */
+ public void setHouseNumber (final Short houseNumber) {
+ this.houseNumber = houseNumber;
+ }
+
+ /**
+ * Getter for house number extension, example: 123a 'a' is then the
+ * extension and 123 is the house number.
+ * <p>
+ * @return House number extension
+ */
+ public String getHouseNumberExtension () {
+ return this.houseNumberExtension;
+ }
+
+ /**
+ * Setter for house number extension
+ * <p>
+ * @param houseNumberExtension House number extension
+ */
+ public void setHouseNumberExtension (final String houseNumberExtension) {
+ this.houseNumberExtension = houseNumberExtension;
+ }
+
+ /**
+ * Getter for land-line number's area code
+ * <p>
+ * @return Land-line number's area code
+ */
+ public Integer getLandLineAreaCode () {
+ return this.landLineAreaCode;
+ }
+
+ /**
+ * Setter for land-line number's area code
+ * <p>
+ * @param landLineAreaCode Land-line number's area code
+ */
+ public void setLandLineAreaCode (final Integer landLineAreaCode) {
+ this.landLineAreaCode = landLineAreaCode;
+ }
+
+ /**
+ * Getter for land-line number's country instance
+ * <p>
+ * @return Land-line number's country instance
+ */
+ public Country getLandLineCountry () {
+ return this.landLineCountry;
+ }
+
+ /**
+ * Setter for land-line number's country instance
+ * <p>
+ * @param landLineCountry Land-line number's country instance
+ */
+ public void setLandLineCountry (final Country landLineCountry) {
+ this.landLineCountry = landLineCountry;
+ }
+
+ /**
+ * Getter for land-line number
+ * <p>
+ * @return Land-line number
+ */
+ public Long getLandLineNumber () {
+ return this.landLineNumber;
+ }
+
+ /**
+ * Setter for land-line number
+ * <p>
+ * @param landLineNumber Land-line number
+ */
+ public void setLandLineNumber (final Long landLineNumber) {
+ this.landLineNumber = landLineNumber;
+ }
+
+ /**
+ * Getter for mobile number
+ * <p>
+ * @return Mobile number
+ */
+ public Long getMobileNumber () {
+ return this.mobileNumber;
+ }
+
+ /**
+ * Setter for mobile number
+ * <p>
+ * @param mobileNumber Mobile number
+ */
+ public void setMobileNumber (final Long mobileNumber) {
+ this.mobileNumber = mobileNumber;
+ }
+
+ /**
+ * Getter for mobile number's carrier
+ * <p>
+ * @return Mobile number's carrier
+ */
+ public MobileProvider getMobileProvider () {
+ return this.mobileProvider;
+ }
+
+ /**
+ * Setter for mobile number's provider
+ * <p>
+ * @param mobileProvider Mobile number's provider
+ */
+ public void setMobileProvider (final MobileProvider mobileProvider) {
+ this.mobileProvider = mobileProvider;
+ }
+
+ /**
+ * Getter for personal title
+ * <p>
+ * @return Personal title
+ */
+ public PersonalTitle getPersonalTitle () {
+ return this.personalTitle;
+ }
+
+ /**
+ * Setter for personal title
+ * <p>
+ * @param personalTitle Personal title
+ */
+ public void setPersonalTitle (final PersonalTitle personalTitle) {
+ this.personalTitle = personalTitle;
+ }
+
+ /**
+ * Getter for street
+ * <p>
+ * @return Street
+ */
+ public String getStreet () {
+ return this.street;
+ }
+
+ /**
+ * Setter for street
+ * <p>
+ * @param street Street
+ */
+ public void setStreet (final String street) {
+ this.street = street;
+ }
+
+ /**
+ * Getter for ZIP code
+ * <p>
+ * @return ZIP code
+ */
+ public Integer getZipCode () {
+ return this.zipCode;
+ }
+
+ /**
+ * Setter for ZIP code
+ * <p>
+ * @param zipCode ZIP code
+ */
+ public void setZipCode (final Integer zipCode) {
+ this.zipCode = zipCode;
+ }
+
+ /**
+ * Post-construction method
+ */
+ @PostConstruct
+ public void init () {
+ // Get all contacts
+ final List<Contact> selectable = new LinkedList<>();
+
+ // Is cache there?
+ if (!this.contactsCache.iterator().hasNext()) {
+ // Get whole list
+ List<Contact> list = this.contactBean.allContacts();
+
+ // Add all
+ for (final Iterator<Contact> iterator = list.iterator(); iterator.hasNext();) {
+ // Get next element
+ final Contact next = iterator.next();
+
+ // Add it to cache
+ this.contactsCache.put(next.getContactId(), next);
+ this.emailAddressCache.put(next.getContactId(), next.getContactEmailAddress());
+ selectable.add(next);
+ }
+ }
+
+ // Get all users
+ List<User> allUsers = this.userController.allUsers();
+
+ // Get iterator
+ Iterator<Contact> iterator = selectable.iterator();
+
+ // Loop through it
+ while (iterator.hasNext()) {
+ // Get next element
+ Contact next = iterator.next();
+
+ // Get iterator
+ Iterator<User> userIterator = allUsers.iterator();
+
+ // Loop through all users
+ while (userIterator.hasNext()) {
+ // Get user instance
+ User nextUser = userIterator.next();
+
+ // Is contact same?
+ if (Objects.equals(next, nextUser.getUserContact())) {
+ // Found same
+ iterator.remove();
+ break;
+ }
+ }
+ }
+
+ // Set contact list
+ this.selectableContacts = selectable;
+ }
+
+ @Override
+ public boolean isEmailAddressRegistered (final Contact contact) {
+ // Cherck parameter
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactEmailAddress() == null) {
+ // Throw again
+ throw new NullPointerException("contact.contactEmailAddress is null"); //NOI18N
+ } else if (contact.getContactEmailAddress().isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("contact.contactEmailAddress is empty."); //NOI18N
+ }
+
+ // Determine it
+ return ((this.emailAddressCache instanceof List) && (this.emailAddressCache.containsKey(contact.getContactId())));
+ }
+
+ @Override
+ public boolean isRequiredChangePersonalDataSet () {
+ return ((this.getPersonalTitle() != null) &&
+ (this.getFirstName() != null) &&
+ (this.getFamilyName() != null) &&
+ (this.getStreet() != null) &&
+ (this.getHouseNumber() != null) &&
+ (this.getZipCode() != null) &&
+ (this.getCity() != null));
+ }
+
+ @Override
+ public boolean isRequiredPersonalDataSet () {
+ return ((this.getPersonalTitle() != null) &&
+ (this.getFirstName() != null) &&
+ (this.getFamilyName() != null) &&
+ (this.getStreet() != null) &&
+ (this.getHouseNumber() != null) &&
+ (this.getZipCode() != null) &&
+ (this.getCity() != null) &&
+ (this.getEmailAddress() != null) &&
+ (this.getEmailAddressRepeat() != null));
+ }
+
+ @Override
+ public boolean isSameEmailAddressEntered () {
+ return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
+ }
+
+ /**
+ * Returns a list of all selectable contacts for user creation. Contacts
+ * from already existing users are excluded in this list.
+ * <p>
+ * @return A list of all selectable contacts
+ */
+ public List<Contact> selectableContacts () {
+ return Collections.unmodifiableList(this.selectableContacts);
+ }
+
+ @Override
+ public void updateContactDataFromController (final Contact contact) {
+ // Is the instance valid?
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
+ // Set all
+ this.copyContact(contact);
+ }
+
+ /**
+ * Adds email address to bean's internal list.
+ * <p>
+ * @param contact Contact instance
+ */
+ private void addUserNameEmailAddress (final Contact contact) {
+ // Make sure the entry is not added yet
+ if (this.emailAddressCache.containsKey(contact.getContactId())) {
+ // Already added
+ throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N
+ }
+
+ // Add email addres
+ this.emailAddressCache.put(contact.getContactId(), contact.getContactEmailAddress());
+ }
+
+ /**
+ * Clears this bean
+ */
+ private void clear () {
+ // Clear all data
+ // - personal data
+ this.setPersonalTitle(null);
+ this.setAcademicTitle(null);
+ this.setFirstName(null);
+ this.setFamilyName(null);
+ this.setStreet(null);
+ this.setHouseNumber(null);
+ this.setHouseNumberExtension(null);
+ this.setZipCode(null);
+ this.setCity(null);
+ this.setCountry(null);
+
+ // - contact data
+ this.clearEmailAddresses();
+ this.setLandLineAreaCode(null);
+ this.setLandLineCountry(null);
+ this.setLandLineNumber(null);
+ this.setMobileProvider(null);
+ this.setMobileNumber(null);
+ this.setFaxAreaCode(null);
+ this.setFaxCountry(null);
+ this.setFaxNumber(null);
+
+ // - other data
+ this.setBirthday(null);
+ this.setComment(null);
+ }
+
+ /**
+ * Copies given contact into the controller
+ * <p>
+ * @param contact Contact instance
+ */
+ private void copyContact (final Contact contact) {
+ // Is the instance valid?
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
+ // Copy all fields:
+ // - base data
+ this.setPersonalTitle(contact.getContactPersonalTitle());
+ this.setAcademicTitle(contact.getContactTitle());
+ this.setFirstName(contact.getContactFirstName());
+ this.setFamilyName(contact.getContactFamilyName());
+ this.setStreet(contact.getContactStreet());
+ this.setHouseNumber(contact.getContactHouseNumber());
+ this.setHouseNumberExtension(contact.getContactHouseNumberExtension());
+ this.setZipCode(contact.getContactZipCode());
+ this.setCity(contact.getContactCity());
+ this.setCountry(contact.getContactCountry());
+ this.setEmailAddress(contact.getContactEmailAddress());
+ this.setBirthday(contact.getContactBirthday());
+ this.setComment(contact.getContactComment());
+
+ // Get mobile, phone and fax instance
+ DialableMobileNumber mobile = contact.getContactMobileNumber();
+ DialableFaxNumber fax = contact.getContactFaxNumber();
+ DialableLandLineNumber phone = contact.getContactLandLineNumber();
+
+ // - contact data
+ if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
+ this.setLandLineCountry(phone.getPhoneCountry());
+ this.setLandLineAreaCode(phone.getPhoneAreaCode());
+ this.setLandLineNumber(phone.getPhoneNumber());
+ }
+
+ if ((mobile instanceof DialableMobileNumber) && (mobile.getMobileProvider() instanceof MobileProvider)) {
+ this.setMobileProvider(mobile.getMobileProvider());
+ this.setMobileNumber(mobile.getPhoneNumber());
+ }
+
+ if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
+ this.setFaxCountry(fax.getPhoneCountry());
+ this.setFaxAreaCode(fax.getPhoneAreaCode());
+ this.setFaxNumber(fax.getPhoneNumber());
+ }
+ }
+
+ /**
+ * Removes given contact from all lists
+ * <p>
+ * @param contact Contact instance to remove
+ */
+ private void removeContact (final Contact contact) {
+ // Is the instance valid?
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
+ // Remove from general list
+ if (!this.contactsCache.remove(contact.getContactId())) {
+ // Did not remove contact
+ throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N
+ }
+
+ // Remove from other lists
+ this.emailAddressCache.remove(contact.getContactId());
+ }
+
+ /**
+ * Adds unique instance to contact list. First any existing instance is
+ * being removed, then the new instance is added.
+ * <p>
+ * @param contact Contact instance to add uniquely
+ */
+ private void uniqueAddContact (final Contact contact) {
+ // Is the instance valid?
+ if (null == contact) {
+ // Throw NPE
+ throw new NullPointerException("contact is null"); //NOI18N
+ } else if (contact.getContactId() == null) {
+ // Throw NPE
+ throw new NullPointerException("contact.contactId is null"); //NOI18N
+ } else if (contact.getContactId() < 1) {
+ // Not valid id number
+ throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N
+ }
+
+ // Get iterator from list
+ Iterator<Cache.Entry<Long, Contact>> iterator = this.contactsCache.iterator();
+
+ // "Walk" through all entries
+ while (iterator.hasNext()) {
+ // Get next element
+ Cache.Entry<Long, Contact> next = iterator.next();
+
+ // Is id number the same?
+ if (Objects.equals(contact.getContactId(), next.getKey())) {
+ // Found entry, so remove it and abort
+ this.removeContact(next.getValue());
+ break;
+ }
+ }
+
+ // Add contact to list
+ this.contactsCache.put(contact.getContactId(), contact);
+ }
+
+}