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