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