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