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