]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
added business method addUser() and took code from registerUser as both is (at persis...
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / register / AddressbookUserRegistrationSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jusercore.model.register;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
21 import javax.ejb.Stateless;
22 import org.mxchange.jcoreee.database.BaseDatabaseBean;
23 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
24 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
25 import org.mxchange.jusercore.model.user.User;
26 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
27
28 /**
29  * A session bean for user registration
30  * <p>
31  * @author Roland Haeder<roland@mxchange.org>
32  */
33 @Stateless (name = "register", mappedName = "ejb/stateless-addressbook-register", description = "A bean handling the user registration")
34 public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 12_348_958_986_818_627L;
40
41         /**
42          * User bean
43          */
44         @EJB
45         private UserSessionBeanRemote userBean;
46
47         @Override
48         public boolean isEmailAddressRegistered (final User user) {
49                 // Trace message
50                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
51
52                 // Check bean
53                 assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
54
55                 // user should not be null
56                 if (null == user) {
57                         // Abort here
58                         throw new NullPointerException("user is null"); //NOI18N
59                 }
60
61                 // Call other bean
62                 return this.userBean.isEmailAddressReqistered(user);
63         }
64
65         @Override
66         public boolean isUserNameRegistered (final User user) {
67                 // Trace message
68                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
69
70                 // Check bean
71                 assert(this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
72
73                 // user should not be null
74                 if (null == user) {
75                         // Abort here
76                         throw new NullPointerException("user is null"); //NOI18N
77                 }
78
79                 // Call other bean
80                 return this.userBean.isUserNameReqistered(user);
81         }
82
83         @Override
84         public User registerUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
85                 // Trace message
86                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0} - CALLED!", user)); //NOI18N
87
88                 // user should not be null
89                 if (null == user) {
90                         // Abort here
91                         throw new NullPointerException("user is null"); //NOI18N
92                 }
93
94                 // Check if user is registered
95                 if (this.isUserNameRegistered(user)) {
96                         // Abort here
97                         throw new UserNameAlreadyRegisteredException(user);
98                 } else if (this.isEmailAddressRegistered(user)) {
99                         // Abort here
100                         throw new EmailAddressAlreadyRegisteredException(user);
101                 }
102
103                 // Call other EJB
104                 User addedUser = this.userBean.addUser(user);
105
106                 // Trace message
107                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userIdd={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N
108
109                 // Return it
110                 return user;
111         }
112 }