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