Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.Access.Administration/3.3/AccessAdministrationClient.cs @ 14927

Last change on this file since 14927 was 14927, checked in by gkronber, 7 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Clients.Common;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26
27namespace HeuristicLab.Clients.Access.Administration {
28  [Item("AccessAdministrationClient", "AccessAdministration client.")]
29  public class AccessAdministrationClient : IContent {
30    private static AccessAdministrationClient instance;
31    public static AccessAdministrationClient Instance {
32      get {
33        if (instance == null) instance = new AccessAdministrationClient();
34        return instance;
35      }
36    }
37
38    #region Properties
39    private ItemList<User> users;
40    public ItemList<User> Users {
41      get {
42        return users;
43      }
44    }
45
46    public ItemList<UserGroup> Groups { get; set; }
47    public ItemList<Role> Roles { get; set; }
48    #endregion
49
50    private AccessAdministrationClient() { }
51
52    #region Refresh
53    public void RefreshUsers() {
54      users = new ItemList<User>();
55      users.AddRange(CallAccessService<ItemList<User>>(s => new ItemList<User>(s.GetAllUsers())));
56    }
57
58    public void RefreshUsersAsync(Action<Exception> exceptionCallback) {
59      ExecuteActionAsync(RefreshUsers, exceptionCallback);
60    }
61
62    public void RefreshUserGroups() {
63      Groups = new ItemList<UserGroup>();
64      Groups.AddRange(CallAccessService<ItemList<UserGroup>>(s => new ItemList<UserGroup>(s.GetAllUserGroups())));
65    }
66
67    public void RefreshUserGroupsAsync(Action<Exception> exceptionCallback) {
68      ExecuteActionAsync(RefreshUserGroups, exceptionCallback);
69    }
70
71    public void RefreshRoles() {
72      Roles = new ItemList<Role>();
73      Roles.AddRange(CallAccessService<ItemList<Role>>(s => new ItemList<Role>(s.GetRoles())));
74    }
75
76    public void RefreshRolesAsync(Action<Exception> exceptionCallback) {
77      ExecuteActionAsync(RefreshRoles, exceptionCallback);
78    }
79
80    #endregion
81
82    #region Store
83    public void StoreUsers() {
84      foreach (User u in users) {
85        if (u.Modified) {
86          if (u.Id == Guid.Empty) {
87            CallAccessService(s => u.Id = s.AddUser(u).Id);
88          } else {
89            CallAccessService(s => s.UpdateUser(u));
90          }
91          u.SetUnmodified();
92        }
93      }
94    }
95
96    public void StoreUsersAsync(Action<Exception> exceptionCallback) {
97      ExecuteActionAsync(StoreUsers, exceptionCallback);
98    }
99
100    public void StoreUserGroups() {
101      foreach (UserGroup g in Groups) {
102        if (g.Modified) {
103          if (g.Id == Guid.Empty) {
104            CallAccessService(s => g.Id = s.AddUserGroup(g));
105          } else {
106            CallAccessService(s => s.UpdateUserGroup(g));
107          }
108          g.SetUnmodified();
109        }
110      }
111    }
112
113    public void StoreUserGroupsAsync(Action<Exception> exceptionCallback) {
114      ExecuteActionAsync(StoreUserGroups, exceptionCallback);
115    }
116
117    public void StoreRoles() {
118      foreach (Role g in Roles) {
119        if (g.Modified) {
120          CallAccessService(s => s.AddRole(g));
121          g.SetUnmodified();
122        }
123      }
124    }
125
126    public void StoreRolesAsync(Action<Exception> exceptionCallback) {
127      ExecuteActionAsync(StoreRoles, exceptionCallback);
128    }
129    #endregion
130
131    #region Delete
132    public void DeleteUser(User u) {
133      CallAccessService(s => s.DeleteUser(u));
134    }
135
136    public void DeleteUserAsync(User u, Action<Exception> exceptionCallback) {
137      Action deleteUserAction = new Action(delegate { DeleteUser(u); });
138      ExecuteActionAsync(deleteUserAction, exceptionCallback);
139    }
140
141    public void DeleteUserGroup(UserGroup u) {
142      CallAccessService(s => s.DeleteUserGroup(u));
143    }
144
145    public void DeleteUserGroupAsync(UserGroup u, Action<Exception> exceptionCallback) {
146      Action deleteUserGroupAction = new Action(delegate { DeleteUserGroup(u); });
147      ExecuteActionAsync(deleteUserGroupAction, exceptionCallback);
148    }
149
150    public void DeleteRole(Role u) {
151      CallAccessService(s => s.DeleteRole(u));
152    }
153
154    public void DeleteRoleAsync(Role u, Action<Exception> exceptionCallback) {
155      Action deleteRoleAction = new Action(delegate { DeleteRole(u); });
156      ExecuteActionAsync(deleteRoleAction, exceptionCallback);
157    }
158    #endregion
159
160    public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
161      var call = new Func<Exception>(delegate () {
162        try {
163          OnRefreshing();
164          action();
165        } catch (Exception ex) {
166          return ex;
167        } finally {
168          OnRefreshed();
169        }
170        return null;
171      });
172      call.BeginInvoke(delegate (IAsyncResult result) {
173        Exception ex = call.EndInvoke(result);
174        if (ex != null) exceptionCallback(ex);
175      }, null);
176    }
177
178    #region Events
179    public event EventHandler Refreshing;
180    private void OnRefreshing() {
181      EventHandler handler = Refreshing;
182      if (handler != null) handler(this, EventArgs.Empty);
183    }
184    public event EventHandler Refreshed;
185    private void OnRefreshed() {
186      EventHandler handler = Refreshed;
187      if (handler != null) handler(this, EventArgs.Empty);
188    }
189    #endregion
190
191    #region Helpers
192    public static void CallAccessService(Action<IAccessService> call) {
193      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
194      try {
195        call(client);
196      } finally {
197        try {
198          client.Close();
199        } catch (Exception) {
200          client.Abort();
201        }
202      }
203    }
204    public static T CallAccessService<T>(Func<IAccessService, T> call) {
205      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
206      try {
207        return call(client);
208      } finally {
209        try {
210          client.Close();
211        } catch (Exception) {
212          client.Abort();
213        }
214      }
215    }
216    #endregion
217  }
218}
Note: See TracBrowser for help on using the repository browser.