]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/user/AddressbookAdminUserWebSessionBean.java
moved user instance to admin controller as this is the right one for this purpose
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / user / AddressbookAdminUserWebSessionBean.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.util.Collections;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.inject.Any;
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.user.AdminAddedUserEvent;
46 import org.mxchange.jusercore.events.user.AdminUserAddedEvent;
47 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
48 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
49 import org.mxchange.jusercore.exceptions.UserNotFoundException;
50 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
51 import org.mxchange.jusercore.model.user.LoginUser;
52 import org.mxchange.jusercore.model.user.User;
53 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
54 import org.mxchange.jusercore.model.user.UserUtils;
55 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
56 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
57
58 /**
59  * A user bean (controller)
60  * <p>
61  * @author Roland Haeder<roland@mxchange.org>
62  */
63 @Named ("adminUserController")
64 @SessionScoped
65 public class AddressbookAdminUserWebSessionBean implements AddressbookAdminUserWebSessionController {
66
67         /**
68          * Serial number
69          */
70         private static final long serialVersionUID = 542_145_347_916L;
71
72         /**
73          * An event fired when the administrator has added a new user
74          */
75         @Inject
76         @Any
77         private Event<AdminAddedUserEvent> addedUserEvent;
78
79         /**
80          * Birth day
81          */
82         private Date birthday;
83
84         /**
85          * Cellphone number's carrier
86          */
87         private MobileProvider cellphoneCarrier;
88
89         /**
90          * Cellphone number
91          */
92         private Long cellphoneNumber;
93
94         /**
95          * City
96          */
97         private String city;
98
99         /**
100          * Optional comments
101          */
102         private String comment;
103
104         /**
105          * Country instance
106          */
107         private Country country;
108
109         /**
110          * Email address
111          */
112         private String emailAddress;
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          * Phone number area code
151          */
152         private Integer phoneAreaCode;
153
154         /**
155          * Country instance for phone number
156          */
157         private Country phoneCountry;
158
159         /**
160          * Phone number
161          */
162         private Long phoneNumber;
163
164         /**
165          * Street
166          */
167         private String street;
168
169         /**
170          * User instance
171          */
172         private User user;
173
174         /**
175          * Remote user bean
176          */
177         private final UserSessionBeanRemote userBean;
178
179         /**
180          * A list of all user profiles
181          */
182         private List<User> userList;
183
184         /**
185          * User name
186          */
187         private String userName;
188
189         /**
190          * User password (unencrypted from web form)
191          */
192         private String userPassword;
193
194         /**
195          * User password repeated (unencrypted from web form)
196          */
197         private String userPasswordRepeat;
198
199         /**
200          * ZIP code
201          */
202         private Integer zipCode;
203
204         /**
205          * Regular user controller
206          */
207         @Inject
208         private AddressbookUserWebSessionController userController;
209
210         /**
211          * Default constructor
212          */
213         public AddressbookAdminUserWebSessionBean () {
214                 // Set gender to UNKNOWN
215                 this.gender = Gender.UNKNOWN;
216
217                 // Try it
218                 try {
219                         // Get initial context
220                         Context context = new InitialContext();
221
222                         // Try to lookup
223                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
224                 } catch (final NamingException e) {
225                         // Throw again
226                         throw new FaceletException(e);
227                 }
228         }
229
230         @Override
231         public void addUser () {
232                 // Create new user instance
233                 User localUser = new LoginUser();
234
235                 // Set user name, CONFIRMED and INVISIBLE
236                 localUser.setUserName(this.getUserName());
237                 localUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
238                 localUser.setUserProfileMode(ProfileMode.INVISIBLE);
239
240                 // Generate phone number
241                 DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber());
242                 DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber());
243                 DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber());
244
245                 // Create new contact
246                 Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName());
247                 contact.setContactStreet(this.getStreet());
248                 contact.setContactHouseNumber(this.getHouseNumber());
249                 contact.setContactZipCode(this.getZipCode());
250                 contact.setContactCity(this.getCity());
251                 contact.setContactCountry(this.getCountry());
252                 contact.setContactEmailAddress(this.getEmailAddress());
253
254                 // Don't set null or wrong references
255                 if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) {
256                         // Now the number must be given
257                         if (phone.getPhoneAreaCode() == null) {
258                                 // Is null
259                                 throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N
260                         } else if (phone.getPhoneAreaCode() < 1) {
261                                 // Abort here
262                                 throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N
263                         } else if (phone.getPhoneNumber() == null) {
264                                 // Is null
265                                 throw new NullPointerException("phone.phoneNumber is null"); //NOI18N
266                         } else if (phone.getPhoneNumber() < 1) {
267                                 // Abort here
268                                 throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N
269                         }
270
271                         // Set phone number
272                         contact.setContactLandLineNumber(phone);
273                 }
274
275                 // Don't set null or wrong references
276                 if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) {
277                         // Now the number must be given
278                         if (fax.getPhoneAreaCode() == null) {
279                                 // Is null
280                                 throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N
281                         } else if (fax.getPhoneAreaCode() < 1) {
282                                 // Abort here
283                                 throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N
284                         } else if (fax.getPhoneNumber() == null) {
285                                 // Is null
286                                 throw new NullPointerException("fax.phoneNumber is null"); //NOI18N
287                         } else if (fax.getPhoneNumber() < 1) {
288                                 // Abort here
289                                 throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N
290                         }
291
292                         // Set fax number
293                         contact.setContactFaxNumber(fax);
294                 }
295
296                 // Is the provider set?
297                 if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) {
298                         // Is the number set?
299                         if (cellphone.getPhoneNumber() == null) {
300                                 // Is null
301                                 throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N
302                         } else if (cellphone.getPhoneNumber() < 1) {
303                                 // Abort here
304                                 throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N
305                         }
306
307                         // Set cellphone number
308                         contact.setContactCellphoneNumber(cellphone);
309                 }
310
311                 contact.setContactBirthday(this.getBirthday());
312                 contact.setContactComment(this.getComment());
313
314                 // Set contact in user
315                 localUser.setUserContact(contact);
316
317                 // Init variable for password
318                 String password = null;
319
320                 // Is the user name or email address used already?
321                 // @TODO Add password length check
322                 if (this.userController.isUserNameRegistered(localUser)) {
323                         // User name is already used
324                         throw new FaceletException(new UserNameAlreadyRegisteredException(localUser));
325                 } else if (this.userController.isEmailAddressRegistered(localUser)) {
326                         // Email address is already used
327                         throw new FaceletException(new EmailAddressAlreadyRegisteredException(localUser));
328                 } else if ((this.getUserPassword() == null && (this.getUserPasswordRepeat() == null)) || ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty()))) {
329                         // Empty password entered, then generate one
330                         password = UserUtils.createRandomPassword(AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH);
331                 } else if (!this.isSamePasswordEntered()) {
332                         // Both passwords don't match
333                         throw new FaceletException(new UserPasswordMismatchException(localUser));
334                 } else {
335                         // Both match, so get it from this bean
336                         password = this.getUserPassword();
337                 }
338
339                 // The password should not be null and at least 5 characters long
340                 assert (password != null) : "password is null"; //NOI18N
341                 assert (password.length() >= AddressbookUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; //NOI18N
342
343                 // Encrypt password and set it
344                 localUser.setUserEncryptedPassword(UserUtils.encryptPassword(password));
345
346                 // Init updated user instance
347                 User updatedUser = null;
348
349                 try {
350                         // Now, that all is set, call EJB
351                         updatedUser = this.userBean.addUser(localUser);
352                 } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) {
353                         // Throw again
354                         throw new FaceletException(ex);
355                 }
356
357                 // Fire event
358                 this.addedUserEvent.fire(new AdminUserAddedEvent(updatedUser));
359
360                 // Add user to local list
361                 this.userList.add(updatedUser);
362
363                 // Clear all
364                 this.clear();
365         }
366
367         @Override
368         public List<User> allUsers () {
369                 // Return it
370                 return Collections.unmodifiableList(this.userList);
371         }
372
373         @Override
374         public Date getBirthday () {
375                 return this.birthday;
376         }
377
378         @Override
379         public void setBirthday (final Date birthday) {
380                 this.birthday = birthday;
381         }
382
383         @Override
384         public MobileProvider getCellphoneCarrier () {
385                 return this.cellphoneCarrier;
386         }
387
388         @Override
389         public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) {
390                 this.cellphoneCarrier = cellphoneCarrier;
391         }
392
393         @Override
394         public Long getCellphoneNumber () {
395                 return this.cellphoneNumber;
396         }
397
398         @Override
399         public void setCellphoneNumber (Long cellphoneNumber) {
400                 this.cellphoneNumber = cellphoneNumber;
401         }
402
403         @Override
404         public String getCity () {
405                 return this.city;
406         }
407
408         @Override
409         public void setCity (final String city) {
410                 this.city = city;
411         }
412
413         @Override
414         public String getComment () {
415                 return this.comment;
416         }
417
418         @Override
419         public void setComment (final String comment) {
420                 this.comment = comment;
421         }
422
423         @Override
424         public Country getCountry () {
425                 return this.country;
426         }
427
428         @Override
429         public void setCountry (final Country country) {
430                 this.country = country;
431         }
432
433         @Override
434         public String getEmailAddress () {
435                 return this.emailAddress;
436         }
437
438         @Override
439         public void setEmailAddress (final String emailAddress) {
440                 this.emailAddress = emailAddress;
441         }
442
443         @Override
444         public String getFamilyName () {
445                 return this.familyName;
446         }
447
448         @Override
449         public void setFamilyName (final String familyName) {
450                 this.familyName = familyName;
451         }
452
453         @Override
454         public Integer getFaxAreaCode () {
455                 return this.faxAreaCode;
456         }
457
458         @Override
459         public void setFaxAreaCode (final Integer faxAreaCode) {
460                 this.faxAreaCode = faxAreaCode;
461         }
462
463         @Override
464         public Country getFaxCountry () {
465                 return this.faxCountry;
466         }
467
468         @Override
469         public void setFaxCountry (final Country faxCountry) {
470                 this.faxCountry = faxCountry;
471         }
472
473         @Override
474         public Long getFaxNumber () {
475                 return this.faxNumber;
476         }
477
478         @Override
479         public void setFaxNumber (final Long faxNumber) {
480                 this.faxNumber = faxNumber;
481         }
482
483         @Override
484         public String getFirstName () {
485                 return this.firstName;
486         }
487
488         @Override
489         public void setFirstName (final String firstName) {
490                 this.firstName = firstName;
491         }
492
493         @Override
494         public Gender getGender () {
495                 return this.gender;
496         }
497
498         @Override
499         public void setGender (final Gender gender) {
500                 this.gender = gender;
501         }
502
503         @Override
504         public Short getHouseNumber () {
505                 return this.houseNumber;
506         }
507
508         @Override
509         public void setHouseNumber (final Short houseNumber) {
510                 this.houseNumber = houseNumber;
511         }
512
513         @Override
514         public Integer getPhoneAreaCode () {
515                 return this.phoneAreaCode;
516         }
517
518         @Override
519         public void setPhoneAreaCode (final Integer phoneAreaCode) {
520                 this.phoneAreaCode = phoneAreaCode;
521         }
522
523         @Override
524         public Country getPhoneCountry () {
525                 return this.phoneCountry;
526         }
527
528         @Override
529         public void setPhoneCountry (final Country phoneCountry) {
530                 this.phoneCountry = phoneCountry;
531         }
532
533         @Override
534         public Long getPhoneNumber () {
535                 return this.phoneNumber;
536         }
537
538         @Override
539         public void setPhoneNumber (final Long phoneNumber) {
540                 this.phoneNumber = phoneNumber;
541         }
542
543         @Override
544         public String getStreet () {
545                 return this.street;
546         }
547
548         @Override
549         public void setStreet (final String street) {
550                 this.street = street;
551         }
552
553         @Override
554         public User getUser () {
555                 return this.user;
556         }
557
558         @Override
559         public void setUser (final User user) {
560                 this.user = user;
561         }
562
563         @Override
564         public String getUserName () {
565                 return this.userName;
566         }
567
568         @Override
569         public void setUserName (final String userName) {
570                 this.userName = userName;
571         }
572
573         @Override
574         public String getUserPassword () {
575                 return this.userPassword;
576         }
577
578         @Override
579         public void setUserPassword (final String userPassword) {
580                 this.userPassword = userPassword;
581         }
582
583         @Override
584         public String getUserPasswordRepeat () {
585                 return this.userPasswordRepeat;
586         }
587
588         @Override
589         public void setUserPasswordRepeat (final String userPasswordRepeat) {
590                 this.userPasswordRepeat = userPasswordRepeat;
591         }
592
593         @Override
594         public Integer getZipCode () {
595                 return this.zipCode;
596         }
597
598         @Override
599         public void setZipCode (final Integer zipCode) {
600                 this.zipCode = zipCode;
601         }
602
603         @Override
604         public boolean hasUsers () {
605                 return (!this.allUsers().isEmpty());
606         }
607
608         /**
609          * Post-initialization of this class
610          */
611         @PostConstruct
612         public void init () {
613                 // Initialize user list
614                 this.userList = this.userBean.allUsers();
615         }
616
617         @Override
618         public User lookupUserById (final Long userId) throws UserNotFoundException {
619                 // Init variable
620                 User user = null;
621
622                 // Try to lookup it in visible user list
623                 for (final Iterator<User> iterator = this.userList.iterator(); iterator.hasNext();) {
624                         // Get next user
625                         User next = iterator.next();
626
627                         // Is the user id found?
628                         if (Objects.equals(next.getUserId(), userId)) {
629                                 // Copy to other variable
630                                 user = next;
631                                 break;
632                         }
633                 }
634
635                 // Is it still null?
636                 if (null == user) {
637                         // Not visible for the current user
638                         throw new UserNotFoundException(userId);
639                 }
640
641                 // Return it
642                 return user;
643         }
644
645         /**
646          * Clears this bean
647          */
648         private void clear () {
649                 // Clear all
650                 this.setBirthday(null);
651                 this.setCellphoneCarrier(null);
652                 this.setCellphoneNumber(null);
653                 this.setCity(null);
654                 this.setComment(null);
655                 this.setCountry(null);
656                 this.setEmailAddress(null);
657                 this.setFamilyName(null);
658                 this.setFaxAreaCode(null);
659                 this.setFaxCountry(null);
660                 this.setFaxNumber(null);
661                 this.setFirstName(null);
662                 this.setGender(null);
663                 this.setHouseNumber(null);
664                 this.setPhoneAreaCode(null);
665                 this.setPhoneCountry(null);
666                 this.setPhoneNumber(null);
667                 this.setStreet(null);
668                 this.setUserName(null);
669                 this.setUserPassword(null);
670                 this.setUserPasswordRepeat(null);
671                 this.setZipCode(null);
672
673                 // - user instance
674                 this.setUser(null);
675         }
676
677         /**
678          * Checks if same password is entered and that they are not empty.
679          * <p>
680          * @return Whether the same password was entered
681          */
682         private boolean isSamePasswordEntered () {
683                 return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat())));
684         }
685
686 }