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