]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/resendlink/JobsResendLinkWebSessionBean.java
23658c34c4a2afee31ad5b481cabb40329f0313d
[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 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;
42
43 /**
44  * A web session-scoped bean for resending confirmation link
45  * <p>
46  * @author Roland Häder<roland@mxchange.org>
47  */
48 @Named ("userResendConfirmationController")
49 @SessionScoped
50 public class JobsResendLinkWebSessionBean extends BaseJobsController implements JobsResendLinkWebSessionController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 186_078_724_659_153L;
56
57         /**
58          * Email address
59          */
60         private String emailAddress;
61
62         /**
63          * Localization controller
64          */
65         @Inject
66         private JobsLocalizationSessionController localizationController;
67
68         /**
69          * EJB for resending confirmation link
70          */
71         private ResendLinkSessionBeanRemote resendLinkBean;
72
73         /**
74          * Regular user controller
75          */
76         @Inject
77         private JobsUserWebSessionController userController;
78
79         /**
80          * Event being fired after confirmation link is being sent
81          */
82         @Inject
83         @Any
84         private Event<ObservableUserResendLinkAccountEvent> userResendLinkEvent;
85
86         /**
87          * Default constructor
88          */
89         public JobsResendLinkWebSessionBean () {
90                 // Call super constructor
91                 super();
92         }
93
94         /**
95          * Resends (new) confirmation link to given email address, if found.
96          * Otherwise an exception is thrown. On success a redirect takes place.
97          * <p>
98          * @return Redirect outcome
99          */
100         public String doResendLink () {
101                 // The email address should not be empty as the JSF validates this
102                 if (this.getEmailAddress() == null) {
103                         // Throw NPE
104                         throw new NullPointerException("this.emailAddress is null"); //NOI18N
105                 }
106
107                 // Init user instance
108                 User user;
109
110                 try {
111                         // Is the email address really not used?
112                         user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
113                 } catch (final UserEmailAddressNotFoundException ex) {
114                         // Always clear bean
115                         this.clear();
116
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
119                         return ""; //NOI18N
120                 }
121
122                 // Is the user account already confirmed?
123                 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
124                         // Always clear bean
125                         this.clear();
126
127                         // Then abort here
128                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
129                         return ""; //NOI18N
130                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
131                         // Always clear bean
132                         this.clear();
133
134                         // User account is locked
135                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
136                         return ""; //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
140                 }
141
142                 // Init managed user instance
143                 User managedUser;
144
145                 try {
146                         // Get base URL
147                         String baseUrl = FacesUtils.generateBaseUrl();
148
149                         // Call EJB and return redirect target
150                         managedUser = this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
151                 } catch (final UserNotFoundException ex) {
152                         // User not found
153                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
154                         return ""; //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
158                         return ""; //NOI18N
159                 }
160
161                 // Clear this bean
162                 this.clear();
163
164                 // Fire event
165                 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
166
167                 // Return redirect target
168                 return "user_resend_done"; //NOI18N
169         }
170
171         /**
172          * Getter for email address 1 (changing)
173          * <p>
174          * @return Email address
175          */
176         public String getEmailAddress () {
177                 return this.emailAddress;
178         }
179
180         /**
181          * Setter for email address 1 (changing)
182          * <p>
183          * @param emailAddress Email address 1
184          */
185         public void setEmailAddress (final String emailAddress) {
186                 this.emailAddress = emailAddress;
187         }
188
189         /**
190          * Post-construction method
191          */
192         @PostConstruct
193         public void init () {
194                 // Try it
195                 try {
196                         // Get initial context
197                         Context context = new InitialContext();
198
199                         // Try to lookup
200                         this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/jjobs-ejb/userResendConfirmationLink!org.mxchange.juserlogincore.model.user.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
201                 } catch (final NamingException e) {
202                         // Throw again
203                         throw new FaceletException(e);
204                 }
205         }
206
207         /**
208          * Clears email address fields so the user has to re-enter them
209          */
210         private void clear () {
211                 // Clear fields
212                 this.setEmailAddress(null);
213         }
214
215 }