]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/JobsAdminUserWebRequestBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / JobsAdminUserWebRequestBean.java
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.jjobs.beans.user;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.event.Observes;
26 import javax.enterprise.inject.Any;
27 import javax.faces.FacesException;
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.jcoreee.events.locale.ObservableLocaleChangeEvent;
36 import org.mxchange.jcoreee.utils.FacesUtils;
37 import org.mxchange.jjobs.beans.BaseJobsController;
38 import org.mxchange.jjobs.beans.contact.JobsAdminContactWebRequestController;
39 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
40 import org.mxchange.jjobs.beans.helper.JobsWebRequestHelperController;
41 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
42 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
43 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
44 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
45 import org.mxchange.jusercore.events.user.delete.AdminDeletedUserEvent;
46 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
47 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
48 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
49 import org.mxchange.jusercore.events.user.locked.AdminLockedUserEvent;
50 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
51 import org.mxchange.jusercore.events.user.unlocked.AdminUnlockedUserEvent;
52 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
53 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
54 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
55 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
56 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
57 import org.mxchange.jusercore.exceptions.UserNotFoundException;
58 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
59 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
60 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
61 import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote;
62 import org.mxchange.jusercore.model.user.LoginUser;
63 import org.mxchange.jusercore.model.user.User;
64 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
65 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
66 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
67 import org.mxchange.juserlogincore.container.login.UserLoginContainer;
68 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
69 import org.mxchange.juserlogincore.exceptions.UserPasswordRepeatMismatchException;
70 import org.mxchange.juserlogincore.login.UserLoginUtils;
71
72 /**
73  * A user controller (bean)
74  * <p>
75  * @author Roland Häder<roland@mxchange.org>
76  */
77 @Named ("adminUserController")
78 @RequestScoped
79 public class JobsAdminUserWebRequestBean extends BaseJobsController implements JobsAdminUserWebRequestController {
80
81         /**
82          * Serial number
83          */
84         private static final long serialVersionUID = 542_145_347_916L;
85
86         /**
87          * An event fired when the administrator has added a new user
88          */
89         @Inject
90         @Any
91         private Event<ObservableAdminAddedUserEvent> addedUserEvent;
92
93         /**
94          * Regular contact controller
95          */
96         @Inject
97         private JobsAdminContactWebRequestController adminContactController;
98
99         /**
100          * Administrative user EJB
101          */
102         private AdminUserSessionBeanRemote adminUserBean;
103
104         /**
105          * Bean helper
106          */
107         @Inject
108         private JobsWebRequestHelperController beanHelper;
109
110         /**
111          * Contact instance
112          */
113         private Contact contact;
114
115         /**
116          * Regular contact controller
117          */
118         @Inject
119         private JobsContactWebSessionController contactController;
120
121         /**
122          * Event being fired when admin has deleted user
123          */
124         @Inject
125         @Any
126         private Event<ObservableAdminDeletedUserEvent> deleteUserEvent;
127
128         /**
129          * Locale instance
130          */
131         private Locale locale;
132
133         /**
134          * Localization controller
135          */
136         @Inject
137         private JobsLocalizationSessionController localizationController;
138
139         /**
140          * An event fired when the administrator has updated a new user
141          */
142         @Inject
143         @Any
144         private Event<ObservableAdminUpdatedUserDataEvent> updatedUserDataEvent;
145
146         /**
147          * User instance
148          */
149         private User user;
150
151         /**
152          * General user EJB
153          */
154         private UserSessionBeanRemote userBean;
155
156         /**
157          * Regular user controller
158          */
159         @Inject
160         private JobsUserWebSessionController userController;
161
162         /**
163          * Delete reason
164          */
165         private String userDeleteReason;
166
167         /**
168          * An event fired when the administrator has linked a user with existing
169          * contact data.
170          */
171         @Inject
172         @Any
173         private Event<ObservableAdminLinkedUserEvent> userLinkedEvent;
174
175         /**
176          * User lock reason
177          */
178         private String userLockReason;
179
180         /**
181          * Event being fired when an administrator has locked a user
182          */
183         @Inject
184         @Any
185         private Event<ObservableAdminLockedUserEvent> userLockedEvent;
186
187         /**
188          * Flag whether user must change password after login
189          */
190         private Boolean userMustChangePassword;
191
192         /**
193          * User name
194          */
195         private String userName;
196
197         /**
198          * User password (clear-text from web form)
199          */
200         private String userPassword;
201
202         /**
203          * User password repeated (clear-text from web form)
204          */
205         private String userPasswordRepeat;
206
207         /**
208          * Event being fired when administrator unlocks an account
209          */
210         @Inject
211         @Any
212         private Event<ObservableAdminUnlockedUserEvent> userUnlockedEvent;
213
214         /**
215          * Default constructor
216          */
217         public JobsAdminUserWebRequestBean () {
218                 // Call super constructor
219                 super();
220         }
221
222         /**
223          * Adds user instance to database by preparing a complete user instance and
224          * sending it to the EJB. The data set in the controller is being verified,
225          * e.g. if the user name or email address is not used yet.
226          * <p>
227          * @return Redirect outcome
228          */
229         public String addUser () {
230                 // As the form cannot validate the data (required="true"), check it here
231                 if (this.getUserName() == null) {
232                         // Throw NPE
233                         throw new NullPointerException("userName is null"); //NOI18N
234                 } else if (this.getUserName().isEmpty()) {
235                         // Is empty
236                         throw new IllegalArgumentException("userName is null"); //NOI18N
237                 } else if (this.getContact() == null) {
238                         // No contact instance set, so test required fields: gender, first name and family name
239                         this.adminContactController.validateContactData();
240                 }
241
242                 // Create new user instance
243                 User newUser = new LoginUser();
244
245                 // Set user name, CONFIRMED and INVISIBLE
246                 newUser.setUserName(this.getUserName());
247                 newUser.setUserMustChangePassword(this.getUserMustChangePassword());
248                 newUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
249                 newUser.setUserProfileMode(ProfileMode.INVISIBLE);
250
251                 // Copy user locale
252                 newUser.setUserLocale(this.getLocale());
253
254                 // Init instance
255                 Contact userContact;
256
257                 // Is a contact instance in helper set?
258                 if (this.getContact() instanceof Contact) {
259                         // Then use it for contact linking
260                         userContact = this.getContact();
261                 } else {
262                         // Create contact instance
263                         userContact = this.contactController.createContactInstance();
264                 }
265
266                 // Set contact in user
267                 newUser.setUserContact(userContact);
268
269                 // Init variable for password
270                 String password = null;
271
272                 // Is the user name or email address used already?
273                 // @TODO Add password length check
274                 if (this.userController.isUserNameRegistered(newUser)) {
275                         // User name is already used
276                         throw new FaceletException(new UserNameAlreadyRegisteredException(newUser));
277                 } else if ((this.getContact() == null) && (this.contactController.isEmailAddressRegistered(newUser.getUserContact()))) {
278                         // Email address is already used
279                         this.showFacesMessage("admin_add_user:emailAddress", "ERROR_EMAIL_ADDRESS_ALREADY_USED"); //NOI18N
280
281                         // Always clear password
282                         this.setUserPassword(null);
283                         this.setUserPasswordRepeat(null);
284
285                         // Skip it
286                         return ""; //NOI18N
287                 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
288                         // Empty password entered, then generate one
289                         password = UserLoginUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
290                 } else if (!this.isSamePasswordEntered()) {
291                         // Both passwords don't match
292                         throw new FaceletException(new UserPasswordRepeatMismatchException(newUser));
293                 } else {
294                         // Both match, so get it from this bean
295                         password = this.getUserPassword();
296                 }
297
298                 // The password should not be null and at least 5 characters long
299                 assert (password != null) : "password is null"; //NOI18N
300                 assert (password.length() >= JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
301
302                 // Encrypt password and set it
303                 newUser.setUserEncryptedPassword(UserLoginUtils.encryptPassword(password));
304
305                 try {
306                         // Now, that all is set, call EJB
307                         if (this.getContact() instanceof Contact) {
308                                 // Link contact with this user
309                                 User updatedUser = this.adminUserBean.linkUser(newUser);
310
311                                 // Fire event
312                                 this.userLinkedEvent.fire(new AdminLinkedUserEvent(updatedUser));
313                         } else {
314                                 // Add new contact
315                                 User updatedUser = this.adminUserBean.addUser(newUser);
316
317                                 // Fire event
318                                 this.addedUserEvent.fire(new AdminAddedUserEvent(updatedUser));
319                         }
320                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
321                         // Throw again
322                         throw new FaceletException(ex);
323                 }
324
325                 // Clear helper
326                 this.setContact(null);
327
328                 // Clear this bean
329                 this.clear();
330
331                 // Return to user list (for now)
332                 return "admin_list_user"; //NOI18N
333         }
334
335         /**
336          * Event observer for when a bean helper has successfully created a user
337          * instance, means the user exists. If the user does not exist, this event
338          * should not fire but instead a proper exception must be thrown.
339          * <p>
340          * @param event User created event
341          */
342         public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
343                 // Is the instance valid?
344                 if (null == event) {
345                         // Throw NPE
346                         throw new NullPointerException("event is null"); //NOI18N
347                 } else if (event.getCreatedUser() == null) {
348                         // Throw NPE again
349                         throw new NullPointerException("event.createdUser is null"); //NOI18N
350                 } else if (event.getCreatedUser().getUserId() == null) {
351                         // Throw NPE again
352                         throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
353                 } else if (event.getCreatedUser().getUserId() < 1) {
354                         // Throw NPE again
355                         throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
356                 }
357
358                 // Set whole user
359                 this.setUser(event.getCreatedUser());
360         }
361
362         /**
363          * Observer method for events being fired when the application's locale has
364          * been changed.
365          * <p>
366          * @param event Event being fired
367          */
368         public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) {
369                 // Is the parameter valid?
370                 if (null == event) {
371                         // Throw NPE
372                         throw new NullPointerException("event is null");
373                 } else if (event.getLocale() == null) {
374                         // Throw NPE again
375                         throw new NullPointerException("event.locale is null");
376                 }
377
378                 // Set it here
379                 this.setLocale(event.getLocale());
380         }
381
382         /**
383          * Event observer for new user registrations
384          * <p>
385          * @param event User registration event
386          */
387         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
388                 // event should not be null
389                 if (null == event) {
390                         // Throw NPE
391                         throw new NullPointerException("event is null"); //NOI18N
392                 } else if (event.getRegisteredUser() == null) {
393                         // Throw NPE again
394                         throw new NullPointerException("event.user is null"); //NOI18N
395                 } else if (event.getRegisteredUser().getUserId() == null) {
396                         // userId is null
397                         throw new NullPointerException("event.user.userId is null"); //NOI18N
398                 } else if (event.getRegisteredUser().getUserId() < 1) {
399                         // Not avalid id
400                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
401                 }
402
403                 // Get user instance
404                 User registeredUser = event.getRegisteredUser();
405
406                 // @TODO Nothing to do with the user here?
407                 // Clear all data
408                 this.clear();
409         }
410
411         /**
412          * Deletes given user account
413          * <p>
414          * @return Redirect outcome
415          */
416         public String deleteUserData () {
417                 // Is the user instance valid and CONFIRMED?
418                 if (this.getUser() == null) {
419                         // Throw NPE
420                         throw new NullPointerException("user is null"); //NOI18N
421                 } else if (this.getUser().getUserId() == null) {
422                         // Throw again
423                         throw new NullPointerException("user.userId is null"); //NOI18N
424                 } else if (this.getUser().getUserId() < 1) {
425                         // Invalid id number
426                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
427                 }
428
429                 try {
430                         // All fine, delete it
431                         this.adminUserBean.deleteUser(this.getUser(), this.getUserDeleteReason());
432                 } catch (final UserNotFoundException ex) {
433                         // Should not happen, so throw again
434                         throw new FaceletException(ex);
435                 }
436
437                 // Fire event
438                 this.deleteUserEvent.fire(new AdminDeletedUserEvent(this.getUser(), this.getUserDeleteReason()));
439
440                 // Redirect
441                 return "admin_list_user"; //NOI18N
442         }
443
444         /**
445          * Edits currently loaded user's data in database.
446          * <p>
447          * @return Redirect outcome
448          */
449         public String editUserData () {
450                 // Null password means not setting it
451                 String encryptedPassword = null;
452
453                 // Check if user instance is in helper and valid
454                 if (this.getUser() == null) {
455                         // Throw NPE
456                         throw new NullPointerException("beanHelper.user is null"); //NOI18N
457                 } else if (this.getUser().getUserId() == null) {
458                         // Throw NPE again
459                         throw new NullPointerException("beanHelper.user.userId is null"); //NOI18N
460                 } else if (this.getUser().getUserId() < 1) {
461                         // Invalid id
462                         throw new IllegalStateException(MessageFormat.format("beanHelper.user.userId={0} is invalid", this.getUser().getUserId())); //NOI18N
463                 } else if (this.getUserName() == null) {
464                         // Not all required fields are set
465                         throw new NullPointerException("this.userName is null"); //NOI18N
466                 } else if (this.getUserName().isEmpty()) {
467                         // Not all required fields are set
468                         throw new IllegalArgumentException("this.userName is empty"); //NOI18N
469                 } else if (((!this.getUserPassword().isEmpty()) || (!this.getUserPasswordRepeat().isEmpty())) && (!this.isSamePasswordEntered())) {
470                         // Clear password fields
471                         this.setUserPassword(null);
472                         this.setUserPasswordRepeat(null);
473
474                         // Not same password entered
475                         this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_PASSWORD_REPEAT_DIFFERENT"); //NOI18N
476                         return ""; //NOI18N
477                 } else if ((!Objects.equals(this.getUser().getUserName(), this.getUserName())) && (this.userBean.ifUserNameExists(this.getUserName()))) {
478                         // Clear all fields
479                         this.clear();
480
481                         // User name already exists
482                         this.showFacesMessage("form_edit_user:userName", "ADMIN_USER_NAME_ALREADY_EXISTS"); //NOI18N
483                         return ""; //NOI18N
484                 } else if (this.isSamePasswordEntered()) {
485                         // Same password entered, create container
486                         if ((Objects.equals(this.getUser().getUserMustChangePassword(), this.getUserMustChangePassword())) && (UserLoginUtils.ifPasswordMatches(new UserLoginContainer(this.getUser(), this.getUserPassword())))) {
487                                 // Clear password fields
488                                 this.setUserPassword(null);
489                                 this.setUserPasswordRepeat(null);
490
491                                 // Same password entered
492                                 this.showFacesMessage("form_edit_user:userPassword", "ADMIN_USER_ENTERED_SAME_AS_OLD_PASSWORD"); //NOI18N
493                                 return ""; //NOI18N
494                         }
495
496                         // Encrypt password
497                         encryptedPassword = UserLoginUtils.encryptPassword(this.getUserPassword());
498                 }
499
500                 // Set user name and flag
501                 this.getUser().setUserName(this.getUserName());
502                 this.getUser().setUserMustChangePassword(this.getUserMustChangePassword());
503
504                 // Is a password set?
505                 if (encryptedPassword != null) {
506                         // Set it as well
507                         this.getUser().setUserEncryptedPassword(encryptedPassword);
508                 }
509
510                 // Call EJB for updating user data
511                 User updatedUser = this.userBean.updateUserData(this.getUser());
512
513                 // Fire event
514                 this.updatedUserDataEvent.fire(new AdminUpdatedUserDataEvent(updatedUser));
515
516                 // Return to user list (for now)
517                 return "admin_list_user"; //NOI18N
518         }
519
520         /**
521          * Getter for contact instance
522          * <p>
523          * @return Contact instance
524          */
525         public Contact getContact () {
526                 return this.contact;
527         }
528
529         /**
530          * Setter for contact instance
531          * <p>
532          * @param contact Contact instance
533          */
534         public void setContact (final Contact contact) {
535                 this.contact = contact;
536         }
537
538         /**
539          * Getter for user instance
540          * <p>
541          * @return User instance
542          */
543         public User getUser () {
544                 return this.user;
545         }
546
547         /**
548          * Setter for user instance
549          * <p>
550          * @param user User instance
551          */
552         public void setUser (final User user) {
553                 this.user = user;
554         }
555
556         /**
557          * Getter for user delete reason
558          * <p>
559          * @return User delete reason
560          */
561         public String getUserDeleteReason () {
562                 return this.userDeleteReason;
563         }
564
565         /**
566          * Setter for user delete reason
567          * <p>
568          * @param userDeleteReason User delete reason
569          */
570         public void setUserDeleteReason (final String userDeleteReason) {
571                 this.userDeleteReason = userDeleteReason;
572         }
573
574         /**
575          * Getter for user lock reason
576          * <p>
577          * @return User lock reason
578          */
579         public String getUserLockReason () {
580                 return this.userLockReason;
581         }
582
583         /**
584          * Setter for user lock reason
585          * <p>
586          * @param userLockReason User lock reason
587          */
588         public void setUserLockReason (final String userLockReason) {
589                 this.userLockReason = userLockReason;
590         }
591
592         /**
593          * Getter for flag if user needs to change password
594          * <p>
595          * @return Flag if user needs to change password
596          */
597         public Boolean getUserMustChangePassword () {
598                 return this.userMustChangePassword;
599         }
600
601         /**
602          * Setter for flag if user needs to change password
603          * <p>
604          * @param userMustChangePassword Flag if user needs to change password
605          */
606         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
607                 this.userMustChangePassword = userMustChangePassword;
608         }
609
610         /**
611          * Getter for user name
612          * <p>
613          * @return User name
614          */
615         public String getUserName () {
616                 return this.userName;
617         }
618
619         /**
620          * Setter for user name
621          * <p>
622          * @param userName User name
623          */
624         public void setUserName (final String userName) {
625                 this.userName = userName;
626         }
627
628         /**
629          * Getter for clear-text user password
630          * <p>
631          * @return Clear-text user password
632          */
633         public String getUserPassword () {
634                 return this.userPassword;
635         }
636
637         /**
638          * Setter for clear-text user password
639          * <p>
640          * @param userPassword Clear-text user password
641          */
642         public void setUserPassword (final String userPassword) {
643                 this.userPassword = userPassword;
644         }
645
646         /**
647          * Getter for clear-text user password repeated
648          * <p>
649          * @return Clear-text user password repeated
650          */
651         public String getUserPasswordRepeat () {
652                 return this.userPasswordRepeat;
653         }
654
655         /**
656          * Setter for clear-text user password repeated
657          * <p>
658          * @param userPasswordRepeat Clear-text user password repeated
659          */
660         public void setUserPasswordRepeat (final String userPasswordRepeat) {
661                 this.userPasswordRepeat = userPasswordRepeat;
662         }
663
664         /**
665          * Post-construction method
666          */
667         @PostConstruct
668         public void init () {
669                 // Try it
670                 try {
671                         // Get initial context
672                         Context context = new InitialContext();
673
674                         // Try to lookup
675                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
676                         this.adminUserBean = (AdminUserSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/adminUser!org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote"); //NOI18N
677                 } catch (final NamingException e) {
678                         // Throw again
679                         throw new FaceletException(e);
680                 }
681         }
682
683         /**
684          * Locks selected user's account. This method makes sure that a lock reason
685          * is provided that th user later can read on login attempts.
686          * <p>
687          * @return Redirect outcome
688          */
689         public String lockUserAccount () {
690                 // Is the user instance valid and CONFIRMED?
691                 if (this.getUser() == null) {
692                         // Throw NPE
693                         throw new NullPointerException("this.user is null"); //NOI18N
694                 } else if (this.getUser().getUserId() == null) {
695                         // Throw again
696                         throw new NullPointerException("this.user.userId is null"); //NOI18N
697                 } else if (this.getUser().getUserId() < 1) {
698                         // Invalid id number
699                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
700                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.LOCKED) {
701                         // User account is locked
702                         throw new FacesException(new UserStatusLockedException(this.getUser()));
703                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
704                         // User account is locked
705                         throw new FaceletException(new UserStatusUnconfirmedException(this.getUser()));
706                 } else if (this.getUserLockReason() == null) {
707                         // Throw NPE again
708                         throw new NullPointerException("this.userLockReason is null"); //NOI18N
709                 } else if (this.getUserLockReason().isEmpty()) {
710                         // Empty lock reason
711                         throw new IllegalArgumentException("this.userLockReason is empty"); //NOI18N
712                 }
713
714                 // Init updated user instance
715                 User updatedUser;
716
717                 try {
718                         // Get base URL
719                         String baseUrl = FacesUtils.generateBaseUrl();
720
721                         // Call EJB to lock account
722                         updatedUser = this.adminUserBean.lockUserAccount(this.getUser(), this.getUserLockReason(), baseUrl);
723                 } catch (final UserStatusLockedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
724                         // Throw again
725                         throw new FaceletException(ex);
726                 }
727
728                 // Fire event
729                 this.userLockedEvent.fire(new AdminLockedUserEvent(updatedUser));
730
731                 // Clear bean
732                 this.clear();
733
734                 // Should go fine at this point, redirect to user profile
735                 return "admin_show_user"; //NOI18N
736         }
737
738         /**
739          * Unlocks selected user's account. This method makes sure that the account
740          * is locked.
741          * <p>
742          * @return Redirect outcome
743          */
744         public String unlockUserAccount () {
745                 // Is the user instance valid and CONFIRMED?
746                 if (this.getUser() == null) {
747                         // Throw NPE
748                         throw new NullPointerException("this.user is null"); //NOI18N
749                 } else if (this.getUser().getUserId() == null) {
750                         // Throw again
751                         throw new NullPointerException("this.user.userId is null"); //NOI18N
752                 } else if (this.getUser().getUserId() < 1) {
753                         // Invalid id number
754                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
755                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
756                         // User account is locked
757                         throw new FacesException(new UserStatusConfirmedException(this.getUser()));
758                 } else if (this.getUser().getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
759                         // User account is locked
760                         throw new FaceletException(new UserStatusUnconfirmedException(this.getUser()));
761                 }
762
763                 // Init updated user instance
764                 User updatedUser;
765
766                 try {
767                         // Get base URL
768                         String baseUrl = FacesUtils.generateBaseUrl();
769
770                         // Call EJB to unlock account
771                         updatedUser = this.adminUserBean.unlockUserAccount(this.getUser(), baseUrl);
772                 } catch (final UserStatusConfirmedException | UserStatusUnconfirmedException | UserNotFoundException ex) {
773                         // Throw again
774                         throw new FaceletException(ex);
775                 }
776
777                 // Fire event
778                 this.userUnlockedEvent.fire(new AdminUnlockedUserEvent(updatedUser));
779
780                 // Clear bean
781                 this.clear();
782
783                 // Should go fine at this point, redirect to user profile
784                 return "admin_show_user"; //NOI18N
785         }
786
787         /**
788          * Clears this bean
789          */
790         private void clear () {
791                 // Clear all data
792                 this.setContact(null);
793                 this.setUserLockReason(null);
794                 this.setUserMustChangePassword(null);
795                 this.setUserName(null);
796         }
797
798         /**
799          * Getter for locale instance
800          * <p>
801          * @return Locale instance
802          */
803         private Locale getLocale () {
804                 return this.locale;
805         }
806
807         /**
808          * Setter for locale instance
809          * <p>
810          * @param locale Locale instance
811          */
812         private void setLocale (final Locale locale) {
813                 this.locale = locale;
814         }
815
816         /**
817          * Checks if same password is entered and that they are not empty.
818          * <p>
819          * @return Whether the same password was entered
820          */
821         private boolean isSamePasswordEntered () {
822                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
823         }
824
825 }