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 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.jjobs.beans.BaseJobsController;
31 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
32 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
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.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent;
40 import org.mxchange.juserlogincore.events.resendlink.UserResendLinkAccountEvent;
41 import org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote;
44 * A web session-scoped bean for resending confirmation link
46 * @author Roland Häder<roland@mxchange.org>
48 @Named ("userResendConfirmationController")
50 public class JobsResendLinkWebSessionBean extends BaseJobsController implements JobsResendLinkWebSessionController {
55 private static final long serialVersionUID = 186_078_724_659_153L;
60 private String emailAddress;
63 * Localization controller
66 private JobsLocalizationSessionController localizationController;
69 * EJB for resending confirmation link
71 private ResendLinkSessionBeanRemote resendLinkBean;
74 * Regular user controller
77 private JobsUserWebSessionController userController;
80 * Event being fired after confirmation link is being sent
84 private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
89 public JobsResendLinkWebSessionBean () {
90 // Call super constructor
95 * Resends (new) confirmation link to given email address, if found.
96 * Otherwise an exception is thrown. On success a redirect takes place.
98 * @return Redirect outcome
100 public String doResendLink () {
101 // The email address should not be empty as the JSF validates this
102 if (this.getEmailAddress() == null) {
104 throw new NullPointerException("this.emailAddress is null"); //NOI18N
107 // Init user instance
111 // Is the email address really not used?
112 user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
113 } catch (final UserEmailAddressNotFoundException ex) {
117 // Not found, should not happen as the registered validator should find it
118 this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N
122 // Is the user account already confirmed?
123 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
128 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
130 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
134 // User account is locked
135 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
137 } else if (user.getUserConfirmKey() == null) {
138 // Status is UNCONFIRMED but confirmation key is NULL
139 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
142 // Init managed user instance
147 String baseUrl = FacesUtils.generateBaseUrl();
149 // Call EJB and return redirect target
150 managedUser = this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
151 } catch (final UserNotFoundException ex) {
153 this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
155 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
156 // Output message, this should not happen as the confirmation key is being removed
157 this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N
165 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
167 // Return redirect target
168 return "user_resend_done"; //NOI18N
172 * Getter for email address 1 (changing)
174 * @return Email address
176 public String getEmailAddress () {
177 return this.emailAddress;
181 * Setter for email address 1 (changing)
183 * @param emailAddress Email address 1
185 public void setEmailAddress (final String emailAddress) {
186 this.emailAddress = emailAddress;
190 * Post-construction method
193 public void init () {
196 // Get initial context
197 Context context = new InitialContext();
200 this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
201 } catch (final NamingException e) {
203 throw new FaceletException(e);
208 * Clears email address fields so the user has to re-enter them
210 private void clear () {
212 this.setEmailAddress(null);