Package at.fhj.itm.beans

Source Code of at.fhj.itm.beans.Registration

package at.fhj.itm.beans;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.apache.log4j.Logger;

import properties.PropertyManager;
import at.fhj.itm.business.ServiceAssembler;
import at.fhj.itm.business.ServiceException;
import at.fhj.itm.business.ServiceUser;
import at.fhj.itm.util.JsfUtil;

/**
* Bean to manage the registration.
* @author Alfort Robert<br />
*   last modification by Manuel Seidl & Hans-Joerg Tagger, Jan. 14, 2011
* @version: 1.4
*/

@ManagedBean
@SessionScoped
public class Registration {
      private final Logger logger = Logger.getLogger(Registration.class);
      private final JsfUtil jsfUtil;
      private final ServiceUser serviceUser;
     
  private String fName;
  private String lName;
  private String eMail;
  private String eMailVerification;
  private String userName;
  private String password;
  private String password2;
  private String phone;
  private int zip;
  private String city;
  private String[] badNames = {"admin","root", "chef", "boss", "administrator"};

  private InputValidator validator = new InputValidator();
 
  protected PropertyManager msg = PropertyManager.getInstance();

 
  public Registration()
  {
      this.serviceUser = ServiceAssembler.getInstance().createServiceUser();
      this.jsfUtil = ServiceAssembler.getInstance().createJsfUtil();
  }

  /*
   * Basic Getter Setter methods
   */
  /**
   * @return the serviceUser
   */
  public ServiceUser getServiceUser() {
    return serviceUser;
  }
 
  /**
  * @return the jsfUtils
  */
  public JsfUtil getJsfUtil()
  {
      return jsfUtil;
  }

  /**
   * <strong> getfName() </strong> returns the firstname
   * @return String firstname
   */
  public String getfName() {
    return fName;
  }

  /**
   * This method is used to set the Firstname
   * Validation is done by other methode's
   * @param fName
   */
  public void setfName(String fName) {
//    if (fName == null)
//      throw new NullPointerException("Firstname can't be null");
    this.fName = fName;
  }

  /**
   * <strong> getlName() </strong> returns the lastname
   * @return String lastname
   */
  public String getlName() {
    return lName;
  }

  /**
   * This method is used to set the Lastname
   * Validation is done by other methode's
   * @param lName
   */
  public void setlName(String lName) {
//    if (lName == null)
//      throw new NullPointerException("Lastname can't be null");
    this.lName = lName;
  }

  /**
   * Returns Mail address
   * @return String eMail
   */
  public String geteMail() {
    return eMail;
  }

  /**
   * Set's the Email address
   * @param eMail
   */
  public void seteMail(String eMail) {
//    if (!validator.isEmailAddressValid(eMail))
//      throw new NullPointerException("Email can't be null");
//    else if (!uniqueCheck.isEmailUnique(eMail))
//      throw new NullPointerException("Email allready registred!");
    this.eMail = eMail.trim();
  }

  /**
   * Sets the Verification Email.
   * Is used to compair both Mail addresses
   * @param eMailVerification
   */
  public void seteMailVerification(String eMailVerification) {
//    if (!validator.isEmailAddressValid(eMailVerification))
//      throw new NullPointerException("Email can't be null");
//    else if (!eMailVerification.equals(geteMail()))
//      throw new NullPointerException("Email don't match");
    this.eMailVerification = eMailVerification.trim();
  }

  /**
   * Returns the Verification Mail
   * @return String eMailVerification
   */
  public String geteMailVerification() {
    return eMailVerification;
  }

  /**
   * Returns the Username
   * @return String userName
   */
  public String getUserName() {
    return userName;
  }

  /**
   * returns the Password
   * @return String password
   */
  public String getPassword() {
    return password;
  }

  /**
   * Is used to set the Password
   * @param String password
   */
  public void setPassword(String password) {
//    if (password == null)
//      throw new NullPointerException("Password can't be null");
    this.password = password.trim();
  }

  /**
   * Is used to get Password2 (Re-Entered Password)
   * @return password
   */
  public String getPassword2() {
    return password2;
  }

