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