2 * Copyright (C) 2016 Roland Haeder
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;
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.RequestScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.inject.Any;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
36 import org.mxchange.jusercore.events.user.AdminAddedUserEvent;
37 import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
38 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
39 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
40 import org.mxchange.jusercore.exceptions.UserNotFoundException;
41 import org.mxchange.jusercore.exceptions.UserPasswordRepeatMismatchException;
42 import org.mxchange.jusercore.model.user.LoginUser;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
45 import org.mxchange.jusercore.model.user.UserUtils;
46 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
47 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
50 * A user bean (controller)
52 * @author Roland Haeder<roland@mxchange.org>
54 @Named ("adminUserController")
56 public class PizzaAdminUserWebRequestBean implements PizzaAdminUserWebRequestController {
61 private static final long serialVersionUID = 542_145_347_916L;
64 * An event fired when the administrator has added a new user
68 private Event<AdminAddedUserEvent> addedUserEvent;
71 * Regular contact controller
74 private PizzaContactWebSessionController contactController;
79 private final UserSessionBeanRemote userBean;
82 * Regular user controller
85 private PizzaUserWebSessionController userController;
88 * A list of all user profiles
90 private List<User> userList;
95 private String userName;
98 * User password (unencrypted from web form)
100 private String userPassword;
103 * User password repeated (unencrypted from web form)
105 private String userPasswordRepeat;
108 * Default constructor
110 public PizzaAdminUserWebRequestBean () {
113 // Get initial context
114 Context context = new InitialContext();
117 this.userBean = (UserSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
118 } catch (final NamingException e) {
120 throw new FaceletException(e);
125 public void addUser () {
126 // Create new user instance
127 User localUser = new LoginUser();
129 // Set user name, CONFIRMED and INVISIBLE
130 localUser.setUserName(this.getUserName());
131 localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
132 localUser.setUserProfileMode(ProfileMode.INVISIBLE);
134 // Create contact instance
135 Contact contact = this.contactController.createContactInstance();
137 // Set contact in user
138 localUser.setUserContact(contact);
140 // Init variable for password
141 String password = null;
143 // Is the user name or email address used already?
144 // @TODO Add password length check
145 if (this.userController.isUserNameRegistered(localUser)) {
146 // User name is already used
147 throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
148 } else if (this.contactController.isEmailAddressRegistered(localUser.getUserContact())) {
149 // Email address is already used
150 throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
151 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
152 // Empty password entered, then generate one
153 password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
154 } else if (!this.isSamePasswordEntered()) {
155 // Both passwords don't match
156 throw new FaceletException(new UserPasswordRepeatMismatchException(localUser));
158 // Both match, so get it from this bean
159 password = this.getUserPassword();
162 // The password should not be null and at least 5 characters long
163 assert (password != null) : "password is null"; //NOI18N
164 assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
166 // Encrypt password and set it
167 localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
169 // Init updated user instance
170 User updatedUser = null;
173 // Now, that all is set, call EJB
174 updatedUser = this.userBean.addUser(localUser);
175 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
177 throw new FaceletException(ex);
181 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
183 // Add user to local list
184 this.userList.add(updatedUser);
189 // Clear contact instance
190 this.contactController.clear();
194 public List<User> allUsers () {
196 return Collections.unmodifiableList(this.userList);
200 public void editUserData () {
201 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
205 public String getUserName () {
206 return this.userName;
210 public void setUserName (final String userName) {
211 this.userName = userName;
215 public String getUserPassword () {
216 return this.userPassword;
220 public void setUserPassword (final String userPassword) {
221 this.userPassword = userPassword;
225 public String getUserPasswordRepeat () {
226 return this.userPasswordRepeat;
230 public void setUserPasswordRepeat (final String userPasswordRepeat) {
231 this.userPasswordRepeat = userPasswordRepeat;
235 public boolean hasUsers () {
236 return (!this.allUsers().isEmpty());
240 * Post-initialization of this class
243 public void init () {
244 // Initialize user list
245 this.userList = this.userBean.allUsers();
249 public User lookupUserById (final Long userId) throws UserNotFoundException {
250 // Parameter must be valid
251 if (null == userId) {
253 throw new NullPointerException("userId is null"); //NOI18N
254 } else if (userId < 1) {
256 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
262 // Try to lookup it in visible user list
263 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
265 User next = iterator.next();
267 // Is the user id found?
268 if (Objects.equals(next.getUserId(), userId)) {
269 // Copy to other variable
277 // Not visible for the current user
278 throw new UserNotFoundException(userId);
288 private void clear () {
290 this.setUserName(null);
291 this.setUserPassword(null);
292 this.setUserPasswordRepeat(null);
296 * Checks if same password is entered and that they are not empty.
298 * @return Whether the same password was entered
300 private boolean isSamePasswordEntered () {
301 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));