Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/Administration/AdministrationClient.cs @ 5533

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

Worked on OKB (#1174)

File size: 11.6 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Common;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Clients.OKB.Administration {
32  [Item("AdministrationClient", "OKB administration client.")]
33  public sealed class AdministrationClient : IContent {
34    private static AdministrationClient instance;
35    public static AdministrationClient Instance {
36      get {
37        if (instance == null) instance = new AdministrationClient();
38        return instance;
39      }
40    }
41
42    #region Properties
43    private ItemCollection<Platform> platforms;
44    public ItemCollection<Platform> Platforms {
45      get { return platforms; }
46    }
47    private ItemCollection<AlgorithmClass> algorithmClasses;
48    public ItemCollection<AlgorithmClass> AlgorithmClasses {
49      get { return algorithmClasses; }
50    }
51    private ItemCollection<Algorithm> algorithms;
52    public ItemCollection<Algorithm> Algorithms {
53      get { return algorithms; }
54    }
55    private ItemCollection<ProblemClass> problemClasses;
56    public ItemCollection<ProblemClass> ProblemClasses {
57      get { return problemClasses; }
58    }
59    private ItemCollection<Problem> problems;
60    public ItemCollection<Problem> Problems {
61      get { return problems; }
62    }
63    #endregion
64
65    private AdministrationClient() {
66      platforms = new ItemCollection<Platform>();
67      platforms.ItemsRemoved += new CollectionItemsChangedEventHandler<Platform>(platforms_ItemsRemoved);
68      algorithmClasses = new ItemCollection<AlgorithmClass>();
69      algorithmClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<AlgorithmClass>(algorithmClasses_ItemsRemoved);
70      algorithms = new ItemCollection<Algorithm>();
71      algorithms.ItemsRemoved += new CollectionItemsChangedEventHandler<Algorithm>(algorithms_ItemsRemoved);
72      problemClasses = new ItemCollection<ProblemClass>();
73      problemClasses.ItemsRemoved += new CollectionItemsChangedEventHandler<ProblemClass>(problemClasses_ItemsRemoved);
74      problems = new ItemCollection<Problem>();
75      problems.ItemsRemoved += new CollectionItemsChangedEventHandler<Problem>(problems_ItemsRemoved);
76    }
77
78    #region Refresh
79    public void Refresh() {
80      OnRefreshing();
81
82      platforms.Clear();
83      algorithmClasses.Clear();
84      algorithms.Clear();
85      problemClasses.Clear();
86      problems.Clear();
87
88      var call = new Func<Exception>(delegate() {
89        try {
90          platforms.AddRange(CallAdministrationService<List<Platform>>(s => s.GetPlatforms()).OrderBy(x => x.Name));
91          algorithmClasses.AddRange(CallAdministrationService<List<AlgorithmClass>>(s => s.GetAlgorithmClasses()).OrderBy(x => x.Name));
92          algorithms.AddRange(CallAdministrationService<List<Algorithm>>(s => s.GetAlgorithms()).OrderBy(x => x.Name));
93          problemClasses.AddRange(CallAdministrationService<List<ProblemClass>>(s => s.GetProblemClasses()).OrderBy(x => x.Name));
94          problems.AddRange(CallAdministrationService<List<Problem>>(s => s.GetProblems()).OrderBy(x => x.Name));
95          return null;
96        }
97        catch (Exception ex) {
98          return ex;
99        }
100      });
101      call.BeginInvoke(delegate(IAsyncResult result) {
102        Exception ex = call.EndInvoke(result);
103        if (ex != null) ErrorHandling.ShowErrorDialog("Refresh failed.", ex);
104        OnRefreshed();
105      }, null);
106    }
107    #endregion
108
109    #region Store
110    public bool Store(IOKBItem item) {
111      try {
112        if (item.Id == 0) {
113          if (item is Platform)
114            item.Id = CallAdministrationService<long>(s => s.AddPlatform((Platform)item));
115          else if (item is AlgorithmClass)
116            item.Id = CallAdministrationService<long>(s => s.AddAlgorithmClass((AlgorithmClass)item));
117          else if (item is Algorithm)
118            item.Id = CallAdministrationService<long>(s => s.AddAlgorithm((Algorithm)item));
119          else if (item is ProblemClass)
120            item.Id = CallAdministrationService<long>(s => s.AddProblemClass((ProblemClass)item));
121          else if (item is Problem)
122            item.Id = CallAdministrationService<long>(s => s.AddProblem((Problem)item));
123        } else {
124          if (item is Platform)
125            CallAdministrationService(s => s.UpdatePlatform((Platform)item));
126          else if (item is AlgorithmClass)
127            CallAdministrationService(s => s.UpdateAlgorithmClass((AlgorithmClass)item));
128          else if (item is Algorithm)
129            CallAdministrationService(s => s.UpdateAlgorithm((Algorithm)item));
130          else if (item is ProblemClass)
131            CallAdministrationService(s => s.UpdateProblemClass((ProblemClass)item));
132          else if (item is Problem)
133            CallAdministrationService(s => s.UpdateProblem((Problem)item));
134        }
135        return true;
136      }
137      catch (Exception ex) {
138        ErrorHandling.ShowErrorDialog("Store failed.", ex);
139        return false;
140      }
141    }
142    #endregion
143
144    #region Algorithm Methods
145    public List<Guid> GetAlgorithmUsers(long algorithmId) {
146      try {
147        return CallAdministrationService<List<Guid>>(s => s.GetAlgorithmUsers(algorithmId));
148      }
149      catch (Exception ex) {
150        ErrorHandling.ShowErrorDialog("Refresh authorized algorithm users failed.", ex);
151        return null;
152      }
153    }
154    public bool UpdateAlgorithmUsers(long algorithmId, List<Guid> users) {
155      try {
156        CallAdministrationService(s => s.UpdateAlgorithmUsers(algorithmId, users));
157        return true;
158      }
159      catch (Exception ex) {
160        ErrorHandling.ShowErrorDialog("Update authorized algorithm users failed.", ex);
161        return false;
162      }
163    }
164    public byte[] GetAlgorithmData(long algorithmId) {
165      try {
166        return CallAdministrationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
167      }
168      catch (Exception ex) {
169        ErrorHandling.ShowErrorDialog("Refresh algorithm data failed.", ex);
170        return null;
171      }
172    }
173    public bool UpdateAlgorithmData(long algorithmId, byte[] algorithmData) {
174      try {
175        CallAdministrationService(s => s.UpdateAlgorithmData(algorithmId, algorithmData));
176        return true;
177      }
178      catch (Exception ex) {
179        ErrorHandling.ShowErrorDialog("Update algorithm data failed.", ex);
180        return false;
181      }
182    }
183    #endregion
184
185    #region Problem Methods
186    public List<Guid> GetProblemUsers(long problemId) {
187      try {
188        return CallAdministrationService<List<Guid>>(s => s.GetProblemUsers(problemId));
189      }
190      catch (Exception ex) {
191        ErrorHandling.ShowErrorDialog("Refresh authorized problem users failed.", ex);
192        return null;
193      }
194    }
195    public bool UpdateProblemUsers(long problemId, List<Guid> users) {
196      try {
197        CallAdministrationService(s => s.UpdateProblemUsers(problemId, users));
198        return true;
199      }
200      catch (Exception ex) {
201        ErrorHandling.ShowErrorDialog("Update authorized problem users failed.", ex);
202        return false;
203      }
204    }
205    public byte[] GetProblemData(long problemId) {
206      try {
207        return CallAdministrationService<byte[]>(s => s.GetProblemData(problemId));
208      }
209      catch (Exception ex) {
210        ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex);
211        return null;
212      }
213    }
214    public bool UpdateProblemData(long problemId, byte[] problemData) {
215      try {
216        CallAdministrationService(s => s.UpdateProblemData(problemId, problemData));
217        return true;
218      }
219      catch (Exception ex) {
220        ErrorHandling.ShowErrorDialog("Update problem data failed.", ex);
221        return false;
222      }
223    }
224    #endregion
225
226    #region Events
227    public event EventHandler Refreshing;
228    private void OnRefreshing() {
229      EventHandler handler = Refreshing;
230      if (handler != null) handler(this, EventArgs.Empty);
231    }
232    public event EventHandler Refreshed;
233    private void OnRefreshed() {
234      EventHandler handler = Refreshed;
235      if (handler != null) handler(this, EventArgs.Empty);
236    }
237
238    private void platforms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Platform> e) {
239      try {
240        foreach (Platform p in e.Items)
241          CallAdministrationService(s => s.DeletePlatform(p.Id));
242      }
243      catch (Exception ex) {
244        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
245      }
246    }
247    private void algorithmClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<AlgorithmClass> e) {
248      try {
249        foreach (AlgorithmClass a in e.Items)
250          CallAdministrationService(s => s.DeleteAlgorithmClass(a.Id));
251      }
252      catch (Exception ex) {
253        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
254      }
255    }
256    private void algorithms_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Algorithm> e) {
257      try {
258        foreach (Algorithm a in e.Items)
259          CallAdministrationService(s => s.DeleteAlgorithm(a.Id));
260      }
261      catch (Exception ex) {
262        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
263      }
264    }
265    private void problemClasses_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ProblemClass> e) {
266      try {
267        foreach (ProblemClass p in e.Items)
268          CallAdministrationService(s => s.DeleteProblemClass(p.Id));
269      }
270      catch (Exception ex) {
271        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
272      }
273    }
274    private void problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Problem> e) {
275      try {
276        foreach (Problem p in e.Items)
277          CallAdministrationService(s => s.DeleteProblem(p.Id));
278      }
279      catch (Exception ex) {
280        ErrorHandling.ShowErrorDialog("Delete failed.", ex);
281      }
282    }
283    #endregion
284
285    #region Helpers
286    private void CallAdministrationService(Action<IAdministrationService> call) {
287      AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
288      try {
289        call(client);
290      }
291      finally {
292        try {
293          client.Close();
294        }
295        catch (Exception) {
296          client.Abort();
297        }
298      }
299    }
300    private T CallAdministrationService<T>(Func<IAdministrationService, T> call) {
301      AdministrationServiceClient client = ClientFactory.CreateClient<AdministrationServiceClient, IAdministrationService>();
302      try {
303        return call(client);
304      }
305      finally {
306        try {
307          client.Close();
308        }
309        catch (Exception) {
310          client.Abort();
311        }
312      }
313    }
314    #endregion
315  }
316}
Note: See TracBrowser for help on using the repository browser.