2 * Copyright (C) 2016 - 2024 Free Software Foundation
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.pizzaapplication.beans.user.resendlink;
19 import java.util.Locale;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.Any;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
28 import org.mxchange.jcoreee.utils.FacesUtils;
29 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
30 import org.mxchange.jusercore.exceptions.UserNotFoundException;
31 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
32 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
33 import org.mxchange.jusercore.model.user.User;
34 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
35 import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent;
36 import org.mxchange.juserlogincore.events.resendlink.UserResendLinkAccountEvent;
37 import org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote;
38 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
39 import org.mxchange.pizzaapplication.beans.localization.PizzaLocalizationSessionController;
40 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebRequestController;
43 * A web session-scoped bean for resending confirmation link
45 * @author Roland Häder<roland@mxchange.org>
47 @Named ("userResendConfirmationController")
49 public class PizzaResendLinkWebRequestBean extends BasePizzaBean implements PizzaResendLinkWebRequestController {
54 private static final long serialVersionUID = 186_078_724_659_153L;
59 private String emailAddress;
64 private Locale locale;
67 * Localization controller
70 private PizzaLocalizationSessionController localizationController;
73 * EJB for resending confirmation link
75 @EJB (lookup = "java:global/pizzaservice-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote")
76 private ResendLinkSessionBeanRemote resendLinkBean;
79 * Regular user controller
82 private PizzaUserWebRequestController userController;
85 * Event being fired after confirmation link is being sent
89 private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
94 public PizzaResendLinkWebRequestBean () {
95 // Call super constructor
100 * Observer method for events being fired when the application's locale has
103 * @param event Event being fired
105 public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
106 // Is the parameter valid?
109 throw new NullPointerException("event is null");
110 } else if (event.getLocale() == null) {
112 throw new NullPointerException("event.locale is null");
116 this.setLocale(event.getLocale());
120 * Resends (new) confirmation link to given email address, if found.
121 * Otherwise an exception is thrown. On success a redirect takes place.
123 * @return Redirect outcome
125 public String doResendLink () {
126 // The email address should not be empty as the JSF validates this
127 if (this.getEmailAddress() == null) {
129 throw new NullPointerException("this.emailAddress is null"); //NOI18N
132 // Init user instance
136 // Is the email address really not used?
137 user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
138 } catch (final UserEmailAddressNotFoundException ex) {
142 // Not found, should not happen as the registered validator should find it
143 this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N
147 // Is the user account already confirmed?
148 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
153 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
155 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
159 // User account is locked
160 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
162 } else if (user.getUserConfirmKey() == null) {
163 // Status is UNCONFIRMED but confirmation key is NULL
164 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
167 // Init managed user instance
168 final User managedUser;
172 final String baseUrl = FacesUtils.generateBaseUrl();
174 // Call EJB and return redirect target
175 managedUser = this.resendLinkBean.resendConfirmationLink(user, this.getLocale(), baseUrl);
176 } catch (final UserNotFoundException ex) {
178 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
180 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
181 // Output message, this should not happen as the confirmation key is being removed
182 this.showFacesException("form_resend_link:resendEmailAddress", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
190 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
192 // Return redirect target
193 return "user_resend_done"; //NOI18N
197 * Getter for email address 1 (changing)
199 * @return Email address
201 public String getEmailAddress () {
202 return this.emailAddress;
206 * Setter for email address 1 (changing)
208 * @param emailAddress Email address 1
210 public void setEmailAddress (final String emailAddress) {
211 this.emailAddress = emailAddress;
215 * Clears email address fields so the user has to re-enter them
217 private void clear () {
219 this.setEmailAddress(null);
223 * Getter for locale instance
225 * @return Locale instance
227 private Locale getLocale () {
232 * Setter for locale instance
234 * @param locale Locale instance
236 private void setLocale (final Locale locale) {
237 this.locale = locale;