]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/resendlink/JobsResendLinkWebSessionBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / resendlink / JobsResendLinkWebSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.beans.user.resendlink;
18
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;
45
46 /**
47  * A web session-scoped bean for resending confirmation link
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Named ("userResendConfirmationController")
52 @SessionScoped
53 public class JobsResendLinkWebSessionBean extends BaseJobsController implements JobsResendLinkWebSessionController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 186_078_724_659_153L;
59
60         /**
61          * Email address
62          */
63         private String emailAddress;
64
65         /**
66          * Locale instance
67          */
68         private Locale locale;
69
70         /**
71          * Localization controller
72          */
73         @Inject
74         private JobsLocalizationSessionController localizationController;
75
76         /**
77          * EJB for resending confirmation link
78          */
79         private ResendLinkSessionBeanRemote resendLinkBean;
80
81         /**
82          * Regular user controller
83          */
84         @Inject
85         private JobsUserWebSessionController userController;
86
87         /**
88          * Event being fired after confirmation link is being sent
89          */
90         @Inject
91         @Any
92         private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
93
94         /**
95          * Default constructor
96          */
97         public JobsResendLinkWebSessionBean () {
98                 // Call super constructor
99                 super();
100         }
101
102         /**
103          * Observer method for events being fired when the application's locale has
104          * been changed.
105          * <p>
106          * @param event Event being fired
107          */
108         public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
109                 // Is the parameter valid?
110                 if (null == event) {
111                         // Throw NPE
112                         throw new NullPointerException("event is null");
113                 } else if (event.getLocale() == null) {
114                         // Throw NPE again
115                         throw new NullPointerException("event.locale is null");
116                 }
117
118                 // Set it here
119                 this.setLocale(event.getLocale());
120         }
121
122         /**
123          * Resends (new) confirmation link to given email address, if found.
124          * Otherwise an exception is thrown. On success a redirect takes place.
125          * <p>
126          * @return Redirect outcome
127          */
128         public String doResendLink () {
129                 // The email address should not be empty as the JSF validates this
130                 if (this.getEmailAddress() == null) {
131                         // Throw NPE
132                         throw new NullPointerException("this.emailAddress is null"); //NOI18N
133                 }
134
135                 // Init user instance
136                 User user;
137
138                 try {
139                         // Is the email address really not used?
140                         user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
141                 } catch (final UserEmailAddressNotFoundException ex) {
142                         // Always clear bean
143                         this.clear();
144
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
147                         return ""; //NOI18N
148                 }
149
150                 // Is the user account already confirmed?
151                 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
152                         // Always clear bean
153                         this.clear();
154
155                         // Then abort here
156                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
157                         return ""; //NOI18N
158                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
159                         // Always clear bean
160                         this.clear();
161
162                         // User account is locked
163                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
164                         return ""; //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
168                 }
169
170                 // Init managed user instance
171                 User managedUser;
172
173                 try {
174                         // Get base URL
175                         String baseUrl = FacesUtils.generateBaseUrl();
176
177                         // Call EJB and return redirect target
178                         managedUser = this.resendLinkBean.resendConfirmationLink(user, this.getLocale(), baseUrl);
179                 } catch (final UserNotFoundException ex) {
180                         // User not found
181                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
182                         return ""; //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
186                         return ""; //NOI18N
187                 }
188
189                 // Clear this bean
190                 this.clear();
191
192                 // Fire event
193                 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
194
195                 // Return redirect target
196                 return "user_resend_done"; //NOI18N
197         }
198
199         /**
200          * Getter for email address 1 (changing)
201          * <p>
202          * @return Email address
203          */
204         public String getEmailAddress () {
205                 return this.emailAddress;
206         }
207
208         /**
209          * Setter for email address 1 (changing)
210          * <p>
211          * @param emailAddress Email address 1
212          */
213         public void setEmailAddress (final String emailAddress) {
214                 this.emailAddress = emailAddress;
215         }
216
217         /**
218          * Getter for locale instance
219          * <p>
220          * @return Locale instance
221          */
222         private Locale getLocale () {
223                 return this.locale;
224         }
225
226         /**
227          * Setter for locale instance
228          * <p>
229          * @param locale Locale instance
230          */
231         private void setLocale (final Locale locale) {
232                 this.locale = locale;
233         }
234
235         /**
236          * Post-construction method
237          */
238         @PostConstruct
239         public void init () {
240                 // Try it
241                 try {
242                         // Get initial context
243                         Context context = new InitialContext();
244
245                         // Try to lookup
246                         this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
247                 } catch (final NamingException e) {
248                         // Throw again
249                         throw new FaceletException(e);
250                 }
251         }
252
253         /**
254          * Clears email address fields so the user has to re-enter them
255          */
256         private void clear () {
257                 // Clear fields
258                 this.setEmailAddress(null);
259         }
260
261 }