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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Clients.Common;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 |
|
---|
27 | namespace 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<UserGroupBase> usersAndGroups;
|
---|
40 | public ItemList<UserGroupBase> UsersAndGroups {
|
---|
41 | get { return usersAndGroups; }
|
---|
42 | }
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | private AccessClient() { }
|
---|
46 |
|
---|
47 | #region Refresh
|
---|
48 | public void Refresh() {
|
---|
49 | usersAndGroups = new ItemList<UserGroupBase>();
|
---|
50 | usersAndGroups.AddRange(CallAccessService<ItemList<UserGroupBase>>(s => new ItemList<UserGroupBase>(s.GetAllLeightweightUsersAndGroups())));
|
---|
51 | }
|
---|
52 | public void RefreshAsync(Action<Exception> exceptionCallback) {
|
---|
53 | var call = new Func<Exception>(delegate () {
|
---|
54 | try {
|
---|
55 | OnRefreshing();
|
---|
56 | Refresh();
|
---|
57 | } catch (Exception ex) {
|
---|
58 | return ex;
|
---|
59 | } finally {
|
---|
60 | OnRefreshed();
|
---|
61 | }
|
---|
62 | return null;
|
---|
63 | });
|
---|
64 | call.BeginInvoke(delegate (IAsyncResult result) {
|
---|
65 | Exception ex = call.EndInvoke(result);
|
---|
66 | if (ex != null) exceptionCallback(ex);
|
---|
67 | }, null);
|
---|
68 | }
|
---|
69 | public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
|
---|
70 | var call = new Func<Exception>(delegate () {
|
---|
71 | try {
|
---|
72 | OnRefreshing();
|
---|
73 | action();
|
---|
74 | } catch (Exception ex) {
|
---|
75 | return ex;
|
---|
76 | } finally {
|
---|
77 | OnRefreshed();
|
---|
78 | }
|
---|
79 | return null;
|
---|
80 | });
|
---|
81 | call.BeginInvoke(delegate (IAsyncResult result) {
|
---|
82 | Exception ex = call.EndInvoke(result);
|
---|
83 | if (ex != null) exceptionCallback(ex);
|
---|
84 | }, null);
|
---|
85 | }
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | #region Events
|
---|
89 | public event EventHandler Refreshing;
|
---|
90 | private void OnRefreshing() {
|
---|
91 | EventHandler handler = Refreshing;
|
---|
92 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
93 | }
|
---|
94 | public event EventHandler Refreshed;
|
---|
95 | private void OnRefreshed() {
|
---|
96 | EventHandler handler = Refreshed;
|
---|
97 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
98 | }
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | #region Helpers
|
---|
102 | public static void CallAccessService(Action<IAccessService> call) {
|
---|
103 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
104 | try {
|
---|
105 | call(client);
|
---|
106 | } finally {
|
---|
107 | try {
|
---|
108 | client.Close();
|
---|
109 | } catch (Exception) {
|
---|
110 | client.Abort();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | public static T CallAccessService<T>(Func<IAccessService, T> call) {
|
---|
115 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
116 | try {
|
---|
117 | return call(client);
|
---|
118 | } finally {
|
---|
119 | try {
|
---|
120 | client.Close();
|
---|
121 | } catch (Exception) {
|
---|
122 | client.Abort();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 | }
|
---|
128 | }
|
---|