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