]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/register/JobsUserRegisterWebSessionBean.java
Continued with localization: (please cherry-pick)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / register / JobsUserRegisterWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jjobs.beans.register;
18
19 import java.text.MessageFormat;
20 import javax.enterprise.context.SessionScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.contact.UserContact;
31 import org.mxchange.jcoreee.utils.FacesUtils;
32 import org.mxchange.jjobs.beans.BaseJobsController;
33 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
34 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
35 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
36 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
37 import org.mxchange.jusercore.events.registration.RegisteredUserEvent;
38 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
39 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
40 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
41 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
42 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.UserUtils;
45 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
46
47 /**
48  * A web bean for user registration
49  * <p>
50  * @author Roland Haeder<roland@mxchange.org>
51  */
52 @Named ("registerController")
53 @SessionScoped
54 public class JobsUserRegisterWebSessionBean extends BaseJobsController implements JobsUserRegisterWebSessionController {
55
56         /**
57          * Serial number
58          */
59         private static final long serialVersionUID = 47_828_986_719_691_592L;
60
61         /**
62          * User controller
63          */
64         @Inject
65         private JobsAdminUserWebRequestController adminUserController;
66
67         /**
68          * Contact controller
69          */
70         @Inject
71         private JobsContactWebSessionController contactController;
72
73         /**
74          * Features controller
75          */
76         @Inject
77         private JobsFeaturesWebApplicationController featureController;
78
79         /**
80          * Reemote register session bean
81          */
82         private UserRegistrationSessionBeanRemote registerBean;
83
84         /**
85          * An en event fireable when a new user has registered
86          */
87         @Inject
88         @Any
89         private Event<UserRegisteredEvent> registeredEvent;
90
91         /**
92          * User controller
93          */
94         @Inject
95         private JobsUserWebSessionController userController;
96
97         /**
98          * Default constructor
99          */
100         public JobsUserRegisterWebSessionBean () {
101                 try {
102                         // Get initial context
103                         Context context = new InitialContext();
104
105                         // Try to lookup
106                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/jjobs-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
107                 } catch (final NamingException ex) {
108                         // Continue to throw
109                         throw new FaceletException(ex);
110                 }
111         }
112
113         @Override
114         public String doFinishRegistration () {
115                 // Is registration enabled?
116                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
117                         // Is not enabled
118                         throw new FaceletException("Registration is disabled."); //NOI18N
119                 }
120
121                 // Get user instance
122                 User user = this.userController.createUserInstance(true);
123
124                 // Is the user already used?
125                 if (null == user) {
126                         // user must be set
127                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
128                 } else if (!this.userController.isRequiredPersonalDataSet()) {
129                         // Not all required fields are set
130                         throw new FaceletException("Not all required fields are set."); //NOI18N
131                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
132                         // Is multi-page enabled?
133                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
134                                 // User name is already used, should not happen here
135                                 throw new FaceletException(new UserNameAlreadyRegisteredException(user));
136                         } else {
137                                 // May happen here, reset field
138                                 this.userController.setUserName(null);
139                                 this.showFacesMessage("form_register_single:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
140                                 return ""; //NOI18N
141                         }
142                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
143                         // Is multi-page enabled?
144                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
145                                 // Email address has already been taken, should not happen here
146                                 throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
147                         } else {
148                                 // May happen here, reset fields
149                                 this.contactController.setEmailAddress(null);
150                                 this.contactController.setEmailAddressRepeat(null);
151                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
152                                 return ""; //NOI18N
153                         }
154                 } else if (!this.contactController.isSameEmailAddressEntered()) {
155                         // Is multi-page enabled?
156                         if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
157                                 // Not same email address entered, should not happen here
158                                 throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
159                         } else {
160                                 // May happen here, reset fields
161                                 this.contactController.setEmailAddress(null);
162                                 this.contactController.setEmailAddressRepeat(null);
163                                 this.showFacesMessage("form_register_single:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
164                                 return ""; //NOI18N
165                         }
166                 } else if (!this.userController.isSamePasswordEntered()) {
167                         // Not same password entered
168                         throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
169                 }
170
171                 // Encrypt password
172                 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
173
174                 // Set it here
175                 user.setUserEncryptedPassword(encryptedPassword);
176
177                 // Is developer mode?
178                 if (this.isDebugModeEnabled("register")) { //NOI18N
179                         // For debugging/programming only:
180                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
181                 } else {
182                         // No debugging of this part
183                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
184
185                         // Ask EJB for generating a not-existing confirmation key
186                         String confirmKey = this.registerBean.generateConfirmationKey(user);
187
188                         // Set it in user
189                         user.setUserConfirmKey(confirmKey);
190                 }
191
192                 try {
193                         // Get base URL
194                         String baseUrl = FacesUtils.generateBaseUrl();
195
196                         // Call bean
197                         User registeredUser = this.registerBean.registerUser(user, baseUrl);
198
199                         // The id number should be set
200                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
201
202                         // Fire event
203                         this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
204
205                         // All fine, redirect to proper page
206                         return "register_done"; //NOI18N
207                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
208                         // Continue to throw
209                         throw new FaceletException(ex);
210                 }
211         }
212
213         @Override
214         public String doRegisterMultiPage1 () {
215                 // Is registration enabled?
216                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
217                         // Is not enabled
218                         throw new FaceletException("Registration is disabled."); //NOI18N
219                 }
220
221                 // Get user instance
222                 User user = this.userController.createUserInstance(false);
223
224                 // First check if user is not null and user name is not used + if same email address is entered
225                 if (null == user) {
226                         // user must be set
227                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
228                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
229                         // User name is already used, so clear it
230                         this.userController.setUserName(null);
231                         this.showFacesMessage("form_register_page1:userName", "ERROR_USER_NAME_ALREADY_USED"); //NOI18N
232                         return ""; //NOI18N
233                 } else if (!this.contactController.isSameEmailAddressEntered()) {
234                         // Not same email address entered, clear both
235                         this.contactController.setEmailAddress(null);
236                         this.contactController.setEmailAddressRepeat(null);
237                         this.showFacesMessage("form_register_page1:emailAddressRepeat", "ERROR_EMAIL_ADDRESSES_MISMATCHING"); //NOI18N
238                         return ""; //NOI18N
239                 }
240
241                 // Create half contact instance with email address
242                 Contact contact = new UserContact();
243                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
244
245                 // Set contact in user
246                 user.setUserContact(contact);
247
248                 // Check if email address is registered
249                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
250                         // Email address has already been taken, clear both
251                         this.contactController.setEmailAddress(null);
252                         this.contactController.setEmailAddressRepeat(null);
253                         this.showFacesMessage("form_register_page1:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
254                         return ""; //NOI18N
255                 }
256
257                 // Now only redirect to next page as the JSF does it
258                 return "register_page2"; //NOI18N
259         }
260
261 }