]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jjobs/beans/resendlink/JobsResendLinkSessionBean.java
@TODO is better than only 'TODO'
[jjobs-ejb.git] / src / java / org / mxchange / jjobs / beans / resendlink / JobsResendLinkSessionBean.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.jjobs.beans.resendlink;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import javax.ejb.EJB;
22 import javax.ejb.EJBException;
23 import javax.ejb.Stateless;
24 import javax.mail.Address;
25 import javax.mail.internet.AddressException;
26 import javax.mail.internet.InternetAddress;
27 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
28 import org.mxchange.jusercore.exceptions.UserNotFoundException;
29 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
30 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
31 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
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.status.UserAccountStatus;
36
37 /**
38  * A session-based EJB for resending confirmation links
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Stateless (name = "resendLink", description = "A bean resending confirmation links")
43 public class JobsResendLinkSessionBean extends BaseJobsDatabaseBean implements ResendLinkSessionBeanRemote {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 71_546_726_857_195_360L;
49
50         /**
51          * Registration bean
52          */
53         @EJB
54         private UserRegistrationSessionBeanRemote registerBean;
55
56         /**
57          * Regular user bean
58          */
59         @EJB
60         private UserSessionBeanRemote userBean;
61
62         @Override
63         public void resendConfirmationLink (final User user, final Locale locale, final String baseUrl) throws UserNotFoundException, UserStatusConfirmedException, UserStatusLockedException {
64                 // Log trace message
65                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: user={1},locale={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, locale, baseUrl)); //NOI18N
66
67                 // The user instance should be valid
68                 if (null == user) {
69                         // Throw NPE
70                         throw new NullPointerException("user is null"); //NOI18N
71                 } else if (user.getUserId() == null) {
72                         // Throw NPE again
73                         throw new NullPointerException("user.userId is null"); //NOI18N
74                 } else if (user.getUserId() < 1) {
75                         // Invalid id number
76                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
77                 } else if (!this.userBean.ifUserExists(user)) {
78                         // User not found
79                         throw new UserNotFoundException(user);
80                 } else if (user.getUserConfirmKey() == null) {
81                         // Throw NPE again
82                         throw new NullPointerException("this.userConfirmKey is null"); //NOI18N
83                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
84                         // User account status is not UNCONFIRMED
85                         throw new UserStatusConfirmedException(user);
86                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
87                         // User account status is not UNCONFIRMED
88                         throw new UserStatusLockedException(user);
89                 } else if (null == locale) {
90                         // Locale should be set
91                         throw new NullPointerException("locale is null"); //NOI18N
92                 }
93
94                 // Get new registration key
95                 String confirmationKey = this.registerBean.generateConfirmationKey(user);
96
97                 // Get managed instance
98                 User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
99
100                 // Set it in user
101                 managedUser.setUserConfirmKey(confirmationKey);
102
103                 // Init variable
104                 Address emailAddress;
105
106                 try {
107                         // Create email address and set
108                         emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
109                 } catch (final AddressException ex) {
110                         // Throw again
111                         throw new EJBException(ex);
112                 }
113
114                 // Send email
115                 // @TODO: Internationlize the subject line somehow
116                 this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user, baseUrl); //NOI18N
117
118                 // Log trace message
119                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: EXIT!", this.getClass().getSimpleName())); //NOI18N
120         }
121
122 }