2 * Copyright (C) 2016 Roland Haeder
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.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcontacts.contact.Contact;
25 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
26 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
27 import org.mxchange.jusercore.model.user.LoginUser;
28 import org.mxchange.jusercore.model.user.User;
29 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
30 import org.mxchange.jusercore.model.user.UserUtils;
31 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
34 * A session bean for user registration
36 * @author Roland Haeder<roland@mxchange.org>
38 @Stateless (name = "register", description = "A bean handling the user registration")
39 public class PizzaUserRegistrationSessionBean extends BasePizzaDatabaseBean implements UserRegistrationSessionBeanRemote {
44 private static final long serialVersionUID = 12_348_958_986_818_627L;
50 private UserSessionBeanRemote userBean;
53 public String generateConfirmationKey (final User user) {
55 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: user={0} - CALLED!", user)); //NOI18N
57 // user should not be null
60 throw new NullPointerException("user is null"); //NOI18N
63 // Create named instance
64 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
66 // Init confirmation key
67 String confirmationKey = null;
70 while (confirmationKey == null) {
72 String key = UserUtils.generatedConfirmationKey(user);
74 // Set key as parameter
75 query.setParameter("confirmKey", key); //NOI18N
79 // Get contact instance
80 Contact contact = (Contact) query.getSingleResult();
83 this.getLoggerBeanLocal().logWarning(MessageFormat.format("generateConfirmationKey: key {0} already found: contact.contactId={1}", key, contact.getContactId())); //NOI18N
84 } catch (final NoResultException ex) {
85 // Not found, normal case
86 confirmationKey = key;
92 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: confirmationKey={0} - EXIT!", confirmationKey)); //NOI18N
95 return confirmationKey;
99 public boolean isEmailAddressRegistered (final User user) {
101 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
104 assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
106 // user should not be null
109 throw new NullPointerException("user is null"); //NOI18N
113 return this.userBean.isEmailAddressRegistered(user);
117 public boolean isUserNameRegistered (final User user) {
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
122 assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
124 // user should not be null
127 throw new NullPointerException("user is null"); //NOI18N
131 return this.userBean.isUserNameRegistered(user);
135 public User registerUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
137 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0} - CALLED!", user)); //NOI18N
139 // user should not be null
142 throw new NullPointerException("user is null"); //NOI18N
145 // Check if user is registered
146 if (this.isUserNameRegistered(user)) {
148 throw new UserNameAlreadyRegisteredException(user);
149 } else if (this.isEmailAddressRegistered(user)) {
151 throw new EmailAddressAlreadyRegisteredException(user);
155 User addedUser = this.userBean.addUser(user);
160 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userId={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N