2 * Copyright (C) 2016, 2017 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.user.resendlink;
19 import java.util.Locale;
20 import javax.annotation.PostConstruct;
21 import javax.enterprise.context.SessionScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jjobs.beans.BaseJobsController;
34 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
35 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
36 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
37 import org.mxchange.jusercore.exceptions.UserNotFoundException;
38 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
39 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
40 import org.mxchange.jusercore.model.user.User;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent;
43 import org.mxchange.juserlogincore.events.resendlink.UserResendLinkAccountEvent;
44 import org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote;
47 * A web session-scoped bean for resending confirmation link
49 * @author Roland Häder<roland@mxchange.org>
51 @Named ("userResendConfirmationController")
53 public class JobsResendLinkWebSessionBean extends BaseJobsController implements JobsResendLinkWebSessionController {
58 private static final long serialVersionUID = 186_078_724_659_153L;
63 private String emailAddress;
68 private Locale locale;
71 * Localization controller
74 private JobsLocalizationSessionController localizationController;
77 * EJB for resending confirmation link
79 private ResendLinkSessionBeanRemote resendLinkBean;
82 * Regular user controller
85 private JobsUserWebSessionController userController;
88 * Event being fired after confirmation link is being sent
92 private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
97 public JobsResendLinkWebSessionBean () {
98 // Call super constructor
103 * Observer method for events being fired when the application's locale has
106 * @param event Event being fired
108 public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
109 // Is the parameter valid?
112 throw new NullPointerException("event is null");
113 } else if (event.getLocale() == null) {
115 throw new NullPointerException("event.locale is null");
119 this.setLocale(event.getLocale());
123 * Resends (new) confirmation link to given email address, if found.
124 * Otherwise an exception is thrown. On success a redirect takes place.
126 * @return Redirect outcome
128 public String doResendLink () {
129 // The email address should not be empty as the JSF validates this
130 if (this.getEmailAddress() == null) {
132 throw new NullPointerException("this.emailAddress is null"); //NOI18N
135 // Init user instance
139 // Is the email address really not used?
140 user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
141 } catch (final UserEmailAddressNotFoundException ex) {
145 // Not found, should not happen as the registered validator should find it
146 this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N
150 // Is the user account already confirmed?
151 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
156 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
158 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
162 // User account is locked
163 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
165 } else if (user.getUserConfirmKey() == null) {
166 // Status is UNCONFIRMED but confirmation key is NULL
167 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
170 // Init managed user instance
175 String baseUrl = FacesUtils.generateBaseUrl();
177 // Call EJB and return redirect target
178 managedUser = this.resendLinkBean.resendConfirmationLink(user, this.getLocale(), baseUrl);
179 } catch (final UserNotFoundException ex) {
181 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
183 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
184 // Output message, this should not happen as the confirmation key is being removed
185 this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N
193 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
195 // Return redirect target
196 return "user_resend_done"; //NOI18N
200 * Getter for email address 1 (changing)
202 * @return Email address
204 public String getEmailAddress () {
205 return this.emailAddress;
209 * Setter for email address 1 (changing)
211 * @param emailAddress Email address 1
213 public void setEmailAddress (final String emailAddress) {
214 this.emailAddress = emailAddress;
218 * Getter for locale instance
220 * @return Locale instance
222 private Locale getLocale () {
227 * Setter for locale instance
229 * @param locale Locale instance
231 private void setLocale (final Locale locale) {
232 this.locale = locale;
236 * Post-construction method
239 public void init () {
242 // Get initial context
243 Context context = new InitialContext();
246 this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
247 } catch (final NamingException e) {
249 throw new FaceletException(e);
254 * Clears email address fields so the user has to re-enter them
256 private void clear () {
258 this.setEmailAddress(null);