]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/juserlogincore/model/user/register/PizzaUserRegistrationSessionBean.java
Please cherry-pick:
[pizzaservice-ejb.git] / src / java / org / mxchange / juserlogincore / model / user / register / PizzaUserRegistrationSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.juserlogincore.model.user.register;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.ejb.EJB;
23 import javax.ejb.Stateless;
24 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
25 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
26 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
27 import org.mxchange.jusercore.model.user.User;
28 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
29 import org.mxchange.juserlogincore.login.UserLoginUtils;
30 import org.mxchange.pizzaaplication.enterprise.BasePizzaEnterpriseBean;
31
32 /**
33  * A session-scoped bean for user registration
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @Stateless (name = "userRegistration", description = "A bean handling the user registration")
38 public class PizzaUserRegistrationSessionBean extends BasePizzaEnterpriseBean implements UserRegistrationSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 12_348_958_986_818_627L;
44
45         /**
46          * Administrative user bean
47          */
48         @EJB (lookup = "java:global/pizzaservice-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote")
49         private AdminUserSessionBeanRemote adminUserBean;
50
51         /**
52          * Regular user EJB
53          */
54         @EJB (lookup = "java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
55         private UserSessionBeanRemote userBean;
56
57         /**
58          * Default constructor
59          */
60         public PizzaUserRegistrationSessionBean () {
61                 // Call super constructor
62                 super("jms/pizzaservice-queue-factory", "jms/pizzaservice-email-queue"); //NOI18N
63         }
64
65         @Override
66         public String generateConfirmationKey (final User user) {
67                 // Trace message
68                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
69
70                 // user should not be null
71                 if (null == user) {
72                         // Abort here
73                         throw new NullPointerException("user is null"); //NOI18N
74                 }
75
76                 // Fetch whole list
77                 final List<User> users = this.userBean.fetchAllUsers();
78
79                 // Init confirmation key
80                 String confirmationKey = null;
81
82                 // Find a free one
83                 while (confirmationKey == null) {
84                         // Create new one
85                         confirmationKey = UserLoginUtils.generatedConfirmationKey(user);
86
87                         // Check all entries
88                         for (final User currentUser : users) {
89                                 // Does the key match?
90                                 if (Objects.equals(currentUser.getUserConfirmKey(), confirmationKey)) {
91                                         // Set key to null and exit loop
92                                         confirmationKey = null;
93                                         break;
94                                 }
95                         }
96                 }
97
98                 // Log trace message
99                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
100
101                 // Return it
102                 return confirmationKey;
103         }
104
105         @Override
106         public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
107                 // Trace message
108                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
109
110                 // user should not be null
111                 if (null == user) {
112                         // Abort here
113                         throw new NullPointerException("user is null"); //NOI18N
114                 } else if (user.getUserContact() == null) {
115                         // Throw NPE again
116                         throw new NullPointerException("user.userContact is null"); //NOI18N
117                 } else if (user.getUserContact().getContactEmailAddress() == null) {
118                         // Throw NPE again
119                         throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
120                 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
121                         // Is empty
122                         throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
123                 }
124
125                 // Check if user is registered
126                 if (this.userBean.isUserNameRegistered(user)) {
127                         // Abort here
128                         throw new UserNameAlreadyRegisteredException(user);
129                 } else if (this.userBean.isEmailAddressRegistered(user)) {
130                         // Abort here
131                         throw new EmailAddressAlreadyRegisteredException(user);
132                 }
133
134                 // Call other EJB
135                 final User addedUser = this.adminUserBean.addUser(user);
136
137                 // Default template is with no random password
138                 String templateName = "user_registration"; //NOI18N
139
140                 // Is password set?
141                 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
142                         // Switch to template with random password exposed
143                         templateName = "user_registration_random"; //NOI18N
144                 }
145
146                 // Send email
147                 // @TODO: Internationlize the subject line somehow
148                 this.sendEmail("Registration", templateName, addedUser, baseUrl, randomPassword); //NOI18N
149
150                 // Trace message
151                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
152
153                 // Return it
154                 return addedUser;
155         }
156
157 }