| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 | using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Linq;using System.Text.RegularExpressions;using System.Windows.Controls;using System.Windows.Input;using Aitex.Core.RT.Log;using MECF.Framework.UI.Client.CenterViews.Configs.Accounts;using MECF.Framework.UI.Client.ClientBase;using OpenSEMI.ClientBase;using OpenSEMI.ClientBase.Command;namespace FurnaceUI.Views.Configs{    public class AccountViewModel : BaseModel    {        #region Property        public bool IsPermission { get => this.Permission == 3; }        private AccountItem _treeSelectedAccount = null;        public AccountItem TreeSelectedAccount        {            get { return _treeSelectedAccount; }            set { _treeSelectedAccount = value; this.NotifyOfPropertyChange("TreeSelectedAccount"); }        }        private CtrlMode _ControlMode = CtrlMode.VIEW;        public CtrlMode ControlMode        {            get { return _ControlMode; }            set { _ControlMode = value; NotifyOfPropertyChange("ControlMode"); }        }        private ObservableCollection<AccountItem> _AccountsList = new ObservableCollection<AccountItem>();        public ObservableCollection<AccountItem> AccountList        {            get { return _AccountsList; }        }        public AccountManager AccountManager        {            get { return AccountManager.Instance; }        }        private PasswordBox NewPasswordBox;        private PasswordBox ConfirmPasswordBox;        #region command define        private ICommand _BtnSaveAccountCommand;        public ICommand BtnSaveAccountCommand        {            get            {                if (this._BtnSaveAccountCommand == null)                    this._BtnSaveAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnSaveAccountCommand(arg));                return this._BtnSaveAccountCommand;            }        }        private ICommand _BtnAddAccountCommand;        public ICommand BtnAddAccountCommand        {            get            {                if (this._BtnAddAccountCommand == null)                    this._BtnAddAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnAddAccountCommand(arg));                return this._BtnAddAccountCommand;            }        }        private ICommand _BtnCloneAccountCommand;        public ICommand BtnCloneAccountCommand        {            get            {                if (this._BtnCloneAccountCommand == null)                    this._BtnCloneAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnCloneAccountCommand(arg));                return this._BtnCloneAccountCommand;            }        }        private ICommand _BtnDeleteAccountCommand;        public ICommand BtnDeleteAccountCommand        {            get            {                if (this._BtnDeleteAccountCommand == null)                    this._BtnDeleteAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnDeleteAccountCommand(arg));                return this._BtnDeleteAccountCommand;            }        }        private ICommand _BtnCancelAccountCommand;        public ICommand BtnCancelAccountCommand        {            get            {                if (this._BtnCancelAccountCommand == null)                    this._BtnCancelAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnCancelAccountCommand(arg));                return this._BtnCancelAccountCommand;            }        }        #endregion        #endregion        public AccountViewModel()        {            this.DisplayName = "Account";        }        protected override void OnViewLoaded(object view)        {            base.OnViewLoaded(view);            AccountView av = view as AccountView;            NewPasswordBox = av.pwNewPassword;            ConfirmPasswordBox = av.pwConfirmPassword;        }        protected override void OnActivate()        {            AccountManager.Initialize();            RefreshAccountList();        }        protected override void OnDeactivate(bool close)        {            if (ControlMode == CtrlMode.EDIT && IsPermission)            {                if (DialogBox.Confirm("The data has been modified. Do you want to save the change(s)?"))                {                    if (SaveChanged())                    {                        ControlMode = CtrlMode.VIEW;                        DialogBox.ShowInfo("Operated successfully.");                    }                }            }            base.OnDeactivate(close);        }        #region Function        private void RefreshAccountList()        {            _AccountsList.Clear();            _treeSelectedAccount = null;            List<AccountItem> Accounts = AccountManager.GetAllAccounts();            if (Accounts == null || Accounts.Count == 0) return;            foreach (AccountItem Acc in Accounts)            {                AccountItem treeAccount = AccountManager.CloneAccount(Acc);                if (treeAccount != null)                {                    if (treeAccount.AccountName == BaseApp.Instance.UserContext.LoginName)                    {                        treeAccount.IsEnableChangeAccountName = false;                    }                    _AccountsList.Add(treeAccount);                }            }            TreeSelectedAccount = _AccountsList.FirstOrDefault();            TreeSelectedAccount.IsSelected = true;            ControlMode = CtrlMode.VIEW;        }        public void OnAccountChanged()        {            if (ControlMode == CtrlMode.EDIT)                return;            //check account to set the mode from view to edit            if (_treeSelectedAccount != null && _treeSelectedAccount.IsAccountChanged())                ControlMode = CtrlMode.EDIT;        }        private bool SaveChanged()        {            if (string.IsNullOrWhiteSpace(TreeSelectedAccount.DisplayAccountName))            {                DialogBox.ShowWarning("{0} cannot be empty.", "Account name");                //TreeSelectedAccount.DisplayAccountName = "NewUser";                return false;            }            if (IsAccountExists(TreeSelectedAccount))            {                DialogBox.ShowWarning("{0} already exists.", "Account");                return false;            }            TreeSelectedAccount.NewPassword = NewPasswordBox.Password;            TreeSelectedAccount.ConfirmPassword = ConfirmPasswordBox.Password;            if (string.IsNullOrWhiteSpace(TreeSelectedAccount.NewPassword) ||                string.IsNullOrWhiteSpace(TreeSelectedAccount.ConfirmPassword))            {                DialogBox.ShowWarning("{0} cannot be empty.", "The password");                return false;            }            if (TreeSelectedAccount.NewPassword != TreeSelectedAccount.ConfirmPassword)            {                DialogBox.ShowWarning("The password does not match.");                return false;            }            if (!string.IsNullOrEmpty(TreeSelectedAccount.DisplayEmail))            {                string reg = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";                Regex r = new Regex(reg);                if (!r.IsMatch(TreeSelectedAccount.DisplayEmail))                {                    DialogBox.ShowWarning("The email is invalid.");                    return false;                }            }            TreeSelectedAccount.AccountName = TreeSelectedAccount.DisplayAccountName;            TreeSelectedAccount.FirstName = TreeSelectedAccount.DisplayFirstName;            TreeSelectedAccount.LastName = TreeSelectedAccount.DisplayLastName;            TreeSelectedAccount.Email = TreeSelectedAccount.DisplayEmail;            TreeSelectedAccount.Description = TreeSelectedAccount.DisplayDescription;            TreeSelectedAccount.AccountTextSaved = TreeSelectedAccount.FirstNameTextSaved =                TreeSelectedAccount.LastNameTextSaved = TreeSelectedAccount.EmailTextSaved = true;            bool isRoleSelected = false;            foreach (RoleStatusItem entity in TreeSelectedAccount.RoleColleciton)            {                if (entity.DisplayRoleStatus)                {                    isRoleSelected = true;                }                entity.RoleStatus = entity.DisplayRoleStatus;            }            if (!isRoleSelected)            {                DialogBox.ShowWarning("Please set role information for this account.");                return false;            }            try            {                AccountManager.SaveAccount(TreeSelectedAccount);            }            catch (System.Exception ex)            {                LOG.Write(ex);                return false;            }            NewPasswordBox.Clear();            ConfirmPasswordBox.Clear();            return true;        }        private bool IsAccountExists(AccountItem account)        {            if (AccountList == null || AccountList.Count == 0)                return false;            var sameNameList = AccountList.Where(t => t.DisplayAccountName == account.DisplayAccountName);            if (sameNameList == null || sameNameList.Count() <= 1)                return false;            return true;        }        private void OnBtnAddAccountCommand(Object arg)        {            AccountItem newAccount = AccountManager.CreateAccount();            if (newAccount != null)            {                _AccountsList.Add(newAccount);                TreeSelectedAccount = newAccount;                TreeSelectedAccount.IsSelected = true;            }            ControlMode = CtrlMode.EDIT;        }        private void OnBtnDeleteAccountCommand(Object arg)        {            if (_treeSelectedAccount == null) return;            if (!DialogBox.Confirm("Are you sure that you want to delete this account?"))            {                return;            }            if (BaseApp.Instance.UserContext.LoginName == _treeSelectedAccount.AccountName)            {                DialogBox.ShowWarning("The action cannot be completed because {0} is currently in use.", "the account");                return;            }            AccountManager.DeleteAccount(TreeSelectedAccount.AccountID);            RefreshAccountList();        }        private void OnBtnCloneAccountCommand(Object arg)        {            if (_treeSelectedAccount != null)            {                AccountItem newAccount = AccountManager.CreateAccount(_treeSelectedAccount);                if (newAccount != null)                {                    newAccount.DisplayAccountName = newAccount.AccountName = "Copy of " + newAccount.DisplayAccountName;                    _AccountsList.Add(newAccount);                    TreeSelectedAccount = newAccount;                    TreeSelectedAccount.IsSelected = true;                    ControlMode = CtrlMode.EDIT;                }            }        }        private void OnBtnSaveAccountCommand(Object arg)        {            if (!TreeSelectedAccount.IsValid)            {                DialogBox.ShowWarning("Input error.");                return;            }            if (SaveChanged())            {                ControlMode = CtrlMode.VIEW;                DialogBox.ShowInfo("Operated successfully.");            }            else                DialogBox.ShowInfo("Operation failed.");        }        private void OnBtnCancelAccountCommand(Object arg)        {            RefreshAccountList();            NewPasswordBox.Clear();            ConfirmPasswordBox.Clear();            ControlMode = CtrlMode.VIEW;        }        #endregion            }}
 |