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