2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.user.confirmlink;
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.addressbook.beans.BaseAddressbookController;
34 import org.mxchange.addressbook.beans.helper.AddressbookWebRequestHelperController;
35 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
36 import org.mxchange.jcoreee.utils.FacesUtils;
37 import org.mxchange.jusercore.events.confirmation.ObservableUserConfirmedAccountEvent;
38 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
39 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
40 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
41 import org.mxchange.jusercore.model.user.User;
42 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
46 * A web request bean for confirmation link handling
48 * @author Roland Häder<roland@mxchange.org>
50 @Named ("confirmationLinkController")
52 public class AddressbookConfirmationLinkWebRequestBean extends BaseAddressbookController implements AddressbookConfirmationLinkWebRequestController {
57 private static final long serialVersionUID = 57_637_182_796_370L;
60 * Bean helper instance
63 private AddressbookWebRequestHelperController beanHelper;
68 private String confirmationKey;
73 private UserSessionBeanRemote userBean;
76 * Event being fired when a user has confirmed the account
80 private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
86 private AddressbookUserWebSessionController userController;
91 public AddressbookConfirmationLinkWebRequestBean () {
92 // Call super constructor
97 public String getConfirmationKey () {
98 return this.confirmationKey;
102 public void setConfirmationKey (final String confirmationKey) {
103 this.confirmationKey = confirmationKey;
107 * Post-construction method
110 public void init () {
113 // Get initial context
114 Context context = new InitialContext();
117 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
118 } catch (final NamingException e) {
120 throw new FaceletException(e);
125 public void maybeConfirmUserAccount () {
126 // Is the confirmation key set?
127 if (this.getConfirmationKey() == null) {
128 // May be null if not set
130 } else if (this.getConfirmationKey().isEmpty()) {
135 // Now try to find the user in user list, first get the whole list
136 List<User> users = this.userController.allUsers();
138 // Get iterator from it
139 Iterator<User> iterator = users.iterator();
144 // Then loop through all
145 while (iterator.hasNext()) {
147 User next = iterator.next();
149 // Same confirmation key?
150 if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
151 // Found it, then set it and abort loop
157 // Is the user instance null?
158 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
159 // Then clear this bean and the helper
160 this.beanHelper.setUser(null);
163 this.beanHelper.setUser(user);
165 // ... and copy it to the controller
166 this.beanHelper.copyUserToController();
169 this.confirmUserAccount();
174 * Tries to confirm the currently set user instance (in helper).
176 private void confirmUserAccount () {
178 User user = this.beanHelper.getUser();
183 throw new NullPointerException("user is null");
184 } else if (user.getUserId() == null) {
186 throw new NullPointerException("user.userId is null"); //NOI18N
187 } else if (user.getUserId() < 1) {
189 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
190 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
191 // Account is already confirmed
192 throw new FaceletException(new UserStatusConfirmedException(user));
193 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
194 // Account is already confirmed
195 throw new FaceletException(new UserStatusLockedException(user));
196 } else if (user.getUserConfirmKey() == null) {
198 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
201 // Updated user instance
206 String baseUrl = FacesUtils.generateBaseUrl();
209 updatedUser = this.userBean.confirmAccount(user, baseUrl);
210 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
211 // Something unexpected happened
212 throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
215 // Fire event that the user has confirmed account
216 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
218 // Set it again in helper
219 this.beanHelper.setUser(updatedUser);