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