]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
3c5e743c24cea79af777f77d40b2f8ac715abf97
[addressbook-mailer-ejb.git] / src / java / org / mxchange / addressbook / beans / resendlink / AddressbookResendLinkSessionBean.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.addressbook.beans.resendlink;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
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 org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
27 import org.mxchange.jusercore.model.user.User;
28 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
29
30 /**
31  * A session-based EJB for resending confirmation links
32  * <p>
33  * @author Roland Haeder<roland@mxchange.org>
34  */
35 @Stateless (name = "resendLink", description = "A bean resending confirmation links")
36 public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBean implements ResendLinkSessionBeanRemote {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 71_546_726_857_195_360L;
42
43         @Override
44         public void resendConfirmationLink (final User user, final Locale locale, final String baseUrl) {
45                 // Log trace message
46                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("resendConfirmationLink: user={0},locale={1},baseUrl={2} - CALLED!", user, locale, baseUrl)); //NOI18N
47
48                 // The user instance should be valid
49                 if (null == user) {
50                         // Throw NPE
51                         throw new NullPointerException("user is null"); //NOI18N
52                 } else if (user.getUserId() == null) {
53                         // Throw NPE again
54                         throw new NullPointerException("user.userId is null"); //NOI18N
55                 } else if (user.getUserId() < 1) {
56                         // Invalid id number
57                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
58                 } else if (user.getUserConfirmKey() == null) {
59                         // Throw NPE again
60                         throw new NullPointerException("this.userConfirmKey is null"); //NOI18N
61                 } else if (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED) {
62                         // User account status is not UNCONFIRMED
63                         throw new IllegalStateException(MessageFormat.format("Account status from user.userId={0} is not UNCONFIRMED:{1}", user.getUserId(), user.getUserAccountStatus())); //NOI18N
64                 } else if (null == locale) {
65                         // Locale should be set
66                         throw new NullPointerException("locale is null"); //NOI18N
67                 }
68
69                 // @TODO Unfinished: Change key + merge database
70
71                 // Init variable
72                 Address emailAddress;
73
74                 try {
75                         // Create email address and set
76                         emailAddress = new InternetAddress(user.getUserContact().getContactEmailAddress());
77                 } catch (final AddressException ex) {
78                         // Throw again
79                         throw new EJBException(ex);
80                 }
81
82                 // Send email
83                 // TODO: Internationlize the subject line somehow
84                 this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user, baseUrl); //NOI18N
85
86                 // Log trace message
87                 this.getLoggerBeanLocal().logTrace("resendConfirmationLink: CALLED!"); //NOI18N
88         }
89
90 }