RoleViewModel.cs 11 KB

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