]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java
addfdd7e4fd0217343da834f94a8c6a7948cff56
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / JobsUserWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.event.Observes;
27 import javax.enterprise.inject.Any;
28 import javax.faces.context.FacesContext;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jjobs.beans.BaseJobsController;
37 import org.mxchange.jjobs.beans.contact.JobsContactWebSessionController;
38 import org.mxchange.jjobs.beans.features.JobsFeaturesWebApplicationController;
39 import org.mxchange.jjobs.beans.localization.JobsLocalizationSessionController;
40 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
41 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
42 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
43 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
44 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
45 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
46 import org.mxchange.jusercore.events.user.locked.AdminLockedUserEvent;
47 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
48 import org.mxchange.jusercore.events.user.unlocked.AdminUnlockedUserEvent;
49 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
50 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
51 import org.mxchange.jusercore.events.user.update.UserUpdatedPersonalDataEvent;
52 import org.mxchange.jusercore.exceptions.UserEmailAddressNotFoundException;
53 import org.mxchange.jusercore.exceptions.UserNotFoundException;
54 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
55 import org.mxchange.jusercore.model.user.LoginUser;
56 import org.mxchange.jusercore.model.user.User;
57 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
58 import org.mxchange.jusercore.model.user.UserUtils;
59 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
60
61 /**
62  * A user controller (bean)
63  * <p>
64  * @author Roland Haeder<roland@mxchange.org>
65  */
66 @Named ("userController")
67 @SessionScoped
68 public class JobsUserWebSessionBean extends BaseJobsController implements JobsUserWebSessionController {
69
70         /**
71          * Serial number
72          */
73         private static final long serialVersionUID = 542_145_347_916L;
74
75         /**
76          * General contact controller
77          */
78         @Inject
79         private JobsContactWebSessionController contactController;
80
81         /**
82          * Features controller
83          */
84         @Inject
85         private JobsFeaturesWebApplicationController featureController;
86
87         /**
88          * Localization controller
89          */
90         @Inject
91         private JobsLocalizationSessionController localizationController;
92
93         /**
94          * Event being fired when user updated personal data
95          */
96         @Inject
97         @Any
98         private Event<UpdatedUserPersonalDataEvent> updatedPersonalDataEvent;
99
100         /**
101          * Remote user bean
102          */
103         private final UserSessionBeanRemote userBean;
104
105         /**
106          * User id
107          */
108         private Long userId;
109
110         /**
111          * A list of all user profiles
112          */
113         private List<User> userList;
114
115         /**
116          * Login controller (bean)
117          */
118         @Inject
119         private JobsUserLoginWebSessionController userLoginController;
120
121         /**
122          * User name
123          */
124         private String userName;
125
126         /**
127          * User name list
128          */
129         private List<String> userNameList;
130
131         /**
132          * User password (unencrypted from web form)
133          */
134         private String userPassword;
135
136         /**
137          * User password repeated (unencrypted from web form)
138          */
139         private String userPasswordRepeat;
140
141         /**
142          * Whether the user wants a public profile
143          */
144         private ProfileMode userProfileMode;
145
146         /**
147          * A list of all public user profiles
148          * <p>
149          * @TODO Hmm, makes that sense? Having visible user list in current
150          * (session-scoped) user's visible user list?
151          */
152         private List<User> visibleUserList;
153
154         /**
155          * Default constructor
156          */
157         public JobsUserWebSessionBean () {
158                 // Try it
159                 try {
160                         // Get initial context
161                         Context context = new InitialContext();
162
163                         // Try to lookup
164                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
165                 } catch (final NamingException e) {
166                         // Throw again
167                         throw new FaceletException(e);
168                 }
169         }
170
171         @Override
172         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
173                 // Trace message
174                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminAddedUserEvent: event={0} - CALLED!", event)); //NOI18N
175
176                 // event should not be null
177                 if (null == event) {
178                         // Throw NPE
179                         throw new NullPointerException("event is null"); //NOI18N
180                 } else if (event.getAddedUser() == null) {
181                         // Throw NPE again
182                         throw new NullPointerException("event.addedUser is null"); //NOI18N
183                 } else if (event.getAddedUser().getUserId() == null) {
184                         // userId is null
185                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
186                 } else if (event.getAddedUser().getUserId() < 1) {
187                         // Not avalid id
188                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
189                 }
190
191                 // Update user list
192                 this.updateList(event.getAddedUser());
193
194                 // Clear all data
195                 this.clear();
196
197                 // Set user id again
198                 this.setUserId(event.getAddedUser().getUserId());
199         }
200
201         @Override
202         public void afterAdminLinkedUserEvent (@Observes final AdminLinkedUserEvent event) {
203                 // event should not be null
204                 if (null == event) {
205                         // Throw NPE
206                         throw new NullPointerException("event is null"); //NOI18N
207                 } else if (event.getLinkedUser() == null) {
208                         // Throw NPE again
209                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
210                 } else if (event.getLinkedUser().getUserId() == null) {
211                         // userId is null
212                         throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
213                 } else if (event.getLinkedUser().getUserId() < 1) {
214                         // Not avalid id
215                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
216                 }
217
218                 // Update user list
219                 this.updateList(event.getLinkedUser());
220
221                 // Clear all data
222                 this.clear();
223
224                 // Trace message
225                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminAddedUserEvent: EXIT!"); //NOI18N
226         }
227
228         @Override
229         public void afterAdminLockedUserEvent (@Observes final AdminLockedUserEvent event) {
230                 // event should not be null
231                 if (null == event) {
232                         // Throw NPE
233                         throw new NullPointerException("event is null"); //NOI18N
234                 } else if (event.getLockedUser() == null) {
235                         // Throw NPE again
236                         throw new NullPointerException("event.lockedUser is null"); //NOI18N
237                 } else if (event.getLockedUser().getUserId() == null) {
238                         // userId is null
239                         throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
240                 } else if (event.getLockedUser().getUserId() < 1) {
241                         // Not avalid id
242                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
243                 }
244
245                 // Update user list
246                 this.updateList(event.getLockedUser());
247         }
248
249         @Override
250         public void afterAdminUnlockedUserEvent (@Observes final AdminUnlockedUserEvent event) {
251                 // event should not be null
252                 if (null == event) {
253                         // Throw NPE
254                         throw new NullPointerException("event is null"); //NOI18N
255                 } else if (event.getUnlockedUser() == null) {
256                         // Throw NPE again
257                         throw new NullPointerException("event.unlockedUser is null"); //NOI18N
258                 } else if (event.getUnlockedUser().getUserId() == null) {
259                         // userId is null
260                         throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
261                 } else if (event.getUnlockedUser().getUserId() < 1) {
262                         // Not avalid id
263                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
264                 }
265
266                 // Update user list
267                 this.updateList(event.getUnlockedUser());
268         }
269
270         @Override
271         public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
272                 // Trace message
273                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterAdminUpdatedUserEvent: event={0} - CALLED!", event)); //NOI18N
274
275                 // event should not be null
276                 if (null == event) {
277                         // Throw NPE
278                         throw new NullPointerException("event is null"); //NOI18N
279                 } else if (event.getUpdatedUser() == null) {
280                         // Throw NPE again
281                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
282                 } else if (event.getUpdatedUser().getUserId() == null) {
283                         // userId is null
284                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
285                 } else if (event.getUpdatedUser().getUserId() < 1) {
286                         // Not avalid id
287                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
288                 }
289
290                 // Update user list
291                 this.updateList(event.getUpdatedUser());
292
293                 // Clear all data
294                 this.clear();
295
296                 // Trace message
297                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterAdminUpdatedUserEvent: EXIT!"); //NOI18N
298         }
299
300         @Override
301         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
302                 // Trace message
303                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
304
305                 // event should not be null
306                 if (null == event) {
307                         // Throw NPE
308                         throw new NullPointerException("event is null"); //NOI18N
309                 } else if (event.getRegisteredUser() == null) {
310                         // Throw NPE again
311                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
312                 } else if (event.getRegisteredUser().getUserId() == null) {
313                         // userId is null
314                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
315                 } else if (event.getRegisteredUser().getUserId() < 1) {
316                         // Not avalid id
317                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
318                 }
319
320                 // Get user instance
321                 User registeredUser = event.getRegisteredUser();
322
323                 // Debug message
324                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
325
326                 // Copy all data from registered->user
327                 this.copyUser(registeredUser);
328
329                 // Clear all data
330                 this.clear();
331
332                 // Update user list
333                 this.updateList(registeredUser);
334
335                 // Add user name
336                 this.addUserName(registeredUser);
337
338                 // Is the account public?
339                 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
340                         // Also add it to this list
341                         this.visibleUserList.add(registeredUser);
342                 }
343
344                 // Set user id again
345                 this.setUserId(registeredUser.getUserId());
346
347                 // Trace message
348                 //* NOISY-DEBUG: */ System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
349         }
350
351         @Override
352         public void afterUserConfirmedAccount (@Observes final UserConfirmedAccountEvent event) {
353                 // Trace message
354                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterAdminUpdatedContactDataEvent: event={0} - CALLED!", event)); //NOI18N
355
356                 // event should not be null
357                 if (null == event) {
358                         // Throw NPE
359                         throw new NullPointerException("event is null"); //NOI18N
360                 } else if (event.getConfirmedUser() == null) {
361                         // Throw NPE again
362                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
363                 } else if (event.getConfirmedUser().getUserId() == null) {
364                         // userId is null
365                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
366                 } else if (event.getConfirmedUser().getUserId() < 1) {
367                         // Not avalid id
368                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
369                 }
370
371                 // Update user list
372                 this.updateList(event.getConfirmedUser());
373         }
374
375         @Override
376         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
377                 // Trace message
378                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
379
380                 // event should not be null
381                 if (null == event) {
382                         // Throw NPE
383                         throw new NullPointerException("event is null"); //NOI18N
384                 } else if (event.getLoggedInUser() == null) {
385                         // Throw NPE again
386                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
387                 } else if (event.getLoggedInUser().getUserId() == null) {
388                         // userId is null
389                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
390                 } else if (event.getLoggedInUser().getUserId() < 1) {
391                         // Not avalid id
392                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
393                 }
394
395                 // "Cache" user instance
396                 User loggedInUser = event.getLoggedInUser();
397
398                 // Copy all data to this bean
399                 this.copyUser(loggedInUser);
400
401                 // Is the user visible?
402                 if (Objects.equals(loggedInUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
403                         // Yes, then add user
404                         this.visibleUserList.add(loggedInUser);
405                 }
406         }
407
408         @Override
409         public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
410                 // Check parameter
411                 if (null == event) {
412                         // Throw NPE
413                         throw new NullPointerException("event is null"); //NOI18N
414                 } else if (event.getPasswordHistory() == null) {
415                         // Throw NPE again
416                         throw new NullPointerException("event.passwordHistory is null"); //NOI18N
417                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
418                         // ... and again
419                         throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
420                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
421                         // Invalid value
422                         throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
423                 }
424
425                 // Update user list
426                 this.updateList(event.getPasswordHistory().getUserPasswordHistoryUser());
427         }
428
429         @Override
430         public void afterUserUpdatedPersonalData (@Observes final UpdatedUserPersonalDataEvent event) {
431                 // Check parameter
432                 if (null == event) {
433                         // Throw NPE
434                         throw new NullPointerException("event is null"); //NOI18N
435                 } else if (event.getUpdatedUser() == null) {
436                         // Throw NPE again
437                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
438                 } else if (event.getUpdatedUser().getUserId() == null) {
439                         // ... and again
440                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
441                 } else if (event.getUpdatedUser().getUserId() < 1) {
442                         // Invalid value
443                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
444                 }
445
446                 // Update user list
447                 this.updateList(event.getUpdatedUser());
448         }
449
450         @Override
451         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
452         public List<User> allUsers () {
453                 // Return it
454                 return this.userList;
455         }
456
457         @Override
458         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
459         public List<User> allVisibleUsers () {
460                 // Return it
461                 return this.visibleUserList;
462         }
463
464         @Override
465         public User createUserInstance (final boolean createContactData) {
466                 // Trace message
467                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: CALLED!", this.getClass().getSimpleName()));
468
469                 // Required personal data must be set
470                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
471
472                 // Create new user instance
473                 User user = new LoginUser();
474
475                 // Is user name required?
476                 if (!this.isUserNameRequired()) {
477                         // Generate pseudo-random user name
478                         String randomName = this.userBean.generateRandomUserName();
479
480                         // Set it and inivisible profile
481                         this.setUserName(randomName);
482                         this.setUserProfileMode(ProfileMode.INVISIBLE);
483
484                         // Generate random password
485                         String randomPassword = UserUtils.createRandomPassword(JobsUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
486
487                         // Set random password
488                         this.setUserPassword(randomPassword);
489                         this.setUserPasswordRepeat(randomPassword);
490                 }
491
492                 // Set local, user name and mode
493                 user.setUserLocale(this.localizationController.getLocale());
494                 user.setUserName(this.getUserName());
495                 user.setUserProfileMode(this.getUserProfileMode());
496
497                 // Is multiple registration page
498                 if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N
499                         // Create contact instance
500                         Contact contact = this.contactController.createContactInstance();
501
502                         // Debug message
503                         //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: contact={1}", this.getClass().getSimpleName(), contact));
504
505                         // Set contact in user
506                         user.setUserContact(contact);
507                 }
508
509                 // Trace message
510                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("{0}.createUserInstance: user={1} - EXIT!", this.getClass().getSimpleName(), user));
511
512                 // Return it
513                 return user;
514         }
515
516         @Override
517         public User createUserLogin () {
518                 // Trace message
519                 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: CALLED!", this.getClass().getSimpleName()));
520
521                 // Is all data set?
522                 if (this.getUserName() == null) {
523                         // Throw NPE
524                         throw new NullPointerException("recruiterName is null"); //NOI18N
525                 } else if (this.getUserName().isEmpty()) {
526                         // Is empty
527                         throw new IllegalStateException("recruiterName is empty."); //NOI18N
528                 }
529
530                 // Create new recruiter instance
531                 User recruiter = new LoginUser();
532
533                 // Update all data ...
534                 recruiter.setUserName(this.getUserName());
535
536                 // Trace message
537                 //* NOISY-DEBUG */ System.out.println(MessageFormat.format("{0}.createUserLogin: recruiter={1} - EXIT!", this.getClass().getSimpleName(), recruiter));
538
539                 // Return the new instance
540                 return recruiter;
541         }
542
543         @Override
544         public String doChangePersonalData () {
545                 // This method shall only be called if the user is logged-in
546                 if (!this.userLoginController.isUserLoggedIn()) {
547                         // Not logged-in
548                         throw new IllegalStateException("User is not logged-in"); //NOI18N
549                 } else if (!this.isRequiredChangePersonalDataSet()) {
550                         // Not all required fields are set
551                         throw new FaceletException("Not all required fields are set."); //NOI18N
552                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
553                         // Password not matching
554                         throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
555                 } else if (!this.featureController.isFeatureEnabled("edit_user_data")) {
556                         // Editing is not allowed
557                         throw new IllegalStateException("User tried to edit personal data."); //NOI18N
558                 }
559
560                 // Get user instance
561                 User user = this.userLoginController.getLoggedInUser();
562
563                 // Copy contact data to contact instance
564                 this.contactController.updateContactDataFromController(user.getUserContact());
565
566                 // It should be there, so run some tests on it
567                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
568                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
569                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
570                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
571                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
572                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
573
574                 // Update all fields
575                 user.setUserProfileMode(this.getUserProfileMode());
576
577                 // Send it to the EJB
578                 User updatedUser = this.userBean.updateUserPersonalData(user);
579
580                 // Fire event
581                 this.updatedPersonalDataEvent.fire(new UserUpdatedPersonalDataEvent(updatedUser));
582
583                 // All fine
584                 return "contact_data_saved"; //NOI18N
585         }
586
587         @Override
588         public Long getUserId () {
589                 return this.userId;
590         }
591
592         @Override
593         public void setUserId (final Long userId) {
594                 this.userId = userId;
595         }
596
597         @Override
598         public String getUserName () {
599                 return this.userName;
600         }
601
602         @Override
603         public void setUserName (final String userName) {
604                 this.userName = userName;
605         }
606
607         @Override
608         public String getUserPassword () {
609                 return this.userPassword;
610         }
611
612         @Override
613         public void setUserPassword (final String userPassword) {
614                 this.userPassword = userPassword;
615         }
616
617         @Override
618         public String getUserPasswordRepeat () {
619                 return this.userPasswordRepeat;
620         }
621
622         @Override
623         public void setUserPasswordRepeat (final String userPasswordRepeat) {
624                 this.userPasswordRepeat = userPasswordRepeat;
625         }
626
627         @Override
628         public ProfileMode getUserProfileMode () {
629                 return this.userProfileMode;
630         }
631
632         @Override
633         public void setUserProfileMode (final ProfileMode userProfileMode) {
634                 this.userProfileMode = userProfileMode;
635         }
636
637         /**
638          * Post-initialization of this class
639          */
640         @PostConstruct
641         public void init () {
642                 // Initialize user list
643                 this.userList = this.userBean.allUsers();
644
645                 // Get full user name list for reducing EJB calls
646                 this.userNameList = this.userBean.getUserNameList();
647
648                 // Is the user logged-in?
649                 if (this.userLoginController.isUserLoggedIn()) {
650                         // Is logged-in, so load also users visible to memebers
651                         this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
652                 } else {
653                         // Initialize user list
654                         this.visibleUserList = this.userBean.allPublicUsers();
655                 }
656         }
657
658         @Override
659         public boolean isContactFound (final Contact contact) {
660                 // The contact must be valid
661                 if (null == contact) {
662                         // Throw NPE
663                         throw new NullPointerException("contact is null"); //NOI18N
664                 } else if (contact.getContactId() == null) {
665                         // Throw again ...
666                         throw new NullPointerException("contact.contactId is null"); //NOI18N
667                 } else if (contact.getContactId() < 1) {
668                         // Not valid
669                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
670                 }
671
672                 // Default is not found
673                 boolean isFound = false;
674
675                 // Get iterator
676                 Iterator<User> iterator = this.allUsers().iterator();
677
678                 // Loop through all entries
679                 while (iterator.hasNext()) {
680                         // Get user
681                         User next = iterator.next();
682
683                         // Compare both objects
684                         if (Objects.equals(contact, next.getUserContact())) {
685                                 // Found it
686                                 isFound = true;
687                                 break;
688                         }
689                 }
690
691                 // Return status
692                 return isFound;
693         }
694
695         @Override
696         public boolean isPublicUserProfileEnabled () {
697                 // Get context parameter
698                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_public_profile_enabled"); //NOI18N
699
700                 // Is it set?
701                 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
702
703                 // This requires user names being enabled, too.
704                 if ((isEnabled) && (!this.isUserNameRequired())) {
705                         // Not valid state, users must be able to modify their profile, especially when it is public
706                         throw new IllegalStateException("Public user profiles are enabled but user name requirement is disabled, this is not possible."); //NOI18N
707                 }
708
709                 // Return value
710                 return isEnabled;
711         }
712
713         @Override
714         public boolean isRequiredChangePersonalDataSet () {
715                 return ((this.getUserProfileMode() != null) &&
716                                 (this.getUserName() != null) && (!this.getUserName().isEmpty()) &&
717                                 (this.contactController.isRequiredChangePersonalDataSet()));
718         }
719
720         @Override
721         public boolean isRequiredPersonalDataSet () {
722                 if (this.featureController.isFeatureEnabled("user_register_multiple_page")) { //NOI18N
723                         // Multiple registration page
724                         return this.contactController.isRequiredPersonalDataSet();
725                 } else {
726                         // Single registration page
727                         return (((this.getUserName() != null) || (!this.isUserNameRequired())) &&
728                                         (this.getUserProfileMode() != null) &&
729                                         (this.contactController.isRequiredPersonalDataSet()) &&
730                                         (this.getUserPassword() != null) &&
731                                         (this.getUserPasswordRepeat() != null));
732                 }
733         }
734
735         @Override
736         public boolean isSamePasswordEntered () {
737                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
738         }
739
740         @Override
741         public boolean isUserIdEmpty () {
742                 return ((this.getUserId() == null) || (this.getUserId() == 0));
743         }
744
745         @Override
746         public boolean isUserNameRegistered (final User user) {
747                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
748         }
749
750         @Override
751         public boolean isUserNameRequired () {
752                 // Get context parameter
753                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("is_user_name_required"); //NOI18N
754
755                 // Is it set?
756                 boolean isRequired = ((contextParameter instanceof String) && (contextParameter.toLowerCase().equals("true"))); //NOI18N
757
758                 // Return value
759                 return isRequired;
760         }
761
762         @Override
763         public boolean isVisibleUserFound () {
764                 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
765         }
766
767         @Override
768         public User lookupUserById (final Long userId) throws UserNotFoundException {
769                 // Parameter must be valid
770                 if (null == userId) {
771                         // Throw NPE
772                         throw new NullPointerException("userId is null"); //NOI18N
773                 } else if (userId < 1) {
774                         // Not valid
775                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
776                 }
777
778                 // Init variable
779                 User user = null;
780
781                 // Try to lookup it in visible user list
782                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
783                         // Get next user
784                         User next = iterator.next();
785
786                         // Is the user id found?
787                         if (Objects.equals(next.getUserId(), userId)) {
788                                 // Copy to other variable
789                                 user = next;
790                                 break;
791                         }
792                 }
793
794                 // Is it still null?
795                 if (null == user) {
796                         // Not visible for the current user
797                         throw new UserNotFoundException(userId);
798                 }
799
800                 // Return it
801                 return user;
802         }
803
804         @Override
805         public User lookupUserByEmailAddress (final String emailAddress) throws UserEmailAddressNotFoundException {
806                 // Parameter must be valid
807                 if (null == emailAddress) {
808                         // Throw NPE
809                         throw new NullPointerException("emailAddress is null"); //NOI18N
810                 } else if (emailAddress.isEmpty()) {
811                         // Not valid
812                         throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
813                 }
814
815                 // Init variable
816                 User user = null;
817
818                 // Try to lookup it in visible user list
819                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
820                         // Get next user
821                         User next = iterator.next();
822
823                         // Contact should be set
824                         if (next.getUserContact() == null) {
825                                 // Contact is null
826                                 throw new NullPointerException(MessageFormat.format("next.userContact is null for user id {0}", next.getUserId())); //NOI18N
827                         } else if (next.getUserContact().getContactEmailAddress() == null) {
828                                 // Email address should be set
829                                 throw new NullPointerException(MessageFormat.format("next.userContact.contactEmailAddress is null for user id {0}", next.getUserId())); //NOI18N //NOI18N
830                         }
831
832                         // Is the email address found?
833                         if (Objects.equals(next.getUserContact().getContactEmailAddress(), emailAddress)) {
834                                 // Copy to other variable
835                                 user = next;
836                                 break;
837                         }
838                 }
839
840                 // Is it still null?
841                 if (null == user) {
842                         // Not visible for the current user
843                         throw new UserEmailAddressNotFoundException(emailAddress);
844                 }
845
846                 // Return it
847                 return user;
848         }
849
850         /**
851          * Adds user's name to bean's internal list. It also updates the public user
852          * list if the user has decided to have a public account,
853          * <p>
854          * @param user User instance
855          */
856         private void addUserName (final User user) {
857                 // Make sure the entry is not added yet
858                 if (this.userNameList.contains(user.getUserName())) {
859                         // Abort here
860                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
861                 } else if (this.contactController.isEmailAddressRegistered(user.getUserContact())) {
862                         // Already added
863                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
864                 }
865
866                 // Add user name
867                 this.userNameList.add(user.getUserName());
868         }
869
870         /**
871          * Clears this bean
872          */
873         private void clear () {
874                 // Clear all data
875                 // - personal data
876                 this.setUserId(null);
877                 this.setUserProfileMode(null);
878
879                 // - other data
880                 this.setUserName(null);
881                 this.setUserPassword(null);
882                 this.setUserPasswordRepeat(null);
883         }
884
885         /**
886          * Copies given user into the controller
887          * <p>
888          * @param user User instance
889          */
890         private void copyUser (final User user) {
891                 // Make sure the instance is valid
892                 if (null == user) {
893                         // Throw NPE
894                         throw new NullPointerException("user is null"); //NOI18N
895                 } else if (user.getUserContact() == null) {
896                         // Throw again ...
897                         throw new NullPointerException("user.userContact is null"); //NOI18N
898                 }
899
900                 // Copy all fields:
901                 // - base data
902                 this.setUserId(user.getUserId());
903                 this.setUserProfileMode(user.getUserProfileMode());
904         }
905
906         /**
907          * Updates list with given user instance
908          * <p>
909          * @param user User instance
910          */
911         private void updateList (final User user) {
912                 // The user should be valid
913                 if (null == user) {
914                         // Throw NPE
915                         throw new NullPointerException("user is null"); //NOI18N
916                 } else if (user.getUserId() == null) {
917                         // ... again NPE
918                         throw new NullPointerException("user.userId is null"); //NOI18N
919                 } else if (user.getUserId() < 1) {
920                         // Invalid id
921                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
922                 } else if (user.getUserContact() == null) {
923                         // Throw again ...
924                         throw new NullPointerException("user.userContact is null"); //NOI18N
925                 } else if (user.getUserContact().getContactId() == null) {
926                         // Throw again ...
927                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
928                 } else if (user.getUserContact().getContactId() < 1) {
929                         // Throw again ...
930                         throw new NullPointerException(MessageFormat.format("user.userContact.contactId={0} is invalid.", user.getUserContact().getContactId())); //NOI18N
931                 }
932
933                 // Get iterator from list
934                 Iterator<User> iterator = this.userList.iterator();
935
936                 // "Walk" through all entries
937                 while (iterator.hasNext()) {
938                         // Get next element
939                         User next = iterator.next();
940
941                         // Is user id number the same?
942                         if (Objects.equals(user.getUserId(), next.getUserId())) {
943                                 // Found entry, so remove it and abort
944                                 this.userList.remove(next);
945                                 break;
946                         }
947                 }
948
949                 // Re-add user
950                 this.userList.add(user);
951         }
952
953 }