public UserEditForm(Feature feature, UserDTO userDto)
 {
     InitializeComponent();
     _feature = feature;
     _selectedUser = userDto;
     InitializeControlStatus();
     InitializeData();
 }
        public List<UserDTO> SearchUsers(SearchUserDTO dto)
        {
            UserDTO user;
            List<UserDTO> list=new List<UserDTO>();
            try
            {
                SqlDataReader reader = ConnectionManager.GetCommand("sp0002",
                                                                    new Dictionary<string, SqlDbType>()
                                                                        {
                                                                            {"@username", SqlDbType.NVarChar},
                                                                            {"@status", SqlDbType.Int},
                                                                            {"@eMail", SqlDbType.NVarChar},
                                                                            {"@idsn", SqlDbType.NVarChar}
                                                                        },
                                                                    new List<object>()
                                                                        {
                                                                            dto.UserName,
                                                                            dto.Status,
                                                                            dto.Email,
                                                                            dto.Isdn
                                                                        }).ExecuteReader();

                while (reader.Read())
                {
                    user=new UserDTO();
                    user.UserId = reader["UserID"].ToString();
                    user.Username = reader["Username"].ToString();
                    //user.Password = reader["Password"].ToString();
                    user.RoleDescription = reader["RoleDescription"].ToString();
                    user.Birthday = (DateTime)reader["Birthday"];
                    user.Address = reader["Address"].ToString();
                    user.Phone = reader["Phone"].ToString();
                    user.Email = reader["Email"].ToString();
                    user.IDSN = reader["IDSN"].ToString();
                    user.IssuedDate = (DateTime)reader["IssuedDate"];
                    user.ExpiredDate = (DateTime)reader["ExpiredDate"];
                    int statusTemp;
                    int.TryParse(reader["Status"].ToString(), out statusTemp);
                    user.Status = (UserStatus)Enum.Parse(typeof(UserStatus), statusTemp.ToString());
                    list.Add(user);
                }

                reader.Close();
            }
            catch (Exception e)
            {
                Log.Error("Error at UserDAO - GetUserByID", e);
                return null;
            }
            return list;
        }
 public static void Error(UserDTO user, object message, Exception e)
 {
     // Insert action to db here
     var appType = Options.IsWeb ? "WEB" : "APP";
     user = user ?? (Options.User ?? ((HttpContext.Current == null ? null : HttpContext.Current.Session["USER"]) == null ? new UserDTO() { UserId = "Unknown" } : (UserDTO)HttpContext.Current.Session["USER"]));
     try
     {
         LogBUS bus = new LogBUS();
         bus.InsertLog(user.UserId, message.ToString(), appType + "/" + "ERROR");
     }
     catch (Exception ex)
     {
         Logger.Error("Error at Log - Error", ex);
     }            // Write to log incase of DB fail
     Logger.Error(message, e);
 }
        public UserDTO GetByUserId(string userId)
        {
            UserDTO user = null;

            try
            {
                SqlDataReader reader = ConnectionManager.GetCommand("SP0601ByUserID",
                                                                    new Dictionary<string, SqlDbType>()
                                                                    {
                                                                        {"@UserID", SqlDbType.NVarChar}
                                                                    },
                                                                    new List<object>()
                                                                    {
                                                                        userId
                                                                    }).ExecuteReader();

                if (reader.Read())
                {
                    user = new UserDTO();
                    user.UserId = reader["UserID"].ToString();
                    user.Username = reader["Username"].ToString();
                    user.Password = reader["Password"].ToString();
                    int roleIdTmp;
                    int.TryParse(reader["RoleID"].ToString(), out roleIdTmp);
                    user.RoleId = roleIdTmp;
                    user.Birthday = (DateTime)reader["Birthday"];
                    user.Address = reader["Address"].ToString();
                    user.Phone = reader["Phone"].ToString();
                    user.Email = reader["Email"].ToString();
                    user.IDSN = reader["IDSN"].ToString();
                    user.IssuedDate = (DateTime)reader["IssuedDate"];
                    user.ExpiredDate = (DateTime)reader["ExpiredDate"];
                    int statusTemp;
                    int.TryParse(reader["Status"].ToString(), out statusTemp);
                    user.Status = (UserStatus)Enum.Parse(typeof(UserStatus), statusTemp.ToString());
                    user.CreatedDate = (DateTime)reader["CreatedDate"];
                    user.UpdatedDate = (DateTime)reader["UpdatedDate"];
                }
            }
            catch (Exception e)
            {
                Log.Error("Error at UserDAO - GetByUserName", e);
                return null;
            }

            return user;
        }
 public bool UpdateUser(UserDTO user)
 {
     return _userDao.Update(user);
 }
 public bool InsertUser(UserDTO user)
 {
     return _userDao.Insert(user);
 }
 public bool UpdateUser(UserDTO userDto)
 {
     Log.Info("Update user " + userDto.UserId + " info");
     try
     {
         return userBus.UpdateUser(userDto);
     }
     catch (Exception e)
     {
         Log.Error("Error at Feature - UpdateUser", e);
     }
     return false;
 }
 public bool ResetPassword(UserDTO userDto)
 {
     try
     {
         var pass = RandomString(8);
         userDto.Password = EncodePassword(pass);
         if (userBus.UpdateUser(userDto))
         {
             SendSampleMail(userDto.Email, Options.MailSubject, "Mật mã của bạn đã được thiết lập lại.<br/>Mật mã mới của bạn là <b>" + pass + "</b>");
         }
         return true;
     }
     catch (Exception e)
     {
         Log.Error("Error at Feature - ResetPassword", e);
     }
     return false;
 }
        public List<RentalDTO> GetRentalByUserIdPaging(UserDTO user, int page, out int NoP)
        {
            var listRental = new List<RentalDTO>();

            try
            {
                // get Rental info
                listRental = rentalBus.GetRentalByUserIdPaging(user.UserId, page, out NoP);

                foreach (var rentalDto in listRental)
                {
                    rentalDto.Username = user.Username;

                    // get Catalogue info
                    string isbn = copyBus.GetCopyById(rentalDto.Barcode).ISBN;
                    if (isbn != null)
                    {
                        var catalogeDto = catalogeBus.GetCatalogueById(isbn);
                        if (catalogeDto != null)
                        {
                            rentalDto.BookTitle = catalogeDto.Title;
                            rentalDto.BookPrice = catalogeDto.Price;
                            rentalDto.ExpandLimit = catalogeDto.ExpandLimit;
                            rentalDto.ExpandDateLimit = catalogeDto.ExpandDateLimit;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(user, "Error at Feature - GetRentalByUserId", e);
                NoP = 0;
            }

            return listRental;
        }
        public List<BookRegisterDTO> GetRegisterByUserIdPaging(UserDTO user, int page, out int noP)
        {
            Log.Info("View Register entries");
            var listRegister = new List<BookRegisterDTO>();

            try
            {
                listRegister = registerBus.GetBookRegisterByUserIdPaging(user.UserId, page, out noP);

                foreach (var registerDto in listRegister)
                {
                    registerDto.Username = user.Username;
                    registerDto.BookTitle = catalogeBus.GetCatalogueById(registerDto.ISBN).Title;
                }
            }
            catch (Exception e)
            {
                Log.Error(user, "Error at Feature - GetRegisterByUserId", e);
                noP = 0;
            }

            return listRegister;
        }
 public bool ChangePassword(UserDTO userDto, string newPassword)
 {
     try
     {
         userDto.Password = newPassword;
         if (userBus.UpdateUser(userDto))
         {
             SendSampleMail(userDto.Email, Options.MailSubject, "Mật mã của bạn đã được thiết lập lại.<br/>");
         }
         return true;
     }
     catch (Exception e)
     {
         Log.Error("Error at Feature - ResetPassword", e);
     }
     return false;
 }
 public bool AddUser(UserDTO userDto)
 {
     Log.Info("Add New User " + userDto.UserId);
     var pass = RandomString(8);
     userDto.Password = EncodePassword(pass);
     try
     {
         if (userBus.InsertUser(userDto))
         {
             SendSampleMail(userDto.Email, Options.MailSubject, "Chào " + userDto.Username + ", <br/><br/>Tài khoản của bạn đã được tạo.<br/>Tên đăng nhập của bạn <b>" + userDto.UserId + "</b><br/>Mật mã của bạn là <b>" + pass + "</b>");
         }
         return true;
     }
     catch (Exception e)
     {
         Log.Error("Error at Feature - AddUser", e);
     }
     return false;
 }
        private void BtnSaveClick(object sender, EventArgs e)
        {
            string msg = ValidateData();

            if (msg != null)
            {
                MessageBox.Show(this, msg, Constants.SYSTEM_INFO,
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            bool result = false;

            // update
            if (_selectedUser != null)
            {
                _selectedUser.Username = txtUsername.Text;
                _selectedUser.RoleId = (int)cboRole.SelectedValue;
                _selectedUser.IDSN = txtIDSN.Text;
                _selectedUser.Phone = txtPhone.Text;
                _selectedUser.Email = txtEmail.Text;
                _selectedUser.Address = txtAddress.Text;
                _selectedUser.Birthday = dteBirthday.Value;
                _selectedUser.ExpiredDate = dteExpireDate.Value;
                _selectedUser.Status = (UserStatus)EnumHelper.Parse(typeof(UserStatus), cboStatus.Text);
                result = _feature.UpdateUser(_selectedUser);

                if (result)
                {
                    MessageBox.Show(this, Constants.USER_EDIT_OK, Constants.SYSTEM_INFO,
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

                // insert
            else
            {
                var userDto = new UserDTO();
                userDto.UserId = txtUserID.Text;
                userDto.Username = txtUsername.Text;
                userDto.Password = txtUserID.Text;
                userDto.RoleId = (int)cboRole.SelectedValue;
                userDto.IDSN = txtIDSN.Text;
                userDto.Phone = txtPhone.Text;
                userDto.Email = txtEmail.Text;
                userDto.Address = txtAddress.Text;
                userDto.Birthday = dteBirthday.Value;
                userDto.IssuedDate = dteIssueDate.Value;
                userDto.ExpiredDate = dteExpireDate.Value;
                userDto.Status = (UserStatus)EnumHelper.Parse(typeof(UserStatus), cboStatus.Text);
                result = _feature.AddUser(userDto);

                if (result)
                {
                    //_feature.SendSampleMail("*****@*****.**");
                    MessageBox.Show(this, Constants.USER_INSERT_OK, Constants.SYSTEM_INFO,
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            if (!result)
            {
                MessageBox.Show(this, Constants.SYSTEM_ERROR, Constants.SYSTEM_INFO,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Close();
        }
        public bool Update(UserDTO user)
        {
            try
            {
                user.UpdatedDate = DateTime.Now;
                ConnectionManager.GetCommand("SP0603",
                                             new Dictionary<string, SqlDbType>()
                                                 {
                                                     {"@UserID", SqlDbType.NVarChar},
                                                     {"@Username", SqlDbType.NVarChar},
                                                     {"@Password", SqlDbType.NVarChar},
                                                     {"@RoleID", SqlDbType.Int},
                                                     {"@Birthday", SqlDbType.Date},
                                                     {"@Address", SqlDbType.NVarChar},
                                                     {"@Phone", SqlDbType.NVarChar},
                                                     {"@Email", SqlDbType.NVarChar},
                                                     {"@IDSN", SqlDbType.NVarChar},
                                                     {"@IssuedDate", SqlDbType.DateTime},
                                                     {"@ExpiredDate", SqlDbType.DateTime},
                                                     {"@Status", SqlDbType.Int},
                                                     {"@CreatedDate", SqlDbType.DateTime},
                                                     {"@UpdatedDate", SqlDbType.DateTime}
                                                 },
                                             new List<object>()
                                                 {
                                                     user.UserId,
                                                     user.Username,
                                                     user.Password,
                                                     user.RoleId,
                                                     user.Birthday,
                                                     user.Address,
                                                     user.Phone,
                                                     user.Email,
                                                     user.IDSN,
                                                     user.IssuedDate,
                                                     user.ExpiredDate,
                                                     user.Status,
                                                     user.CreatedDate,
                                                     user.UpdatedDate
                                                 }).ExecuteReader();

                return true;
            }
            catch (Exception e)
            {
                Log.Error("Error at UserDAO - Update", e);
                return false;
            }
        }