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