]> git.mxchange.org Git - addressbook-war.git/blobdiff - src/java/org/mxchange/addressbook/beans/user/AddressbookUserWebSessionBean.java
Cleanup a bit + fix:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / user / AddressbookUserWebSessionBean.java
index f20cb812c0c31ec48cca435e3cb2221603b69d2c..be477ac84efbc855b8b75db95ef8c827120d05f2 100644 (file)
@@ -26,18 +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;
@@ -49,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;
 
 /**
@@ -58,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
         */
@@ -77,6 +85,12 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC
        @Inject
        private AddressbookUserLoginWebSessionController loginController;
 
+       /**
+        * Registration controller
+        */
+       @Inject
+       private AddressbookUserRegisterWebSessionController registerController;
+
        /**
         * A list of all selectable contacts
         */
@@ -151,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
@@ -342,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 random password
+                       this.setUserPassword(randomPassword);
+                       this.setUserPasswordRepeat(randomPassword);
+               }
+
+               // Set user name and mode
+               user.setUserName(this.getUserName());
+               user.setUserProfileMode(this.getUserProfileMode());
 
-               // Set contact in user
-               localUser.setUserContact(contact);
+               // 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
@@ -511,7 +569,7 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC
                List<User> allUsers = this.allUsers();
 
                // Get all contacts
-               List<Contact> allContacts = this.contactController.allContacts();
+               List<Contact> allContacts = this.contactBean.getAllContacts();
 
                // Get iterator
                Iterator<Contact> iterator = allContacts.iterator();
@@ -579,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) &&
@@ -588,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
@@ -610,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));
@@ -698,15 +792,19 @@ public class AddressbookUserWebSessionBean implements AddressbookUserWebSessionC
         * @param user User instance
         */
        private void copyUser (final User user) {
+               // Make sure the instance is valid
+               if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserContact() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("user.userContact is null"); //NOI18N
+               }
+
                // Copy all fields:
                // - base data
                this.setUserId(user.getUserId());
                this.setUserProfileMode(user.getUserProfileMode());
-
-               // Get cellphone, phone and fax instance
-               DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
-               DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
-               DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
        }
 
        /**