  /**
   * Set's the Re-Entered Password for Validation
   * @param String password2
   */
  public void setPassword2(String password2) {
//    if (password2 == null)
//      throw new NullPointerException("Password reentered can't be null");
//    else if (!password2.equals(getPassword()))
//      throw new NullPointerException("Password not equals");
    this.password2 = password2.trim();
  }

  /**
   * Set's the Username
   * @param String userName
   */
  public void setUserName(String userName) {
//    if (userName == null)
//      throw new NullPointerException("Username can't be null");
    // else if (!uniqueCheck.isUnameUnique(userName))
    // throw new NullPointerException("Username allready exist!");
    this.userName = userName.trim();
  }

  /**
   * Returns the Phone number
   * @return String phone
   */
  public String getPhone() {
    return phone;
  }

  /**
   * Set's the Phone number
   * @param String phone
   */
  public void setPhone(String phone) {
//    if (validator.isPhoneNumberValid(phone)
//        || validator.isMobilePhoneNumberValid(phone))
      this.phone = phone;
  }

  /**
   * Return's the City
   * @return String city
   */
  public String getCity() {
    return city;
  }

  /**
   * Set's the City
   * @param String city
   */
  public void setCity(String city) {
//    if (!validator.isCityValid(city))
//      throw new NullPointerException("City can't be null");
    this.city = city;
  }

  /**
   * Returns the Zip-Code
   * @return int zip
   */
  public int getZip() {
    return zip;
  }

  /**
   * Set's the Zip Code
   * @param int zip
   */
  public void setZip(int zip) {
//    if (!validator.isZipCodeValid(zip))
//      throw new NullPointerException("Zip can't be null");
    this.zip = zip;
  }

  /*
   * End Getter/Setter Methodes
   */
 
  public void setBadNames(String[] badNames) {
   
    this.badNames = badNames;
  }

  public String[] getBadNames() {
    return badNames;
  }

  /**
   * JSF Validator - checker method
   * Checks following Items:
   *     <p>+ Username (Unique and not Null)</p>
   *     <p>+ City (is Valid)</p>
   *     <p>+ Zip (is Valid and not Null and not 0)</p>
   *     <p>+ eMail (is Unique and is Valid)</p>
   *     <p>+ fName (not Null)</p>
   *     <p>+ lName (not Null)</p>
   * Returns Errorcode
   * @param fc - FacesContext
   * @param component - UIComponent
   * @param value - Object
   * @return validatorMessage(fc, value, id, errorMSG, withValue)
   */

