/// <summary>
 /// Builds the display name.
 /// </summary>
 /// <param name="user">The user.</param>
 /// <returns>User display name</returns>
 private string BuildDisplayName(User user)
 {
     return (user.CommonName + GuiHelper.FieldSplitter + user.LoginName);
 }
 public static void Setup(User item) {
     SetTestRepo();
     _testRepo._items.Add(item);
 }
 public static void Setup(int testItems) {
     SetTestRepo();
     for(int i=0;i<testItems;i++){
         User item=new User();
         _testRepo._items.Add(item);
     }
 }
        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            // Name cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtName, Resources.MsgNameRequired))
            {                
                return;
            }

            // Login name cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtLoginName, Resources.MsgLoginNameRequired))
            {
                return;
            }

            // Password cannot be empty
            if (!FormHelper.ValidateNotEmpty(txtPassword, Resources.MsgPasswordRequired))
            {
                return;
            }

            if (this.User == null)
            {
                // Login name cannot be the built-in administrator name
                if (GuiHelper.AdministratorName.Equals(txtLoginName.Text, StringComparison.OrdinalIgnoreCase))
                {
                    FormHelper.ShowError(Resources.MsgAdminUserNameNotAllowed);
                    txtLoginName.Focus();
                    return;
                }
            }

            // Validate passwords are the same
            if (!txtPassword.Text.Equals(txtPasswordAgain.Text))
            {
                FormHelper.ShowError(Resources.MsgPasswordNotMatched);
                txtPassword.Focus();
                return;
            }

            using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    try
                    {
                        if (User == null)
                        {
                            // Login name must be unique
                            if (IsDuplicate())
                            {
                                return;
                            }

                            // Create a new one
                            User user = new User();
                            user.CommonName = txtName.Text;
                            user.Mobtel = txtPhoneNumber.Text;
                            user.Email = txtEmail.Text;
                            user.LoginName = txtLoginName.Text;
                            user.Password = txtPassword.Text;
                            if (user.LoginName.Equals(GuiHelper.AdministratorName))
                                user.CanBeDeleted = false;
                            else
                                user.CanBeDeleted = true;
                            user.Save();

                            this.User = user;
                        }
                        else
                        {
                            if (!this.User.LoginName.Equals(txtLoginName.Text.Trim(), StringComparison.OrdinalIgnoreCase))
                            {
                                if (IsDuplicate())
                                {
                                    return;
                                }
                            }

                            // Update an existing one
                            this.User.CommonName = txtName.Text;
                            this.User.Mobtel = txtPhoneNumber.Text;
                            this.User.Email = txtEmail.Text;
                            this.User.LoginName = txtLoginName.Text;
                            this.User.Password = txtPassword.Text;
                            if (this.User.LoginName.Equals(GuiHelper.AdministratorName))
                                this.User.CanBeDeleted = false;
                            else
                                this.User.CanBeDeleted = true;
                            this.User.Update();
                        }


                        // Delete groups for this users
                        UserRoleMap.Delete(urm => urm.UserId == this.User.Id);

                        // Save the groups for this user
                        foreach (object item in lstBelongedGroups.Items)
                        {
                            string groupName = item as string;

                            // Get the user id and save the user role mapping
                            Role role = Role.SingleOrDefault(r => r.Name == groupName);
                            if (role != null)
                            {
                                UserRoleMap urm = new UserRoleMap();
                                urm.UserId = this.User.Id;
                                urm.RoleId = role.Id;
                                urm.Save();
                            }
                        }

                        FormHelper.ShowInfo(Resources.MsgUserSaved);
                    }
                    catch (Exception ex)
                    {
                        FormHelper.ShowError(ex.Message);
                    }
                    finally
                    {
                        try
                        {
                            ts.Complete();
                        }
                        catch (Exception) { }
                    }
                }
            }

            this.DialogResult = DialogResult.OK;            
        }