/*******************************************************************************
 * Copyright (c), 2001, 2002 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Author:  Drake H. Henderson
 * Created:  11-12-01
 *
 ******************************************************************************/
package com.n2bb.security;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.n2bb.util.ActionConstants;
import com.n2bb.util.N2bbException;


public class ChangePwdForm extends ActionForm {
  
  private Log n2bbLog = LogFactory.getLog(ChangePwdForm.class);
  
  private String action = ActionConstants.add;
  private String password = null;
  private String confirmPassword = null;
  
  public String getAction() {
    return (this.action);
  }

  public String getPassword() {
    return password == null ? "" : password;
  }

  public String getConfirmPassword() {
    return confirmPassword == null ? "" : confirmPassword;
  }

  public void setAction(String action) {
    this.action = action;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void setConfirmPassword(String confirmPassword) {
    this.confirmPassword = confirmPassword;
  }

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

    ActionErrors errors = new ActionErrors();
    try {

      // password
      if (getPassword().equals(""))
        errors.add("password", new ActionError("error.user.passwordRequired"));

      if (getPassword().length() < 6 || getPassword().length() > 16)
        errors.add("password", new ActionError("error.user.passwordLength"));

      if (getPassword().equals(request.getRemoteUser()))
        errors.add("password", new ActionError("error.user.passwordUserName"));

      // confirm password
      if (getConfirmPassword().equals(""))
        errors.add("confirmPassword", new ActionError("error.user.confirmPasswordRequired"));

      if (!getPassword().equals(getConfirmPassword()))
        errors.add("confirmPassword", new ActionError("error.user.confirmPasswordPassword"));
          
    } catch (Exception e) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.other.validation"));
      n2bbLog.error(e.getMessage(), e);
    }

    return errors;
  }
}


