From f08255c405a283db7cfad0dd5a197eccbde97152 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Sun, 10 Apr 2016 16:41:40 +0200 Subject: [PATCH] added registration controller (bean) from addressbook project --- .../PizzaUserRegisterWebSessionBean.java | 138 ++++++++++++++++++ ...PizzaUserRegisterWebSessionController.java | 36 +++++ 2 files changed, 174 insertions(+) create mode 100644 src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java create mode 100644 src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionController.java diff --git a/src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java new file mode 100644 index 00000000..60f77b0e --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionBean.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.pizzaapplication.beans.register; + +import java.text.MessageFormat; +import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +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.jusercore.events.registration.RegisteredUserEvent; +import org.mxchange.jusercore.events.registration.UserRegisteredEvent; +import org.mxchange.jusercore.exceptions.DataRepeatMismatchException; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserUtils; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; +import org.mxchange.pizzaapplication.beans.user.PizzaUserWebSessionController; + +/** + * A web bean for user registration + *

+ * @author Roland Haeder + */ +@Named ("registerController") +@SessionScoped +public class PizzaUserRegisterWebSessionBean implements PizzaUserRegisterWebSessionController { + + /** + * Serial number + */ + private static final long serialVersionUID = 47_828_986_719_691_592L; + + /** + * Reemote register session bean + */ + private UserRegistrationSessionBeanRemote registerBean; + + /** + * An en event fireable when a new user has registered + */ + @Inject + @Any + private Event registeredEvent; + + /** + * User controller + */ + @Inject + private PizzaUserWebSessionController userController; + + /** + * Default constructor + */ + public PizzaUserRegisterWebSessionBean () { + try { + // Get initial context + Context context = new InitialContext(); + + // Try to lookup + this.registerBean = (UserRegistrationSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/register!org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new FaceletException(ex); + } + } + + @Override + public String doRegister () { + // Get user instance + User user = this.userController.createUserInstance(); + + // Is the user already used? + if (null == user) { + // user must be set + throw new NullPointerException("user is null"); //NOI18N + } else if (!this.userController.isRequiredPersonalDataSet()) { + // Not all required fields are set + throw new FaceletException("Not all required fields are set."); //NOI18N + } else if (this.userController.isUserNameRegistered(user)) { + // User name is already used + throw new FaceletException(new UserNameAlreadyRegisteredException(user)); + } else if (this.userController.isEmailAddressRegistered(user)) { + // Email address has already been taken + throw new FaceletException(new EmailAddressAlreadyRegisteredException(user)); + } else if (!this.userController.isSameEmailAddressEntered()) { + // Not same email address entered + throw new FaceletException(new DataRepeatMismatchException(MessageFormat.format("Email addresses not matching: {0} != {1}", this.userController.getEmailAddress(), this.userController.getEmailAddressRepeat()))); //NOI18N + } else if (!this.userController.isSamePasswordEntered()) { + // Not same password entered + throw new FaceletException(new DataRepeatMismatchException("Passwords not matching.")); //NOI18N + } + + // Encrypt password + String encryptedPassword = UserUtils.encryptPassword(this.userController.getUserPassword()); + + // Set it here + user.setUserEncryptedPassword(encryptedPassword); + + // For debugging/programming only: + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + + try { + // Call bean + User registeredUser = this.registerBean.registerUser(user); + + // Fire event + this.registeredEvent.fire(new RegisteredUserEvent(registeredUser)); + + // All fine, redirect to proper page + return "register_done"; //NOI18N + } catch (final UserNameAlreadyRegisteredException | EmailAddressAlreadyRegisteredException ex) { + // Continue to throw + throw new FaceletException(ex); + } + } + +} diff --git a/src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionController.java new file mode 100644 index 00000000..e9c960ff --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/beans/register/PizzaUserRegisterWebSessionController.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.pizzaapplication.beans.register; + +import java.io.Serializable; + +/** + * An interface for registration web controllers + *

+ * @author Roland Haeder + */ +public interface PizzaUserRegisterWebSessionController extends Serializable { + + /** + * Registers the user, if not found. Otherwise this method should throw an + * exception. + *

+ * @return Redirection target + */ + String doRegister (); + +} -- 2.39.5