]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/user/resendlink/PizzaResendLinkWebSessionBean.java
6d4fe162926f115f92e5e9d09d4554710affc6b3
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / resendlink / PizzaResendLinkWebSessionBean.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.pizzaapplication.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.jusercore.events.resendlink.ObservableUserResendLinkAccountEvent;
31 import org.mxchange.jusercore.events.resendlink.UserResendLinkAccountEvent;
32 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
33 import org.mxchange.jusercore.exceptions.UserNotFoundException;
34 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
35 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
36 import org.mxchange.jusercore.model.user.User;
37 import org.mxchange.jusercore.model.user.resendlink.ResendLinkSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
39 import org.mxchange.pizzaapplication.beans.BasePizzaController;
40 import org.mxchange.pizzaapplication.beans.localization.PizzaLocalizationSessionController;
41 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
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 PizzaResendLinkWebSessionBean extends BasePizzaController implements PizzaResendLinkWebSessionController {
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 PizzaLocalizationSessionController 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 PizzaUserWebSessionController 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 PizzaResendLinkWebSessionBean () {
90                 // Call super constructor
91                 super();
92         }
93
94         @Override
95         public String doResendLink () {
96                 // The email address should not be empty as the JSF validates this
97                 if (this.getEmailAddress() == null) {
98                         // Throw NPE
99                         throw new NullPointerException("this.emailAddress is null"); //NOI18N
100                 }
101
102                 // Init user instance
103                 User user;
104
105                 try {
106                         // Is the email address really not used?
107                         user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
108                 } catch (final UserEmailAddressNotFoundException ex) {
109                         // Always clear bean
110                         this.clear();
111
112                         // Not found, should not happen as the registered validator should find it
113                         this.showFacesMessage("form_resend_link:", "ERROR_USER_EMAIL_ADDRESS_NOT_FOUND"); //NOI18N
114                         return ""; //NOI18N
115                 }
116
117                 // Is the user account already confirmed?
118                 if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
119                         // Always clear bean
120                         this.clear();
121
122                         // Then abort here
123                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_ALREADY_CONFIRMED"); //NOI18N
124                         return ""; //NOI18N
125                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
126                         // Always clear bean
127                         this.clear();
128
129                         // User account is locked
130                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_STATUS_LOCKED"); //NOI18N
131                         return ""; //NOI18N
132                 } else if (user.getUserConfirmKey() == null) {
133                         // Status is UNCONFIRMED but confirmation key is NULL
134                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
135                 }
136
137                 // Init managed user instance
138                 User managedUser;
139
140                 try {
141                         // Get base URL
142                         String baseUrl = FacesUtils.generateBaseUrl();
143
144                         // Call EJB and return redirect target
145                         managedUser = this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl);
146                 } catch (final UserNotFoundException ex) {
147                         // User not found
148                         this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N
149                         return ""; //NOI18N
150                 } catch (final UserStatusLockedException | UserStatusConfirmedException ex) {
151                         // Output message, this should not happen as the confirmation key is being removed
152                         this.showFacesMessage("form_resend_link:resendEmailAddress", ex); //NOI18N
153                         return ""; //NOI18N
154                 }
155
156                 // Clear this bean
157                 this.clear();
158
159                 // Fire event
160                 this.userResendLinkEvent.fire(new UserResendLinkAccountEvent(managedUser));
161
162                 // Return redirect target
163                 return "user_resend_done"; //NOI18N
164         }
165
166         @Override
167         public String getEmailAddress () {
168                 return this.emailAddress;
169         }
170
171         @Override
172         public void setEmailAddress (final String emailAddress) {
173                 this.emailAddress = emailAddress;
174         }
175
176         /**
177          * Post-construction method
178          */
179         @PostConstruct
180         public void init () {
181                 // Try it
182                 try {
183                         // Get initial context
184                         Context context = new InitialContext();
185
186                         // Try to lookup
187                         this.resendLinkBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/userResendConfirmationLink!org.mxchange.jusercore.model.user.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
188                 } catch (final NamingException e) {
189                         // Throw again
190                         throw new FaceletException(e);
191                 }
192         }
193
194         /**
195          * Clears email address fields so the user has to re-enter them
196          */
197         private void clear () {
198                 // Clear fields
199                 this.setEmailAddress(null);
200         }
201
202 }