2 * Copyright (C) 2016 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.register;
19 import java.text.MessageFormat;
21 import javax.ejb.EJBException;
22 import javax.ejb.Stateless;
23 import javax.mail.Address;
24 import javax.mail.internet.AddressException;
25 import javax.mail.internet.InternetAddress;
26 import javax.persistence.NoResultException;
27 import javax.persistence.Query;
28 import org.mxchange.jcontacts.contact.Contact;
29 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
30 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
31 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
32 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
33 import org.mxchange.jusercore.model.user.LoginUser;
34 import org.mxchange.jusercore.model.user.User;
35 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
36 import org.mxchange.jusercore.model.user.UserUtils;
39 * A session bean for user registration
41 * @author Roland Häder<roland@mxchange.org>
43 @Stateless (name = "register", description = "A bean handling the user registration")
44 public class JobsUserRegistrationSessionBean extends BaseJobsDatabaseBean implements UserRegistrationSessionBeanRemote {
49 private static final long serialVersionUID = 12_348_958_986_818_627L;
52 * Administrative user bean
55 private AdminUserSessionBeanRemote adminUserBean;
61 private UserSessionBeanRemote userBean;
64 public String generateConfirmationKey (final User user) {
66 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
68 // user should not be null
71 throw new NullPointerException("user is null"); //NOI18N
74 // Create named instance
75 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
77 // Init confirmation key
78 String confirmationKey = null;
81 while (confirmationKey == null) {
83 String key = UserUtils.generatedConfirmationKey(user);
85 // Set key as parameter
86 query.setParameter("confirmKey", key); //NOI18N
90 // Get contact instance
91 Contact contact = (Contact) query.getSingleResult();
94 this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N
95 } catch (final NoResultException ex) {
96 // Not found, normal case
97 confirmationKey = key;
103 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
106 return confirmationKey;
110 public boolean isEmailAddressRegistered (final User user) {
112 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
115 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
117 // user should not be null
120 throw new NullPointerException("user is null"); //NOI18N
124 return this.userBean.isEmailAddressRegistered(user);
128 public boolean isUserNameRegistered (final User user) {
130 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
133 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
135 // user should not be null
138 throw new NullPointerException("user is null"); //NOI18N
142 return this.userBean.isUserNameRegistered(user);
146 public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
150 // user should not be null
153 throw new NullPointerException("user is null"); //NOI18N
154 } else if (user.getUserContact() == null) {
156 throw new NullPointerException("user.userContact is null"); //NOI18N
157 } else if (user.getUserContact().getContactEmailAddress() == null) {
159 throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
160 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
162 throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
165 // Check if user is registered
166 if (this.isUserNameRegistered(user)) {
168 throw new UserNameAlreadyRegisteredException(user);
169 } else if (this.isEmailAddressRegistered(user)) {
171 throw new EmailAddressAlreadyRegisteredException(user);
175 User addedUser = this.adminUserBean.addUser(user);
178 Address emailAddress;
181 // Create email address and set
182 emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
183 } catch (final AddressException ex) {
185 throw new EJBException(ex);
189 // @TODO: Internationlize the subject line somehow
190 this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N
193 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N