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