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