Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7368 was 7368, checked in by ascheibe, 13 years ago

#1648 added first version of an access client, access items and views for displaying them

File size: 3.6 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    private void Refresh() {
51      OnRefreshing();
52      users = new ItemList<LightweightUser>();
53
54      try {
55        users.AddRange(CallRunCreationService<ItemList<LightweightUser>>(s => new ItemList<LightweightUser>(s.GetAllLightweightUsers())));
56      }
57      finally {
58        OnRefreshed();
59      }
60    }
61    public void RefreshAsync(Action<Exception> exceptionCallback) {
62      var call = new Func<Exception>(delegate() {
63        try {
64          Refresh();
65        }
66        catch (Exception ex) {
67          return ex;
68        }
69        return null;
70      });
71      call.BeginInvoke(delegate(IAsyncResult result) {
72        Exception ex = call.EndInvoke(result);
73        if (ex != null) exceptionCallback(ex);
74      }, null);
75    }
76    #endregion
77
78    public static void Store(IAccessItem item) {
79      //TODO: never used? delete?
80    }
81
82    #region Events
83    public event EventHandler Refreshing;
84    private void OnRefreshing() {
85      EventHandler handler = Refreshing;
86      if (handler != null) handler(this, EventArgs.Empty);
87    }
88    public event EventHandler Refreshed;
89    private void OnRefreshed() {
90      EventHandler handler = Refreshed;
91      if (handler != null) handler(this, EventArgs.Empty);
92    }
93    #endregion
94
95    #region Helpers
96    private static void CallRunCreationService(Action<IAccessService> call) {
97      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
98      try {
99        call(client);
100      }
101      finally {
102        try {
103          client.Close();
104        }
105        catch (Exception) {
106          client.Abort();
107        }
108      }
109    }
110    private static T CallRunCreationService<T>(Func<IAccessService, T> call) {
111      AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
112      try {
113        return call(client);
114      }
115      finally {
116        try {
117          client.Close();
118        }
119        catch (Exception) {
120          client.Abort();
121        }
122      }
123    }
124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.