X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Faddressbook%2Fbeans%2Fuser%2FAddressbookUserWebSessionBean.java;h=be477ac84efbc855b8b75db95ef8c827120d05f2;hb=28b1fd4a8d4809b3c5cc439efebe0ed31bbbb95b;hp=74a8975c88f71d4c4b9a6efadc7af378a19e98a7;hpb=93deae43a53f7f3a0884d7b424ceb9e797c0d9f4;p=addressbook-war.git diff --git a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java index 74a8975c..be477ac8 100644 --- a/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java +++ b/src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java @@ -26,19 +26,20 @@ import javax.enterprise.context.SessionScoped; import javax.enterprise.event.Event; import javax.enterprise.event.Observes; import javax.enterprise.inject.Any; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.mxchange.addressbook.beans.BaseAddressbookController; import org.mxchange.addressbook.beans.contact.AddressbookContactWebSessionController; import org.mxchange.addressbook.beans.login.AddressbookUserLoginWebSessionController; +import org.mxchange.addressbook.beans.register.AddressbookUserRegisterWebSessionController; import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; +import org.mxchange.jcontacts.events.contact.add.AdminAddedContactEvent; import org.mxchange.jusercore.events.login.UserLoggedInEvent; import org.mxchange.jusercore.events.registration.UserRegisteredEvent; import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent; @@ -50,6 +51,7 @@ import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; import org.mxchange.jusercore.model.user.LoginUser; import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.UserUtils; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; /** @@ -59,13 +61,18 @@ import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; */ @Named ("userController") @SessionScoped -public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionController { +public class AddressbookUserWebSessionBean extends BaseAddressbookController implements AddressbookUserWebSessionController { /** * Serial number */ private static final long serialVersionUID = 542_145_347_916L; + /** + * Contact EJB + */ + private ContactSessionBeanRemote contactBean; + /** * General contact controller */ @@ -78,6 +85,12 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC @Inject private AddressbookUserLoginWebSessionController loginController; + /** + * Registration controller + */ + @Inject + private AddressbookUserRegisterWebSessionController registerController; + /** * A list of all selectable contacts */ @@ -152,12 +165,36 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC // Try to lookup this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N + + // Try to lookup + this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/addressbook-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N } catch (final NamingException e) { // Throw again throw new FaceletException(e); } } + @Override + public void afterAdminAddedContact (@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.customerId is null"); //NOI18N + } else if (event.getAddedContact().getContactId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N + } + + // Call other method + this.selectableContacts.add(event.getAddedContact()); + } + @Override public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) { // Trace message @@ -343,26 +380,46 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N // Create new user instance - User localUser = new LoginUser(); + User user = new LoginUser(); - // Update all data ... - localUser.setUserName(this.getUserName()); - localUser.setUserProfileMode(this.getUserProfileMode()); + // Is user name required? + if (!this.isUserNameRequired()) { + // Generate pseudo-random user name + String randomName = this.userBean.generateRandomUserName(); - // Create contact instance - Contact contact = this.contactController.createContactInstance(); + // Set it and inivisible profile + this.setUserName(randomName); + this.setUserProfileMode(ProfileMode.INVISIBLE); - // Debug message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact)); + // Generate random password + String randomPassword = UserUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH); - // Set contact in user - localUser.setUserContact(contact); + // Set random password + this.setUserPassword(randomPassword); + this.setUserPasswordRepeat(randomPassword); + } + + // Set user name and mode + user.setUserName(this.getUserName()); + user.setUserProfileMode(this.getUserProfileMode()); + + // Is multiple registration page + if (!this.registerController.isMultiplePageEnabled()) { + // Create contact instance + Contact contact = this.contactController.createContactInstance(); + + // Debug message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact)); + + // Set contact in user + user.setUserContact(contact); + } // Trace message //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user)); // Return it - return localUser; + return user; } @Override @@ -512,7 +569,7 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC List allUsers = this.allUsers(); // Get all contacts - List allContacts = this.contactController.allContacts(); + List allContacts = this.contactBean.getAllContacts(); // Get iterator Iterator iterator = allContacts.iterator(); @@ -580,6 +637,24 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC return isFound; } + @Override + public boolean isPublicUserProfileEnabled () { + // Get context parameter + String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N + + // Is it set? + boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N + + // This requires user names being enabled, too. + if ((isEnabled) && (!this.isUserNameRequired())) { + // Not valid state, users must be able to modify their profile, especially when it is public + throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N + } + + // Return value + return isEnabled; + } + @Override public boolean isRequiredChangePersonalDataSet () { return ((this.getUserProfileMode() != null) && @@ -589,11 +664,17 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC @Override public boolean isRequiredPersonalDataSet () { - return ((this.getUserName() != null) && - (this.getUserProfileMode() != null) && - (this.contactController.isRequiredPersonalDataSet()) && - (this.getUserPassword() != null) && - (this.getUserPasswordRepeat() != null)); + if (this.registerController.isMultiplePageEnabled()) { + // Multiple registration page + return this.contactController.isRequiredPersonalDataSet(); + } else { + // Single registration page + return (((this.getUserName() != null) || (!this.isUserNameRequired())) && + (this.getUserProfileMode() != null) && + (this.contactController.isRequiredPersonalDataSet()) && + (this.getUserPassword() != null) && + (this.getUserPasswordRepeat() != null)); + } } @Override @@ -611,6 +692,18 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName()))); } + @Override + public boolean isUserNameRequired () { + // Get context parameter + String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_name_required"); //NOI18N + + // Is it set? + boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N + + // Return value + return isRequired; + } + @Override public boolean isVisibleUserFound () { return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));