AccountViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using Aitex.Core.RT.Log;
  9. using MECF.Framework.UI.Client.ClientBase;
  10. using OpenSEMI.ClientBase;
  11. using OpenSEMI.ClientBase.Command;
  12. namespace MECF.Framework.UI.Client.CenterViews.Configs.Accounts
  13. {
  14. public class AccountViewModel : BaseModel
  15. {
  16. #region Property
  17. public bool IsPermission { get => this.Permission == 3; }
  18. private AccountItem _treeSelectedAccount = null;
  19. public AccountItem TreeSelectedAccount
  20. {
  21. get { return _treeSelectedAccount; }
  22. set { _treeSelectedAccount = value; this.NotifyOfPropertyChange("TreeSelectedAccount"); }
  23. }
  24. private CtrlMode _ControlMode = CtrlMode.VIEW;
  25. public CtrlMode ControlMode
  26. {
  27. get { return _ControlMode; }
  28. set { _ControlMode = value; NotifyOfPropertyChange("ControlMode"); }
  29. }
  30. private ObservableCollection<AccountItem> _AccountsList = new ObservableCollection<AccountItem>();
  31. public ObservableCollection<AccountItem> AccountList
  32. {
  33. get { return _AccountsList; }
  34. }
  35. public AccountManager AccountManager
  36. {
  37. get { return AccountManager.Instance; }
  38. }
  39. private PasswordBox NewPasswordBox;
  40. private PasswordBox ConfirmPasswordBox;
  41. #region command define
  42. private ICommand _BtnSaveAccountCommand;
  43. public ICommand BtnSaveAccountCommand
  44. {
  45. get
  46. {
  47. if (this._BtnSaveAccountCommand == null)
  48. this._BtnSaveAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnSaveAccountCommand(arg));
  49. return this._BtnSaveAccountCommand;
  50. }
  51. }
  52. private ICommand _BtnAddAccountCommand;
  53. public ICommand BtnAddAccountCommand
  54. {
  55. get
  56. {
  57. if (this._BtnAddAccountCommand == null)
  58. this._BtnAddAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnAddAccountCommand(arg));
  59. return this._BtnAddAccountCommand;
  60. }
  61. }
  62. private ICommand _BtnCloneAccountCommand;
  63. public ICommand BtnCloneAccountCommand
  64. {
  65. get
  66. {
  67. if (this._BtnCloneAccountCommand == null)
  68. this._BtnCloneAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnCloneAccountCommand(arg));
  69. return this._BtnCloneAccountCommand;
  70. }
  71. }
  72. private ICommand _BtnDeleteAccountCommand;
  73. public ICommand BtnDeleteAccountCommand
  74. {
  75. get
  76. {
  77. if (this._BtnDeleteAccountCommand == null)
  78. this._BtnDeleteAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnDeleteAccountCommand(arg));
  79. return this._BtnDeleteAccountCommand;
  80. }
  81. }
  82. private ICommand _BtnCancelAccountCommand;
  83. public ICommand BtnCancelAccountCommand
  84. {
  85. get
  86. {
  87. if (this._BtnCancelAccountCommand == null)
  88. this._BtnCancelAccountCommand = new BaseCommand<Object>((Object arg) => this.OnBtnCancelAccountCommand(arg));
  89. return this._BtnCancelAccountCommand;
  90. }
  91. }
  92. #endregion
  93. #endregion
  94. public AccountViewModel()
  95. {
  96. this.DisplayName = "Account";
  97. }
  98. protected override void OnViewLoaded(object view)
  99. {
  100. base.OnViewLoaded(view);
  101. AccountView av = view as AccountView;
  102. NewPasswordBox = av.pwNewPassword;
  103. ConfirmPasswordBox = av.pwConfirmPassword;
  104. }
  105. protected override void OnActivate()
  106. {
  107. AccountManager.Initialize();
  108. RefreshAccountList();
  109. }
  110. protected override void OnDeactivate(bool close)
  111. {
  112. if (ControlMode == CtrlMode.EDIT && IsPermission)
  113. {
  114. if (DialogBox.Confirm("The data has been modified. Do you want to save the change(s)?"))
  115. {
  116. if (SaveChanged())
  117. {
  118. ControlMode = CtrlMode.VIEW;
  119. DialogBox.ShowInfo("Operated successfully.");
  120. }
  121. }
  122. }
  123. base.OnDeactivate(close);
  124. }
  125. #region Function
  126. private void RefreshAccountList()
  127. {
  128. _AccountsList.Clear();
  129. _treeSelectedAccount = null;
  130. List<AccountItem> Accounts = AccountManager.GetAllAccounts();
  131. if (Accounts == null || Accounts.Count == 0) return;
  132. foreach (AccountItem Acc in Accounts)
  133. {
  134. AccountItem treeAccount = AccountManager.CloneAccount(Acc);
  135. if (treeAccount != null)
  136. {
  137. if (treeAccount.AccountName == BaseApp.Instance.UserContext.LoginName)
  138. {
  139. treeAccount.IsEnableChangeAccountName = false;
  140. }
  141. _AccountsList.Add(treeAccount);
  142. }
  143. }
  144. TreeSelectedAccount = _AccountsList.FirstOrDefault();
  145. TreeSelectedAccount.IsSelected = true;
  146. ControlMode = CtrlMode.VIEW;
  147. }
  148. public void OnAccountChanged()
  149. {
  150. if (ControlMode == CtrlMode.EDIT)
  151. return;
  152. //check account to set the mode from view to edit
  153. if (_treeSelectedAccount != null && _treeSelectedAccount.IsAccountChanged())
  154. ControlMode = CtrlMode.EDIT;
  155. }
  156. private bool SaveChanged()
  157. {
  158. if (string.IsNullOrWhiteSpace(TreeSelectedAccount.DisplayAccountName))
  159. {
  160. DialogBox.ShowWarning("{0} cannot be empty.", "Account name");
  161. //TreeSelectedAccount.DisplayAccountName = "NewUser";
  162. return false;
  163. }
  164. if (IsAccountExists(TreeSelectedAccount))
  165. {
  166. DialogBox.ShowWarning("{0} already exists.", "Account");
  167. return false;
  168. }
  169. TreeSelectedAccount.NewPassword = NewPasswordBox.Password;
  170. TreeSelectedAccount.ConfirmPassword = ConfirmPasswordBox.Password;
  171. if (string.IsNullOrWhiteSpace(TreeSelectedAccount.NewPassword) ||
  172. string.IsNullOrWhiteSpace(TreeSelectedAccount.ConfirmPassword))
  173. {
  174. DialogBox.ShowWarning("{0} cannot be empty.", "The password");
  175. return false;
  176. }
  177. if (TreeSelectedAccount.NewPassword != TreeSelectedAccount.ConfirmPassword)
  178. {
  179. DialogBox.ShowWarning("The password does not match.");
  180. return false;
  181. }
  182. if (!string.IsNullOrEmpty(TreeSelectedAccount.DisplayEmail))
  183. {
  184. string reg = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
  185. Regex r = new Regex(reg);
  186. if (!r.IsMatch(TreeSelectedAccount.DisplayEmail))
  187. {
  188. DialogBox.ShowWarning("The email is invalid.");
  189. return false;
  190. }
  191. }
  192. TreeSelectedAccount.AccountName = TreeSelectedAccount.DisplayAccountName;
  193. TreeSelectedAccount.FirstName = TreeSelectedAccount.DisplayFirstName;
  194. TreeSelectedAccount.LastName = TreeSelectedAccount.DisplayLastName;
  195. TreeSelectedAccount.Email = TreeSelectedAccount.DisplayEmail;
  196. TreeSelectedAccount.Description = TreeSelectedAccount.DisplayDescription;
  197. TreeSelectedAccount.AccountTextSaved = TreeSelectedAccount.FirstNameTextSaved =
  198. TreeSelectedAccount.LastNameTextSaved = TreeSelectedAccount.EmailTextSaved = true;
  199. bool isRoleSelected = false;
  200. foreach (RoleStatusItem entity in TreeSelectedAccount.RoleColleciton)
  201. {
  202. if (entity.DisplayRoleStatus)
  203. {
  204. isRoleSelected = true;
  205. }
  206. entity.RoleStatus = entity.DisplayRoleStatus;
  207. }
  208. if (!isRoleSelected)
  209. {
  210. DialogBox.ShowWarning("Please set role information for this account.");
  211. return false;
  212. }
  213. try
  214. {
  215. AccountManager.SaveAccount(TreeSelectedAccount);
  216. }
  217. catch (System.Exception ex)
  218. {
  219. LOG.Write(ex);
  220. return false;
  221. }
  222. NewPasswordBox.Clear();
  223. ConfirmPasswordBox.Clear();
  224. return true;
  225. }
  226. private bool IsAccountExists(AccountItem account)
  227. {
  228. if (AccountList == null || AccountList.Count == 0)
  229. return false;
  230. var sameNameList = AccountList.Where(t => t.DisplayAccountName == account.DisplayAccountName);
  231. if (sameNameList == null || sameNameList.Count() <= 1)
  232. return false;
  233. return true;
  234. }
  235. private void OnBtnAddAccountCommand(Object arg)
  236. {
  237. AccountItem newAccount = AccountManager.CreateAccount();
  238. if (newAccount != null)
  239. {
  240. _AccountsList.Add(newAccount);
  241. TreeSelectedAccount = newAccount;
  242. TreeSelectedAccount.IsSelected = true;
  243. }
  244. ControlMode = CtrlMode.EDIT;
  245. }
  246. private void OnBtnDeleteAccountCommand(Object arg)
  247. {
  248. if (_treeSelectedAccount == null) return;
  249. if (!DialogBox.Confirm("Are you sure that you want to delete this account?"))
  250. {
  251. return;
  252. }
  253. if (BaseApp.Instance.UserContext.LoginName == _treeSelectedAccount.AccountName)
  254. {
  255. DialogBox.ShowWarning("The action cannot be completed because {0} is currently in use.", "the account");
  256. return;
  257. }
  258. AccountManager.DeleteAccount(TreeSelectedAccount.AccountID);
  259. RefreshAccountList();
  260. }
  261. private void OnBtnCloneAccountCommand(Object arg)
  262. {
  263. if (_treeSelectedAccount != null)
  264. {
  265. AccountItem newAccount = AccountManager.CreateAccount(_treeSelectedAccount);
  266. if (newAccount != null)
  267. {
  268. newAccount.DisplayAccountName = newAccount.AccountName = "Copy of " + newAccount.DisplayAccountName;
  269. _AccountsList.Add(newAccount);
  270. TreeSelectedAccount = newAccount;
  271. TreeSelectedAccount.IsSelected = true;
  272. ControlMode = CtrlMode.EDIT;
  273. }
  274. }
  275. }
  276. private void OnBtnSaveAccountCommand(Object arg)
  277. {
  278. if (!TreeSelectedAccount.IsValid)
  279. {
  280. DialogBox.ShowWarning("Input error.");
  281. return;
  282. }
  283. if (SaveChanged())
  284. {
  285. ControlMode = CtrlMode.VIEW;
  286. DialogBox.ShowInfo("Operated successfully.");
  287. }
  288. else
  289. DialogBox.ShowInfo("Operation failed.");
  290. }
  291. private void OnBtnCancelAccountCommand(Object arg)
  292. {
  293. RefreshAccountList();
  294. NewPasswordBox.Clear();
  295. ConfirmPasswordBox.Clear();
  296. ControlMode = CtrlMode.VIEW;
  297. }
  298. #endregion
  299. }
  300. }