]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/resendlink/JobsResendLinkWebSessionBean.java
this.clear(); needs to be called to remove the email address from form.
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / resendlink / JobsResendLinkWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.resendlink;
18
19 import java.text.MessageFormat;
20 import javax.enterprise.context.SessionScoped;
21 import javax.faces.view.facelets.FaceletException;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.jcoreee.utils.FacesUtils;
28 import org.mxchange.jjobs.beans.BaseJobsController;
29 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
30 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
31 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
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
37 /**
38  * A web session bean for resending confirmation link
39  * <p>
40  * @author Roland Haeder<roland@mxchange.org>
41  */
42 @Named ("resendController")
43 @SessionScoped
44 public class JobsResendLinkWebSessionBean extends BaseJobsController implements JobsResendLinkWebSessionController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 186_078_724_659_153L;
50
51         /**
52          * Email address
53          */
54         private String emailAddress;
55
56         /**
57          * EJB for resending confirmation link
58          */
59         private ResendLinkSessionBeanRemote emailBean;
60
61         /**
62          * Localization controller
63          */
64         @Inject
65         private JobsLocalizationSessionController localizationController;
66
67         /**
68          * Regular user controller
69          */
70         @Inject
71         private JobsUserWebSessionController userController;
72
73         /**
74          * Default constructor
75          */
76         public JobsResendLinkWebSessionBean () {
77                 // Try it
78                 try {
79                         // Get initial context
80                         Context context = new InitialContext();
81
82                         // Try to lookup
83                         this.emailBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/jjobs-ejb/resendLink!org.mxchange.jjobs.beans.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
84                 } catch (final NamingException e) {
85                         // Throw again
86                         throw new FaceletException(e);
87                 }
88         }
89
90         @Override
91         public String doResendLink () {
92                 // The email address should not be empty as the JSF validates this
93                 if (this.getEmailAddress() == null) {
94                         // Throw NPE
95                         throw new NullPointerException("this.emailAddress is null"); //NOI18N
96                 }
97
98                 // Init user instance
99                 User user;
100
101                 try {
102                         // Is the email address really not used?
103                         user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
104                 } catch (final UserEmailAddressNotFoundException ex) {
105                         // Always clear bean
106                         this.clear();
107
108                         // Not found, should not happen as the registered validator should find it
109                         throw new FaceletException(MessageFormat.format("this.emailAddress={0} should be resolveable into User instance.", this.getEmailAddress()), ex); //NOI18N
110                 }
111
112                 // Is the user account already confirmed?
113                 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
114                         // Always clear bean
115                         this.clear();
116
117                         // Then abort here
118                         this.showFacesMessage("form_resend_link:resendEmailAddress", new UserStatusConfirmedException(user)); //NOI18N
119                         return ""; //NOI18N
120                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
121                         // Always clear bean
122                         this.clear();
123
124                         // User account is locked
125                         this.showFacesMessage("form_resend_link:resendEmailAddress", new UserStatusLockedException(user)); //NOI18N
126                         return ""; //NOI18N
127                 } else if (user.getUserConfirmKey() == null) {
128                         // Status is UNCONFIRMED but confirmation key is NULL
129                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
130                 }
131
132                 // Get base URL
133                 String baseUrl = FacesUtils.generateBaseUrl();
134
135                 // Call EJB and return redirect target
136                 this.emailBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
137
138                 // Clear this bean
139                 this.clear();
140
141                 // Return redirect target
142                 return "resend_done"; //NOI18N
143         }
144
145         @Override
146         public String getEmailAddress () {
147                 return this.emailAddress;
148         }
149
150         @Override
151         public void setEmailAddress (final String emailAddress) {
152                 this.emailAddress = emailAddress;
153         }
154
155         /**
156          * Clears email address fields so the user has to re-enter them
157          */
158         private void clear () {
159                 // Clear fields
160                 this.setEmailAddress(null);
161         }
162
163 }