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