2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user.register;
19 import java.text.MessageFormat;
20 import java.util.Objects;
22 import javax.ejb.EJBException;
23 import javax.ejb.Stateless;
24 import javax.mail.Address;
25 import javax.mail.internet.AddressException;
26 import javax.mail.internet.InternetAddress;
27 import javax.persistence.NoResultException;
28 import javax.persistence.Query;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
31 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
32 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
33 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
34 import org.mxchange.jusercore.model.user.LoginUser;
35 import org.mxchange.jusercore.model.user.User;
36 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
37 import org.mxchange.jusercore.model.user.UserUtils;
40 * A session-scoped bean for user registration
42 * @author Roland Häder<roland@mxchange.org>
44 @Stateless (name = "register", description = "A bean handling the user registration")
45 public class JobsUserRegistrationSessionBean extends BaseJobsDatabaseBean implements UserRegistrationSessionBeanRemote {
50 private static final long serialVersionUID = 12_348_958_986_818_627L;
53 * Administrative user bean
56 private AdminUserSessionBeanRemote adminUserBean;
62 private UserSessionBeanRemote userBean;
67 public JobsUserRegistrationSessionBean () {
68 // Call super constructor
73 public String generateConfirmationKey (final User user) {
75 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
77 // user should not be null
80 throw new NullPointerException("user is null"); //NOI18N
83 // Create named instance
84 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
86 // Init confirmation key
87 String confirmationKey = null;
90 while (confirmationKey == null) {
92 String key = UserUtils.generatedConfirmationKey(user);
94 // Set key as parameter
95 query.setParameter("confirmKey", key); //NOI18N
99 // Get contact instance
100 Contact contact = (Contact) query.getSingleResult();
103 this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
104 } catch (final NoResultException ex) {
105 // Not found, normal case
106 confirmationKey = key;
112 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
115 return confirmationKey;
119 public boolean isEmailAddressRegistered (final User user) {
121 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
124 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
126 // user should not be null
129 throw new NullPointerException("user is null"); //NOI18N
133 return this.userBean.isEmailAddressRegistered(user);
137 public boolean isUserNameRegistered (final User user) {
139 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
142 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
144 // user should not be null
147 throw new NullPointerException("user is null"); //NOI18N
151 return this.userBean.isUserNameRegistered(user);
155 public User registerUser (final User user, final String baseUrl, final String randomPassword) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2},randomPassword[]={3} - CALLED!", this.getClass().getSimpleName(), user, baseUrl, Objects.toString(randomPassword))); //NOI18N
159 // user should not be null
162 throw new NullPointerException("user is null"); //NOI18N
163 } else if (user.getUserContact() == null) {
165 throw new NullPointerException("user.userContact is null"); //NOI18N
166 } else if (user.getUserContact().getContactEmailAddress() == null) {
168 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
169 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
171 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
174 // Check if user is registered
175 if (this.isUserNameRegistered(user)) {
177 throw new UserNameAlreadyRegisteredException(user);
178 } else if (this.isEmailAddressRegistered(user)) {
180 throw new EmailAddressAlreadyRegisteredException(user);
184 User addedUser = this.adminUserBean.addUser(user);
187 Address emailAddress;
190 // Create email address and set
191 emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
192 } catch (final AddressException ex) {
194 throw new EJBException(ex);
197 // Default template is with no random password
198 String templateName = "user_registration"; //NOI18N
201 if ((randomPassword instanceof String) && (!randomPassword.isEmpty())) {
202 // Switch to other template
203 templateName = "user_registration_random"; //NOI18N
207 // @TODO: Internationlize the subject line somehow
208 this.sendEmail("Registration", templateName, emailAddress, addedUser, baseUrl, randomPassword); //NOI18N
211 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N