Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/19/10 05:25:00 (14 years ago)
Author:
swagner
Message:

Worked on OKB data model and services (#1174)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OKB/HeuristicLab.Clients.OKB-3.3/Administrator.cs

    r4426 r4433  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Clients.Common;
    2425using HeuristicLab.Collections;
     
    3031  [Item("Administrator", "OKB administrator front-end.")]
    3132  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
    3241    private ItemCollection<AlgorithmClass> algorithmClasses;
    3342    public ItemCollection<AlgorithmClass> AlgorithmClasses {
     
    3948    }
    4049
    41     public Administrator() {
    42       algorithmClasses = new ItemCollection<AlgorithmClass>();
    43       algorithms = new ItemCollection<Algorithm>();
     50    private Administrator() { }
    4451
    45       algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
    46       algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
     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);
    4773    }
    4874
    49     public void Refresh() {
    50       using (AdminServiceClient adminService = ClientFactory.CreateClient<AdminServiceClient, IAdminService>()) {
     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 {
    51112        try {
    52           algorithmClasses.Clear();
    53           algorithmClasses.AddRange(adminService.GetAlgorithmClasses());
    54           algorithms.Clear();
    55           algorithms.AddRange(adminService.GetAlgorithms());
     113          client.Close();
    56114        }
    57         catch (Exception ex) {
    58           ErrorHandling.ShowErrorDialog(ex);
     115        catch (Exception) {
     116          client.Abort();
    59117        }
    60118      }
    61119    }
    62 
    63     private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
    64       using (AdminServiceClient adminService = ClientFactory.CreateClient<AdminServiceClient, IAdminService>()) {
    65         foreach (AlgorithmClass ac in e.Items)
    66           adminService.DeleteAlgorithmClass(ac.Id);
     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);
    67124      }
     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;
    68137    }
    69     private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
    70       using (AdminServiceClient adminService = ClientFactory.CreateClient<AdminServiceClient, IAdminService>()) {
    71         foreach (Algorithm a in e.Items)
    72           adminService.DeleteAlgorithm(a.Id);
    73       }
    74     }
     138    #endregion
    75139  }
    76140}
Note: See TracChangeset for help on using the changeset viewer.