]> git.mxchange.org Git - jfinancials-war.git/blob
4ed0d081d013eead2b0cee23b383949805748a62
[jfinancials-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.jfinancials.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.jfinancials.beans.BaseFinancialsController;
34 import org.mxchange.jfinancials.beans.user.FinancialsUserWebRequestController;
35 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
36 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
37 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
38 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
39 import org.mxchange.jusercore.model.user.User;
40 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
43 import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent;
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 FinancialsConfirmationLinkWebRequestBean extends BaseFinancialsController implements FinancialsConfirmationLinkWebRequestController {
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/jfinancials-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 FinancialsUserWebRequestController 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 FinancialsConfirmationLinkWebRequestBean () {
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                 // Is the confirmation key set?
118                 if (this.getConfirmationKey() == null) {
119                         // May be null if not set
120                         return;
121                 } else if (this.getConfirmationKey().isEmpty()) {
122                         // Is empty string
123                         return;
124                 }
125
126                 // Now try to find the user in user list, first get the whole list
127                 final List<User> users = this.userController.allUsers();
128
129                 // Get iterator from it
130                 final Iterator<User> iterator = users.iterator();
131
132                 // Init instance
133                 User user = null;
134
135                 // Then loop through all
136                 while (iterator.hasNext()) {
137                         // Get next user
138                         final User next = iterator.next();
139
140                         // Same confirmation key?
141                         if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
142                                 // Found it, then set it and abort loop
143                                 user = next;
144                                 break;
145                         }
146                 }
147
148                 // Is the user instance null?
149                 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
150                         // Then clear this bean and the helper
151                         this.cleanHelperEvent.fire(new HelperCleanupEvent());
152                 } else {
153                         // Try to confirm it
154                         this.confirmUserAccount(user);
155                 }
156         }
157
158         /**
159          * Tries to confirm the currently set user instance (in helper).
160          * <p>
161          * @param user User instance
162          */
163         private void confirmUserAccount (final User user) {
164                 // Should be set
165                 if (null == user) {
166                         // Throw NPE
167                         throw new NullPointerException("user is null"); //NOI18N
168                 } else if (user.getUserId() == null) {
169                         // Abort here
170                         throw new NullPointerException("user.userId is null"); //NOI18N
171                 } else if (user.getUserId() < 1) {
172                         // Invalid number
173                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
174                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
175                         // Account is already confirmed
176                         throw new FaceletException(new UserStatusConfirmedException(user));
177                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
178                         // Account is already confirmed
179                         throw new FaceletException(new UserStatusLockedException(user));
180                 } else if (user.getUserConfirmKey() == null) {
181                         // Throw NPE
182                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
183                 } else if (user.getUserConfirmKey().isEmpty()) {
184                         // Is empty string
185                         throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
186                 }
187
188                 // Updated user instance
189                 final User updatedUser;
190
191                 try {
192                         // Get base URL
193                         final String baseUrl = FacesUtils.generateBaseUrl();
194
195                         // Confirm account
196                         updatedUser = this.userBean.confirmAccount(user, baseUrl);
197                 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
198                         // Something unexpected happened
199                         throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
200                 }
201
202                 // Fire event that the user has confirmed account
203                 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
204
205                 // Fire event
206                 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));
207         }
208
209 }