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 |
|
---|
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 | #endregion
|
---|
70 |
|
---|
71 | #region Store
|
---|
72 | public void StoreUsers() {
|
---|
73 | foreach (User u in users) {
|
---|
74 | if (u.Modified) {
|
---|
75 | if (u.Id == Guid.Empty) {
|
---|
76 | CallAccessService(s => u.Id = s.AddUser(u).Id);
|
---|
77 | } else {
|
---|
78 | CallAccessService(s => s.UpdateUser(u));
|
---|
79 | }
|
---|
80 | u.SetUnmodified();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void StoreUsersAsync(Action<Exception> exceptionCallback) {
|
---|
86 | ExecuteActionAsync(StoreUsers, exceptionCallback);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void StoreUserGroups() {
|
---|
90 | foreach (UserGroup g in Groups) {
|
---|
91 | if (g.Modified) {
|
---|
92 | if (g.Id == Guid.Empty) {
|
---|
93 | CallAccessService(s => g.Id = s.AddUserGroup(g));
|
---|
94 | } else {
|
---|
95 | CallAccessService(s => s.UpdateUserGroup(g));
|
---|
96 | }
|
---|
97 | g.SetUnmodified();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void StoreUserGroupsAsync(Action<Exception> exceptionCallback) {
|
---|
103 | ExecuteActionAsync(StoreUserGroups, exceptionCallback);
|
---|
104 | }
|
---|
105 |
|
---|
106 | //i don't think such a generic method is a good idea
|
---|
107 | /*public static void Store(IAccessItem item) {
|
---|
108 |
|
---|
109 | } */
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region Delete
|
---|
113 | public void DeleteUser(User u) {
|
---|
114 | CallAccessService(s => s.DeleteUser(u));
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void DeleteUserAsync(User u, Action<Exception> exceptionCallback) {
|
---|
118 | Action deleteUserAction = new Action(delegate { DeleteUser(u); });
|
---|
119 | ExecuteActionAsync(deleteUserAction, exceptionCallback);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void DeleteUserGroup(UserGroup u) {
|
---|
123 | CallAccessService(s => s.DeleteUserGroup(u));
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void DeleteUserGroupAsync(UserGroup u, Action<Exception> exceptionCallback) {
|
---|
127 | Action deleteUserGroupAction = new Action(delegate { DeleteUserGroup(u); });
|
---|
128 | ExecuteActionAsync(deleteUserGroupAction, exceptionCallback);
|
---|
129 | }
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
|
---|
133 | var call = new Func<Exception>(delegate() {
|
---|
134 | try {
|
---|
135 | OnRefreshing();
|
---|
136 | action();
|
---|
137 | }
|
---|
138 | catch (Exception ex) {
|
---|
139 | return ex;
|
---|
140 | }
|
---|
141 | finally {
|
---|
142 | OnRefreshed();
|
---|
143 | }
|
---|
144 | return null;
|
---|
145 | });
|
---|
146 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
147 | Exception ex = call.EndInvoke(result);
|
---|
148 | if (ex != null) exceptionCallback(ex);
|
---|
149 | }, null);
|
---|
150 | }
|
---|
151 |
|
---|
152 | #region Events
|
---|
153 | public event EventHandler Refreshing;
|
---|
154 | private void OnRefreshing() {
|
---|
155 | EventHandler handler = Refreshing;
|
---|
156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
157 | }
|
---|
158 | public event EventHandler Refreshed;
|
---|
159 | private void OnRefreshed() {
|
---|
160 | EventHandler handler = Refreshed;
|
---|
161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 | #endregion
|
---|
164 |
|
---|
165 | #region Helpers
|
---|
166 | public static void CallAccessService(Action<IAccessService> call) {
|
---|
167 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
168 | try {
|
---|
169 | call(client);
|
---|
170 | }
|
---|
171 | finally {
|
---|
172 | try {
|
---|
173 | client.Close();
|
---|
174 | }
|
---|
175 | catch (Exception) {
|
---|
176 | client.Abort();
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 | public static T CallAccessService<T>(Func<IAccessService, T> call) {
|
---|
181 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
182 | try {
|
---|
183 | return call(client);
|
---|
184 | }
|
---|
185 | finally {
|
---|
186 | try {
|
---|
187 | client.Close();
|
---|
188 | }
|
---|
189 | catch (Exception) {
|
---|
190 | client.Abort();
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | #endregion
|
---|
195 | }
|
---|
196 | }
|
---|