]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/JobsUserWebSessionBean.java
Continued a bit:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / JobsUserWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jjobs.beans.user;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jcontacts.contact.Contact;
35 import org.mxchange.jcontacts.contact.UserContact;
36 import org.mxchange.jcontacts.contact.gender.Gender;
37 import org.mxchange.jcountry.data.Country;
38 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
39 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
40 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
41 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
42 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
43 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
44 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
45 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
46 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
47 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
48 import org.mxchange.jusercore.exceptions.UserNotFoundException;
49 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
50 import org.mxchange.jusercore.model.user.LoginUser;
51 import org.mxchange.jusercore.model.user.User;
52 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
53 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
54
55 /**
56  * A user bean (controller)
57  * <p>
58  * @author Roland Haeder<roland@mxchange.org>
59  */
60 @Named ("userController")
61 @SessionScoped
62 public class JobsUserWebSessionBean implements JobsUserWebSessionController {
63
64         /**
65          * Serial number
66          */
67         private static final long serialVersionUID = 542_145_347_916L;
68
69         /**
70          * Birth day
71          */
72         private Date birthday;
73
74         /**
75          * Cellphone number's carrier
76          */
77         private MobileProvider cellphoneCarrier;
78
79         /**
80          * Cellphone number
81          */
82         private Long cellphoneNumber;
83
84         /**
85          * City
86          */
87         private String city;
88
89         /**
90          * Optional comments
91          */
92         private String comment;
93
94         /**
95          * Country instance
96          */
97         private Country country;
98
99         /**
100          * Email address
101          */
102         private String emailAddress;
103
104         /**
105          * Email address list
106          */
107         private List<String> emailAddressList;
108
109         /**
110          * Email address repeated
111          */
112         private String emailAddressRepeat;
113
114         /**
115          * Family name
116          */
117         private String familyName;
118
119         /**
120          * Fax number's area code
121          */
122         private Integer faxAreaCode;
123
124         /**
125          * Country instance for fax number
126          */
127         private Country faxCountry;
128
129         /**
130          * Fax number
131          */
132         private Long faxNumber;
133
134         /**
135          * First name
136          */
137         private String firstName;
138
139         /**
140          * Gender instance
141          */
142         private Gender gender;
143
144         /**
145          * House number
146          */
147         private Short houseNumber;
148
149         /**
150          * Login bean (controller)
151          */
152         @Inject
153         private JobsUserLoginWebSessionController loginController;
154
155         /**
156          * Phone number area code
157          */
158         private Integer phoneAreaCode;
159
160         /**
161          * Country instance for phone number
162          */
163         private Country phoneCountry;
164
165         /**
166          * Phone number
167          */
168         private Long phoneNumber;
169
170         /**
171          * Street
172          */
173         private String street;
174
175         /**
176          * User instance
177          */
178         private User user;
179
180         /**
181          * Remote user bean
182          */
183         private final UserSessionBeanRemote userBean;
184
185         /**
186          * User id
187          */
188         private Long userId;
189
190         /**
191          * User name
192          */
193         private String userName;
194
195         /**
196          * User name list
197          */
198         private List<String> userNameList;
199
200         /**
201          * User password (unencrypted from web form)
202          */
203         private String userPassword;
204
205         /**
206          * User password repeated (unencrypted from web form)
207          */
208         private String userPasswordRepeat;
209
210         /**
211          * Whether the user wants a public profile
212          */
213         private ProfileMode userProfileMode;
214
215         /**
216          * A list of all public user profiles
217          */
218         private List<User> visibleUserList;
219
220         /**
221          * ZIP code
222          */
223         private Integer zipCode;
224
225         /**
226          * Default constructor
227          */
228         public JobsUserWebSessionBean () {
229                 // Set gender to UNKNOWN
230                 this.gender = Gender.UNKNOWN;
231
232                 // Try it
233                 try {
234                         // Get initial context
235                         Context context = new InitialContext();
236
237                         // Try to lookup
238                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
239                 } catch (final NamingException e) {
240                         // Throw again
241                         throw new FaceletException(e);
242                 }
243         }
244
245         @Override
246         public void afterRegistrationEvent (final @Observes UserRegisteredEvent event) {
247                 // Trace message
248                 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: event={0} - CALLED!", event)); //NOI18N
249
250                 // event should not be null
251                 if (null == event) {
252                         // Throw NPE
253                         throw new NullPointerException("event is null"); //NOI18N
254                 } else if (event.getRegisteredUser() == null) {
255                         // Throw NPE again
256                         throw new NullPointerException("event.user is null"); //NOI18N
257                 } else if (event.getRegisteredUser().getUserId() == null) {
258                         // userId is null
259                         throw new NullPointerException("event.user.userId is null"); //NOI18N
260                 } else if (event.getRegisteredUser().getUserId() < 1) {
261                         // Not avalid id
262                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
263                 }
264
265                 // Get user instance
266                 User registeredUser = event.getRegisteredUser();
267
268                 // Debug message
269                 System.out.println(MessageFormat.format("UserWebBean:afterRegistration: registeredUser={0}", registeredUser)); //NOI18N
270
271                 // Copy all data from registered->user
272                 this.copyUser(registeredUser);
273
274                 // Add user name and email address
275                 this.addUserNameEmailAddress(registeredUser);
276
277                 // Clear all data
278                 this.clear();
279
280                 // Set user id again
281                 this.setUserId(registeredUser.getUserId());
282
283                 // Is the account public?
284                 if (Objects.equals(registeredUser.getUserProfileMode(), ProfileMode.PUBLIC)) {
285                         // Also add it to this list
286                         this.visibleUserList.add(registeredUser);
287                 }
288
289                 // Trace message
290                 System.out.println("UserWebBean:afterRegistration: EXIT!"); //NOI18N
291         }
292
293         @Override
294         public void afterUserLogin (final @Observes UserLoggedInEvent event) {
295                 // Trace message
296                 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
297
298                 // event should not be null
299                 if (null == event) {
300                         // Throw NPE
301                         throw new NullPointerException("event is null"); //NOI18N
302                 } else if (event.getLoggedInUser() == null) {
303                         // Throw NPE again
304                         throw new NullPointerException("event.user is null"); //NOI18N
305                 } else if (event.getLoggedInUser().getUserId() == null) {
306                         // userId is null
307                         throw new NullPointerException("event.user.userId is null"); //NOI18N
308                 } else if (event.getLoggedInUser().getUserId() < 1) {
309                         // Not avalid id
310                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
311                 }
312
313                 // Re-initialize list
314                 this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
315
316                 // Copy all data to this bean
317                 this.copyUser(event.getLoggedInUser());
318
319                 // Trace message
320                 System.out.println(MessageFormat.format("UserWebBean:afterUserLogin: this.visibleUserList.size()={0} - EXIT!", this.visibleUserList.size())); //NOI18N
321         }
322
323         @Override
324         public List<User> allVisibleUsers () {
325                 // Return it
326                 return Collections.unmodifiableList(this.visibleUserList);
327         }
328
329         @Override
330         public User createUserInstance () {
331                 // User message
332                 //this.getLogger().logTrace("createUserInstance: CALLED!");
333
334                 // Required personal data must be set
335                 assert (this.isRequiredPersonalDataSet()) : "not all personal data is set"; //NOI18N
336
337                 // Create new user instance
338                 User localUser = new LoginUser();
339
340                 // Update all data ...
341                 localUser.setUserName(this.getUserName());
342                 localUser.setUserProfileMode(this.getUserProfileMode());
343
344                 // Generate phone number
345                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
346                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
347                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
348
349                 // Create new contact
350                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
351                 contact.setContactStreet(this.getStreet());
352                 contact.setContactHouseNumber(this.getHouseNumber());
353                 contact.setContactZipCode(this.getZipCode());
354                 contact.setContactCity(this.getCity());
355                 contact.setContactCountry(this.getCountry());
356                 contact.setContactEmailAddress(this.getEmailAddress());
357
358                 // Don't set null or wrong references
359                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
360                         // Now the number must be given
361                         if (phone.getPhoneAreaCode() == null) {
362                                 // Is null
363                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
364                         } else if (phone.getPhoneAreaCode() < 1) {
365                                 // Abort here
366                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
367                         } else if (phone.getPhoneNumber() == null) {
368                                 // Is null
369                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
370                         } else if (phone.getPhoneNumber() < 1) {
371                                 // Abort here
372                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
373                         }
374
375                         // Set phone number
376                         contact.setContactLandLineNumber(phone);
377                 }
378
379                 // Don't set null or wrong references
380                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
381                         // Now the number must be given
382                         if (fax.getPhoneAreaCode() == null) {
383                                 // Is null
384                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
385                         } else if (fax.getPhoneAreaCode() < 1) {
386                                 // Abort here
387                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
388                         } else if (fax.getPhoneNumber() == null) {
389                                 // Is null
390                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
391                         } else if (fax.getPhoneNumber() < 1) {
392                                 // Abort here
393                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
394                         }
395
396                         // Set fax number
397                         contact.setContactFaxNumber(fax);
398                 }
399
400                 // Is the provider set?
401                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
402                         // Is the number set?
403                         if (cellphone.getPhoneNumber() == null) {
404                                 // Is null
405                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
406                         } else if (cellphone.getPhoneNumber() < 1) {
407                                 // Abort here
408                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
409                         }
410
411                         // Set cellphone number
412                         contact.setContactCellphoneNumber(cellphone);
413                 }
414
415                 contact.setContactBirthday(this.getBirthday());
416                 contact.setContactComment(this.getComment());
417
418                 // Created timestamp and ownContact
419                 contact.setContactOwnContact(Boolean.TRUE);
420
421                 // Set contact in user
422                 localUser.setUserContact(contact);
423
424                 // Trace message
425                 //this.getLogger().logTrace(MessageFormat.format("createUserInstance: user={0} - EXIT!", user));
426
427                 // Return it
428                 return localUser;
429         }
430
431         @Override
432         public String doChangePersonalData () {
433                 // This method shall only be called if the user is logged-in
434                 if (!this.loginController.isUserLoggedIn()) {
435                         // Not logged-in
436                         throw new IllegalStateException("User is not logged-in"); //NOI18N
437                 } else if (!this.isRequiredChangePersonalDataSet()) {
438                         // Not all required fields are set
439                         throw new FaceletException("Not all required fields are set."); //NOI18N
440                 } else if (!this.loginController.ifCurrentPasswordMatches()) {
441                         // Password not matching
442                         throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
443                 }
444
445                 // Get user instance
446                 User user = this.loginController.getLoggedInUser();
447
448                 // It should be there, so run some tests on it
449                 assert (user instanceof User) : "Instance loginController.loggedInUser is null";
450                 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
451                 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
452                 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
453                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
454                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
455
456                 // Update all fields
457                 user.setUserProfileMode(this.getUserProfileMode());
458                 user.getUserContact().setContactGender(this.getGender());
459                 user.getUserContact().setContactFirstName(this.getFirstName());
460                 user.getUserContact().setContactFamilyName(this.getFamilyName());
461                 user.getUserContact().setContactStreet(this.getStreet());
462                 user.getUserContact().setContactHouseNumber(this.getHouseNumber());
463                 user.getUserContact().setContactZipCode(this.getZipCode());
464                 user.getUserContact().setContactCity(this.getCity());
465                 user.getUserContact().setContactCountry(this.getCountry());
466
467                 // Is there a phone number?
468                 if (user.getUserContact().getContactLandLineNumber() instanceof DialableLandLineNumber) {
469                         // Debug message
470                         System.out.println(MessageFormat.format("UserWebBean:doChangePersonalData: phoneId={0}", user.getUserContact().getContactLandLineNumber().getPhoneId())); //NOI18N
471
472                         // Yes, then update as well
473                         user.getUserContact().getContactLandLineNumber().setPhoneAreaCode(this.getPhoneAreaCode());
474                         user.getUserContact().getContactLandLineNumber().setPhoneNumber(this.getPhoneNumber());
475                 }
476
477                 // Is there a fax number?
478                 if (user.getUserContact().getContactFaxNumber() instanceof DialableFaxNumber) {
479                         // Debug message
480                         System.out.println(MessageFormat.format("UserWebBean:doChangePersonalData: faxId={0}", user.getUserContact().getContactFaxNumber().getPhoneId())); //NOI18N
481
482                         // Yes, then update as well
483                         user.getUserContact().getContactFaxNumber().setPhoneAreaCode(this.getFaxAreaCode());
484                         user.getUserContact().getContactFaxNumber().setPhoneNumber(this.getFaxNumber());
485                 }
486
487                 // Is there a cellphone number?
488                 if (user.getUserContact().getContactCellphoneNumber() instanceof DialableCellphoneNumber) {
489                         // Debug message
490                         System.out.println(MessageFormat.format("UserWebBean:doChangePersonalData: cellPhoneId={0}", user.getUserContact().getContactCellphoneNumber().getPhoneId())); //NOI18N
491
492                         // Yes, then update as well
493                         user.getUserContact().getContactCellphoneNumber().setCellphoneProvider(this.getCellphoneCarrier());
494                         user.getUserContact().getContactCellphoneNumber().setPhoneNumber(this.getCellphoneNumber());
495                 }
496
497                 // Send it to the EJB
498                 this.userBean.updateUserPersonalData(user);
499
500                 // All fine
501                 return "login_data_saved"; //NOI18N
502         }
503
504         @Override
505         public Date getBirthday () {
506                 return this.birthday;
507         }
508
509         @Override
510         public void setBirthday (final Date birthday) {
511                 this.birthday = birthday;
512         }
513
514         @Override
515         public MobileProvider getCellphoneCarrier () {
516                 return this.cellphoneCarrier;
517         }
518
519         @Override
520         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
521                 this.cellphoneCarrier = cellphoneCarrier;
522         }
523
524         @Override
525         public Long getCellphoneNumber () {
526                 return this.cellphoneNumber;
527         }
528
529         @Override
530         public void setCellphoneNumber (Long cellphoneNumber) {
531                 this.cellphoneNumber = cellphoneNumber;
532         }
533
534         @Override
535         public String getCity () {
536                 return this.city;
537         }
538
539         @Override
540         public void setCity (final String city) {
541                 this.city = city;
542         }
543
544         @Override
545         public String getComment () {
546                 return this.comment;
547         }
548
549         @Override
550         public void setComment (final String comment) {
551                 this.comment = comment;
552         }
553
554         @Override
555         public Country getCountry () {
556                 return this.country;
557         }
558
559         @Override
560         public void setCountry (final Country country) {
561                 this.country = country;
562         }
563
564         @Override
565         public String getEmailAddress () {
566                 return this.emailAddress;
567         }
568
569         @Override
570         public void setEmailAddress (final String emailAddress) {
571                 this.emailAddress = emailAddress;
572         }
573
574         @Override
575         public String getEmailAddressRepeat () {
576                 return this.emailAddressRepeat;
577         }
578
579         @Override
580         public void setEmailAddressRepeat (final String emailAddressRepeat) {
581                 this.emailAddressRepeat = emailAddressRepeat;
582         }
583
584         @Override
585         public String getFamilyName () {
586                 return this.familyName;
587         }
588
589         @Override
590         public void setFamilyName (final String familyName) {
591                 this.familyName = familyName;
592         }
593
594         @Override
595         public Integer getFaxAreaCode () {
596                 return this.faxAreaCode;
597         }
598
599         @Override
600         public void setFaxAreaCode (final Integer faxAreaCode) {
601                 this.faxAreaCode = faxAreaCode;
602         }
603
604         @Override
605         public Country getFaxCountry () {
606                 return this.faxCountry;
607         }
608
609         @Override
610         public void setFaxCountry (final Country faxCountry) {
611                 this.faxCountry = faxCountry;
612         }
613
614         @Override
615         public Long getFaxNumber () {
616                 return this.faxNumber;
617         }
618
619         @Override
620         public void setFaxNumber (final Long faxNumber) {
621                 this.faxNumber = faxNumber;
622         }
623
624         @Override
625         public String getFirstName () {
626                 return this.firstName;
627         }
628
629         @Override
630         public void setFirstName (final String firstName) {
631                 this.firstName = firstName;
632         }
633
634         @Override
635         public Gender getGender () {
636                 return this.gender;
637         }
638
639         @Override
640         public void setGender (final Gender gender) {
641                 this.gender = gender;
642         }
643
644         @Override
645         public Short getHouseNumber () {
646                 return this.houseNumber;
647         }
648
649         @Override
650         public void setHouseNumber (final Short houseNumber) {
651                 this.houseNumber = houseNumber;
652         }
653
654         @Override
655         public Integer getPhoneAreaCode () {
656                 return this.phoneAreaCode;
657         }
658
659         @Override
660         public void setPhoneAreaCode (final Integer phoneAreaCode) {
661                 this.phoneAreaCode = phoneAreaCode;
662         }
663
664         @Override
665         public Country getPhoneCountry () {
666                 return this.phoneCountry;
667         }
668
669         @Override
670         public void setPhoneCountry (final Country phoneCountry) {
671                 this.phoneCountry = phoneCountry;
672         }
673
674         @Override
675         public Long getPhoneNumber () {
676                 return this.phoneNumber;
677         }
678
679         @Override
680         public void setPhoneNumber (final Long phoneNumber) {
681                 this.phoneNumber = phoneNumber;
682         }
683
684         @Override
685         public String getStreet () {
686                 return this.street;
687         }
688
689         @Override
690         public void setStreet (final String street) {
691                 this.street = street;
692         }
693
694         @Override
695         public User getUser () {
696                 return this.user;
697         }
698
699         @Override
700         public void setUser (final User user) {
701                 this.user = user;
702         }
703
704         @Override
705         public Long getUserId () {
706                 return this.userId;
707         }
708
709         @Override
710         public void setUserId (final Long userId) {
711                 this.userId = userId;
712         }
713
714         @Override
715         public String getUserName () {
716                 return this.userName;
717         }
718
719         @Override
720         public void setUserName (final String userName) {
721                 this.userName = userName;
722         }
723
724         @Override
725         public String getUserPassword () {
726                 return this.userPassword;
727         }
728
729         @Override
730         public void setUserPassword (final String userPassword) {
731                 this.userPassword = userPassword;
732         }
733
734         @Override
735         public String getUserPasswordRepeat () {
736                 return this.userPasswordRepeat;
737         }
738
739         @Override
740         public void setUserPasswordRepeat (final String userPasswordRepeat) {
741                 this.userPasswordRepeat = userPasswordRepeat;
742         }
743
744         @Override
745         public ProfileMode getUserProfileMode () {
746                 return this.userProfileMode;
747         }
748
749         @Override
750         public void setUserProfileMode (final ProfileMode userProfileMode) {
751                 this.userProfileMode = userProfileMode;
752         }
753
754         @Override
755         public Integer getZipCode () {
756                 return this.zipCode;
757         }
758
759         @Override
760         public void setZipCode (final Integer zipCode) {
761                 this.zipCode = zipCode;
762         }
763
764         /**
765          * Post-initialization of this class
766          */
767         @PostConstruct
768         public void init () {
769                 // Get full user name list for reducing EJB calls
770                 this.userNameList = this.userBean.getUserNameList();
771
772                 // Get full email address list for reducing EJB calls
773                 this.emailAddressList = this.userBean.getEmailAddressList();
774
775                 // Is the user logged-in?
776                 if (this.loginController.isUserLoggedIn()) {
777                         // Is logged-in, so load also users visible to memebers
778                         this.visibleUserList = this.userBean.allMemberPublicVisibleUsers();
779                 } else {
780                         // Initialize user list
781                         this.visibleUserList = this.userBean.allPublicUsers();
782                 }
783         }
784
785         @Override
786         public boolean isEmailAddressRegistered (final User user) {
787                 return ((this.emailAddressList instanceof List) && (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())));
788         }
789
790         @Override
791         public boolean isRequiredChangePersonalDataSet () {
792                 return ((this.getUserProfileMode() != null) &&
793                                 (this.getGender() != null) &&
794                                 (this.getFirstName() != null) &&
795                                 (this.getFamilyName() != null) &&
796                                 (this.getStreet() != null) &&
797                                 (this.getHouseNumber() != null) &&
798                                 (this.getZipCode() != null) &&
799                                 (this.getCity() != null));
800         }
801
802         @Override
803         public boolean isRequiredPersonalDataSet () {
804                 return ((this.getUserName() != null) &&
805                                 (this.getUserProfileMode() != null) &&
806                                 (this.getGender() != null) &&
807                                 (this.getFirstName() != null) &&
808                                 (this.getFamilyName() != null) &&
809                                 (this.getStreet() != null) &&
810                                 (this.getHouseNumber() != null) &&
811                                 (this.getZipCode() != null) &&
812                                 (this.getCity() != null) &&
813                                 (this.getEmailAddress() != null) &&
814                                 (this.getEmailAddressRepeat() != null) &&
815                                 (this.getUserPassword() != null) &&
816                                 (this.getUserPasswordRepeat() != null));
817         }
818
819         @Override
820         public boolean isSameEmailAddressEntered () {
821                 return (Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat()));
822         }
823
824         @Override
825         public boolean isSamePasswordEntered () {
826                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
827         }
828
829         @Override
830         public boolean isUserIdEmpty () {
831                 return ((this.getUserId() == null) || (this.getUserId() == 0));
832         }
833
834         @Override
835         public boolean isUserNameRegistered (final User user) {
836                 return ((this.userNameList instanceof List) && (this.userNameList.contains(user.getUserName())));
837         }
838
839         @Override
840         public boolean isVisibleUserFound () {
841                 return ((this.visibleUserList instanceof List) && (this.visibleUserList.size() > 0));
842         }
843
844         @Override
845         public User lookupUserById (final Long userId) throws UserNotFoundException {
846                 // Init variable
847                 User localUser = null;
848
849                 // Clear this bean
850                 this.clear();
851
852                 // Try to lookup it in visible user list
853                 for (final Iterator<User> iterator = this.visibleUserList.iterator(); iterator.hasNext();) {
854                         // Get next user
855                         User next = iterator.next();
856
857                         // Is the user id found?
858                         if (Objects.equals(next.getUserId(), userId)) {
859                                 // Copy to other variable
860                                 localUser = next;
861                                 break;
862                         }
863                 }
864
865                 // Is it still null?
866                 if (null == localUser) {
867                         // Not visible for the current user
868                         throw new UserNotFoundException(userId);
869                 }
870
871                 // Copy all data to this bean
872                 this.copyUser(localUser);
873
874                 // Return it
875                 return localUser;
876         }
877
878         /**
879          * Adds user's name and email address to bean's internal list. It also
880          * updates the public user list if the user has decided to ha   }
881          * <p>
882          * @param user User instance
883          */
884         private void addUserNameEmailAddress (final User user) {
885                 // Make sure the entry is not added yet
886                 if (this.userNameList.contains(user.getUserName())) {
887                         // Abort here
888                         throw new IllegalArgumentException(MessageFormat.format("User name {0} already added.", user.getUserName())); //NOI18N
889                 } else if (this.emailAddressList.contains(user.getUserContact().getContactEmailAddress())) {
890                         // Already added
891                         throw new IllegalArgumentException(MessageFormat.format("Email address {0} already added.", user.getUserContact().getContactEmailAddress())); //NOI18N
892                 }
893
894                 // Add user name
895                 this.userNameList.add(user.getUserName());
896
897                 // Add email addres
898                 this.emailAddressList.add(user.getUserContact().getContactEmailAddress());
899         }
900
901         /**
902          * Clears this bean
903          */
904         private void clear () {
905                 // Clear all data
906                 // - personal data
907                 this.setUserId(null);
908                 this.setGender(Gender.UNKNOWN);
909                 this.setUserProfileMode(null);
910                 this.setFirstName(null);
911                 this.setFamilyName(null);
912                 this.setStreet(null);
913                 this.setHouseNumber(null);
914                 this.setZipCode(null);
915                 this.setCity(null);
916                 this.setCountry(null);
917
918                 // - contact data
919                 this.setEmailAddress(null);
920                 this.setEmailAddressRepeat(null);
921                 this.setPhoneAreaCode(null);
922                 this.setCellphoneCarrier(null);
923                 this.setFaxAreaCode(null);
924
925                 // - other data
926                 this.setBirthday(null);
927                 this.setComment(null);
928                 this.setUserName(null);
929                 this.setUserPassword(null);
930                 this.setUserPasswordRepeat(null);
931
932                 // - user instance
933                 this.setUser(null);
934         }
935
936         /**
937          * Copies given user into the controller
938          * <p>
939          * @param user User instance
940          */
941         private void copyUser (final User user) {
942                 // Copy all fields:
943                 // - base data
944                 this.setUserId(user.getUserId());
945                 this.setUserProfileMode(user.getUserProfileMode());
946                 this.setGender(user.getUserContact().getContactGender());
947                 this.setFirstName(user.getUserContact().getContactFirstName());
948                 this.setFamilyName(user.getUserContact().getContactFamilyName());
949                 this.setStreet(user.getUserContact().getContactStreet());
950                 this.setHouseNumber(user.getUserContact().getContactHouseNumber());
951                 this.setZipCode(user.getUserContact().getContactZipCode());
952                 this.setCity(user.getUserContact().getContactCity());
953                 this.setCountry(user.getUserContact().getContactCountry());
954
955                 // Get cellphone, phone and fax instance
956                 DialableCellphoneNumber cellphone = user.getUserContact().getContactCellphoneNumber();
957                 DialableFaxNumber fax = user.getUserContact().getContactFaxNumber();
958                 DialableLandLineNumber phone = user.getUserContact().getContactLandLineNumber();
959
960                 // - contact data
961                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneAreaCode() > 0)) {
962                         this.setPhoneCountry(phone.getPhoneCountry());
963                         this.setPhoneAreaCode(phone.getPhoneAreaCode());
964                         this.setPhoneNumber(phone.getPhoneNumber());
965                 }
966                 if ((cellphone instanceof DialableCellphoneNumber) && (cellphone.getCellphoneProvider() instanceof MobileProvider)) {
967                         this.setCellphoneCarrier(cellphone.getCellphoneProvider());
968                         this.setCellphoneNumber(cellphone.getPhoneNumber());
969                 }
970                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneAreaCode() > 0)) {
971                         this.setFaxCountry(fax.getPhoneCountry());
972                         this.setFaxAreaCode(fax.getPhoneAreaCode());
973                         this.setFaxNumber(fax.getPhoneNumber());
974                 }
975                 this.setEmailAddress(user.getUserContact().getContactEmailAddress());
976
977                 // -- other data
978                 this.setBirthday(user.getUserContact().getContactBirthday());
979                 this.setComment(user.getUserContact().getContactComment());
980         }
981 }