]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/register/JobsUserRegisterWebSessionBean.java
New "feature" controller introduced: (please cherry-pick this)
[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();
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                         // User name is already used
133                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
134                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
135                         // Email address has already been taken
136                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
137                 } else if (!this.contactController.isSameEmailAddressEntered()) {
138                         // Not same email address entered
139                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
140                 } else if (!this.userController.isSamePasswordEntered()) {
141                         // Not same password entered
142                         throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
143                 }
144
145                 // Encrypt password
146                 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
147
148                 // Set it here
149                 user.setUserEncryptedPassword(encryptedPassword);
150
151                 // Is developer mode?
152                 if (this.isDebugModeEnabled("register")) { //NOI18N
153                         // For debugging/programming only:
154                         user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
155                 } else {
156                         // No debugging of this part
157                         user.setUserAccountStatus(UserAccountStatus.UNCONFIRMED);
158
159                         // Ask EJB for generating a not-existing confirmation key
160                         String confirmKey = this.registerBean.generateConfirmationKey(user);
161
162                         // Set it in user
163                         user.setUserConfirmKey(confirmKey);
164                 }
165
166                 try {
167                         // Get base URL
168                         String baseUrl = FacesUtils.generateBaseUrl();
169
170                         // Call bean
171                         User registeredUser = this.registerBean.registerUser(user, baseUrl);
172
173                         // The id number should be set
174                         assert (registeredUser.getUserId() instanceof Long) : "registeredUser.userId is null after registerUser() was called."; //NOI18N
175
176                         // Fire event
177                         this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
178
179                         // All fine, redirect to proper page
180                         return "register_done"; //NOI18N
181                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
182                         // Continue to throw
183                         throw new FaceletException(ex);
184                 }
185         }
186
187         @Override
188         public String doRegisterMultiPage1 () {
189                 // Is registration enabled?
190                 if (!this.featureController.isFeatureEnabled("user_registration")) { //NOI18N
191                         // Is not enabled
192                         throw new FaceletException("Registration is disabled."); //NOI18N
193                 }
194
195                 // Get user instance
196                 User user = this.userController.createUserInstance();
197
198                 // First check if user is not null and user name is not used + if same email address is entered
199                 if (null == user) {
200                         // user must be set
201                         throw new NullPointerException("user is null after createUserInstance() was called"); //NOI18N
202                 } else if ((this.featureController.isFeatureEnabled("user_name_required")) && (this.userController.isUserNameRegistered(user))) { //NOI18N
203                         // User name is already used
204                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
205                 } else if (!this.contactController.isSameEmailAddressEntered()) {
206                         // Not same email address entered
207                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
208                 }
209
210                 // Create half contact instance with email address
211                 Contact contact = new UserContact();
212                 contact.setContactEmailAddress(this.contactController.getEmailAddress());
213
214                 // Set contact in user
215                 user.setUserContact(contact);
216
217                 // Check if email address is registered
218                 if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
219                         // Email address has already been taken
220                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
221                 }
222
223                 // Now only redirect to next page as the JSF does it
224                 return "register_page2"; //NOI18N
225         }
226
227 }