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