RoleViewModel.cs 11 KB

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