Package tk.vovanok.webapplicationexample.web

Source Code of tk.vovanok.webapplicationexample.web.UserRegistrationBean

package tk.vovanok.webapplicationexample.web;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import tk.vovanok.webapplicationexample.dao.UserDao;
import tk.vovanok.webapplicationexample.dao.UserDaoInterface;
import tk.vovanok.webapplicationexample.entities.User;

/**
*
* @author Vovan <vovanok1992 at gmail.com>
*/
@Named
@ViewScoped
public class UserRegistrationBean implements Serializable{
   
    private String login;
   
    private String password;
   
    private String passwordConfirm;
   
    @Inject
    UserDaoInterface userDao;

   
    void save(){
        User newUser = new User();
        newUser.setCreated(new Date());
        newUser.setLogin(login);
        newUser.setPassword(password);
        userDao.save(newUser);
    }
   
    boolean validate(){
        boolean error = false;
       
        if(this.login == null ||
           this.password == null ||
           this.passwordConfirm == null ){
           
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Поля пустые!",
                            "Contact admin."));
            return true;
        }
       
        if(!password.equals(passwordConfirm)){
            error = true;
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Пароли не совпадают!",
                            "Contact admin."));
        }
       
        if(userDao.exists(login)){
            error = true;
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Пользователь с таким именем уже существует!",
                            "Contact admin."));
        }
       
        return error;
    }
   
    public void processRegistrationRequest() {

        boolean error = validate();
        if (error) {
            return;
        }
       
        save();

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(UserRegistrationBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }
   

   
}
TOP

Related Classes of tk.vovanok.webapplicationexample.web.UserRegistrationBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.