2 * Copyright (C) 2016 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.resendlink;
19 import javax.enterprise.context.SessionScoped;
20 import javax.enterprise.event.Event;
21 import javax.enterprise.inject.Any;
22 import javax.faces.view.facelets.FaceletException;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcoreee.utils.FacesUtils;
29 import org.mxchange.jjobs.beans.BaseJobsController;
30 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
31 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
32 import org.mxchange.jusercore.events.resendlink.UserResendLinkAccountEvent;
33 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
34 import org.mxchange.jusercore.exceptions.UserNotFoundException;
35 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
36 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
37 import org.mxchange.jusercore.model.user.User;
38 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
39 import org.mxchange.jusercore.events.resendlink.ObservableUserResendLinkAccountEvent;
42 * A web session bean for resending confirmation link
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("resendController")
48 public class JobsResendLinkWebSessionBean extends BaseJobsController implements JobsResendLinkWebSessionController {
53 private static final long serialVersionUID = 186_078_724_659_153L;
58 private String emailAddress;
61 * EJB for resending confirmation link
63 private ResendLinkSessionBeanRemote emailBean;
66 * Localization controller
69 private JobsLocalizationSessionController localizationController;
72 * Regular user controller
75 private JobsUserWebSessionController userController;
78 * Event being fired after confirmation link is being sent
82 private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
87 public JobsResendLinkWebSessionBean () {
90 // Get initial context
91 Context context = new InitialContext();
94 this.emailBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/jjobs-ejb/resendLink!org.mxchange.jjobs.beans.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
95 } catch (final NamingException e) {
97 throw new FaceletException(e);
102 public String doResendLink () {
103 // The email address should not be empty as the JSF validates this
104 if (this.getEmailAddress() == null) {
106 throw new NullPointerException("this.emailAddress is null"); //NOI18N
109 // Init user instance
113 // Is the email address really not used?
114 user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
115 } catch (final UserEmailAddressNotFoundException ex) {
119 // Not found, should not happen as the registered validator should find it
120 this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N
124 // Is the user account already confirmed?
125 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
130 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
132 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
136 // User account is locked
137 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
139 } else if (user.getUserConfirmKey() == null) {
140 // Status is UNCONFIRMED but confirmation key is NULL
141 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
146 String baseUrl = FacesUtils.generateBaseUrl();
148 // Call EJB and return redirect target
149 this.emailBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
150 } catch (final UserNotFoundException ex) {
152 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
154 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
155 // Output message, this should not happen as the confirmation key is being removed
156 this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N
161 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(user));
166 // Return redirect target
167 return "user_resend_done"; //NOI18N
171 public String getEmailAddress () {
172 return this.emailAddress;
176 public void setEmailAddress (final String emailAddress) {
177 this.emailAddress = emailAddress;
181 * Clears email address fields so the user has to re-enter them
183 private void clear () {
185 this.setEmailAddress(null);