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