Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Clients.Access.Administration/3.3/AccessAdministrationClient.cs @ 7635

Last change on this file since 7635 was 7635, checked in by ascheibe, 12 years ago

#1648 added admin views for displaying user groups

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    #endregion
48
49    private AccessAdministrationClient() { }
50
51    #region Refresh
52    public void RefreshUsers() {
53      users = new ItemList<User>();
54      users.AddRange(CallAccessService<ItemList<User>>(s => new ItemList<User>(s.GetAllUsers())));
55    }
56
57    public void RefreshUsersAsync(Action<Exception> exceptionCallback) {
58      ExecuteActionAsync(RefreshUsers, exceptionCallback);
59    }
60
61    public void RefreshUserGroups() {
62      Groups = new ItemList<UserGroup>();
63      Groups.AddRange(CallAccessService<ItemList<UserGroup>>(s => new ItemList<UserGroup>(s.GetAllUserGroups())));
64    }
65
66    public void RefreshUserGroupsAsync(Action<Exception> exceptionCallback) {
67      ExecuteActionAsync(RefreshUserGroups, exceptionCallback);
68    }
69
70    public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
71      var call = new Func<Exception>(delegate() {
72        try {
73          OnRefreshing();
74          action();
75        }
76        catch (Exception ex) {
77          return ex;
78        }
79        finally {
80          OnRefreshed();
81        }
82        return null;
83      });
84      call.BeginInvoke(delegate(IAsyncResult result) {
85        Exception ex = call.EndInvoke(result);
86        if (ex != null) exceptionCallback(ex);
87      }, null);
88    }
89    #endregion
90
91    public static void Store(IAccessItem item) {
92      //TODO: storing
93    }
94
95    #region Events
96    public event EventHandler Refreshing;
97    private void OnRefreshing() {
98      EventHandler handler = Refreshing;
99      if (handler != null) handler(this, EventArgs.Empty);
100    }
101    public event EventHandler Refreshed;
102    private void OnRefreshed() {
103      EventHandler handler = Refreshed;
104      if (handler != null) handler(this, EventArgs.Empty);
105    }
106    #endregion
107
108    #region Helpers
109    public static void CallAccessService(Action<IAccessService> call) {
110      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
111      try {
112        call(client);
113      }
114      finally {
115        try {
116          client.Close();
117        }
118        catch (Exception) {
119          client.Abort();
120        }
121      }
122    }
123    public static T CallAccessService<T>(Func<IAccessService, T> call) {
124      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
125      try {
126        return call(client);
127      }
128      finally {
129        try {
130          client.Close();
131        }
132        catch (Exception) {
133          client.Abort();
134        }
135      }
136    }
137    #endregion
138  }
139}
Note: See TracBrowser for help on using the repository browser.