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