]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java
updated own name and resources
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / register / AddressbookUserRegistrationSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Häder
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.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.addressbook.database.BaseAddressbookDatabaseBean;
29 import org.mxchange.jcontacts.contact.Contact;
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;
37
38 /**
39  * A session bean for user registration
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @Stateless (name = "register", description = "A bean handling the user registration")
44 public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 12_348_958_986_818_627L;
50
51         /**
52          * Administrative user bean
53          */
54         @EJB
55         private AdminUserSessionBeanRemote adminUserBean;
56
57         /**
58          * Regular user EJB
59          */
60         @EJB
61         private UserSessionBeanRemote userBean;
62
63         @Override
64         public String generateConfirmationKey (final User user) {
65                 // Trace message
66                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
67
68                 // user should not be null
69                 if (null == user) {
70                         // Abort here
71                         throw new NullPointerException("user is null"); //NOI18N
72                 }
73
74                 // Create named instance
75                 Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N
76
77                 // Init confirmation key
78                 String confirmationKey = null;
79
80                 // Find a free one
81                 while (confirmationKey == null) {
82                         // Create new one
83                         String key = UserUtils.generatedConfirmationKey(user);
84
85                         // Set key as parameter
86                         query.setParameter("confirmKey", key); //NOI18N
87
88                         // Try it
89                         try {
90                                 // Get contact instance
91                                 Contact contact = (Contact) query.getSingleResult();
92
93                                 // Warning message
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;
98                                 break;
99                         }
100                 }
101
102                 // Log trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N
104
105                 // Return it
106                 return confirmationKey;
107         }
108
109         @Override
110         public boolean isEmailAddressRegistered (final User user) {
111                 // Trace message
112                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
113
114                 // Check bean
115                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
116
117                 // user should not be null
118                 if (null == user) {
119                         // Abort here
120                         throw new NullPointerException("user is null"); //NOI18N
121                 }
122
123                 // Call other bean
124                 return this.userBean.isEmailAddressRegistered(user);
125         }
126
127         @Override
128         public boolean isUserNameRegistered (final User user) {
129                 // Trace message
130                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
131
132                 // Check bean
133                 assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N
134
135                 // user should not be null
136                 if (null == user) {
137                         // Abort here
138                         throw new NullPointerException("user is null"); //NOI18N
139                 }
140
141                 // Call other bean
142                 return this.userBean.isUserNameRegistered(user);
143         }
144
145         @Override
146         public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
147                 // Trace message
148                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
149
150                 // user should not be null
151                 if (null == user) {
152                         // Abort here
153                         throw new NullPointerException("user is null"); //NOI18N
154                 } else if (user.getUserContact() == null) {
155                         // Throw NPE again
156                         throw new NullPointerException("user.userContact is null"); //NOI18N
157                 } else if (user.getUserContact().getContactEmailAddress() == null) {
158                         // Throw NPE again
159                         throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N
160                 } else if (user.getUserContact().getContactEmailAddress().isEmpty()) {
161                         // Is empty
162                         throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N
163                 }
164
165                 // Check if user is registered
166                 if (this.isUserNameRegistered(user)) {
167                         // Abort here
168                         throw new UserNameAlreadyRegisteredException(user);
169                 } else if (this.isEmailAddressRegistered(user)) {
170                         // Abort here
171                         throw new EmailAddressAlreadyRegisteredException(user);
172                 }
173
174                 // Call other EJB
175                 User addedUser = this.adminUserBean.addUser(user);
176
177                 // Init variable
178                 Address emailAddress;
179
180                 try {
181                         // Create email address and set
182                         emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress());
183                 } catch (final AddressException ex) {
184                         // Throw again
185                         throw new EJBException(ex);
186                 }
187
188                 // Send email
189                 // TODO: Internationlize the subject line somehow
190                 this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N
191
192                 // Trace message
193                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N
194
195                 // Return it
196                 return addedUser;
197         }
198
199 }