]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java
Rewrites:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / register / PizzaUserRegisterWebSessionBean.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.pizzaapplication.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.jusercore.events.registration.RegisteredUserEvent;
30 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
31 import org.mxchange.jusercore.exceptions.DataRepeatMismatchException;
32 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
33 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
34 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.UserUtils;
37 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
38 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
39 import org.mxchange.pizzaapplication.beans.user.PizzaAdminUserWebRequestController;
40 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
41
42 /**
43  * A web bean for user registration
44  * <p>
45  * @author Roland Haeder<roland@mxchange.org>
46  */
47 @Named ("registerController")
48 @SessionScoped
49 public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSessionController {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 47_828_986_719_691_592L;
55
56         /**
57          * User controller
58          */
59         @Inject
60         private PizzaAdminUserWebRequestController adminUserController;
61
62         /**
63          * User controller
64          */
65         @Inject
66         private PizzaContactWebSessionController contactController;
67
68         /**
69          * Reemote register session bean
70          */
71         private UserRegistrationSessionBeanRemote registerBean;
72
73         /**
74          * An en event fireable when a new user has registered
75          */
76         @Inject
77         @Any
78         private Event<UserRegisteredEvent> registeredEvent;
79
80         /**
81          * User controller
82          */
83         @Inject
84         private PizzaUserWebSessionController userController;
85
86         /**
87          * Default constructor
88          */
89         public PizzaUserRegisterWebSessionBean () {
90                 try {
91                         // Get initial context
92                         Context context = new InitialContext();
93
94                         // Try to lookup
95                         this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N
96                 } catch (final NamingException ex) {
97                         // Continue to throw
98                         throw new FaceletException(ex);
99                 }
100         }
101
102         @Override
103         public String doRegister () {
104                 // Get user instance
105                 User user = this.userController.createUserInstance();
106
107                 // Is the user already used?
108                 if (null == user) {
109                         // user must be set
110                         throw new NullPointerException("user is null"); //NOI18N
111                 } else if (!this.userController.isRequiredPersonalDataSet()) {
112                         // Not all required fields are set
113                         throw new FaceletException("Not all required fields are set."); //NOI18N
114                 } else if (this.adminUserController.isUserNameRegistered(user)) {
115                         // User name is already used
116                         throw new FaceletException(new UserNameAlreadyRegisteredException(user));
117                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
118                         // Email address has already been taken
119                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(user));
120                 } else if (!this.contactController.isSameEmailAddressEntered()) {
121                         // Not same email address entered
122                         throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.contactController.getEmailAddress(), this.contactController.getEmailAddressRepeat()))); //NOI18N
123                 } else if (!this.userController.isSamePasswordEntered()) {
124                         // Not same password entered
125                         throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N
126                 }
127
128                 // Encrypt password
129                 String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword());
130
131                 // Set it here
132                 user.setUserEncryptedPassword(encryptedPassword);
133
134                 // For debugging/programming only:
135                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
136
137                 try {
138                         // Call bean
139                         User registeredUser = this.registerBean.registerUser(user);
140
141                         // Fire event
142                         this.registeredEvent.fire(new RegisteredUserEvent(registeredUser));
143
144                         // All fine, redirect to proper page
145                         return "register_done"; //NOI18N
146                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
147                         // Continue to throw
148                         throw new FaceletException(ex);
149                 }
150         }
151
152 }