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.pizzaapplication.beans.user.resendlink;
19 import javax.annotation.PostConstruct;
20 import javax.enterprise.context.SessionScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcoreee.utils.FacesUtils;
30 import org.mxchange.jusercore.events.resendlink.ObservableUserResendLinkAccountEvent;
31 import org.mxchange.jusercore.events.resendlink.UserResendLinkAccountEvent;
32 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
33 import org.mxchange.jusercore.exceptions.UserNotFoundException;
34 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
35 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.resendlink.ResendLinkSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
39 import org.mxchange.pizzaapplication.beans.BasePizzaController;
40 import org.mxchange.pizzaapplication.beans.localization.PizzaLocalizationSessionController;
41 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
44 * A web session-scoped bean for resending confirmation link
46 * @author Roland Häder<roland@mxchange.org>
48 @Named ("userResendConfirmationController")
50 public class PizzaResendLinkWebSessionBean extends BasePizzaController implements PizzaResendLinkWebSessionController {
55 private static final long serialVersionUID = 186_078_724_659_153L;
60 private String emailAddress;
63 * Localization controller
66 private PizzaLocalizationSessionController localizationController;
69 * EJB for resending confirmation link
71 private ResendLinkSessionBeanRemote resendLinkBean;
74 * Regular user controller
77 private PizzaUserWebSessionController userController;
80 * Event being fired after confirmation link is being sent
84 private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
89 public PizzaResendLinkWebSessionBean () {
90 // Call super constructor
95 public String doResendLink () {
96 // The email address should not be empty as the JSF validates this
97 if (this.getEmailAddress() == null) {
99 throw new NullPointerException("this.emailAddress is null"); //NOI18N
102 // Init user instance
106 // Is the email address really not used?
107 user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
108 } catch (final UserEmailAddressNotFoundException ex) {
112 // Not found, should not happen as the registered validator should find it
113 this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N
117 // Is the user account already confirmed?
118 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
123 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
125 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
129 // User account is locked
130 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
132 } else if (user.getUserConfirmKey() == null) {
133 // Status is UNCONFIRMED but confirmation key is NULL
134 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
137 // Init managed user instance
142 String baseUrl = FacesUtils.generateBaseUrl();
144 // Call EJB and return redirect target
145 managedUser = this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
146 } catch (final UserNotFoundException ex) {
148 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
150 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
151 // Output message, this should not happen as the confirmation key is being removed
152 this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N
160 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
162 // Return redirect target
163 return "user_resend_done"; //NOI18N
167 public String getEmailAddress () {
168 return this.emailAddress;
172 public void setEmailAddress (final String emailAddress) {
173 this.emailAddress = emailAddress;
177 * Post-construction method
180 public void init () {
183 // Get initial context
184 Context context = new InitialContext();
187 this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/userResendConfirmationLink!org.mxchange.jusercore.model.user.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
188 } catch (final NamingException e) {
190 throw new FaceletException(e);
195 * Clears email address fields so the user has to re-enter them
197 private void clear () {
199 this.setEmailAddress(null);