[4426] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
[4466] | 23 | using System.Collections.Generic;
|
---|
[4433] | 24 | using System.Linq;
|
---|
[4426] | 25 | using HeuristicLab.Clients.Common;
|
---|
| 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.PluginInfrastructure;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Clients.OKB {
|
---|
| 32 | [Item("Administrator", "OKB administrator front-end.")]
|
---|
| 33 | public sealed class Administrator : IContent {
|
---|
[4433] | 34 | private static Administrator instance;
|
---|
| 35 | public static Administrator Instance {
|
---|
| 36 | get {
|
---|
| 37 | if (instance == null) instance = new Administrator();
|
---|
| 38 | return instance;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[4441] | 42 | private ItemCollection<Platform> platforms;
|
---|
| 43 | public ItemCollection<Platform> Platforms {
|
---|
| 44 | get { return platforms; }
|
---|
| 45 | }
|
---|
[4426] | 46 | private ItemCollection<AlgorithmClass> algorithmClasses;
|
---|
| 47 | public ItemCollection<AlgorithmClass> AlgorithmClasses {
|
---|
| 48 | get { return algorithmClasses; }
|
---|
| 49 | }
|
---|
| 50 | private ItemCollection<Algorithm> algorithms;
|
---|
| 51 | public ItemCollection<Algorithm> Algorithms {
|
---|
| 52 | get { return algorithms; }
|
---|
| 53 | }
|
---|
[4466] | 54 | private ItemCollection<DataType> dataTypes;
|
---|
| 55 | public ItemCollection<DataType> DataTypes {
|
---|
| 56 | get { return dataTypes; }
|
---|
| 57 | }
|
---|
| 58 | private IEnumerable<User> users;
|
---|
| 59 | public IEnumerable<User> Users {
|
---|
| 60 | get { return users; }
|
---|
| 61 | }
|
---|
[4426] | 62 |
|
---|
[4433] | 63 | private Administrator() { }
|
---|
[4426] | 64 |
|
---|
[4433] | 65 | public void Refresh() {
|
---|
| 66 | OnRefreshing();
|
---|
[4441] | 67 | if (platforms == null) {
|
---|
| 68 | platforms = new ItemCollection<Platform>();
|
---|
| 69 | platforms.ItemsRemoved += new CollectionItemsChangedEventHandler<Platform>(platforms_ItemsRemoved);
|
---|
| 70 | }
|
---|
| 71 | platforms.Clear();
|
---|
[4433] | 72 | if (algorithmClasses == null) {
|
---|
| 73 | algorithmClasses = new ItemCollection<AlgorithmClass>();
|
---|
| 74 | algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
|
---|
| 75 | }
|
---|
[4441] | 76 | algorithmClasses.Clear();
|
---|
[4433] | 77 | if (algorithms == null) {
|
---|
| 78 | algorithms = new ItemCollection<Algorithm>();
|
---|
| 79 | algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
|
---|
| 80 | }
|
---|
| 81 | algorithms.Clear();
|
---|
[4466] | 82 | if (dataTypes == null) {
|
---|
| 83 | dataTypes = new ItemCollection<DataType>();
|
---|
| 84 | dataTypes.ItemsRemoved += new CollectionItemsChangedEventHandler<DataType>(dataTypes_ItemsRemoved);
|
---|
| 85 | }
|
---|
| 86 | dataTypes.Clear();
|
---|
[4433] | 87 |
|
---|
[4441] | 88 | var call = new Func<Exception>(delegate() {
|
---|
| 89 | try {
|
---|
[4466] | 90 | platforms.AddRange(CallAdminService<Platform[]>(s => s.GetPlatforms()).OrderBy(x => x.Name));
|
---|
| 91 | algorithmClasses.AddRange(CallAdminService<AlgorithmClass[]>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
|
---|
| 92 | algorithms.AddRange(CallAdminService<Algorithm[]>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
|
---|
| 93 | dataTypes.AddRange(CallAdminService<DataType[]>(s => s.GetDataTypes()).OrderBy(x => x.Name));
|
---|
| 94 | users = CallAuthenticationService<User[]>(s => s.GetUsers()).OrderBy(x => x.Name);
|
---|
[4441] | 95 | return null;
|
---|
| 96 | }
|
---|
| 97 | catch (Exception ex) {
|
---|
| 98 | return ex;
|
---|
| 99 | }
|
---|
[4433] | 100 | });
|
---|
[4441] | 101 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 102 | Exception ex = call.EndInvoke(result);
|
---|
| 103 | if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
|
---|
[4433] | 104 | OnRefreshed();
|
---|
| 105 | }, null);
|
---|
[4426] | 106 | }
|
---|
| 107 |
|
---|
[4441] | 108 | public bool Store(IOKBItem item) {
|
---|
| 109 | try {
|
---|
| 110 | if (item is Platform)
|
---|
| 111 | CallAdminService(s => s.StorePlatform((Platform)item));
|
---|
| 112 | else if (item is AlgorithmClass)
|
---|
| 113 | CallAdminService(s => s.StoreAlgorithmClass((AlgorithmClass)item));
|
---|
| 114 | else if (item is Algorithm)
|
---|
| 115 | CallAdminService(s => s.StoreAlgorithm((Algorithm)item));
|
---|
[4466] | 116 | else if (item is DataType)
|
---|
| 117 | CallAdminService(s => s.StoreDataType((DataType)item));
|
---|
[4441] | 118 | return true;
|
---|
| 119 | }
|
---|
| 120 | catch (Exception ex) {
|
---|
| 121 | ErrorHandling.ShowErrorDialog("Store failed.", ex);
|
---|
| 122 | return false;
|
---|
| 123 | }
|
---|
[4433] | 124 | }
|
---|
| 125 |
|
---|
[4466] | 126 | public Guid[] GetAlgorithmUsers(long algorithmId) {
|
---|
| 127 | try {
|
---|
| 128 | return CallAdminService<Guid[]>(s => s.GetAlgorithmUsers(algorithmId));
|
---|
| 129 | }
|
---|
| 130 | catch (Exception ex) {
|
---|
| 131 | ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
|
---|
| 132 | return null;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | public bool StoreAlgorithmUsers(long algorithmId, Guid[] users) {
|
---|
| 136 | try {
|
---|
| 137 | CallAdminService(s => s.StoreAlgorithmUsers(algorithmId, users));
|
---|
| 138 | return true;
|
---|
| 139 | }
|
---|
| 140 | catch (Exception ex) {
|
---|
| 141 | ErrorHandling.ShowErrorDialog("Store authorized algorithm users failed.", ex);
|
---|
| 142 | return false;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | public bool StoreAlgorithmData(AlgorithmData algorithmData) {
|
---|
| 147 | try {
|
---|
| 148 | CallAdminService(s => s.StoreAlgorithmData(algorithmData));
|
---|
| 149 | return true;
|
---|
| 150 | }
|
---|
| 151 | catch (Exception ex) {
|
---|
| 152 | ErrorHandling.ShowErrorDialog("Store serialized algorithm failed.", ex);
|
---|
| 153 | return false;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[4433] | 157 | public event EventHandler Refreshing;
|
---|
| 158 | private void OnRefreshing() {
|
---|
| 159 | EventHandler handler = Refreshing;
|
---|
| 160 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 161 | }
|
---|
| 162 | public event EventHandler Refreshed;
|
---|
| 163 | private void OnRefreshed() {
|
---|
| 164 | EventHandler handler = Refreshed;
|
---|
| 165 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[4441] | 168 | private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
|
---|
| 169 | try {
|
---|
| 170 | foreach (Platform p in e.Items)
|
---|
| 171 | CallAdminService(s => s.DeletePlatform(p.Id));
|
---|
| 172 | }
|
---|
| 173 | catch (Exception ex) {
|
---|
| 174 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
[4433] | 177 | private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
|
---|
[4441] | 178 | try {
|
---|
| 179 | foreach (AlgorithmClass a in e.Items)
|
---|
| 180 | CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
|
---|
| 181 | }
|
---|
| 182 | catch (Exception ex) {
|
---|
| 183 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 184 | }
|
---|
[4433] | 185 | }
|
---|
| 186 | private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
|
---|
[4441] | 187 | try {
|
---|
| 188 | foreach (Algorithm a in e.Items)
|
---|
| 189 | CallAdminService(s => s.DeleteAlgorithm(a.Id));
|
---|
| 190 | }
|
---|
| 191 | catch (Exception ex) {
|
---|
| 192 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 193 | }
|
---|
[4433] | 194 | }
|
---|
[4466] | 195 | private void dataTypes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataType> e) {
|
---|
| 196 | try {
|
---|
| 197 | foreach (DataType d in e.Items)
|
---|
| 198 | CallAdminService(s => s.DeleteDataType(d.Id));
|
---|
| 199 | }
|
---|
| 200 | catch (Exception ex) {
|
---|
| 201 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
[4433] | 204 |
|
---|
| 205 | #region Helpers
|
---|
| 206 | private void CallAdminService(Action<IAdminService> call) {
|
---|
| 207 | AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
|
---|
| 208 | try {
|
---|
| 209 | call(client);
|
---|
| 210 | }
|
---|
| 211 | finally {
|
---|
[4426] | 212 | try {
|
---|
[4433] | 213 | client.Close();
|
---|
[4426] | 214 | }
|
---|
[4433] | 215 | catch (Exception) {
|
---|
| 216 | client.Abort();
|
---|
[4426] | 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
[4433] | 220 | private T CallAdminService<T>(Func<IAdminService, T> call) where T : class {
|
---|
| 221 | AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
|
---|
| 222 | try {
|
---|
| 223 | return call(client);
|
---|
[4426] | 224 | }
|
---|
[4433] | 225 | finally {
|
---|
| 226 | try {
|
---|
| 227 | client.Close();
|
---|
| 228 | }
|
---|
| 229 | catch (Exception) {
|
---|
| 230 | client.Abort();
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
[4426] | 233 | }
|
---|
[4466] | 234 | private T CallAuthenticationService<T>(Func<IAuthenticationService, T> call) where T : class {
|
---|
| 235 | AuthenticationServiceClient client = ClientFactory.CreateClient<AuthenticationServiceClient, IAuthenticationService>();
|
---|
| 236 | try {
|
---|
| 237 | return call(client);
|
---|
| 238 | }
|
---|
| 239 | finally {
|
---|
| 240 | try {
|
---|
| 241 | client.Close();
|
---|
| 242 | }
|
---|
| 243 | catch (Exception) {
|
---|
| 244 | client.Abort();
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
[4433] | 248 | #endregion
|
---|
[4426] | 249 | }
|
---|
| 250 | }
|
---|