]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
Continued:
[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 de.chotime.landingpage.beans.resendlink.ResendLinkSessionBeanRemote;
20 import java.text.MessageFormat;
21 import java.util.Locale;
22 import javax.annotation.PostConstruct;
23 import javax.ejb.Stateless;
24 import javax.faces.FacesException;
25 import javax.jms.Connection;
26 import javax.jms.JMSException;
27 import javax.jms.MessageProducer;
28 import javax.jms.ObjectMessage;
29 import javax.jms.Queue;
30 import javax.jms.QueueConnectionFactory;
31 import javax.jms.Session;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
38
39 /**
40  * A session-based EJB for resending confirmation links
41  * <p>
42  * @author Roland Haeder<roland@mxchange.org>
43  */
44 @Stateless (name = "resendLink", description = "A bean resending confirmation links")
45 public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBean implements ResendLinkSessionBeanRemote {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 71_546_726_857_195_360L;
51
52         /**
53          * Connection
54          */
55         private Connection connection;
56
57         /**
58          * Object message
59          */
60         private ObjectMessage message;
61
62         /**
63          * Message producer
64          */
65         private MessageProducer messageProducer;
66
67         /**
68          * Mailer message queue
69          */
70         private Queue queue;
71
72         /**
73          * Session instance
74          */
75         private Session session;
76
77         /**
78          * Initialization of this bean
79          */
80         @PostConstruct
81         public void init () {
82                 // Trace message
83                 this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
84
85                 try {
86                         // Get initial context
87                         Context context = new InitialContext();
88
89                         // Get factory from JMS resource
90                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
91
92                         // Lookup queue
93                         this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //NOI18N
94
95                         // Create connection
96                         this.connection = connectionFactory.createConnection();
97
98                         // Init session instance
99                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
100
101                         // And message producer
102                         this.messageProducer = this.session.createProducer(this.queue);
103
104                         // Finally the message instance itself
105                         this.message = this.session.createObjectMessage();
106                 } catch (final NamingException | JMSException e) {
107                         // Continued to throw
108                         throw new FacesException(e);
109                 }
110         }
111
112         @Override
113         public String resendConfirmationLink (final User user, final Locale locale) {
114                 // Log trace message
115                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("resendConfirmationLink: user={0} - CALLED!", user)); //NOI18N
116
117                 // The user instance should be valid
118                 if (null == user) {
119                         // Throw NPE
120                         throw new NullPointerException("user is null"); //NOI18N
121                 } else if (user.getUserId() == null) {
122                         // Throw NPE again
123                         throw new NullPointerException("user.userId is null"); //NOI18N
124                 } else if (user.getUserId() < 1) {
125                         // Invalid id number
126                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
127                 } else if (user.getUserConfirmKey() == null) {
128                         // Throw NPE again
129                         throw new NullPointerException("this.userConfirmKey is null"); //NOI18N
130                 } else if (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED) {
131                         // User account status is not UNCONFIRMED
132                         throw new IllegalStateException(MessageFormat.format("Account status from user.userId={0} is not UNCONFIRMED:{1}", user.getUserId(), user.getUserAccountStatus())); //NOI18N
133                 } else if (null == locale) {
134                         // Locale should be set
135                         throw new NullPointerException("locale is null"); //NOI18N
136                 }
137
138                 // @TODO Unfinished!
139                 // All fine
140                 return "resend_done"; //NOI18N
141         }
142
143 }