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