/*******************************************************************************
 * 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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
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.user.UserBean;
import com.n2bb.util.ActionConstants;
import com.tandbergtv.workflow.auth.Module;

public final class RoleForm extends ActionForm {

  /**
	 * 
	 */
	private static final long serialVersionUID = 119843652504351718L;

private Logger n2bbLog = Logger.getLogger(RoleForm.class);

  /**
   * The maintenance action we are performing (Add or Edit).
   */
  private String action = ActionConstants.add;
  private String n2RoleName;
  private String description;
  private String[] permissions = new String[0];
  private List<Module> modules;
  private Set<UserBean> users; 
  private String[] assignedUsers = new String[0];
  private Map<String, String>  permissionMap = new HashMap<String,String>();

  public RoleForm(){
	  
  }
  /**
   * Return the maintenance action.
   */
  public String getAction() {
    return (this.action);
  }
  
  public void setPermissionsMap(String key, String value){
	  permissionMap.put(key,value);
  }
  
  public String getPermissionsMap(String key){
	  return  permissionMap.get(key);
  }
  // Do not overload the below method with getPermissionsMap(String key)as
  // some reflection is not happening properly in the struts to get the map and getting empty map
    
  public Map<String,String> getPermissionMap(){
	  return permissionMap;
  }
  

  /**
   * Set the maintenance action.
   *
   * @param action The new maintenance action.
   */
  public void setAction(String action) {
    this.action = action;
  }

  public String getN2RoleName() {
    return n2RoleName == null ? "" : n2RoleName;
  }

  public String getDescription() {
    return description == null ? "" : description;
  }

  public String[] getPermissions() {
    return permissions;
  }

  public void setN2RoleName(String n2RoleName) {
    this.n2RoleName = n2RoleName;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public void setPermissions(String[] permissions) {
    this.permissions = permissions;
  }

  /**
   * Reset all properties to their default values.
   *
   * @param mapping The mapping used to select this instance
   * @param request The servlet request we are processing
   */
  public void reset(ActionMapping mapping, HttpServletRequest request) {
    this.action = ActionConstants.add;
    this.n2RoleName = null;
    this.description = null;
    this.permissions = new String[0];
  }

  /**
   * Validate the properties that have been set from this HTTP request,
   * and return an <code>ActionErrors</code> object that encapsulates any
   * validation errors that have been found.  If no errors are found, return
   * <code>null</code> or an <code>ActionErrors</code> object with no
   * recorded error messages.
   *
   * @param mapping The mapping used to select this instance
   * @param request The servlet request we are processing
   */
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

    ActionErrors errors = new ActionErrors();

    try {
      if (getN2RoleName().equals(""))
        errors.add("roleName", new ActionError("error.role.roleName.required"));
      
    }
    catch (Exception e) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.other.validation"));
      n2bbLog.error(e.getMessage(), e);
    }
    return errors;
  }

  public List<Module> getModules() {
		return modules;
	}

	public void setModules(List<Module> modules) {
		this.modules = modules;
	}

	public Set<UserBean> getUsers() {
		return users;
	}

	public void setUsers(Set<UserBean> users) {
		this.users = users;
	}
	public String[] getAssignedUsers() {
		return assignedUsers;
	}

	public void setAssignedUsers(String[] assignedUsers) {
		this.assignedUsers = assignedUsers;
	}
  

}