  public void checker(FacesContext fc, UIComponent component, Object value)
      throws ValidatorException {
   
    String lang = fc.getViewRoot().getLocale().getLanguage();
   
    if (component.getId().equals("userName")) {
      String user = (String) value;
      try{
            if (this.getServiceUser().checkUsername(user) == true)
                validatorMessage(fc, value, getI18NString(lang,"reg_username"), getI18NString(lang,"validator_allreadyExists"), true);
      }catch (ServiceException e) {
          logger.error("Error in Database access", e);
          try
          {
        this.getJsfUtil().redirect("./errorDatabase.jsf");
          } catch (IOException e1)
          {
        logger.error("Error while redirecting to database error page!", e1);
          }
      }
      if (user == null)
        validatorMessage(fc, value, getI18NString(lang,"reg_username"), getI18NString(lang,"validator_cantBeNull"), true);
      if (user.length() < 5)
        validatorMessage(fc, value, getI18NString(lang,"reg_username"), getI18NString(lang,"validator_tooShort"), true);
      for (String bad : badNames) {
        if (user.equals(bad)) {
          validatorMessage(fc, value, getI18NString(lang,"reg_username"), getI18NString(lang,"validator_isRestricted"), true);
        }
      }
    }
    if (component.getId().equals("city")) {
      if (!validator.isCityValid((String) value))
        validatorMessage(fc, value, getI18NString(lang,"reg_city"), getI18NString(lang,"validator_notAllowed"), true);
    }
    if (component.getId().equals("fname")) {
      if ((String) value == null)
        validatorMessage(fc, value, getI18NString(lang,"reg_firstname"), getI18NString(lang,"validator_cantBeNull"), true);
      if (!validator.isStreetNameValid((String) value))
        validatorMessage(fc, value, getI18NString(lang,"reg_firstname"), getI18NString(lang,"validator_notAllowed"), true);
    }
    if (component.getId().equals("lname")) {
      if ((String) value == null)
        validatorMessage(fc, value, getI18NString(lang,"reg_lastname"), getI18NString(lang,"validator_cantBeNull"), true);
      if (!validator.isStreetNameValid((String) value))
        validatorMessage(fc, value, getI18NString(lang,"reg_lastname"), getI18NString(lang,"validator_notAllowed"), true);
    }
    if (component.getId().equals("eMail")) {
          String email = (String) value;
          try{
              if (this.getServiceUser().checkMail(email) == true)
        validatorMessage(fc, value, getI18NString(lang,"reg_email"), getI18NString(lang,"validator_allreadyExists"), true);
          }catch (ServiceException e) {
              logger.error("Error in Database access", e);
          try
          {
        this.getJsfUtil().redirect("./errorDatabase.jsf");
          } catch (IOException e1)
          {
        logger.error("Error while redirecting to database error page!", e1);
          }
      }
      if (!validator.isEmailAddressValid((String) value))
        validatorMessage(fc, value, getI18NString(lang,"reg_email"), getI18NString(lang,"validator_notValid"), true);
    }
    if (component.getId().equals("zip")) {
      if (!validator.isZipCodeValid((int)(Integer)value))
        validatorMessage(fc, value, getI18NString(lang,"reg_zip"), getI18NString(lang,"validator_notAllowed"), true);
    }
  }
 
  protected String getI18NString(String lang, String prop) {
    String value = "";
    if (lang.equals("de"))
      value = PropertyManager.getInstance().getProperty("MyMessageBundle",prop);
    if (lang.equals("en"))
      value = PropertyManager.getInstance().getProperty("MyMessageBundle_en_US",prop);
   
    return value;
  }

  /**
   * Extracted Method, because every Errormsg uses the same Source.
   * Create a Error-Message + Summary for logging
   * This Methode gets 3 additional properties.
   * <p> String id - Used to declare which Item throws the Error (for error msg) </p>
   * <p> String errorMSG - Defines the spez. Error-Message</p>
   * <p> boolean withValue - if True, the Value of the item will be displayed, else not</p>
   * @param fc
   * @param value
   * @param id
   * @param errorMSG
   * @param withValue
   * @return ValidatorException(message)
   */
  private void validatorMessage(FacesContext fc, Object value, String id, String errorMSG, boolean withValue) {
    FacesMessage message = new FacesMessage();
    message.setSeverity(FacesMessage.SEVERITY_ERROR);
    message.setSummary(id+" "+errorMSG);
    if (withValue) {
      message.setDetail(id+": " + value.toString()
          + " "+errorMSG);
    } else {
      message.setDetail(id+" "+errorMSG);
    }
//    fc.addMessage("registration:"+id, message);
    throw new ValidatorException(message);
  }
 
  protected void unsetAll() {
    setfName("");
    setlName("");
    setUserName("");
    setPassword("");
    setPassword2("");
    seteMail("");
    seteMailVerification("");
    setPhone("");
    setCity("");
    setZip(0);
  }
  /**
   * Calls the doRegistration method of the service layer to register a user
   * by the data input into the registration form.
   * @return "regSuccessful" if registration was successful,
   * "errorDatabase" in case of an ServiceExeption during the registration procedure
   */
  public String regButtonClicked() {

    try{
        this.getServiceUser().doRegistration(this.getfName(), this.getlName(), this.getUserName(),
          this.getPassword(), this.geteMail(), this.getPhone(), this.getZip(), this.getCity());
    }catch (ServiceException e) {
        logger.error("Error in Database access", e);
        return "errorDatabase";
    }
   
    unsetAll();
    return "regSuccessfull";
  }
}
TOP

Related Classes of at.fhj.itm.beans.Registration

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.