]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / register / PizzaUserRegisterWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaapplication.beans.register;
18
19 import java.text.MessageFormat;
20 import javax.annotation.PostConstruct;
21 import javax.enterprise.context.SessionScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcontacts.contact.UserContact;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jusercore.events.registration.ObservableUserRegisteredEvent;
34 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
35 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
36 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
37 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
38 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.User;
40 import org.mxchange.jusercore.model.user.UserUtils;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 import org.mxchange.pizzaapplication.beans.BasePizzaController;
43 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
44 import org.mxchange.pizzaapplication.beans.features.PizzaFeaturesWebApplicationController;
45 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
46 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
47
48 /**
49  * A web bean for user registration
50  * <p>
51  * @author Roland Häder<roland@mxchange.org>
52  */
53 @Named ("registerController")
54 @SessionScoped
55 public class PizzaUserRegisterWebSessionBean extends BasePizzaController implements PizzaUserRegisterWebSessionController {
56
57         /**
58          * Serial number
59          */
60         private static final long serialVersionUID = 47_828_986_719_691_592L;
61
62         /**
63          * User controller
64          */
65         @Inject
66         private PizzaAdminUserWebRequestController adminUserController;
67
68         /**
69          * Contact controller
70          */
71         @Inject
72         private PizzaContactWebSessionController contactController;
73
74         /**
75          * Features controller
76          */
77         @Inject
78         private PizzaFeaturesWebApplicationController featureController;
79
80         /**
81          * Remote register session-scoped bean
82          */
83         private UserRegistrationSessionBeanRemote registerBean;
84
85         /**
86          * User controller
87          */
88         @Inject
89         private PizzaUserWebSessionController userController;
90
91         /**
92          * An en event fireable when a new user has registered
93          */
94         @Inject
95         @Any
96         private Event<ObservableUserRegisteredEvent> userRegisteredEvent;
97
98         /**
99          * Default constructor
100          */
101         public PizzaUserRegisterWebSessionBean () {
102                 // Call super constructor
103                 super();
104         }
105
106         @Override
107         public String doFinishRegistration () {
108                 // Is registration enabled?
109                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
110                         // Is not enabled
111                         throw new FaceletException("Registration is disabled."); //NOI18N
112                 }
113
114                 // Get user instance
115                 User user = this.userController.createUserInstance(true);
116
117                 // Is the user already used?
118                 if (null == user) {
119                         // user must be set
120                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
121                 } else if (!this.userController.isRequiredPersonalDataSet()) {
122                         // Not all required fields are set
123                         throw new FaceletException("Not all required fields are set."); //NOI18N
124                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
125                         // User name is already used
126                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
127                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
128                         // Email address has already been taken
129                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
130                 } else if (!this.contactController.isSameEmailAddressEntered()) {
131                         // Not same email address entered
132                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
133                 } else if (!this.userController.isSamePasswordEntered()) {
134                         // Not same password entered
135                         throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
136                 }
137
138                 // Encrypt password
139                 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
140
141                 // Set it here
142                 user.setUserEncryptedPassword(encryptedPassword);
143
144                 // Is developer mode?
145                 if (this.isDebugModeEnabled("register")) { //NOI18N
146                         // For debugging/programming only:
147                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
148                 } else {
149                         // No debugging of this part
150                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
151
152                         // Ask EJB for generating a not-existing confirmation key
153                         String confirmKey = this.registerBean.generateConfirmationKey(user);
154
155                         // Set it in user
156                         user.setUserConfirmKey(confirmKey);
157                 }
158
159                 try {
160                         // Get base URL
161                         String baseUrl = FacesUtils.generateBaseUrl();
162
163                         // Call bean
164                         User registeredUser = this.registerBean.registerUser(user, baseUrl);
165
166                         // The id number should be set
167                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
168
169                         // Fire event
170                         this.userRegisteredEvent.fire(new UserRegisteredEvent(registeredUser));
171
172                         // All fine, redirect to proper page
173                         return "register_done"; //NOI18N
174                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
175                         // Continue to throw
176                         throw new FaceletException(ex);
177                 }
178         }
179
180         @Override
181         public String doRegisterMultiPage1 () {
182                 // Is registration enabled?
183                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
184                         // Is not enabled
185                         throw new FaceletException("Registration is disabled."); //NOI18N
186                 }
187
188                 // Get user instance
189                 User user = this.userController.createUserInstance(false);
190
191                 // First check if user is not null and user name is not used + if same email address is entered
192                 if (null == user) {
193                         // user must be set
194                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
195                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
196                         // User name is already used
197                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
198                 } else if (!this.contactController.isSameEmailAddressEntered()) {
199                         // Not same email address entered
200                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
201                 }
202
203                 // Create half contact instance with email address
204                 Contact contact = new UserContact();
205                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
206
207                 // Set contact in user
208                 user.setUserContact(contact);
209
210                 // Check if email address is registered
211                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
212                         // Email address has already been taken
213                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
214                 }
215
216                 // Now only redirect to next page as the JSF does it
217                 return "user_register_page2"; //NOI18N
218         }
219
220         /**
221          * Post-construction method
222          */
223         @PostConstruct
224         public void init () {
225                 try {
226                         // Get initial context
227                         Context context = new InitialContext();
228
229                         // Try to lookup
230                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
231                 } catch (final NamingException ex) {
232                         // Continue to throw
233                         throw new FaceletException(ex);
234                 }
235         }
236
237 }