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