AccountViewModel.cs 12 KB

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