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