]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / register / PizzaUserRegisterWebSessionBean.java
index 1eb3e905a133766e9dda8c6c4f59eafb822d54ac..84aa05a4ff31d3fd4ab21dafa52f2c75ae312008 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Roland Haeder
+ * 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
@@ -17,6 +17,7 @@
 package org.mxchange.pizzaapplication.beans.register;
 
 import java.text.MessageFormat;
+import javax.annotation.PostConstruct;
 import javax.enterprise.context.SessionScoped;
 import javax.enterprise.event.Event;
 import javax.enterprise.inject.Any;
@@ -26,8 +27,10 @@ import javax.inject.Named;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
-import org.mxchange.jratecalc.beans.contact.PizzaContactWebSessionController;
-import org.mxchange.jusercore.events.registration.RegisteredUserEvent;
+import org.mxchange.jcontacts.contact.Contact;
+import org.mxchange.jcontacts.contact.UserContact;
+import org.mxchange.jcoreee.utils.FacesUtils;
+import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
@@ -36,16 +39,20 @@ import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.User;
 import org.mxchange.jusercore.model.user.UserUtils;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
+import org.mxchange.pizzaapplication.beans.BasePizzaController;
+import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
+import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
+import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
 
 /**
  * A web bean for user registration
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
  */
 @Named ("registerController")
 @SessionScoped
-public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSessionController {
+public class PizzaUserRegisterWebSessionBean extends BasePizzaController implements PizzaUserRegisterWebSessionController {
 
        /**
         * Serial number
@@ -56,19 +63,24 @@ public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSess
         * User controller
         */
        @Inject
-       private PizzaContactWebSessionController contactController;
+       private PizzaAdminUserWebRequestController adminUserController;
 
        /**
-        * Reemote register session bean
+        * Contact controller
         */
-       private UserRegistrationSessionBeanRemote registerBean;
+       @Inject
+       private PizzaContactWebSessionController contactController;
 
        /**
-        * An en event fireable when a new user has registered
+        * Features controller
         */
        @Inject
-       @Any
-       private Event<UserRegisteredEvent> registeredEvent;
+       private PizzaFeaturesWebApplicationController featureController;
+
+       /**
+        * Remote register session-scoped bean
+        */
+       private UserRegistrationSessionBeanRemote registerBean;
 
        /**
         * User controller
@@ -76,35 +88,40 @@ public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSess
        @Inject
        private PizzaUserWebSessionController userController;
 
+       /**
+        * An en event fireable when a new user has registered
+        */
+       @Inject
+       @Any
+       private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
+
        /**
         * Default constructor
         */
        public PizzaUserRegisterWebSessionBean () {
-               try {
-                       // Get initial context
-                       Context context = new InitialContext();
-
-                       // Try to lookup
-                       this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
-               } catch (final NamingException ex) {
-                       // Continue to throw
-                       throw new FaceletException(ex);
-               }
+               // Call super constructor
+               super();
        }
 
        @Override
-       public String doRegister () {
+       public String doFinishRegistration () {
+               // Is registration enabled?
+               if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
+                       // Is not enabled
+                       throw new FaceletException("Registration is disabled."); //NOI18N
+               }
+
                // Get user instance
-               User user = this.userController.createUserInstance();
+               User user = this.userController.createUserInstance(true);
 
                // Is the user already used?
                if (null == user) {
                        // user must be set
-                       throw new NullPointerException("user is null"); //NOI18N
+                       throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
                } else if (!this.userController.isRequiredPersonalDataSet()) {
                        // Not all required fields are set
                        throw new FaceletException("Not all required fields are set."); //NOI18N
-               } else if (this.userController.isUserNameRegistered(user)) {
+               } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
                        // User name is already used
                        throw new FaceletException(new UserNameAlreadyRegisteredException(user));
                } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
@@ -124,15 +141,33 @@ public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSess
                // Set it here
                user.setUserEncryptedPassword(encryptedPassword);
 
-               // For debugging/programming only:
-               user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
+               // Is developer mode?
+               if (this.isDebugModeEnabled("register")) { //NOI18N
+                       // For debugging/programming only:
+                       user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
+               } else {
+                       // No debugging of this part
+                       user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
+
+                       // Ask EJB for generating a not-existing confirmation key
+                       String confirmKey = this.registerBean.generateConfirmationKey(user);
+
+                       // Set it in user
+                       user.setUserConfirmKey(confirmKey);
+               }
 
                try {
+                       // Get base URL
+                       String baseUrl = FacesUtils.generateBaseUrl();
+
                        // Call bean
-                       User registeredUser = this.registerBean.registerUser(user);
+                       User registeredUser = this.registerBean.registerUser(user, baseUrl);
+
+                       // The id number should be set
+                       assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
 
                        // Fire event
-                       this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
+                       this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
 
                        // All fine, redirect to proper page
                        return "register_done"; //NOI18N
@@ -142,4 +177,61 @@ public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSess
                }
        }
 
+       @Override
+       public String doRegisterMultiPage1 () {
+               // Is registration enabled?
+               if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
+                       // Is not enabled
+                       throw new FaceletException("Registration is disabled."); //NOI18N
+               }
+
+               // Get user instance
+               User user = this.userController.createUserInstance(false);
+
+               // First check if user is not null and user name is not used + if same email address is entered
+               if (null == user) {
+                       // user must be set
+                       throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
+               } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
+                       // User name is already used
+                       throw new FaceletException(new UserNameAlreadyRegisteredException(user));
+               } else if (!this.contactController.isSameEmailAddressEntered()) {
+                       // Not same email address entered
+                       throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
+               }
+
+               // Create half contact instance with email address
+               Contact contact = new UserContact();
+               contact.setContactEmailAddress(this.contactController.getEmailAddress());
+
+               // Set contact in user
+               user.setUserContact(contact);
+
+               // Check if email address is registered
+               if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
+                       // Email address has already been taken
+                       throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
+               }
+
+               // Now only redirect to next page as the JSF does it
+               return "user_register_page2"; //NOI18N
+       }
+
+       /**
+        * Post-construction method
+        */
+       @PostConstruct
+       public void init () {
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Try to lookup
+                       this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
+               } catch (final NamingException ex) {
+                       // Continue to throw
+                       throw new FaceletException(ex);
+               }
+       }
+
 }