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