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