]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java
Continued:
[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          * An en event fireable when a new user has registered
87          */
88         @Inject
89         @Any
90         private Event<ObservableUserRegisteredEvent> registeredEvent;
91
92         /**
93          * User controller
94          */
95         @Inject
96         private PizzaUserWebSessionController userController;
97
98         /**
99          * Default constructor
100          */
101         public PizzaUserRegisterWebSessionBean () {
102         }
103
104         @Override
105         public String doFinishRegistration () {
106                 // Is registration enabled?
107                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
108                         // Is not enabled
109                         throw new FaceletException("Registration is disabled."); //NOI18N
110                 }
111
112                 // Get user instance
113                 User user = this.userController.createUserInstance(true);
114
115                 // Is the user already used?
116                 if (null == user) {
117                         // user must be set
118                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
119                 } else if (!this.userController.isRequiredPersonalDataSet()) {
120                         // Not all required fields are set
121                         throw new FaceletException("Not all required fields are set."); //NOI18N
122                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
123                         // User name is already used
124                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
125                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
126                         // Email address has already been taken
127                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
128                 } else if (!this.contactController.isSameEmailAddressEntered()) {
129                         // Not same email address entered
130                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
131                 } else if (!this.userController.isSamePasswordEntered()) {
132                         // Not same password entered
133                         throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
134                 }
135
136                 // Encrypt password
137                 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
138
139                 // Set it here
140                 user.setUserEncryptedPassword(encryptedPassword);
141
142                 // Is developer mode?
143                 if (this.isDebugModeEnabled("register")) { //NOI18N
144                         // For debugging/programming only:
145                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
146                 } else {
147                         // No debugging of this part
148                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
149
150                         // Ask EJB for generating a not-existing confirmation key
151                         String confirmKey = this.registerBean.generateConfirmationKey(user);
152
153                         // Set it in user
154                         user.setUserConfirmKey(confirmKey);
155                 }
156
157                 try {
158                         // Get base URL
159                         String baseUrl = FacesUtils.generateBaseUrl();
160
161                         // Call bean
162                         User registeredUser = this.registerBean.registerUser(user, baseUrl);
163
164                         // The id number should be set
165                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
166
167                         // Fire event
168                         this.registeredEvent.fire(new UserRegisteredEvent(registeredUser));
169
170                         // All fine, redirect to proper page
171                         return "register_done"; //NOI18N
172                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
173                         // Continue to throw
174                         throw new FaceletException(ex);
175                 }
176         }
177
178         @Override
179         public String doRegisterMultiPage1 () {
180                 // Is registration enabled?
181                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
182                         // Is not enabled
183                         throw new FaceletException("Registration is disabled."); //NOI18N
184                 }
185
186                 // Get user instance
187                 User user = this.userController.createUserInstance(false);
188
189                 // First check if user is not null and user name is not used + if same email address is entered
190                 if (null == user) {
191                         // user must be set
192                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
193                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
194                         // User name is already used
195                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
196                 } else if (!this.contactController.isSameEmailAddressEntered()) {
197                         // Not same email address entered
198                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
199                 }
200
201                 // Create half contact instance with email address
202                 Contact contact = new UserContact();
203                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
204
205                 // Set contact in user
206                 user.setUserContact(contact);
207
208                 // Check if email address is registered
209                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
210                         // Email address has already been taken
211                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
212                 }
213
214                 // Now only redirect to next page as the JSF does it
215                 return "user_register_page2"; //NOI18N
216         }
217
218         /**
219          * Post-construction method
220          */
221         @PostConstruct
222         public void init () {
223                 try {
224                         // Get initial context
225                         Context context = new InitialContext();
226
227                         // Try to lookup
228                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
229                 } catch (final NamingException ex) {
230                         // Continue to throw
231                         throw new FaceletException(ex);
232                 }
233         }
234
235 }