/*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016, 2017 Roland Häder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
*/
package org.mxchange.addressbook.beans.resendlink;
-import de.chotime.landingpage.beans.resendlink.ResendLinkSessionBeanRemote;
import java.text.MessageFormat;
import java.util.Locale;
-import javax.annotation.PostConstruct;
+import javax.ejb.EJB;
+import javax.ejb.EJBException;
import javax.ejb.Stateless;
-import javax.faces.FacesException;
-import javax.jms.Connection;
-import javax.jms.JMSException;
-import javax.jms.MessageProducer;
-import javax.jms.ObjectMessage;
-import javax.jms.Queue;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.Session;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
+import javax.mail.Address;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+import org.mxchange.jusercore.exceptions.UserNotFoundException;
+import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
+import org.mxchange.jusercore.exceptions.UserStatusLockedException;
+import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
+import org.mxchange.jusercore.model.resendlink.ResendLinkSessionBeanRemote;
+import org.mxchange.jusercore.model.user.LoginUser;
import org.mxchange.jusercore.model.user.User;
+import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
import org.mxchange.jusercore.model.user.status.UserAccountStatus;
/**
* A session-based EJB for resending confirmation links
* <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
*/
@Stateless (name = "resendLink", description = "A bean resending confirmation links")
public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBean implements ResendLinkSessionBeanRemote {
private static final long serialVersionUID = 71_546_726_857_195_360L;
/**
- * Connection
+ * Registration bean
*/
- private Connection connection;
+ @EJB
+ private UserRegistrationSessionBeanRemote registerBean;
/**
- * Object message
+ * Regular user bean
*/
- private ObjectMessage message;
-
- /**
- * Message producer
- */
- private MessageProducer messageProducer;
-
- /**
- * Mailer message queue
- */
- private Queue queue;
-
- /**
- * Session instance
- */
- private Session session;
-
- /**
- * Initialization of this bean
- */
- @PostConstruct
- public void init () {
- // Trace message
- this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
-
- try {
- // Get initial context
- Context context = new InitialContext();
-
- // Get factory from JMS resource
- QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
-
- // Lookup queue
- this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //NOI18N
-
- // Create connection
- this.connection = connectionFactory.createConnection();
-
- // Init session instance
- this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
- // And message producer
- this.messageProducer = this.session.createProducer(this.queue);
-
- // Finally the message instance itself
- this.message = this.session.createObjectMessage();
- } catch (final NamingException | JMSException e) {
- // Continued to throw
- throw new FacesException(e);
- }
- }
+ @EJB
+ private UserSessionBeanRemote userBean;
@Override
- public String resendConfirmationLink (final User user, final Locale locale) {
+ public void resendConfirmationLink (final User user, final Locale locale, final String baseUrl) throws UserNotFoundException, UserStatusLockedException, UserStatusConfirmedException {
// Log trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("resendConfirmationLink: user={0} - CALLED!", user)); //NOI18N
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: user={1},locale={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, locale, baseUrl)); //NOI18N
// The user instance should be valid
if (null == user) {
} else if (user.getUserId() < 1) {
// Invalid id number
throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
+ } else if (!this.userBean.ifUserExists(user)) {
+ // User not found
+ throw new UserNotFoundException(user);
} else if (user.getUserConfirmKey() == null) {
// Throw NPE again
throw new NullPointerException("this.userConfirmKey is null"); //NOI18N
- } else if (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED) {
+ } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
// User account status is not UNCONFIRMED
- throw new IllegalStateException(MessageFormat.format("Account status from user.userId={0} is not UNCONFIRMED:{1}", user.getUserId(), user.getUserAccountStatus())); //NOI18N
+ throw new UserStatusConfirmedException(user);
+ } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
+ // User account status is not UNCONFIRMED
+ throw new UserStatusLockedException(user);
} else if (null == locale) {
// Locale should be set
throw new NullPointerException("locale is null"); //NOI18N
}
- // @TODO Unfinished!
- // All fine
- return "resend_done"; //NOI18N
+ // Get new registration key
+ String confirmationKey = this.registerBean.generateConfirmationKey(user);
+
+ // Get managed instance
+ User managedUser = this.getEntityManager().find(LoginUser.class, user.getUserId());
+
+ // Set it in user
+ managedUser.setUserConfirmKey(confirmationKey);
+
+ // Init variable
+ Address emailAddress;
+
+ try {
+ // Create email address and set
+ emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
+ } catch (final AddressException ex) {
+ // Throw again
+ throw new EJBException(ex);
+ }
+
+ // Send email
+ // @TODO: Internationlize the subject line somehow
+ this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user, baseUrl); //NOI18N
+
+ // Log trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: EXIT!", this.getClass().getSimpleName())); //NOI18N
}
}