RoleViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using Aitex.Core.RT.Log;
  2. using Caliburn.Micro;
  3. using MECF.Framework.UI.Client.CenterViews.Dialogs;
  4. using MECF.Framework.UI.Client.ClientBase;
  5. using OpenSEMI.ClientBase;
  6. using OpenSEMI.ClientBase.Command;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. namespace MECF.Framework.UI.Client.CenterViews.Configs.Roles
  15. {
  16. public class RoleViewModel : BaseModel
  17. {
  18. public bool IsPermission { get => this.Permission == 3; }
  19. private bool _IsEnabledRoleName = false;
  20. public bool IsEnabledRoleName
  21. {
  22. get { return _IsEnabledRoleName; }
  23. set { _IsEnabledRoleName = value; NotifyOfPropertyChange("IsEnabledRoleName"); }
  24. }
  25. public RoleViewModel()
  26. {
  27. this.DisplayName = "Role";
  28. }
  29. protected override void OnInitialize()
  30. {
  31. base.OnInitialize();
  32. }
  33. protected override void OnActivate()
  34. {
  35. _RolesList.Clear();
  36. _TreeSelectedRole = null;
  37. RoleManager.Initialize();
  38. LoadRoleList();
  39. base.OnActivate();
  40. }
  41. protected override void OnDeactivate(bool close)
  42. {
  43. if (ControlMode == CtrlMode.EDIT && IsPermission)
  44. {
  45. if (DialogBox.Confirm("The data has been modified. Do you want to save the change(s)?"))
  46. {
  47. if (SaveChanged())
  48. {
  49. ControlMode = CtrlMode.VIEW;
  50. DialogBox.ShowInfo("Operated successfully.");
  51. }
  52. }
  53. }
  54. base.OnDeactivate(close);
  55. }
  56. public void OnRoleChanged()
  57. {
  58. string[] RoleNameList = { "Manager", "Engineer", "Technician", "Operator" };
  59. string _RoleName = RoleNameList.Contains(TreeSelectedRole.DisplayRoleName) ? TreeSelectedRole.DisplayRoleName : "";
  60. IsEnabledRoleName = true;
  61. if (!string.IsNullOrEmpty(_RoleName))
  62. IsEnabledRoleName = false;
  63. if (ControlMode == CtrlMode.EDIT)
  64. {
  65. if (!string.IsNullOrEmpty(TreeSelectedRole.DisplayRoleName) && TreeSelectedRole.DisplayRoleName.Length > 18)
  66. TreeSelectedRole.DisplayRoleName = TreeSelectedRole.DisplayRoleName.Substring(0, 18);
  67. return;
  68. }
  69. //check role to set the mode from view to edit
  70. if (_TreeSelectedRole != null && _TreeSelectedRole.IsRoleChanged())
  71. ControlMode = CtrlMode.EDIT;
  72. }
  73. public bool OnAutoLogoutTimeChecked(object sender)
  74. {
  75. ControlMode = CtrlMode.EDIT;
  76. return ((CheckBox)(sender)).IsChecked.Value;
  77. }
  78. private bool SaveChanged()
  79. {
  80. if (String.IsNullOrWhiteSpace(TreeSelectedRole.DisplayRoleName))
  81. {
  82. DialogBox.ShowWarning("{0} cannot be empty.", "Role name");
  83. //TreeSelectedRole.DisplayRoleName = "No Name";
  84. return false;
  85. }
  86. if (IsRoleExists(TreeSelectedRole))
  87. {
  88. DialogBox.ShowWarning("{0} already exists.", "Role");
  89. return false;
  90. }
  91. foreach (MenuInfo menu in TreeSelectedRole.MenuCollection)
  92. {
  93. menu.IndexPermission = menu.DisplayIndexPermission + 1;
  94. }
  95. TreeSelectedRole.RoleName = TreeSelectedRole.DisplayRoleName;
  96. TreeSelectedRole.IsAutoLogout = TreeSelectedRole.DisplayIsAutoLogout;
  97. TreeSelectedRole.AutoLogoutTime = TreeSelectedRole.DisplayAutoLogoutTime;
  98. TreeSelectedRole.RoleNameTextSaved = TreeSelectedRole.TimeTextSaved = true;
  99. TreeSelectedRole.Description = TreeSelectedRole.DisplayDescription;
  100. try
  101. {
  102. RoleManager.SaveRole(TreeSelectedRole);
  103. }
  104. catch (Exception ex)
  105. {
  106. LOG.Write(ex);
  107. return false;
  108. }
  109. return true;
  110. }
  111. private Boolean IsRoleExists(RoleItem role)
  112. {
  113. if (RoleList == null || RoleList.Count == 0)
  114. return false;
  115. var sameNameList = RoleList.Where(t => t.DisplayRoleName == role.DisplayRoleName);
  116. if (sameNameList == null || sameNameList.Count() <= 1)
  117. return false;
  118. return true;
  119. }
  120. private void LoadRoleList()
  121. {
  122. _RolesList.Clear();
  123. List<RoleItem> roles = RoleManager.GetAllRoles();
  124. if (roles == null || roles.Count == 0)
  125. return;
  126. foreach (RoleItem r in roles)
  127. {
  128. for (int i = 0; i < r.MenuCollection.Count; i++)
  129. {
  130. r.MenuCollection[i].StringPermission = PermissionDictionary[r.MenuCollection[i].IndexPermission-1].StringPermission;
  131. }
  132. RoleItem treeRole = RoleManager.CloneRole(r);
  133. if (treeRole != null)
  134. {
  135. _RolesList.Add(treeRole);
  136. }
  137. }
  138. TreeSelectedRole = _RolesList.FirstOrDefault();
  139. TreeSelectedRole.IsSelected = true;
  140. ControlMode = CtrlMode.VIEW;
  141. }
  142. private void OnRoleTreeSelectedChanged(EventCommandParameter<object, RoutedEventArgs> arg)
  143. {
  144. RoleItem roleItem = arg.CustomParameter as RoleItem;
  145. if (roleItem == null)
  146. return;
  147. TreeSelectedRole = roleItem;
  148. }
  149. private void OnBtnAddRoleCommand(Object arg)
  150. {
  151. RoleItem newRole = RoleManager.CreateRole();
  152. if (newRole != null)
  153. {
  154. _RolesList.Add(newRole);
  155. TreeSelectedRole = newRole;
  156. TreeSelectedRole.IsSelected = true;
  157. }
  158. ControlMode = CtrlMode.EDIT;
  159. }
  160. private void OnBtnDeleteRoleCommand(Object arg)
  161. {
  162. if (TreeSelectedRole == null) return;
  163. if (!DialogBox.Confirm("Are you sure that you want to delete this role?"))
  164. {
  165. return;
  166. }
  167. if (BaseApp.Instance.UserContext.RoleID == TreeSelectedRole.RoleID)
  168. {
  169. DialogBox.ShowWarning("The action cannot be completed because {0} is currently in use.", "the role");
  170. return;
  171. }
  172. try
  173. {
  174. int index = _RolesList.IndexOf(TreeSelectedRole);
  175. _RolesList.Remove(TreeSelectedRole);
  176. RoleManager.DeleteRole(TreeSelectedRole.RoleID);
  177. index = index > 1 ? index - 1 : 0;
  178. TreeSelectedRole = _RolesList == null ? null : _RolesList[index];
  179. TreeSelectedRole.IsSelected = true;
  180. DialogBox.ShowInfo("Operated successfully.");
  181. }
  182. catch (Exception ex)
  183. {
  184. LOG.Error(ex.StackTrace);
  185. DialogBox.ShowInfo("Operation failed.");
  186. }
  187. }
  188. private void OnBtnCloneRoleCommand(Object arg)
  189. {
  190. if (_TreeSelectedRole != null)
  191. {
  192. RoleItem newRole = RoleManager.CreateRole(_TreeSelectedRole);
  193. if (newRole != null)
  194. {
  195. newRole.DisplayRoleName = newRole.RoleName = "Copy of " + newRole.DisplayRoleName;
  196. _RolesList.Add(newRole);
  197. TreeSelectedRole = newRole;
  198. TreeSelectedRole.IsSelected = true;
  199. ControlMode = CtrlMode.EDIT;
  200. }
  201. }
  202. }
  203. private void OnBtnSaveCommand(Object arg)
  204. {
  205. try
  206. {
  207. if (SaveChanged())
  208. {
  209. ControlMode = CtrlMode.VIEW;
  210. DialogBox.ShowInfo("Operated successfully.");
  211. }
  212. else
  213. DialogBox.ShowInfo("Operation failed.");
  214. }
  215. catch (Exception ex)
  216. {
  217. LOG.Error(ex.StackTrace);
  218. DialogBox.ShowInfo("Operation failed.");
  219. }
  220. }
  221. private void OnBtnCancelRoleCommand(Object arg)
  222. {
  223. LoadRoleList();
  224. ControlMode = CtrlMode.VIEW;
  225. }
  226. #region commands
  227. private ICommand _RoleTreeSelectChangedCmd;
  228. public ICommand RoleTreeSelectChangedCommand
  229. {
  230. get
  231. {
  232. if (this._RoleTreeSelectChangedCmd == null)
  233. this._RoleTreeSelectChangedCmd = new BaseCommand<EventCommandParameter<object, RoutedEventArgs>>((EventCommandParameter<object, RoutedEventArgs> arg) => this.OnRoleTreeSelectedChanged(arg));
  234. return this._RoleTreeSelectChangedCmd;
  235. }
  236. }
  237. private ICommand _BtnSaveCommand;
  238. public ICommand btnSaveCommand
  239. {
  240. get
  241. {
  242. if (this._BtnSaveCommand == null)
  243. this._BtnSaveCommand = new BaseCommand<Object>((Object arg) => this.OnBtnSaveCommand(arg));
  244. return this._BtnSaveCommand;
  245. }
  246. }
  247. private ICommand _BtnAddRoleCommand;
  248. public ICommand btnAddRoleCommand
  249. {
  250. get
  251. {
  252. if (this._BtnAddRoleCommand == null)
  253. this._BtnAddRoleCommand = new BaseCommand<Object>((Object arg) => this.OnBtnAddRoleCommand(arg));
  254. return this._BtnAddRoleCommand;
  255. }
  256. }
  257. private ICommand _BtnDeleteRoleCommand;
  258. public ICommand btnDeleteRoleCommand
  259. {
  260. get
  261. {
  262. if (this._BtnDeleteRoleCommand == null)
  263. this._BtnDeleteRoleCommand = new BaseCommand<Object>((Object arg) => this.OnBtnDeleteRoleCommand(arg));
  264. return this._BtnDeleteRoleCommand;
  265. }
  266. }
  267. private ICommand _BtnCloneRoleCommand;
  268. public ICommand btnCloneRoleCommand
  269. {
  270. get
  271. {
  272. if (this._BtnCloneRoleCommand == null)
  273. this._BtnCloneRoleCommand = new BaseCommand<Object>((Object arg) => this.OnBtnCloneRoleCommand(arg));
  274. return this._BtnCloneRoleCommand;
  275. }
  276. }
  277. private ICommand _BtnCancelRoleCommand;
  278. public ICommand BtnCancelRoleCommand
  279. {
  280. get
  281. {
  282. if (this._BtnCancelRoleCommand == null)
  283. this._BtnCancelRoleCommand = new BaseCommand<Object>((Object arg) => this.OnBtnCancelRoleCommand(arg));
  284. return this._BtnCancelRoleCommand;
  285. }
  286. }
  287. #endregion
  288. public RoleManager RoleManager
  289. {
  290. get { return RoleManager.Instance; }
  291. }
  292. public ObservableCollection<PermissionType> PermissionDictionary
  293. {
  294. get { return RolePermissionMapper.Instance.PermissionDictionary; }
  295. }
  296. private ObservableCollection<RoleItem> _RolesList = new ObservableCollection<RoleItem>();
  297. public ObservableCollection<RoleItem> RoleList
  298. {
  299. get { return _RolesList; }
  300. }
  301. private RoleItem _TreeSelectedRole = null;
  302. public RoleItem TreeSelectedRole
  303. {
  304. get { return _TreeSelectedRole; }
  305. set
  306. {
  307. _TreeSelectedRole = value;
  308. this.NotifyOfPropertyChange("TreeSelectedRole");
  309. }
  310. }
  311. private CtrlMode _ControlMode = CtrlMode.VIEW;
  312. public CtrlMode ControlMode
  313. {
  314. get { return _ControlMode; }
  315. set { _ControlMode = value; NotifyOfPropertyChange("ControlMode"); }
  316. }
  317. public void SelectRolePermission(MenuInfo obj)
  318. {
  319. ListDialogProViewModel dialog = new ListDialogProViewModel();
  320. dialog.DisplayName = "Select Permission";
  321. dialog.Items.Clear();
  322. foreach (var item in PermissionDictionary)
  323. {
  324. dialog.Items.Add(item.StringPermission.ToString());
  325. }
  326. WindowManager wm = new WindowManager();
  327. bool? bret = wm.ShowDialog(dialog);
  328. if ((bool)bret)
  329. {
  330. string strDialogResult = dialog.DialogResult;
  331. int iPermissionIndex = 0;
  332. for (int i = 0; i < PermissionDictionary.Count; i++)
  333. {
  334. if(PermissionDictionary[i].StringPermission == strDialogResult)
  335. {
  336. iPermissionIndex = i;
  337. break;
  338. }
  339. }
  340. obj.DisplayIndexPermission = iPermissionIndex;
  341. obj.StringPermission = strDialogResult;
  342. obj.NotifyOfPropertyChange(nameof(obj.StringPermission));
  343. OnRoleChanged();
  344. }
  345. }
  346. }
  347. }