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