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