Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/Administrator.cs @ 4433

Last change on this file since 4433 was 4433, checked in by swagner, 14 years ago

Worked on OKB data model and services (#1174)

File size: 4.7 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Clients.Common;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Clients.OKB {
31  [Item("Administrator", "OKB administrator front-end.")]
32  public sealed class Administrator : IContent {
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
41    private ItemCollection<AlgorithmClass> algorithmClasses;
42    public ItemCollection<AlgorithmClass> AlgorithmClasses {
43      get { return algorithmClasses; }
44    }
45    private ItemCollection<Algorithm> algorithms;
46    public ItemCollection<Algorithm> Algorithms {
47      get { return algorithms; }
48    }
49
50    private Administrator() { }
51
52    public void Refresh() {
53      OnRefreshing();
54      if (algorithmClasses == null) {
55        algorithmClasses = new ItemCollection<AlgorithmClass>();
56        algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
57      }
58      if (algorithms == null) {
59        algorithms = new ItemCollection<Algorithm>();
60        algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
61      }
62      algorithmClasses.Clear();
63      algorithms.Clear();
64
65      var action = new Action(delegate() {
66        algorithmClasses.AddRange(CallAdminService<ItemCollection<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(a => a.Name));
67        algorithms.AddRange(CallAdminService<ItemCollection<Algorithm>>(s => s.GetAlgorithms()).OrderBy(a => a.Name));
68      });
69      action.BeginInvoke(delegate(IAsyncResult result) {
70        action.EndInvoke(result);
71        OnRefreshed();
72      }, null);
73    }
74
75    public void Store(IOKBItem item) {
76      if (item is AlgorithmClass)
77        CallAdminService(s => s.StoreAlgorithmClass((AlgorithmClass)item));
78      else if (item is Algorithm)
79        CallAdminService(s => s.StoreAlgorithm((Algorithm)item));
80    }
81
82    public event EventHandler Refreshing;
83    private void OnRefreshing() {
84      EventHandler handler = Refreshing;
85      if (handler != null) handler(this, EventArgs.Empty);
86    }
87    public event EventHandler Refreshed;
88    private void OnRefreshed() {
89      EventHandler handler = Refreshed;
90      if (handler != null) handler(this, EventArgs.Empty);
91    }
92
93    private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
94      foreach (AlgorithmClass a in e.Items)
95        CallAdminService(s => s.DeleteAlgorithmClass(a.Id));
96    }
97    private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
98      foreach (Algorithm a in e.Items)
99        CallAdminService(s => s.DeleteAlgorithm(a.Id));
100    }
101
102    #region Helpers
103    private void CallAdminService(Action<IAdminService> call) {
104      AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
105      try {
106        call(client);
107      }
108      catch (Exception ex) {
109        ErrorHandling.ShowErrorDialog(ex);
110      }
111      finally {
112        try {
113          client.Close();
114        }
115        catch (Exception) {
116          client.Abort();
117        }
118      }
119    }
120    private T CallAdminService<T>(Func<IAdminService, T> call) where T : class {
121      AdminServiceClient client = ClientFactory.CreateClient<AdminServiceClient, IAdminService>();
122      try {
123        return call(client);
124      }
125      catch (Exception ex) {
126        ErrorHandling.ShowErrorDialog(ex);
127      }
128      finally {
129        try {
130          client.Close();
131        }
132        catch (Exception) {
133          client.Abort();
134        }
135      }
136      return null;
137    }
138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.