X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fpizzaapplication%2Fbeans%2Fuser%2FPizzaAdminUserWebSessionBean.java;h=4f33d8446654935f795ed28fe8ca1be026c34230;hb=b2ce3db57eb7c64f7b856e24d614556c3a57a048;hp=92af11c2ca55fd2be4d32007e1f8b004a74d04dd;hpb=31c87725a0edfabbf329e46e6d9a3d234fa363a2;p=pizzaservice-war.git diff --git a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebSessionBean.java index 92af11c2..4f33d844 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/user/PizzaAdminUserWebSessionBean.java @@ -24,16 +24,30 @@ import java.util.Objects; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; import javax.inject.Named; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.contact.UserContact; import org.mxchange.jcontacts.contact.gender.Gender; import org.mxchange.jcountry.data.Country; -import org.mxchange.jphone.phonenumbers.smsprovider.SmsProvider; +import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.fax.FaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; +import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; +import org.mxchange.jusercore.model.user.LoginUser; import org.mxchange.jusercore.model.user.User; import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.UserUtils; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; /** @@ -59,7 +73,7 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon /** * Cellphone number's carrier */ - private SmsProvider cellphoneCarrier; + private MobileProvider cellphoneCarrier; /** * Cellphone number @@ -181,6 +195,12 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon */ private Integer zipCode; + /** + * Regular user controller + */ + @Inject + private PizzaUserWebSessionController userController; + /** * Default constructor */ @@ -201,6 +221,128 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon } } + @Override + public void addUser () { + // Create new user instance + User user = new LoginUser(); + user.setUserName(this.getUserName()); + user.setUserProfileMode(this.getUserProfileMode()); + + // Generate phone number + DialableLandLineNumber phone = new LandLineNumber(this.getPhoneCountry(), this.getPhoneAreaCode(), this.getPhoneNumber()); + DialableCellphoneNumber cellphone = new CellphoneNumber(this.getCellphoneCarrier(), this.getCellphoneNumber()); + DialableFaxNumber fax = new FaxNumber(this.getFaxCountry(), this.getFaxAreaCode(), this.getFaxNumber()); + + // Create new contact + Contact contact = new UserContact(this.getGender(), this.getFirstName(), this.getFamilyName()); + contact.setContactStreet(this.getStreet()); + contact.setContactHouseNumber(this.getHouseNumber()); + contact.setContactZipCode(this.getZipCode()); + contact.setContactCity(this.getCity()); + contact.setContactCountry(this.getCountry()); + contact.setContactEmailAddress(this.getEmailAddress()); + + // Don't set null or wrong references + if ((phone instanceof DialableLandLineNumber) && (phone.getPhoneCountry() instanceof Country) && (this.getPhoneAreaCode() != null) && (this.getPhoneNumber() != null) && (this.getPhoneAreaCode() > 0) && (this.getPhoneNumber() > 0)) { + // Now the number must be given + if (phone.getPhoneAreaCode() == null) { + // Is null + throw new NullPointerException("phone.phoneAreaCode is null"); //NOI18N + } else if (phone.getPhoneAreaCode() < 1) { + // Abort here + throw new IllegalArgumentException("phone.phoneAreaCode is zero or below."); //NOI18N + } else if (phone.getPhoneNumber() == null) { + // Is null + throw new NullPointerException("phone.phoneNumber is null"); //NOI18N + } else if (phone.getPhoneNumber() < 1) { + // Abort here + throw new IllegalArgumentException("phone.phoneNumber is zero or below."); //NOI18N + } + + // Set phone number + contact.setContactLandLineNumber(phone); + } + + // Don't set null or wrong references + if ((fax instanceof DialableFaxNumber) && (fax.getPhoneCountry() instanceof Country) && (this.getFaxAreaCode() != null) && (this.getFaxNumber() != null) && (this.getFaxAreaCode() > 0) && (this.getFaxNumber() > 0)) { + // Now the number must be given + if (fax.getPhoneAreaCode() == null) { + // Is null + throw new NullPointerException("fax.phoneAreaCode is null"); //NOI18N + } else if (fax.getPhoneAreaCode() < 1) { + // Abort here + throw new IllegalArgumentException("fax.phoneAreaCode is zero or below."); //NOI18N + } else if (fax.getPhoneNumber() == null) { + // Is null + throw new NullPointerException("fax.phoneNumber is null"); //NOI18N + } else if (fax.getPhoneNumber() < 1) { + // Abort here + throw new IllegalArgumentException("fax.phoneNumber is zero or below."); //NOI18N + } + + // Set fax number + contact.setContactFaxNumber(fax); + } + + // Is the provider set? + if ((cellphone instanceof DialableCellphoneNumber) && (this.getCellphoneCarrier() instanceof MobileProvider) && (this.getCellphoneNumber() != null) && (this.getCellphoneNumber() > 0)) { + // Is the number set? + if (cellphone.getPhoneNumber() == null) { + // Is null + throw new NullPointerException("cellphone.phoneNumber is null"); //NOI18N + } else if (cellphone.getPhoneNumber() < 1) { + // Abort here + throw new IllegalArgumentException("cellphone.phoneNumber is zero or below."); //NOI18N + } + + // Set cellphone number + contact.setContactCellphoneNumber(cellphone); + } + + contact.setContactBirthday(this.getBirthday()); + contact.setContactComment(this.getComment()); + + // Set contact in user + user.setUserContact(contact); + + // Init variable for password + String password = null; + + // Is the user name or email address used already? + // @TODO Add password length check + if (this.userController.isUserNameRegistered(user)) { + // User name is already used + throw new FaceletException(new UserNameAlreadyRegisteredException(user)); + } else if (this.userController.isEmailAddressRegistered(user)) { + // Email address is already used + throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); + } else if ((this.getUserPassword().isEmpty()) && (this.getUserPasswordRepeat().isEmpty())) { + // Empty password entered, then generate one + password = UserUtils.createRandomPassword(PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH); + } else if (!this.isSamePasswordEntered()) { + // Both passwords don't match + throw new FaceletException(new UserPasswordMismatchException(user)); + } else { + // Both match, so get it from this bean + password = this.getUserPassword(); + } + + // The password should not be null and at least 5 characters long + assert (password != null) : "password is null"; + assert (password.length() >= PizzaUserWebSessionController.MINIMUM_PASSWORD_LENGTH) : "Password is not long enough."; + + // Encrypt password and set it + user.setUserEncryptedPassword(UserUtils.encryptPassword(password)); + + try { + // Now, that all is set, call EJB + this.userBean.addUser(user); + } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { + // Throw again + throw new FaceletException(ex); + } + } + @Override public List allUsers () { // Return it @@ -218,12 +360,12 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon } @Override - public SmsProvider getCellphoneCarrier () { + public MobileProvider getCellphoneCarrier () { return this.cellphoneCarrier; } @Override - public void setCellphoneCarrier (final SmsProvider cellphoneCarrier) { + public void setCellphoneCarrier (final MobileProvider cellphoneCarrier) { this.cellphoneCarrier = cellphoneCarrier; } @@ -447,6 +589,11 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon this.zipCode = zipCode; } + @Override + public boolean hasUsers () { + return (!this.allUsers().isEmpty()); + } + /** * Post-initialization of this class */ @@ -456,6 +603,15 @@ public class PizzaAdminUserWebSessionBean implements PizzaAdminUserWebSessionCon this.userList = this.userBean.allUsers(); } + /** + * Checks if same password is entered and that they are not empty. + *

+ * @return Whether the same password was entered + */ + private boolean isSamePasswordEntered () { + return ((!this.getUserPassword().isEmpty()) && (Objects.equals(this.getUserPassword(), this.getUserPasswordRepeat()))); + } + @Override public User lookupUserById (final Long userId) throws UserNotFoundException { // Init variable