]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/resendlink/JobsResendLinkWebRequestBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / resendlink / JobsResendLinkWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2020 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.jjobs.beans.user.resendlink;
18
19 import javax.ejb.EJB;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.application.FacesMessage;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.jcoreee.utils.FacesUtils;
27 import org.mxchange.jjobs.beans.BaseJobsBean;
28 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
29 import org.mxchange.jjobs.beans.user.list.JobsUserListWebViewController;
30 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
31 import org.mxchange.jusercore.exceptions.UserNotFoundException;
32 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
33 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
34 import org.mxchange.jusercore.model.user.User;
35 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
36 import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent;
37 import org.mxchange.juserlogincore.events.resendlink.UserResendLinkAccountEvent;
38 import org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote;
39
40 /**
41  * A web session-scoped bean for resending confirmation link
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("userResendConfirmationController")
46 @RequestScoped
47 public class JobsResendLinkWebRequestBean extends BaseJobsBean implements JobsResendLinkWebRequestController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 186_078_724_659_153L;
53
54         /**
55          * Email address
56          */
57         private String emailAddress;
58
59         /**
60          * Localization controller
61          */
62         @Inject
63         private JobsLocalizationSessionController localizationController;
64
65         /**
66          * EJB for resending confirmation link
67          */
68         @EJB (lookup = "java:global/jjobs-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote")
69         private ResendLinkSessionBeanRemote resendLinkBean;
70
71         /**
72          * Regular user controller
73          */
74         @Inject
75         private JobsUserListWebViewController userListController;
76
77         /**
78          * Event being fired after confirmation link is being sent
79          */
80         @Inject
81         @Any
82         private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
83
84         /**
85          * Default constructor
86          */
87         public JobsResendLinkWebRequestBean () {
88                 // Call super constructor
89                 super();
90         }
91
92         /**
93          * Resends (new) confirmation link to given email address, if found.
94          * Otherwise an exception is thrown. On success a redirect takes place.
95          * <p>
96          * @return Redirect outcome
97          */
98         public String doResendLink () {
99                 // The email address should not be empty as the JSF validates this
100                 if (this.getEmailAddress() == null) {
101                         // Throw NPE
102                         throw new NullPointerException("this.emailAddress is null"); //NOI18N
103                 }
104
105                 // Init user instance
106                 final User user;
107
108                 try {
109                         // Is the email address really not used?
110                         user = this.userListController.lookupUserByEmailAddress(this.getEmailAddress());
111                 } catch (final UserEmailAddressNotFoundException ex) {
112                         // Always clear bean
113                         this.clear();
114
115                         // Not found, should not happen as the registered validator should find it
116                         this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND", FacesMessage.SEVERITY_ERROR); //NOI18N
117                         return ""; //NOI18N
118                 }
119
120                 // Is the user account already confirmed?
121                 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
122                         // Always clear bean
123                         this.clear();
124
125                         // Then abort here
126                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED", FacesMessage.SEVERITY_WARN); //NOI18N
127                         return ""; //NOI18N
128                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
129                         // Always clear bean
130                         this.clear();
131
132                         // User account is locked
133                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED", FacesMessage.SEVERITY_WARN); //NOI18N
134                         return ""; //NOI18N
135                 } else if (user.getUserConfirmKey() == null) {
136                         // Status is UNCONFIRMED but confirmation key is NULL
137                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
138                 }
139
140                 // Init managed user instance
141                 final User managedUser;
142
143                 try {
144                         // Get base URL
145                         final String baseUrl = FacesUtils.generateBaseUrl();
146
147                         // Call EJB and return redirect target
148                         managedUser = this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
149                 } catch (final UserNotFoundException ex) {
150                         // User not found
151                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND", FacesMessage.SEVERITY_ERROR); //NOI18N
152                         return ""; //NOI18N
153                 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
154                         // Output message, this should not happen as the confirmation key is being removed
155                         this.showFacesMessage("form_resend_link:resendEmailAddress", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
156                         return ""; //NOI18N
157                 }
158
159                 // Clear this bean
160                 this.clear();
161
162                 // Fire event
163                 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
164
165                 // Return redirect target
166                 return "user_resend_done"; //NOI18N
167         }
168
169         /**
170          * Getter for email address 1 (changing)
171          * <p>
172          * @return Email address
173          */
174         public String getEmailAddress () {
175                 return this.emailAddress;
176         }
177
178         /**
179          * Setter for email address 1 (changing)
180          * <p>
181          * @param emailAddress Email address 1
182          */
183         public void setEmailAddress (final String emailAddress) {
184                 this.emailAddress = emailAddress;
185         }
186
187         /**
188          * Clears email address fields so the user has to re-enter them
189          */
190         private void clear () {
191                 // Clear fields
192                 this.setEmailAddress(null);
193         }
194
195 }