[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;
|
---|
[4433] | 23 | using System.Linq;
|
---|
[4426] | 24 | using HeuristicLab.Clients.Common;
|
---|
| 25 | using HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.PluginInfrastructure;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Clients.OKB {
|
---|
| 31 | [Item("Administrator", "OKB administrator front-end.")]
|
---|
| 32 | public sealed class Administrator : IContent {
|
---|
[4433] | 33 | private static Administrator instance;
|
---|
| 34 | public static Administrator Instance {
|
---|
| 35 | get {
|
---|
| 36 | if (instance == null) instance = new Administrator();
|
---|
| 37 | return instance;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[4441] | 41 | private ItemCollection<Platform> platforms;
|
---|
| 42 | public ItemCollection<Platform> Platforms {
|
---|
| 43 | get { return platforms; }
|
---|
| 44 | }
|
---|
[4426] | 45 | private ItemCollection<AlgorithmClass> algorithmClasses;
|
---|
| 46 | public ItemCollection<AlgorithmClass> AlgorithmClasses {
|
---|
| 47 | get { return algorithmClasses; }
|
---|
| 48 | }
|
---|
| 49 | private ItemCollection<Algorithm> algorithms;
|
---|
| 50 | public ItemCollection<Algorithm> Algorithms {
|
---|
| 51 | get { return algorithms; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[4433] | 54 | private Administrator() { }
|
---|
[4426] | 55 |
|
---|
[4433] | 56 | public void Refresh() {
|
---|
| 57 | OnRefreshing();
|
---|
[4441] | 58 | if (platforms == null) {
|
---|
| 59 | platforms = new ItemCollection<Platform>();
|
---|
| 60 | platforms.ItemsRemoved += new CollectionItemsChangedEventHandler<Platform>(platforms_ItemsRemoved);
|
---|
| 61 | }
|
---|
| 62 | platforms.Clear();
|
---|
[4433] | 63 | if (algorithmClasses == null) {
|
---|
| 64 | algorithmClasses = new ItemCollection<AlgorithmClass>();
|
---|
| 65 | algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
|
---|
| 66 | }
|
---|
[4441] | 67 | algorithmClasses.Clear();
|
---|
[4433] | 68 | if (algorithms == null) {
|
---|
| 69 | algorithms = new ItemCollection<Algorithm>();
|
---|
| 70 | algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
|
---|
| 71 | }
|
---|
| 72 | algorithms.Clear();
|
---|
| 73 |
|
---|
[4441] | 74 | var call = new Func<Exception>(delegate() {
|
---|
| 75 | try {
|
---|
| 76 | platforms.AddRange(CallAdminService<ItemCollection<Platform>>(s => s.GetPlatforms()).OrderBy(a => a.Name));
|
---|
| 77 | algorithmClasses.AddRange(CallAdminService<ItemCollection<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(a => a.Name));
|
---|
| 78 | algorithms.AddRange(CallAdminService<ItemCollection<Algorithm>>(s => s.GetAlgorithms()).OrderBy(a => a.Name));
|
---|
| 79 | return null;
|
---|
| 80 | }
|
---|
| 81 | catch (Exception ex) {
|
---|
| 82 | return ex;
|
---|
| 83 | }
|
---|
[4433] | 84 | });
|
---|
[4441] | 85 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 86 | Exception ex = call.EndInvoke(result);
|
---|
| 87 | if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
|
---|
[4433] | 88 | OnRefreshed();
|
---|
| 89 | }, null);
|
---|
[4426] | 90 | }
|
---|
| 91 |
|
---|
[4441] | 92 | public bool Store(IOKBItem item) {
|
---|
| 93 | try {
|
---|
| 94 | if (item is Platform)
|
---|
| 95 | CallAdminService(s => s.StorePlatform((Platform)item));
|
---|
| 96 | else if (item is AlgorithmClass)
|
---|
| 97 | CallAdminService(s => s.StoreAlgorithmClass((AlgorithmClass)item));
|
---|
| 98 | else if (item is Algorithm)
|
---|
| 99 | CallAdminService(s => s.StoreAlgorithm((Algorithm)item));
|
---|
| 100 | return true;
|
---|
| 101 | }
|
---|
| 102 | catch (Exception ex) {
|
---|
| 103 | ErrorHandling.ShowErrorDialog("Store failed.", ex);
|
---|
| 104 | return false;
|
---|
| 105 | }
|
---|
[4433] | 106 | }
|
---|
| 107 |
|
---|
| 108 | public event EventHandler Refreshing;
|
---|
| 109 | private void OnRefreshing() {
|
---|
| 110 | EventHandler handler = Refreshing;
|
---|
| 111 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 112 | }
|
---|
| 113 | public event EventHandler Refreshed;
|
---|
| 114 | private void OnRefreshed() {
|
---|
| 115 | EventHandler handler = Refreshed;
|
---|
| 116 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[4441] | 119 | private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
|
---|
| 120 | try {
|
---|
| 121 | foreach (Platform p in e.Items)
|
---|
| 122 | CallAdminService(s => s.DeletePlatform(p.Id));
|
---|
| 123 | }
|
---|
| 124 | catch (Exception ex) {
|
---|
| 125 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[4433] | 128 | private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
|
---|
[4441] | 129 | try {
|
---|
| 130 | foreach (AlgorithmClass a in e.Items)
|
---|
| 131 | CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
|
---|
| 132 | }
|
---|
| 133 | catch (Exception ex) {
|
---|
| 134 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 135 | }
|
---|
[4433] | 136 | }
|
---|
| 137 | private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
|
---|
[4441] | 138 | try {
|
---|
| 139 | foreach (Algorithm a in e.Items)
|
---|
| 140 | CallAdminService(s => s.DeleteAlgorithm(a.Id));
|
---|
| 141 | }
|
---|
| 142 | catch (Exception ex) {
|
---|
| 143 | ErrorHandling.ShowErrorDialog("Delete failed.", ex);
|
---|
| 144 | }
|
---|
[4433] | 145 | }
|
---|
| 146 |
|
---|
| 147 | #region Helpers
|
---|
| 148 | private void CallAdminService(Action<IAdminService> call) {
|
---|
| 149 | AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
|
---|
| 150 | try {
|
---|
| 151 | call(client);
|
---|
| 152 | }
|
---|
| 153 | finally {
|
---|
[4426] | 154 | try {
|
---|
[4433] | 155 | client.Close();
|
---|
[4426] | 156 | }
|
---|
[4433] | 157 | catch (Exception) {
|
---|
| 158 | client.Abort();
|
---|
[4426] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
[4433] | 162 | private T CallAdminService<T>(Func<IAdminService, T> call) where T : class {
|
---|
| 163 | AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
|
---|
| 164 | try {
|
---|
| 165 | return call(client);
|
---|
[4426] | 166 | }
|
---|
[4433] | 167 | finally {
|
---|
| 168 | try {
|
---|
| 169 | client.Close();
|
---|
| 170 | }
|
---|
| 171 | catch (Exception) {
|
---|
| 172 | client.Abort();
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
[4426] | 175 | }
|
---|
[4433] | 176 | #endregion
|
---|
[4426] | 177 | }
|
---|
| 178 | }
|
---|