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;
20 import java.util.GregorianCalendar;
22 import javax.ejb.Stateless;
23 import org.mxchange.jcoreee.database.BaseDatabaseBean;
24 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
25 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
26 import org.mxchange.jusercore.model.user.User;
27 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
30 * A session bean for user registration
32 * @author Roland Haeder<roland@mxchange.org>
34 @Stateless (name = "register", mappedName = "ejb/stateless-addressbook-register", description = "A bean handling the user registration")
35 public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
40 private static final long serialVersionUID = 12_348_958_986_818_627L;
46 private UserSessionBeanRemote userBean;
49 public boolean isEmailAddressRegistered (final User user) {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
54 assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
56 // user should not be null
59 throw new NullPointerException("user is null"); //NOI18N
63 return this.userBean.isEmailAddressReqistered(user);
67 public boolean isUserNameRegistered (final User user) {
69 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
72 assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
74 // user should not be null
77 throw new NullPointerException("user is null"); //NOI18N
81 return this.userBean.isUserNameReqistered(user);
85 public User registerUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
87 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0} - CALLED!", user)); //NOI18N
89 // user should not be null
92 throw new NullPointerException("user is null"); //NOI18N
95 // Check if user is registered
96 if (this.isUserNameRegistered(user)) {
98 throw new UserNameAlreadyRegisteredException(user);
99 } else if (this.isEmailAddressRegistered(user)) {
101 throw new EmailAddressAlreadyRegisteredException(user);
104 // Set created timestamp
105 user.setUserCreated(new GregorianCalendar());
106 user.getUserContact().setContactCreated(new GregorianCalendar());
109 this.getEntityManager().persist(user);
111 // Flush to get id back
112 this.getEntityManager().flush();
115 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N