Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Clients.OKB/3.3/Administration/AdministrationClient.cs @ 15917

Last change on this file since 15917 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Common;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Clients.OKB.Administration {
30  [Item("AdministrationClient", "OKB administration client.")]
31  public sealed class AdministrationClient : IContent {
32    private static AdministrationClient instance;
33    public static AdministrationClient Instance {
34      get {
35        if (instance == null) instance = new AdministrationClient();
36        return instance;
37      }
38    }
39
40    #region Properties
41    private ItemCollection<Platform> platforms;
42    public ItemCollection<Platform> Platforms {
43      get { return platforms; }
44    }
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    private ItemCollection<ProblemClass> problemClasses;
54    public ItemCollection<ProblemClass> ProblemClasses {
55      get { return problemClasses; }
56    }
57    private ItemCollection<Problem> problems;
58    public ItemCollection<Problem> Problems {
59      get { return problems; }
60    }
61    #endregion
62
63    public AdministrationClient() { }
64
65    #region Refresh
66    public void Refresh() {
67      OnRefreshing();
68
69      platforms = new OKBItemCollection<Platform>();
70      algorithmClasses = new OKBItemCollection<AlgorithmClass>();
71      algorithms = new OKBItemCollection<Algorithm>();
72      problemClasses = new OKBItemCollection<ProblemClass>();
73      problems = new OKBItemCollection<Problem>();
74
75      try {
76        platforms.AddRange(CallAdministrationService<List<Platform>>(s => s.GetPlatforms()).OrderBy(x => x.Name));
77        algorithmClasses.AddRange(CallAdministrationService<List<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
78        algorithms.AddRange(CallAdministrationService<List<Algorithm>>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
79        problemClasses.AddRange(CallAdministrationService<List<ProblemClass>>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
80        problems.AddRange(CallAdministrationService<List<Problem>>(s => s.GetProblems()).OrderBy(x => x.Name));
81      } finally {
82        OnRefreshed();
83      }
84    }
85    public void RefreshAsync(Action<Exception> exceptionCallback) {
86      var call = new Func<Exception>(delegate () {
87        try {
88          Refresh();
89        } catch (Exception ex) {
90          return ex;
91        }
92        return null;
93      });
94      call.BeginInvoke(delegate (IAsyncResult result) {
95        Exception ex = call.EndInvoke(result);
96        if (ex != null) exceptionCallback(ex);
97      }, null);
98    }
99    #endregion
100
101    #region Store
102    public static void Store(IOKBItem item) {
103      if (item.Id == 0) {
104        if (item is Platform)
105          item.Id = CallAdministrationService<long>(s => s.AddPlatform((Platform)item));
106        else if (item is AlgorithmClass)
107          item.Id = CallAdministrationService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
108        else if (item is Algorithm)
109          item.Id = CallAdministrationService<long>(s => s.AddAlgorithm((Algorithm)item));
110        else if (item is ProblemClass)
111          item.Id = CallAdministrationService<long>(s => s.AddProblemClass((ProblemClass)item));
112        else if (item is Problem)
113          item.Id = CallAdministrationService<long>(s => s.AddProblem((Problem)item));
114      } else {
115        if (item is Platform)
116          CallAdministrationService(s => s.UpdatePlatform((Platform)item));
117        else if (item is AlgorithmClass)
118          CallAdministrationService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
119        else if (item is Algorithm)
120          CallAdministrationService(s => s.UpdateAlgorithm((Algorithm)item));
121        else if (item is ProblemClass)
122          CallAdministrationService(s => s.UpdateProblemClass((ProblemClass)item));
123        else if (item is Problem)
124          CallAdministrationService(s => s.UpdateProblem((Problem)item));
125      }
126    }
127    #endregion
128
129    #region Delete
130    public static void Delete(IOKBItem item) {
131      if (item is Platform)
132        CallAdministrationService(s => s.DeletePlatform(item.Id));
133      else if (item is AlgorithmClass)
134        CallAdministrationService(s => s.DeleteAlgorithmClass(item.Id));
135      else if (item is Algorithm)
136        CallAdministrationService(s => s.DeleteAlgorithm(item.Id));
137      else if (item is ProblemClass)
138        CallAdministrationService(s => s.DeleteProblemClass(item.Id));
139      else if (item is Problem)
140        CallAdministrationService(s => s.DeleteProblem(item.Id));
141      item.Id = 0;
142    }
143    #endregion
144
145    #region Algorithm Methods
146    public static List<Guid> GetAlgorithmUsers(long algorithmId) {
147      return CallAdministrationService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
148    }
149    public static void UpdateAlgorithmUsers(long algorithmId, List<Guid> users) {
150      CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
151    }
152    public static byte[] GetAlgorithmData(long algorithmId) {
153      return CallAdministrationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
154    }
155    public static void UpdateAlgorithmData(long algorithmId, byte[] algorithmData) {
156      CallAdministrationService(s => s.UpdateAlgorithmData(algorithmId, algorithmData));
157    }
158    #endregion
159
160    #region Problem Methods
161    public static List<Guid> GetProblemUsers(long problemId) {
162      return CallAdministrationService<List<Guid>>(s => s.GetProblemUsers(problemId));
163    }
164    public static void UpdateProblemUsers(long problemId, List<Guid> users) {
165      CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
166    }
167    public static byte[] GetProblemData(long problemId) {
168      return CallAdministrationService<byte[]>(s => s.GetProblemData(problemId));
169    }
170    public static void UpdateProblemData(long problemId, byte[] problemData) {
171      CallAdministrationService(s => s.UpdateProblemData(problemId, problemData));
172    }
173    #endregion
174
175    #region Events
176    public event EventHandler Refreshing;
177    private void OnRefreshing() {
178      EventHandler handler = Refreshing;
179      if (handler != null) handler(this, EventArgs.Empty);
180    }
181    public event EventHandler Refreshed;
182    private void OnRefreshed() {
183      var handler = Refreshed;
184      if (handler != null) handler(this, EventArgs.Empty);
185    }
186    #endregion
187
188    #region Helpers
189    private static void CallAdministrationService(Action<IAdministrationService> call) {
190      AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
191      try {
192        call(client);
193      } finally {
194        try {
195          client.Close();
196        } catch (Exception) {
197          client.Abort();
198        }
199      }
200    }
201    private static T CallAdministrationService<T>(Func<IAdministrationService, T> call) {
202      AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
203      try {
204        return call(client);
205      } finally {
206        try {
207          client.Close();
208        } catch (Exception) {
209          client.Abort();
210        }
211      }
212    }
213    #endregion
214  }
215}
Note: See TracBrowser for help on using the repository browser.