]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
Continued:
[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 javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.jcontacts.contact.Contact;
25 import org.mxchange.jcoreee.database.BaseDatabaseBean;
26 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
27 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
28 import org.mxchange.jusercore.model.user.LoginUser;
29 import org.mxchange.jusercore.model.user.User;
30 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
31 import org.mxchange.jusercore.model.user.UserUtils;
32
33 /**
34  * A session bean for user registration
35  * <p>
36  * @author Roland Haeder<roland@mxchange.org>
37  */
38 @Stateless (name = "register", description = "A bean handling the user registration")
39 public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean implements UserRegistrationSessionBeanRemote {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 12_348_958_986_818_627L;
45
46         /**
47          * User EJB
48          */
49         @EJB
50         private UserSessionBeanRemote userBean;
51
52         @Override
53         public String generateConfirmationKey (final User user) {
54                 // Trace message
55                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: user={0} - CALLED!", user)); //NOI18N
56
57                 // user should not be null
58                 if (null == user) {
59                         // Abort here
60                         throw new NullPointerException("user is null"); //NOI18N
61                 }
62
63                 // Create named instance
64                 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
65
66                 // Init confirmation key
67                 String confirmationKey = null;
68
69                 // Find a free one
70                 while (confirmationKey == null) {
71                         // Create new one
72                         String key = UserUtils.generatedConfirmationKey(user);
73
74                         // Set key as parameter
75                         query.setParameter("confirmKey", key); //NOI18N
76
77                         // Try it
78                         try {
79                                 // Get contact instance
80                                 Contact contact = (Contact) query.getSingleResult();
81
82                                 // Warning message
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;
87                                 break;
88                         }
89                 }
90
91                 // Log trace message
92                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateConfirmationKey: confirmationKey={0} - EXIT!", confirmationKey)); //NOI18N
93
94                 // Return it
95                 return confirmationKey;
96         }
97
98         @Override
99         public boolean isEmailAddressRegistered (final User user) {
100                 // Trace message
101                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
102
103                 // Check bean
104                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
105
106                 // user should not be null
107                 if (null == user) {
108                         // Abort here
109                         throw new NullPointerException("user is null"); //NOI18N
110                 }
111
112                 // Call other bean
113                 return this.userBean.isEmailAddressRegistered(user);
114         }
115
116         @Override
117         public boolean isUserNameRegistered (final User user) {
118                 // Trace message
119                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
120
121                 // Check bean
122                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
123
124                 // user should not be null
125                 if (null == user) {
126                         // Abort here
127                         throw new NullPointerException("user is null"); //NOI18N
128                 }
129
130                 // Call other bean
131                 return this.userBean.isUserNameRegistered(user);
132         }
133
134         @Override
135         public User registerUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
136                 // Trace message
137                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0} - CALLED!", user)); //NOI18N
138
139                 // user should not be null
140                 if (null == user) {
141                         // Abort here
142                         throw new NullPointerException("user is null"); //NOI18N
143                 }
144
145                 // Check if user is registered
146                 if (this.isUserNameRegistered(user)) {
147                         // Abort here
148                         throw new UserNameAlreadyRegisteredException(user);
149                 } else if (this.isEmailAddressRegistered(user)) {
150                         // Abort here
151                         throw new EmailAddressAlreadyRegisteredException(user);
152                 }
153
154                 // Call other EJB
155                 User addedUser = this.userBean.addUser(user);
156
157                 // Trace message
158                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userIdd={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N
159
160                 // Return it
161                 return addedUser;
162         }
163
164 }