]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/user/confirmlink/PizzaConfirmationLinkWebRequestBean.java
WIP: Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / user / confirmlink / PizzaConfirmationLinkWebRequestBean.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.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.annotation.PostConstruct;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.inject.Any;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcoreee.events.helper.clear.HelperCleanupEvent;
34 import org.mxchange.jcoreee.events.helper.clear.ObservableHelperCleanupEvent;
35 import org.mxchange.jcoreee.utils.FacesUtils;
36 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.model.user.User;
39 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
41 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
42 import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent;
43 import org.mxchange.pizzaapplication.beans.BasePizzaController;
44 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
45 import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController;
46
47 /**
48  * A web request bean for confirmation link handling
49  * <p>
50  * @author Roland Häder<roland@mxchange.org>
51  */
52 @Named ("userConfirmationLinkController")
53 @RequestScoped
54 public class PizzaConfirmationLinkWebRequestBean extends BasePizzaController implements PizzaConfirmationLinkWebRequestController {
55
56         /**
57          * Serial number
58          */
59         private static final long serialVersionUID = 57_637_182_796_370L;
60
61         /**
62          * Bean helper instance
63          */
64         @Inject
65         private PizzaWebRequestHelperController beanHelper;
66
67         /**
68          * Event being fired when a bean helper should be cleaned
69          */
70         @Inject
71         @Any
72         private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
73
74         /**
75          * Confirmation key
76          */
77         private String confirmationKey;
78
79         /**
80          * Remote user bean
81          */
82         private UserSessionBeanRemote userBean;
83
84         /**
85          * Event being fired when a user has confirmed the account
86          */
87         @Inject
88         @Any
89         private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
90
91         /**
92          * User controller
93          */
94         @Inject
95         private PizzaUserWebSessionController userController;
96
97         /**
98          * Default constructor
99          */
100         public PizzaConfirmationLinkWebRequestBean () {
101                 // Call super constructor
102                 super();
103         }
104
105         @Override
106         public String getConfirmationKey () {
107                 return this.confirmationKey;
108         }
109
110         @Override
111         public void setConfirmationKey (final String confirmationKey) {
112                 this.confirmationKey = confirmationKey;
113         }
114
115         /**
116          * Post-construction method
117          */
118         @PostConstruct
119         public void init () {
120                 // Try it
121                 try {
122                         // Get initial context
123                         Context context = new InitialContext();
124
125                         // Try to lookup
126                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
127                 } catch (final NamingException e) {
128                         // Throw again
129                         throw new FaceletException(e);
130                 }
131         }
132
133         @Override
134         public void maybeConfirmUserAccount () {
135                 // Trace message
136                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: CALLED!", this.getClass().getSimpleName())); //NOI18N
137
138                 // Is the confirmation key set?
139                 if (this.getConfirmationKey() == null) {
140                         // May be null if not set
141                         return;
142                 } else if (this.getConfirmationKey().isEmpty()) {
143                         // Is empty string
144                         return;
145                 }
146
147                 // Now try to find the user in user list, first get the whole list
148                 List<User> users = this.userController.allUsers();
149
150                 // Debug message
151                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: users.size()={1}", this.getClass().getSimpleName(), users.size())); //NOI18N
152
153                 // Get iterator from it
154                 Iterator<User> iterator = users.iterator();
155
156                 // Init instance
157                 User user = null;
158
159                 // Then loop through all
160                 while (iterator.hasNext()) {
161                         // Get next user
162                         User next = iterator.next();
163
164                         // Debug message
165                         System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: this.confirmationKey={1},next.confirmationKey={2}", this.getClass().getSimpleName(), this.getConfirmationKey(), next.getUserConfirmKey())); //NOI18N
166
167                         // Same confirmation key?
168                         if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
169                                 // Debug message
170                                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: next={1} - Aborting ...", this.getClass().getSimpleName(), next)); //NOI18N
171
172                                 // Found it, then set it and abort loop
173                                 user = next;
174                                 break;
175                         }
176                 }
177
178                 // Debug message
179                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: user={1}", this.getClass().getSimpleName(), user)); //NOI18N
180
181                 // Is the user instance null?
182                 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
183                         // Then clear this bean and the helper
184                         this.cleanHelperEvent.fire(new HelperCleanupEvent());
185                 } else {
186                         // Try to confirm it
187                         this.confirmUserAccount(user);
188                 }
189
190                 // Trace message
191                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: EXIT!", this.getClass().getSimpleName())); //NOI18N
192         }
193
194         /**
195          * Tries to confirm the currently set user instance (in helper).
196          * <p>
197          * @param user User instance
198          */
199         private void confirmUserAccount (final User user) {
200                 // Trace message
201                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
202
203                 // Should be set
204                 if (null == user) {
205                         // Throw NPE
206                         throw new NullPointerException("user is null"); //NOI18N
207                 } else if (user.getUserId() == null) {
208                         // Abort here
209                         throw new NullPointerException("user.userId is null"); //NOI18N
210                 } else if (user.getUserId() < 1) {
211                         // Invalid number
212                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
213                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
214                         // Account is already confirmed
215                         throw new FaceletException(new UserStatusConfirmedException(user));
216                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
217                         // Account is already confirmed
218                         throw new FaceletException(new UserStatusLockedException(user));
219                 } else if (user.getUserConfirmKey() == null) {
220                         // Throw NPE
221                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
222                 } else if (user.getUserConfirmKey().isEmpty()) {
223                         // Is empty string
224                         throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
225                 }
226
227                 // Updated user instance
228                 User updatedUser;
229
230                 try {
231                         // Get base URL
232                         String baseUrl = FacesUtils.generateBaseUrl();
233
234                         // Debug message
235                         System.out.println(MessageFormat.format("{0}.confirmUserAccount: baseUrl={1}", this.getClass().getSimpleName(), baseUrl)); //NOI18N
236
237                         // Confirm account
238                         updatedUser = this.userBean.confirmAccount(user, baseUrl);
239
240                         // Debug message
241                         System.out.println(MessageFormat.format("{0}.confirmUserAccount: updatedUser={1} - Returned from EJB", this.getClass().getSimpleName(), updatedUser)); //NOI18N
242                 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
243                         // Something unexpected happened
244                         throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
245                 }
246
247                 // Fire event that the user has confirmed account
248                 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
249
250                 // Debug message
251                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: updatedUser={1}", this.getClass().getSimpleName(), updatedUser)); //NOI18N
252
253                 // Set it again in helper
254                 this.beanHelper.setUser(updatedUser);
255
256                 // ... and copy it to the controller
257                 this.beanHelper.copyUserToController();
258
259                 // Trace message
260                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: EXIT!", this.getClass().getSimpleName())); //NOI18N
261         }
262
263 }