]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/confirmlink/PizzaConfirmationLinkWebRequestBean.java
fixed JNDI name?
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / confirmlink / PizzaConfirmationLinkWebRequestBean.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.confirmlink;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.inject.Any;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jusercore.events.confirmation.ConfirmedUserAccountEvent;
34 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
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.UserSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
40 import org.mxchange.pizzaapplication.beans.BasePizzaController;
41 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
42 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
43
44 /**
45  * A web request bean for confirmation link handling
46  * <p>
47  * @author Roland Haeder<roland@mxchange.org>
48  */
49 @Named ("confirmationLinkController")
50 @RequestScoped
51 public class PizzaConfirmationLinkWebRequestBean extends BasePizzaController implements PizzaConfirmationLinkWebRequestController {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 57_637_182_796_370L;
57
58         /**
59          * Admin helper instance
60          */
61         @Inject
62         private PizzaWebRequestController beanHelper;
63
64         /**
65          * Confirmation key
66          */
67         private String confirmationKey;
68
69         /**
70          * Remote user bean
71          */
72         private final UserSessionBeanRemote userBean;
73
74         /**
75          * User controller
76          */
77         @Inject
78         private PizzaUserWebSessionController userController;
79
80         /**
81          * Event being fired when a user has confirmed the account
82          */
83         @Inject
84         @Any
85         private Event<UserConfirmedAccountEvent> userConfirmedEvent;
86
87         /**
88          * Default constructor
89          */
90         public PizzaConfirmationLinkWebRequestBean () {
91                 // Try it
92                 try {
93                         // Get initial context
94                         Context context = new InitialContext();
95
96                         // Try to lookup
97                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
98                 } catch (final NamingException e) {
99                         // Throw again
100                         throw new FaceletException(e);
101                 }
102         }
103
104         @Override
105         public String getConfirmationKey () {
106                 return this.confirmationKey;
107         }
108
109         @Override
110         public void setConfirmationKey (final String confirmationKey) {
111                 this.confirmationKey = confirmationKey;
112         }
113
114         @Override
115         public void maybeConfirmUserAccount () {
116                 // Is the confirmation key set?
117                 if (this.getConfirmationKey() == null) {
118                         // May be null if not set
119                         return;
120                 } else if (this.getConfirmationKey().isEmpty()) {
121                         // Is empty string
122                         return;
123                 }
124
125                 // Now try to find the user in user list, first get the whole list
126                 List<User> users = this.userController.allUsers();
127
128                 // Get iterator from it
129                 Iterator<User> iterator = users.iterator();
130
131                 // Init instance
132                 User user = null;
133
134                 // Then loop through all
135                 while (iterator.hasNext()) {
136                         // Get next user
137                         User next = iterator.next();
138
139                         // Same confirmation key?
140                         if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
141                                 // Found it, then set it and abort loop
142                                 user = next;
143                                 break;
144                         }
145                 }
146
147                 // Is the user instance null?
148                 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
149                         // Then clear this bean and the helper
150                         this.beanHelper.setUser(null);
151                 } else {
152                         // Set user ...
153                         this.beanHelper.setUser(user);
154
155                         // ... and copy it to the controller
156                         this.beanHelper.copyUserToController();
157
158                         // Try to confirm it
159                         this.confirmUserAccount();
160                 }
161         }
162
163         /**
164          * Tries to confirm the currently set user instance (in helper).
165          */
166         private void confirmUserAccount () {
167                 // Get user instance
168                 User user = this.beanHelper.getUser();
169
170                 // Should be set
171                 if (null == user) {
172                         // Throw NPE
173                         throw new NullPointerException("user is null");
174                 } else if (user.getUserId() == null) {
175                         // Abort here
176                         throw new NullPointerException("user.userId is null"); //NOI18N
177                 } else if (user.getUserId() < 1) {
178                         // Invalid number
179                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
180                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
181                         // Account is already confirmed
182                         throw new FaceletException(new UserStatusConfirmedException(user));
183                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
184                         // Account is already confirmed
185                         throw new FaceletException(new UserStatusLockedException(user));
186                 } else if (user.getUserConfirmKey() == null) {
187                         // Throw NPE
188                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
189                 }
190
191                 // Updated user instance
192                 User updatedUser;
193
194                 try {
195                         // Get base URL
196                         String baseUrl = FacesUtils.generateBaseUrl();
197
198                         // Confirm account
199                         updatedUser = this.userBean.confirmAccount(user, baseUrl);
200                 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
201                         // Something unexpected happened
202                         throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
203                 }
204
205                 // Fire event that the user has confirmed account
206                 this.userConfirmedEvent.fire(new ConfirmedUserAccountEvent(updatedUser));
207
208                 // Set it again in helper
209                 this.beanHelper.setUser(updatedUser);
210         }
211
212 }