]> git.mxchange.org Git - pizzaservice-war.git/blob
9d615b41479654e6e2af7ca8a68067e1d6756229
[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.ejb.EJB;
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 org.mxchange.jcoreee.events.helper.clear.HelperCleanupEvent;
31 import org.mxchange.jcoreee.events.helper.clear.ObservableHelperCleanupEvent;
32 import org.mxchange.jcoreee.utils.FacesUtils;
33 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
34 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
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.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
41 import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent;
42 import org.mxchange.pizzaapplication.beans.BasePizzaController;
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          * Event being fired when a bean helper should be cleaned
61          */
62         @Inject
63         @Any
64         private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
65
66         /**
67          * Confirmation key
68          */
69         private String confirmationKey;
70
71         /**
72          * Remote user bean
73          */
74         @EJB (lookup = "java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
75         private UserSessionBeanRemote userBean;
76
77         /**
78          * Event being fired when a user has confirmed the account
79          */
80         @Inject
81         @Any
82         private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
83
84         /**
85          * User controller
86          */
87         @Inject
88         private PizzaUserWebSessionController userController;
89
90         /**
91          * Event for when a user instance was created
92          */
93         @Any
94         @Inject
95         private Event<ObservableCreatedUserEvent> userCreatedEvent;
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         @Override
116         public void maybeConfirmUserAccount () {
117                 // Trace message
118                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: CALLED!", this.getClass().getSimpleName())); //NOI18N
119
120                 // Is the confirmation key set?
121                 if (this.getConfirmationKey() == null) {
122                         // May be null if not set
123                         return;
124                 } else if (this.getConfirmationKey().isEmpty()) {
125                         // Is empty string
126                         return;
127                 }
128
129                 // Now try to find the user in user list, first get the whole list
130                 List<User> users = this.userController.allUsers();
131
132                 // Debug message
133                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: users.size()={1}", this.getClass().getSimpleName(), users.size())); //NOI18N
134
135                 // Get iterator from it
136                 Iterator<User> iterator = users.iterator();
137
138                 // Init instance
139                 User user = null;
140
141                 // Then loop through all
142                 while (iterator.hasNext()) {
143                         // Get next user
144                         User next = iterator.next();
145
146                         // Debug message
147                         System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: this.confirmationKey={1},next.confirmationKey={2}", this.getClass().getSimpleName(), this.getConfirmationKey(), next.getUserConfirmKey())); //NOI18N
148
149                         // Same confirmation key?
150                         if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
151                                 // Debug message
152                                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: next={1} - Aborting ...", this.getClass().getSimpleName(), next)); //NOI18N
153
154                                 // Found it, then set it and abort loop
155                                 user = next;
156                                 break;
157                         }
158                 }
159
160                 // Debug message
161                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: user={1}", this.getClass().getSimpleName(), user)); //NOI18N
162
163                 // Is the user instance null?
164                 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
165                         // Then clear this bean and the helper
166                         this.cleanHelperEvent.fire(new HelperCleanupEvent());
167                 } else {
168                         // Try to confirm it
169                         this.confirmUserAccount(user);
170                 }
171
172                 // Trace message
173                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: EXIT!", this.getClass().getSimpleName())); //NOI18N
174         }
175
176         /**
177          * Tries to confirm the currently set user instance (in helper).
178          * <p>
179          * @param user User instance
180          */
181         private void confirmUserAccount (final User user) {
182                 // Trace message
183                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
184
185                 // Should be set
186                 if (null == user) {
187                         // Throw NPE
188                         throw new NullPointerException("user is null"); //NOI18N
189                 } else if (user.getUserId() == null) {
190                         // Abort here
191                         throw new NullPointerException("user.userId is null"); //NOI18N
192                 } else if (user.getUserId() < 1) {
193                         // Invalid number
194                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
195                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
196                         // Account is already confirmed
197                         throw new FaceletException(new UserStatusConfirmedException(user));
198                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
199                         // Account is already confirmed
200                         throw new FaceletException(new UserStatusLockedException(user));
201                 } else if (user.getUserConfirmKey() == null) {
202                         // Throw NPE
203                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
204                 } else if (user.getUserConfirmKey().isEmpty()) {
205                         // Is empty string
206                         throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
207                 }
208
209                 // Updated user instance
210                 User updatedUser;
211
212                 try {
213                         // Get base URL
214                         String baseUrl = FacesUtils.generateBaseUrl();
215
216                         // Debug message
217                         System.out.println(MessageFormat.format("{0}.confirmUserAccount: baseUrl={1}", this.getClass().getSimpleName(), baseUrl)); //NOI18N
218
219                         // Confirm account
220                         updatedUser = this.userBean.confirmAccount(user, baseUrl);
221
222                         // Debug message
223                         System.out.println(MessageFormat.format("{0}.confirmUserAccount: updatedUser={1} - Returned from EJB", this.getClass().getSimpleName(), updatedUser)); //NOI18N
224                 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
225                         // Something unexpected happened
226                         throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
227                 }
228
229                 // Fire event that the user has confirmed account
230                 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
231
232                 // Debug message
233                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: updatedUser={1}", this.getClass().getSimpleName(), updatedUser)); //NOI18N
234
235                 // Fire event
236                 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));
237
238                 // Trace message
239                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: EXIT!", this.getClass().getSimpleName())); //NOI18N
240         }
241
242 }