#region License Information
/* HeuristicLab
* Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Clients.Common;
using HeuristicLab.Common;
using HeuristicLab.Core;
namespace HeuristicLab.Clients.Access.Administration {
[Item("AccessAdministrationClient", "AccessAdministration client.")]
public class AccessAdministrationClient : IContent {
private static AccessAdministrationClient instance;
public static AccessAdministrationClient Instance {
get {
if (instance == null) instance = new AccessAdministrationClient();
return instance;
}
}
#region Properties
private ItemList users;
public ItemList Users {
get {
return users;
}
}
public ItemList Groups { get; set; }
public ItemList Roles { get; set; }
#endregion
private AccessAdministrationClient() { }
#region Refresh
public void RefreshUsers() {
users = new ItemList();
users.AddRange(CallAccessService>(s => new ItemList(s.GetAllUsers())));
}
public void RefreshUsersAsync(Action exceptionCallback) {
ExecuteActionAsync(RefreshUsers, exceptionCallback);
}
public void RefreshUserGroups() {
Groups = new ItemList();
Groups.AddRange(CallAccessService>(s => new ItemList(s.GetAllUserGroups())));
}
public void RefreshUserGroupsAsync(Action exceptionCallback) {
ExecuteActionAsync(RefreshUserGroups, exceptionCallback);
}
public void RefreshRoles() {
Roles = new ItemList();
Roles.AddRange(CallAccessService>(s => new ItemList(s.GetRoles())));
}
public void RefreshRolesAsync(Action exceptionCallback) {
ExecuteActionAsync(RefreshRoles, exceptionCallback);
}
#endregion
#region Store
public void StoreUsers() {
foreach (User u in users) {
if (u.Modified) {
if (u.Id == Guid.Empty) {
CallAccessService(s => u.Id = s.AddUser(u).Id);
} else {
CallAccessService(s => s.UpdateUser(u));
}
u.SetUnmodified();
}
}
}
public void StoreUsersAsync(Action exceptionCallback) {
ExecuteActionAsync(StoreUsers, exceptionCallback);
}
public void StoreUserGroups() {
foreach (UserGroup g in Groups) {
if (g.Modified) {
if (g.Id == Guid.Empty) {
CallAccessService(s => g.Id = s.AddUserGroup(g));
} else {
CallAccessService(s => s.UpdateUserGroup(g));
}
g.SetUnmodified();
}
}
}
public void StoreUserGroupsAsync(Action exceptionCallback) {
ExecuteActionAsync(StoreUserGroups, exceptionCallback);
}
public void StoreRoles() {
foreach (Role g in Roles) {
if (g.Modified) {
CallAccessService(s => s.AddRole(g));
g.SetUnmodified();
}
}
}
public void StoreRolesAsync(Action exceptionCallback) {
ExecuteActionAsync(StoreRoles, exceptionCallback);
}
#endregion
#region Delete
public void DeleteUser(User u) {
CallAccessService(s => s.DeleteUser(u));
}
public void DeleteUserAsync(User u, Action exceptionCallback) {
Action deleteUserAction = new Action(delegate { DeleteUser(u); });
ExecuteActionAsync(deleteUserAction, exceptionCallback);
}
public void DeleteUserGroup(UserGroup u) {
CallAccessService(s => s.DeleteUserGroup(u));
}
public void DeleteUserGroupAsync(UserGroup u, Action exceptionCallback) {
Action deleteUserGroupAction = new Action(delegate { DeleteUserGroup(u); });
ExecuteActionAsync(deleteUserGroupAction, exceptionCallback);
}
public void DeleteRole(Role u) {
CallAccessService(s => s.DeleteRole(u));
}
public void DeleteRoleAsync(Role u, Action exceptionCallback) {
Action deleteRoleAction = new Action(delegate { DeleteRole(u); });
ExecuteActionAsync(deleteRoleAction, exceptionCallback);
}
#endregion
public void ExecuteActionAsync(Action action, Action exceptionCallback) {
var call = new Func(delegate() {
try {
OnRefreshing();
action();
}
catch (Exception ex) {
return ex;
}
finally {
OnRefreshed();
}
return null;
});
call.BeginInvoke(delegate(IAsyncResult result) {
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#region Events
public event EventHandler Refreshing;
private void OnRefreshing() {
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed() {
EventHandler handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Helpers
public static void CallAccessService(Action call) {
AccessServiceClient client = ClientFactory.CreateClient();
try {
call(client);
}
finally {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
}
}
public static T CallAccessService(Func call) {
AccessServiceClient client = ClientFactory.CreateClient();
try {
return call(client);
}
finally {
try {
client.Close();
}
catch (Exception) {
client.Abort();
}
}
}
#endregion
}
}