/// <summary>
 /// delete an item in the data
 /// </summary>
 /// <param name="user"></param>
 public void DeleteItem(UserVO user)
 {
     for (int i = 0; i < Users.Count; i++)
     {
         if (Users[i].UserName.Equals(user.UserName))
         {
             Users.RemoveAt(i);
             break;
         }
     }
 }
        /// <summary>
        /// add a role to this user 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="role"></param>
        public void AddRoleToUser(UserVO user, RoleEnum role)
        {
            bool result = false;

            if (!DoesUserHaveRole(user, role))
            {
                for (int i = 0; i < Roles.Count; i++)
                {
                    if (Roles[i].UserName.Equals(user.UserName))
                    {
                        IList<RoleEnum> userRoles = Roles[i].Roles;
                        userRoles.Add(role);
                        result = true;
                        break;
                    }
                }
            }

            SendNotification(ApplicationFacade.ADD_ROLE_RESULT, result);
        }
        public void ClearForm()
        {
            if (!CheckAccess())
            {
                Dispatcher.BeginInvoke(new ClearFormDelegate(ClearForm));
                return;
            }

            m_user = null;
            m_roles = null;
            formGrid.DataContext = null;
            userRoles.ItemsSource = null;
            roleList.SelectedItem = RoleEnum.NONE_SELECTED;
            UpdateButtons();
        }
        public void ShowUser(UserVO user, IList<RoleEnum> roles)
        {
            if (!CheckAccess())
            {
                Dispatcher.BeginInvoke(new ShowUserDelegate(ShowUser), new object[] { user, roles });
                return;
            }

            if (user == null)
            {
                ClearForm();
            }
            else
            {
                m_user = user;
                m_roles = roles;
                formGrid.DataContext = user;
                userRoles.ItemsSource = roles;
                roleList.SelectedItem = RoleEnum.NONE_SELECTED;
                UpdateButtons();
            }
        }
 void userList_NewUser(object sender, EventArgs e)
 {
     UserVO user = new UserVO();
     SendNotification(ApplicationFacade.NEW_USER, user);
 }
        private void bUpdate_Click(object sender, RoutedEventArgs e)
        {
            m_user = new UserVO(userName.Text, firstName.Text, lastName.Text, email.Text, password.Password, (DeptEnum) department.SelectedItem);

            if (m_user.IsValid)
            {
                if (m_mode == UserFormMode.ADD)
                {
                    SetAddUser();
                }
                else
                {
                    SetUpdateUser();
                }
            }
        }
        public void ShowUser(UserVO user, UserFormMode mode)
        {
            if (!CheckAccess())
            {
                Dispatcher.BeginInvoke(new ShowUserDelegate(ShowUser), new object[] { user, mode });
                return;
            }

            m_mode = mode;

            if (user == null)
            {
                ClearForm();
            }
            else
            {
                m_user = user;
                formGrid.DataContext = user;
                firstName.Text = user.FirstName;
                lastName.Text = user.LastName;
                email.Text = user.Email;
                userName.Text = user.UserName;
                password.Password = confirmPassword.Password = user != null ? user.Password : "";
                department.SelectedItem = user.Department;
                firstName.Focus();
                UpdateButtons();
            }
        }
        public void ClearForm()
        {
            if (!CheckAccess())
            {
                Dispatcher.BeginInvoke(new ClearFormDelegate(ClearForm));
                return;
            }

            m_user = null;
            formGrid.DataContext = null;
            firstName.Text = lastName.Text = email.Text = userName.Text = "";
            password.Password = confirmPassword.Password = "";
            department.SelectedItem = DeptEnum.NONE_SELECTED;
            UpdateButtons();
        }
        // remove a role from the user
        public void RemoveRoleFromUser(UserVO user, RoleEnum role)
        {
            if (DoesUserHaveRole(user, role))
            {
                for (int i = 0; i < Roles.Count; i++)
                {
                    if (Roles[i].UserName.Equals(user.UserName))
                    {
                        IList<RoleEnum> userRoles = Roles[i].Roles;

                        foreach (RoleEnum curRole in userRoles)
                        {
                            if (curRole.Equals(role))
                            {
                                userRoles.Remove(role);
                                break;
                            }
                        }

                        break;
                    }
                }
            }
        }
        /// <summary>
        /// determine if the user has a given role
        /// </summary>
        /// <param name="user"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool DoesUserHaveRole(UserVO user, RoleEnum role)
        {
            bool hasRole = false;

            for (int i = 0; i < Roles.Count; i++)
            {
                if (Roles[i].UserName.Equals(user.UserName))
                {
                    IList<RoleEnum> userRoles = Roles[i].Roles;

                    foreach (RoleEnum curRole in userRoles)
                    {
                        if (curRole.Equals(role))
                        {
                            hasRole = true;
                            break;
                        }
                    }
                }
            }

            return hasRole;
        }
 /// <summary>
 /// update an item in the data
 /// </summary>
 /// <param name="user"></param>
 public void UpdateItem(UserVO user)
 {
     for (int i = 0; i < Users.Count; i++)
     {
         if (Users[i].UserName.Equals(user.UserName))
         {
             Users[i] = user;
             break;
         }
     }
 }
 /// <summary>
 /// add an item to the data
 /// </summary>
 /// <param name="user"></param>
 public void AddItem(UserVO user)
 {
     Users.Add(user);
 }