]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/resendlink/PizzaResendLinkWebSessionBean.java
e9de53bff1d567dc5a7000cd52a637e87df13774
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / resendlink / PizzaResendLinkWebSessionBean.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.pizzaapplication.beans.resendlink;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import javax.enterprise.context.SessionScoped;
22 import javax.faces.context.FacesContext;
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.exceptions.UserEmailAddressNotFoundException;
31 import org.mxchange.jusercore.model.user.User;
32 import org.mxchange.pizzaapplication.beans.BasePizzaController;
33 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
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 PizzaResendLinkWebSessionBean extends BasePizzaController implements PizzaResendLinkWebSessionController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 186_078_724_659_153L;
48
49         /**
50          * Email address 1 (changing)
51          */
52         private String emailAddress;
53
54         /**
55          * EJB for resending confirmation link
56          */
57         private ResendLinkSessionBeanRemote emailBean;
58
59         /**
60          * User controller
61          */
62         @Inject
63         private PizzaUserWebSessionController userController;
64
65         /**
66          * Default constructor
67          */
68         public PizzaResendLinkWebSessionBean () {
69                 // Try it
70                 try {
71                         // Get initial context
72                         Context context = new InitialContext();
73
74                         // Try to lookup
75                         this.emailBean = (ResendLinkSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/resendLink!org.mxchange.pizzaapplication.beans.resendlink.ResendLinkSessionBeanRemote"); //NOI18N
76                 } catch (final NamingException e) {
77                         // Throw again
78                         throw new FaceletException(e);
79                 }
80         }
81
82         @Override
83         public String doResendLink () {
84                 // The email address should not be empty as the JSF validates this
85                 if (this.getEmailAddress() == null) {
86                         // Throw NPE
87                         throw new NullPointerException("this.emailAddress is null"); //NOI18N
88                 }
89
90                 // Init user instance
91                 User user;
92
93                 try {
94                         // Is the email address really not used?
95                         user = this.userController.lookupUserByEmailAddress(this.getEmailAddress());
96                 } catch (final UserEmailAddressNotFoundException ex) {
97                         // Not found, should not happen as the registeredvalidator should find it
98                         throw new FaceletException(MessageFormat.format("this.emailAddress={0} should be resolveable into User instance.", this.getEmailAddress()), ex); //NOI18N
99                 }
100
101                 // Get locale from faces context
102                 Locale locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
103
104                 // Get base URL
105                 String baseUrl = FacesUtils.generateBaseUrl();
106
107                 // Debug message
108                 System.out.println(MessageFormat.format("doResendLink: DEBUG - locale={0},baseUrl={1}", locale, baseUrl));
109
110                 // Call EJB and return redirect target
111                 String redirectTarget = this.emailBean.resendConfirmationLink(user, locale, baseUrl);
112
113                 // Return redirect target
114                 return redirectTarget;
115         }
116
117         @Override
118         public String getEmailAddress () {
119                 return this.emailAddress;
120         }
121
122         @Override
123         public void setEmailAddress (final String emailAddress) {
124                 this.emailAddress = emailAddress;
125         }
126
127         /**
128          * Clears email address fields so the user has to re-enter them
129          */
130         private void clear () {
131                 // Clear fields
132                 this.setEmailAddress(null);
133         }
134
135 }