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