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