/// <summary>
        /// Create new user
        /// </summary>
        /// <param name="model"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        public static FocusConstants.FocusResultCode CreateUser(UserViewModel model, HasanEntities database)
        {
            try
            {

                if (DoesUserNameExist(model.EMail, database))
                    return FocusConstants.FocusResultCode.DuplicateUser;

                User entity = new User();
                entity.UserTypeRef = model.UserTypeRef;
                entity.GroupCompanyRef = model.GroupCompanyRef;
                entity.CustomerRef = model.CustomerRef;
                entity.FactoryRef = model.FactoryRef;
                entity.PersonRef = model.PersonRef;
                entity.EmployeeRef = model.EmployeeRef;
                entity.EMail = model.EMail;
                entity.Password = model.Password;
                entity.PIN = model.PIN;
                entity.Name = model.Name;
                entity.Surname = model.Surname;
                entity.ChangePasswordAtNextLogon = model.ChangePasswordAtNextLogon;
                entity.MailNotification = model.MailNotification;
                entity.Note = model.Note;
                entity.Sort = model.Sort;
                entity.Active = model.Active;
                entity.Deleted = false;
                database.User.Add(entity);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("CreateUserType", ex);
                return FocusConstants.FocusResultCode.Exception;
            }
            return FocusConstants.FocusResultCode.Success;
        }
        /// <summary>
        /// Change Next login password
        /// </summary>
        /// <param name="PIN"></param>
        /// <param name="userId"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        public static ProfileResult ChangeNextLoginPassrord(string OldPassword, string NewPassword, string ConfirmPassrord, int userId, HasanEntities database)
        {
            ProfileResult res = new ProfileResult();

            if (string.IsNullOrEmpty(OldPassword))
            {
                res.ResponseCode = FocusConstants.FocusResultCode.OldPasswordEmpty;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.OldPasswordEmpty);
                return res;
            }
            if (string.IsNullOrEmpty(NewPassword))
            {
                res.ResponseCode = FocusConstants.FocusResultCode.NewPasswordEmpty;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.NewPasswordEmpty);
                return res;
            }
            if (string.IsNullOrEmpty(ConfirmPassrord))
            {
                res.ResponseCode = FocusConstants.FocusResultCode.ConfirmPasswordEmpty;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.ConfirmPasswordEmpty);
                return res;
            }
            else if (!IsMatchPassword(OldPassword, userId, database))
            {
                res.ResponseCode = FocusConstants.FocusResultCode.InvalidOldPassword;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.InvalidOldPassword);
                return res;
            }
            else if (NewPassword != ConfirmPassrord)
            {
                res.ResponseCode = FocusConstants.FocusResultCode.NewAndConfirmPasswordMissmatch;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.NewAndConfirmPasswordMissmatch);
                return res;
            }
            else
            {

                //Regex regex = new Regex(@"" + Core.GlobalProperty.FocusConfUserPasswordFormat);
                //Match match = regex.Match(NewPassword);
                //if (!match.Success)
                //{
                //    res.ResponseCode = FocusConstants.FocusResultCode.InvalidPasswordFormat;
                //    res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.InvalidPasswordFormat);
                //    return res;
                //}
            }
            try
            {
                User entity = database.User.Where(it => it.Id == userId).SingleOrDefault();
                entity.Password = NewPassword;
                entity.ChangePasswordAtNextLogon = false;
                database.SaveChanges();
                res.ResponseCode = FocusConstants.FocusResultCode.PasswordChangeSuccess;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.PasswordChangeSuccess);
                return res;
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("ChangePassword", ex);
                res.ResponseCode = FocusConstants.FocusResultCode.Exception;
                res.ResponseMessage = FocusMessage.GetResourceResultCodeValue(FocusConstants.FocusResultCode.Exception);
                return res;

            }
        }
        /// <summary>
        /// Update existing user
        /// </summary>
        /// <param name="model"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        public static FocusConstants.FocusResultCode UpdateUser(UserViewModel model, HasanEntities database)
        {
            try
            {
                User entity = database.User.Where(it => it.Id == model.Id).SingleOrDefault();
                entity.UserTypeRef = model.UserTypeRef;
                entity.GroupCompanyRef = model.GroupCompanyRef;
                entity.CustomerRef = model.CustomerRef;
                entity.FactoryRef = model.FactoryRef;
                entity.PersonRef = model.PersonRef;
                entity.EmployeeRef = model.EmployeeRef;
                entity.EMail = model.EMail;
                entity.Password = model.Password;
                entity.PIN = model.PIN;
                entity.Name = model.Name;
                entity.Surname = model.Surname;
                entity.ChangePasswordAtNextLogon = model.ChangePasswordAtNextLogon;
                entity.MailNotification = model.MailNotification;
                entity.Note = model.Note;
                entity.Sort = model.Sort;
                entity.Active = model.Active;
                entity.Deleted = model.Deleted;
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.ErrorException("UpdateUserType", ex);
                return FocusConstants.FocusResultCode.Exception;
            }

            return FocusConstants.FocusResultCode.Success;
        }
 /// <summary>
 /// Delete user
 /// </summary>
 /// <param name="model"></param>
 /// <param name="database"></param>
 /// <returns></returns>
 public static FocusConstants.FocusResultCode DeleteUser(UserViewModel model, HasanEntities database)
 {
     User entity = database.User.Where(it => it.Id == model.Id).SingleOrDefault();
     entity.Deleted = true;
     database.SaveChanges();
     return FocusConstants.FocusResultCode.Success;
 }