Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClientUserManagement/HeuristicLab.Clients.Access/3.3/AccessClient.cs @ 7380

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

#1648

  • fixed user query ws methods
  • improved user view
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 {
28  [Item("AccessClient", "Access client.")]
29  public sealed class AccessClient : IContent {
30    private static AccessClient instance;
31    public static AccessClient Instance {
32      get {
33        if (instance == null) instance = new AccessClient();
34        return instance;
35      }
36    }
37
38    #region Properties
39    private ItemList<LightweightUser> users;
40    public ItemList<LightweightUser> Users {
41      get { return users; }
42    }
43    #endregion
44
45    private AccessClient() {
46      users = new ItemList<LightweightUser>();
47    }
48
49    #region Refresh
50    public void Refresh() {
51      users = new ItemList<LightweightUser>();
52      users.AddRange(CallRunCreationService<ItemList<LightweightUser>>(s => new ItemList<LightweightUser>(s.GetAllLightweightUsers())));
53    }
54    public void RefreshAsync(Action<Exception> exceptionCallback) {
55      var call = new Func<Exception>(delegate() {
56        try {
57          OnRefreshing();
58          Refresh();
59        }
60        catch (Exception ex) {
61          return ex;
62        }
63        finally {
64          OnRefreshed();
65        }
66        return null;
67      });
68      call.BeginInvoke(delegate(IAsyncResult result) {
69        Exception ex = call.EndInvoke(result);
70        if (ex != null) exceptionCallback(ex);
71      }, null);
72    }
73    public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
74      var call = new Func<Exception>(delegate() {
75        try {
76          OnRefreshing();
77          action();
78        }
79        catch (Exception ex) {
80          return ex;
81        }
82        finally {
83          OnRefreshed();
84        }
85        return null;
86      });
87      call.BeginInvoke(delegate(IAsyncResult result) {
88        Exception ex = call.EndInvoke(result);
89        if (ex != null) exceptionCallback(ex);
90      }, null);
91    }
92    #endregion
93
94    public static void Store(IAccessItem item) {
95      //TODO: prevent storing of lightweight users
96    }
97
98    #region Events
99    public event EventHandler Refreshing;
100    private void OnRefreshing() {
101      EventHandler handler = Refreshing;
102      if (handler != null) handler(this, EventArgs.Empty);
103    }
104    public event EventHandler Refreshed;
105    private void OnRefreshed() {
106      EventHandler handler = Refreshed;
107      if (handler != null) handler(this, EventArgs.Empty);
108    }
109    #endregion
110
111    #region Helpers
112    private static void CallRunCreationService(Action<IAccessService> call) {
113      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
114      try {
115        call(client);
116      }
117      finally {
118        try {
119          client.Close();
120        }
121        catch (Exception) {
122          client.Abort();
123        }
124      }
125    }
126    private static T CallRunCreationService<T>(Func<IAccessService, T> call) {
127      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
128      try {
129        return call(client);
130      }
131      finally {
132        try {
133          client.Close();
134        }
135        catch (Exception) {
136          client.Abort();
137        }
138      }
139    }
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.