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.pizzaapplication.beans.user.confirmlink;
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Objects;
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.PizzaUserWebRequestController;
46 * A web request bean for confirmation link handling
48 * @author Roland Häder<roland@mxchange.org>
50 @Named ("userConfirmationLinkController")
52 public class PizzaConfirmationLinkWebRequestBean extends BasePizzaController implements PizzaConfirmationLinkWebRequestController {
57 private static final long serialVersionUID = 57_637_182_796_370L;
60 * Event being fired when a bean helper should be cleaned
64 private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
69 private String confirmationKey;
74 @EJB (lookup = "java:global/pizzaservice-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
75 private UserSessionBeanRemote userBean;
78 * Event being fired when a user has confirmed the account
82 private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
88 private PizzaUserWebRequestController userController;
91 * Event for when a user instance was created
95 private Event<ObservableCreatedUserEvent> userCreatedEvent;
100 public PizzaConfirmationLinkWebRequestBean () {
101 // Call super constructor
106 public String getConfirmationKey () {
107 return this.confirmationKey;
111 public void setConfirmationKey (final String confirmationKey) {
112 this.confirmationKey = confirmationKey;
116 public void maybeConfirmUserAccount () {
117 // Is the confirmation key set?
118 if (this.getConfirmationKey() == null) {
119 // May be null if not set
121 } else if (this.getConfirmationKey().isEmpty()) {
126 // Now try to find the user in user list, first get the whole list
127 final List<User> users = this.userController.allUsers();
129 // Get iterator from it
130 final Iterator<User> iterator = users.iterator();
135 // Then loop through all
136 while (iterator.hasNext()) {
138 final User next = iterator.next();
140 // Same confirmation key?
141 if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
142 // Found it, then set it and abort loop
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());
154 this.confirmUserAccount(user);
159 * Tries to confirm the currently set user instance (in helper).
161 * @param user User instance
163 private void confirmUserAccount (final User user) {
167 throw new NullPointerException("user is null"); //NOI18N
168 } else if (user.getUserId() == null) {
170 throw new NullPointerException("user.userId is null"); //NOI18N
171 } else if (user.getUserId() < 1) {
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) {
182 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
183 } else if (user.getUserConfirmKey().isEmpty()) {
185 throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
188 // Updated user instance
189 final User updatedUser;
193 final String baseUrl = FacesUtils.generateBaseUrl();
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
202 // Fire event that the user has confirmed account
203 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
206 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));