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