AccountViewModel.cs 13 KB

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