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