X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjjobs%2Fbeans%2Fcontact%2FJobsContactWebSessionBean.java;h=b6cd5ee002f9d6e40d226e05c950f466a30894be;hb=09d2137f89da9234cd2fed7af9021b24c3934662;hp=cb1e5057c8e03b9d21ef65c007b9a1dcf6ca75bf;hpb=3c6646065421eafaedd4dc105105f49142a68449;p=jjobs-war.git diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java index cb1e5057..b6cd5ee0 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java @@ -17,8 +17,10 @@ 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; @@ -34,29 +36,41 @@ import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; import org.mxchange.jcontacts.contact.UserContact; import org.mxchange.jcontacts.contact.gender.Gender; +import org.mxchange.jcontacts.contact.utils.ContactUtils; +import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; +import org.mxchange.jcontacts.events.contact.deleted.AdminDeletedContactEvent; import org.mxchange.jcontacts.events.contact.update.AdminUpdatedContactEvent; +import org.mxchange.jcontacts.events.fax.unlinked.AdminUnlinkedFaxNumberEvent; +import org.mxchange.jcontacts.events.landline.unlinked.AdminUnlinkedLandLineNumberEvent; +import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent; import org.mxchange.jcontacts.exceptions.ContactNotFoundException; import org.mxchange.jcountry.data.Country; +import org.mxchange.jjobs.beans.BaseJobsController; import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController; -import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jjobs.beans.user.JobsUserWebSessionController; +import org.mxchange.jphone.events.mobile.remove.AdminRemoveMobileNumberFromListEvent; 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.confirmation.UserConfirmedAccountEvent; import org.mxchange.jusercore.events.login.UserLoggedInEvent; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; -import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; +import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; +import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent; +import org.mxchange.jusercore.model.user.User; /** - * A general contact bean (controller) + * A general contact controller (bean) *

* @author Roland Haeder */ @Named ("contactController") @SessionScoped -public class JobsContactWebSessionBean implements JobsContactWebSessionController { +public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController { /** * Serial number @@ -68,16 +82,6 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle */ private Date birthday; - /** - * Cellphone number's carrier - */ - private MobileProvider cellphoneCarrier; - - /** - * Cellphone number - */ - private Long cellphoneNumber; - /** * City */ @@ -96,7 +100,7 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle /** * Contact list */ - private List contactList; + private final List contacts; /** * Country instance @@ -109,14 +113,14 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle private String emailAddress; /** - * Email address list + * Email address repeated */ - private List emailAddressList; + private String emailAddressRepeat; /** - * Email address repeated + * Email address list */ - private String emailAddressRepeat; + private final List emailAddresses; /** * Family name @@ -154,31 +158,77 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle private Short houseNumber; /** - * Login bean (controller) + * House number extension */ - @Inject - private JobsUserLoginWebSessionController loginController; + private String houseNumberExtension; /** - * Phone number area code + * Whether a fax entry has been unlinked */ - private Integer phoneAreaCode; + private boolean isFaxUnlinked; /** - * Country instance for phone number + * Whether a land-line number has been unlinked */ - private Country phoneCountry; + private boolean isLandLineUnlinked; /** - * Phone number + * Whether a mobile entry has been unlinked */ - private Long phoneNumber; + private boolean isMobileUnlinked; + + /** + * Land-line number area code + */ + private Integer landLineAreaCode; + + /** + * Country instance for land-line number + */ + private Country landLineCountry; + + /** + * Land-line number + */ + private Long landLineNumber; + + /** + * Mobile number + */ + private Long mobileNumber; + + /** + * Mobile provider + */ + private MobileProvider mobileProvider; + + /** + * A list of all selectable contacts + */ + private List selectableContacts; /** * Street */ private String street; + /** + * Title + */ + private String title; + + /** + * Regular user controller + */ + @Inject + private JobsUserWebSessionController userController; + + /** + * Login controller (bean) + */ + @Inject + private JobsUserLoginWebSessionController userLoginController; + /** * ZIP code */ @@ -188,9 +238,6 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle * Default constructor */ public JobsContactWebSessionBean () { - // Set gender to UNKNOWN - this.gender = Gender.UNKNOWN; - // Try it try { // Get initial context @@ -202,75 +249,220 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle // Throw again throw new FaceletException(e); } + + // Init lists/maps + this.contacts = new LinkedList<>(); + this.emailAddresses = new LinkedList<>(); } @Override - public void addEmailAddress (final String contactEmailAddress) { - // Add it - this.emailAddressList.add(contactEmailAddress); + public void afterAdminAddedContactEvent (@Observes final AdminAddedContactEvent 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 + } + + // Clear this bean + this.clear(); + + // Call other method + this.uniqueAddContact(event.getAddedContact()); + + // Add to selectable contacts + this.selectableContacts.add(event.getAddedContact()); } @Override - public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent event) { - // Trace message - System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N + public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent 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(); + } + + @Override + public void afterAdminDeletedContactEvent (@Observes final AdminDeletedContactEvent event) { // event should not be null if (null == event) { // Throw NPE throw new NullPointerException("event is null"); //NOI18N - } else if (event.getUpdatedContact()== null) { + } else if (event.getDeletedContact() == null) { // Throw NPE again - throw new NullPointerException("event.user is null"); //NOI18N - } else if (event.getUpdatedContact().getContactId() == null) { + throw new NullPointerException("event.deletedContact is null"); //NOI18N + } else if (event.getDeletedContact().getContactId() == null) { // userId is null - throw new NullPointerException("event.user.userId is null"); //NOI18N - } else if (event.getUpdatedContact().getContactId() < 1) { + throw new NullPointerException("event.deletedContact.contactId is null"); //NOI18N + } else if (event.getDeletedContact().getContactId() < 1) { // Not avalid id - throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedContact(), event.getDeletedContact().getContactId())); //NOI18N } - // Get iterator from list - Iterator iterator = this.contactList.iterator(); + // Remove from both lists + this.contacts.remove(event.getDeletedContact()); + this.selectableContacts.remove(event.getDeletedContact()); - // "Walk" through all entries - while (iterator.hasNext()) { - // Get next element - Contact next = iterator.next(); + // Clear all data + this.clear(); + } - // Is id number the same? - if (Objects.equals(event.getUpdatedContact().getContactId(), next.getContactId())) { - // Found entry, so remove it and abort - this.contactList.remove(next); + @Override + public void afterAdminLinkedUserEvent (@Observes final AdminLinkedUserEvent 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 also email from list - this.emailAddressList.remove(next.getContactEmailAddress()); - break; - } + // Remove contact from list available contacts list + this.selectableContacts.remove(event.getLinkedUser().getUserContact()); + + // Clear all data + this.clear(); + } + + @Override + public void afterAdminUnlinkedFaxContactDataEvent (@Observes final AdminUnlinkedFaxNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlinkedFaxNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N + } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N + } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N } - // Add contact to list - this.contactList.add(event.getUpdatedContact()); + // Remove it from list + this.removeFromContacts(event.getUnlinkedFaxNumber()); - // Add email address to list - this.emailAddressList.add(event.getUpdatedContact().getContactEmailAddress()); + // Clear all data + this.clear(); } @Override - public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) { - // Trace message - System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N + public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final AdminUnlinkedLandLineNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlinkedLandLineNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N + } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N + } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N + } + // Remove it from list + this.removeFromContacts(event.getUnlinkedLandLineNumber()); + + // Clear all data + this.clear(); + } + + @Override + public void afterAdminUnlinkedMobileContactDataEvent (@Observes final AdminUnlinkedMobileNumberEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUnlinkedMobileNumber() == null) { + // Throw NPE again + throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N + } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) { + // userId is null + throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N + } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N + } + + // Remove it from list + this.removeFromContacts(event.getUnlinkedMobileNumber()); + + // Clear all data + this.clear(); + } + + @Override + public void afterAdminUpdatedContactDataEvent (@Observes final AdminUpdatedContactEvent 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("contactId of contact={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.emailAddresses.add(event.getUpdatedContact().getContactEmailAddress()); + } + + @Override + public void afterUserRegistrationEvent (@Observes final UserRegisteredEvent 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.user is null"); //NOI18N + throw new NullPointerException("event.registeredUser is null"); //NOI18N } else if (event.getRegisteredUser().getUserId() == null) { // userId is null - throw new NullPointerException("event.user.userId is null"); //NOI18N + 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 @@ -279,37 +471,52 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle // Get user instance Contact registeredContact = event.getRegisteredUser().getUserContact(); - // Debug message - System.out.println(MessageFormat.format("ContactWebBean:afterRegistration: registeredContact={0}", registeredContact)); //NOI18N - // 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(); - - // Trace message - System.out.println("ContactWebBean:afterRegistration: EXIT!"); //NOI18N } @Override - public void afterUserLogin (final @Observes UserLoggedInEvent event) { - // Trace message - System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N + public void afterUserConfirmedAccountEventEvent (@Observes final UserConfirmedAccountEvent 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()); + } + @Override + public void afterUserLoginEvent (@Observes final UserLoggedInEvent 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.user is null"); //NOI18N + throw new NullPointerException("event.loggedInUser is null"); //NOI18N } else if (event.getLoggedInUser().getUserId() == null) { // userId is null - throw new NullPointerException("event.user.userId is null"); //NOI18N + 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 @@ -317,66 +524,50 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle // Copy all data to this bean this.copyContact(event.getLoggedInUser().getUserContact()); - - // Trace message - System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N } - /** - * Clears this bean - */ @Override - public void clear () { - // Clear all data - // - personal data - this.setGender(Gender.UNKNOWN); - this.setFirstName(null); - this.setFamilyName(null); - this.setStreet(null); - this.setHouseNumber(null); - this.setZipCode(null); - this.setCity(null); - this.setCountry(null); - - // - contact data - this.setEmailAddress(null); - this.setEmailAddressRepeat(null); - this.setPhoneAreaCode(null); - this.setCellphoneCarrier(null); - this.setFaxAreaCode(null); + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allContacts () { + // Debug message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactController.allContacts: contactList.size()={0} - EXIT!", this.contactList.size())); - // - other data - this.setBirthday(null); - this.setComment(null); + // Return un-modified list + return this.contacts; } @Override public Contact createContactInstance () { // User message - //this.getLogger().logTrace("createContactInstance: CALLED!"); + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: CALLED!", this.getClass().getSimpleName())); - // Required personal data must be set - assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N - - // Create new contact instance - Contact localContact = new UserContact(); + // 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 + } // Generate phone number - DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber()); - DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber()); + 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.getGender(), 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()); + // Debug message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: this.emailAddress={1}", this.getClass().getSimpleName(), this.getEmailAddress())); // Don't set null or wrong references - if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) { + 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 @@ -418,54 +609,48 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle } // Is the provider set? - if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) { + if ((mobile instanceof DialableMobileNumber) && (this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) { // Is the number set? - if (cellphone.getPhoneNumber() == null) { + if (mobile.getPhoneNumber() == null) { // Is null - throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N - } else if (cellphone.getPhoneNumber() < 1) { + throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N + } else if (mobile.getPhoneNumber() < 1) { // Abort here - throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N + throw new IllegalArgumentException("mobileNumber.phoneNumber is zero or below."); //NOI18N } - // Set cellphone number - contact.setContactCellphoneNumber(cellphone); + // Set mobile number + contact.setContactMobileNumber(mobile); } - contact.setContactBirthday(this.getBirthday()); - contact.setContactComment(this.getComment()); - - // Created timestamp and ownContact - contact.setContactOwnContact(Boolean.TRUE); - // Trace message - //this.getLogger().logTrace(MessageFormat.format("createContactInstance: localContact={0} - EXIT!", localContact)); - + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createContactInstance: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); // Return it - return localContact; + return contact; } @Override public String doChangePersonalContactData () { // This method shall only be called if the user is logged-in - if (!this.loginController.isUserLoggedIn()) { + 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.loginController.ifCurrentPasswordMatches()) { + } else if (!this.userLoginController.ifCurrentPasswordMatches()) { // Password not matching - throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser())); + this.showFacesMessage("form_login_change_personal:currentPassword", "ERROR_USER_CURRENT_PASSWORD_MISMATCHING"); //NOI18N + return ""; //NOI18N } // Get contact instance - Contact contact = this.loginController.getLoggedInUser().getUserContact(); + Contact contact = this.userLoginController.getLoggedInUser().getUserContact(); // It should be there, so run some tests on it - assert (contact instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N - assert (contact.getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N - assert (contact.getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", contact.getContactId()); //NOI18N + 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.setContactGender(this.getGender()); @@ -473,77 +658,39 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle 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()); - // Is there a phone number? - if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { - // Debug message - System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: phoneId={0}", contact.getContactLandLineNumber().getPhoneId())); //NOI18N + // Update contact's mobile number + this.isMobileUnlinked = ContactUtils.updateCellPhoneNumber(contact, this.getMobileProvider(), this.getMobileNumber()); - // Yes, then update as well - contact.getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode()); - contact.getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber()); - } + // Update contact's land-line number + this.isLandLineUnlinked = ContactUtils.updateLandLineNumber(contact, this.getLandLineCountry(), this.getLandLineAreaCode(), this.getLandLineNumber()); - // Is there a fax number? - if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { - // Debug message - System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: faxId={0}", contact.getContactFaxNumber().getPhoneId())); //NOI18N - - // Yes, then update as well - contact.getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode()); - contact.getContactFaxNumber().setPhoneNumber(this.getFaxNumber()); - } - - // Is there a cellphone number? - if (contact.getContactCellphoneNumber() instanceof DialableCellphoneNumber) { - // Debug message - System.out.println(MessageFormat.format("ContactWebBean:doChangePersonalData: cellPhoneId={0}", contact.getContactCellphoneNumber().getPhoneId())); //NOI18N - - // Yes, then update as well - contact.getContactCellphoneNumber().setCellphoneProvider(this.getCellphoneCarrier()); - contact.getContactCellphoneNumber().setPhoneNumber(this.getCellphoneNumber()); - } + // Update contact's fax number + this.isFaxUnlinked = ContactUtils.updateFaxNumber(contact, this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); // Send it to the EJB - this.contactBean.updateContactPersonalData(contact); + this.contactBean.updateContactData(contact, this.isMobileUnlinked, this.isLandLineUnlinked, this.isFaxUnlinked); // All fine return "contact_data_saved"; //NOI18N } @Override + @SuppressWarnings ("ReturnOfDateField") public Date getBirthday () { return this.birthday; } @Override + @SuppressWarnings ("AssignmentToDateFieldFromParameter") public void setBirthday (final Date birthday) { this.birthday = birthday; } - @Override - public MobileProvider getCellphoneCarrier () { - return this.cellphoneCarrier; - } - - @Override - public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) { - this.cellphoneCarrier = cellphoneCarrier; - } - - @Override - public Long getCellphoneNumber () { - return this.cellphoneNumber; - } - - @Override - public void setCellphoneNumber (Long cellphoneNumber) { - this.cellphoneNumber = cellphoneNumber; - } - @Override public String getCity () { return this.city; @@ -564,6 +711,17 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle 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 + } + @Override public Country getCountry () { return this.country; @@ -665,33 +823,63 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle } @Override - public Integer getPhoneAreaCode () { - return this.phoneAreaCode; + public String getHouseNumberExtension () { + return this.houseNumberExtension; } @Override - public void setPhoneAreaCode (final Integer phoneAreaCode) { - this.phoneAreaCode = phoneAreaCode; + public void setHouseNumberExtension (final String houseNumberExtension) { + this.houseNumberExtension = houseNumberExtension; } @Override - public Country getPhoneCountry () { - return this.phoneCountry; + public Integer getLandLineAreaCode () { + return this.landLineAreaCode; } @Override - public void setPhoneCountry (final Country phoneCountry) { - this.phoneCountry = phoneCountry; + public void setLandLineAreaCode (final Integer landLineAreaCode) { + this.landLineAreaCode = landLineAreaCode; } @Override - public Long getPhoneNumber () { - return this.phoneNumber; + public Country getLandLineCountry () { + return this.landLineCountry; } @Override - public void setPhoneNumber (final Long phoneNumber) { - this.phoneNumber = phoneNumber; + public void setLandLineCountry (final Country landLineCountry) { + this.landLineCountry = landLineCountry; + } + + @Override + public Long getLandLineNumber () { + return this.landLineNumber; + } + + @Override + public void setLandLineNumber (final Long landLineNumber) { + this.landLineNumber = landLineNumber; + } + + @Override + public Long getMobileNumber () { + return this.mobileNumber; + } + + @Override + public void setMobileNumber (Long mobileNumber) { + this.mobileNumber = mobileNumber; + } + + @Override + public MobileProvider getMobileProvider () { + return this.mobileProvider; + } + + @Override + public void setMobileProvider (final MobileProvider mobileProvider) { + this.mobileProvider = mobileProvider; } @Override @@ -704,6 +892,16 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle this.street = street; } + @Override + public String getTitle () { + return this.title; + } + + @Override + public void setTitle (final String title) { + this.title = title; + } + @Override public Integer getZipCode () { return this.zipCode; @@ -720,15 +918,62 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle @PostConstruct public void init () { // Get full email address list for reducing EJB calls - this.emailAddressList = this.contactBean.getEmailAddressList(); + this.emailAddresses.addAll(this.contactBean.getEmailAddressList()); // Get full contact list - this.contactList = this.contactBean.getAllContacts(); + this.contacts.addAll(this.contactBean.getAllContacts()); + + // Get all users + List allUsers = this.userController.allUsers(); + + // Get all contacts + List allContacts = this.contactBean.getAllContacts(); + + // Get iterator + Iterator iterator = allContacts.iterator(); + + // Loop through it + while (iterator.hasNext()) { + // Get next element + Contact next = iterator.next(); + + // Get iterator + Iterator 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 = allContacts; } @Override public boolean isEmailAddressRegistered (final Contact contact) { - return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(contact.getContactEmailAddress()))); + // 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.emailAddresses instanceof List) && (this.emailAddresses.contains(contact.getContactEmailAddress()))); } @Override @@ -769,7 +1014,7 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle this.clear(); // Try to lookup it in visible user list - for (final Iterator iterator = this.contactList.iterator(); iterator.hasNext();) { + for (final Iterator iterator = this.contacts.iterator(); iterator.hasNext();) { // Get next user Contact next = iterator.next(); @@ -795,21 +1040,50 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle } @Override - public void updateContactDataFromController (final Contact userContact) { + public void removeMobileNumberFromListEvent (@Observes final AdminRemoveMobileNumberFromListEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getMobileNumberList() == null) { + // Throw NPE again + throw new NullPointerException("event.mobileList is null"); //NOI18N + } else if (event.getMobileNumberList().isEmpty()) { + // List is empty, no need to check + return; + } + + // Check all entries + for (final Contact contact : this.contacts) { + // Is the mobile instance set and in list? + if ((contact.getContactMobileNumber() instanceof DialableMobileNumber) && (event.getMobileNumberList().contains(contact.getContactMobileNumber()))) { + // Found it, so remvoe it from list + event.getMobileNumberList().remove(contact.getContactMobileNumber()); + } + } + } + + @Override + public List selectableContacts () { + return Collections.unmodifiableList(this.selectableContacts); + } + + @Override + public void updateContactDataFromController (final Contact contact) { // Is the instance valid? - if (null == userContact) { + if (null == contact) { // Throw NPE - throw new NullPointerException("userContact is null"); //NOI18N - } else if (userContact.getContactId() == null) { + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { // Throw NPE - throw new NullPointerException("userContact.contactId is null"); //NOI18N - } else if (userContact.getContactId() < 1) { + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("userContact.contactId={0} is not valid.", userContact.getContactId())); //NOI18N + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N } // Set all - this.copyContact(userContact); + this.copyContact(contact); } /** @@ -819,13 +1093,47 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle */ private void addUserNameEmailAddress (final Contact contact) { // Make sure the entry is not added yet - if (this.emailAddressList.contains(contact.getContactEmailAddress())) { + if (this.emailAddresses.contains(contact.getContactEmailAddress())) { // Already added throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", contact.getContactEmailAddress())); //NOI18N } // Add email addres - this.emailAddressList.add(contact.getContactEmailAddress()); + this.emailAddresses.add(contact.getContactEmailAddress()); + } + + /** + * Clears this bean + */ + private void clear () { + // Clear all data + // - personal data + this.setGender(null); + this.setTitle(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.setEmailAddress(null); + this.setEmailAddressRepeat(null); + 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); } /** @@ -834,42 +1142,216 @@ public class JobsContactWebSessionBean implements JobsContactWebSessionControlle * @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.setGender(contact.getContactGender()); + this.setTitle(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 cellphone, phone and fax instance - DialableCellphoneNumber cellphone = contact.getContactCellphoneNumber(); + // 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.setPhoneCountry(phone.getPhoneCountry()); - this.setPhoneAreaCode(phone.getPhoneAreaCode()); - this.setPhoneNumber(phone.getPhoneNumber()); + this.setLandLineCountry(phone.getPhoneCountry()); + this.setLandLineAreaCode(phone.getPhoneAreaCode()); + this.setLandLineNumber(phone.getPhoneNumber()); } - if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) { - this.setCellphoneCarrier(cellphone.getCellphoneProvider()); - this.setCellphoneNumber(cellphone.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()); } - this.setEmailAddress(contact.getContactEmailAddress()); + } - // -- other data - this.setBirthday(contact.getContactBirthday()); - this.setComment(contact.getContactComment()); + /** + * Removes given contact from all lists + *

+ * @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.contacts.remove(contact)) { + // Did not remove contact + throw new IllegalStateException(MessageFormat.format("contact {0} was not removed.", contact.getContactId())); //NOI18N + } + + // Remove from other lists + this.emailAddresses.remove(contact.getContactEmailAddress()); + } + + /** + * Removes given fax number from all contacts + *

+ * @param faxNumber Fax number to remove + */ + private void removeFromContacts (final DialableFaxNumber faxNumber) { + // Is it valid? + if (null == faxNumber) { + // Throw NPE + throw new NullPointerException("faxNumber is null"); //NOI18N + } else if (faxNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N + } else if (faxNumber.getPhoneId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N + } + + // Loop through all contacts + for (final Contact contact : this.contacts) { + // Is the number set? + if (Objects.equals(contact.getContactFaxNumber(), faxNumber)) { + // Found it + contact.setContactFaxNumber(null); + } + } + } + + /** + * Removes given land-line number from all contacts + *

+ * @param landLineNumber Land-line number to remove + */ + private void removeFromContacts (final DialableLandLineNumber landLineNumber) { + // Is it valid? + if (null == landLineNumber) { + // Throw NPE + throw new NullPointerException("landLineNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N + } else if (landLineNumber.getPhoneId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N + } + + // Loop through all contacts + for (final Contact contact : this.contacts) { + // Is the number set? + if (Objects.equals(contact.getContactLandLineNumber(), landLineNumber)) { + // Found it + contact.setContactLandLineNumber(null); + } + } + } + + /** + * Removes given mobile number from all contacts + *

+ * @param mobileNumber Mobile number to remove + */ + private void removeFromContacts (final DialableMobileNumber mobileNumber) { + // Log message + System.out.println(MessageFormat.format("{0}.removeFromContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Is it valid? + if (null == mobileNumber) { + // Throw NPE + throw new NullPointerException("mobileNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N + } else if (mobileNumber.getPhoneId() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N + } + + // Loop through all contacts + for (final Contact contact : this.contacts) { + // Log message + System.out.println(MessageFormat.format("{0}.removeFromContacts: contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N + + // Is the number set? + if (Objects.equals(contact.getContactMobileNumber(), mobileNumber)) { + // Log message + System.out.println(MessageFormat.format("{0}.removeFromContacts: Unsetting ...", this.getClass().getSimpleName())); //NOI18N + + // Unset it + contact.setContactMobileNumber(null); + } + } + } + + /** + * Adds unique instance to contact list. First any existing instance is + * being removed, then the new instance is added. + *

+ * @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 iterator = this.contacts.iterator(); + + // "Walk" through all entries + while (iterator.hasNext()) { + // Get next element + Contact next = iterator.next(); + + // Is id number the same? + if (Objects.equals(contact.getContactId(), next.getContactId())) { + // Found entry, so remove it and abort + this.removeContact(next); + break; + } + } + + // Add contact to list + this.contacts.add(contact); } }